xref: /sqlite-3.40.0/test/nockpt.test (revision 2d066bbf)
1298af023Sdan# 2016 October 31
2298af023Sdan#
3298af023Sdan# The author disclaims copyright to this source code.  In place of
4298af023Sdan# a legal notice, here is a blessing:
5298af023Sdan#
6298af023Sdan#    May you do good and not evil.
7298af023Sdan#    May you find forgiveness for yourself and forgive others.
8298af023Sdan#    May you share freely, never taking more than you give.
9298af023Sdan#
10298af023Sdan#***********************************************************************
11298af023Sdan# This file implements regression tests for SQLite library.  The
12298af023Sdan# focus of this file is testing the SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE
13298af023Sdan# option.
14298af023Sdan#
15298af023Sdan
16298af023Sdanset testdir [file dirname $argv0]
17298af023Sdansource $testdir/tester.tcl
18298af023Sdansource $testdir/lock_common.tcl
19298af023Sdansource $testdir/malloc_common.tcl
20298af023Sdansource $testdir/wal_common.tcl
21298af023Sdanifcapable !wal {finish_test ; return }
22402124deSdrhif {[permutation]=="journaltest" || [permutation]=="inmemory_journal"} {
23402124deSdrh  finish_test
24402124deSdrh  return
25402124deSdrh}
26298af023Sdan
27298af023Sdanset testprefix nockpt
28298af023Sdan
29298af023Sdando_execsql_test 1.0 {
30c5954199Sdrh  PRAGMA auto_vacuum=OFF;
31298af023Sdan  PRAGMA page_size = 1024;
32298af023Sdan  PRAGMA journal_mode = wal;
33298af023Sdan  CREATE TABLE c1(x, y, z);
34298af023Sdan  INSERT INTO c1 VALUES(1, 2, 3);
35298af023Sdan} {wal}
36298af023Sdan
37298af023Sdando_test 1.1 { file exists test.db-wal } 1
38298af023Sdando_test 1.2 { file size test.db-wal } [wal_file_size 3 1024]
39298af023Sdando_test 1.3 { db close } {}
40298af023Sdando_test 1.4 { file exists test.db-wal } 0
41298af023Sdan
42298af023Sdansqlite3 db test.db
43298af023Sdando_execsql_test 1.5 {
44298af023Sdan  INSERT INTO c1 VALUES(4, 5, 6);
45298af023Sdan  INSERT INTO c1 VALUES(7, 8, 9);
46298af023Sdan}
47298af023Sdando_test 1.6 { file exists test.db-wal } 1
48298af023Sdando_test 1.7 { sqlite3_db_config db NO_CKPT_ON_CLOSE 1 } {1}
49298af023Sdando_test 1.8 { file size test.db-wal } [wal_file_size 2 1024]
50298af023Sdando_test 1.9 { db close } {}
51298af023Sdando_test 1.10 { file exists test.db-wal } 1
52298af023Sdando_test 1.11 { file size test.db-wal } [wal_file_size 2 1024]
53298af023Sdan
54298af023Sdansqlite3 db test.db
55298af023Sdando_execsql_test 1.12 {
56298af023Sdan  SELECT * FROM c1
57298af023Sdan} {1 2 3 4 5 6 7 8 9}
58298af023Sdan
594a5bad57Sdando_execsql_test 1.13 { PRAGMA main.journal_mode } {wal}
604a5bad57Sdando_test 1.14 { sqlite3_db_config db NO_CKPT_ON_CLOSE 1 } {1}
614a5bad57Sdando_execsql_test 1.14 { PRAGMA main.journal_mode = delete } {delete}
624a5bad57Sdando_test 1.15 { file exists test.db-wal } {0}
634a5bad57Sdan
64b189e410Smistachkinif {$::tcl_platform(platform)!="windows"} {
65fa68815fSdan#-------------------------------------------------------------------------
66fa68815fSdan# Test an unusual scenario:
67fa68815fSdan#
68fa68815fSdan#   1. A wal mode db is opened and written. Then sqlite3_close_v2() used
69fa68815fSdan#      to close the db handle while there is still an unfinalized
70fa68815fSdan#      statement (so the db handle stays open).
71fa68815fSdan#
72fa68815fSdan#   2. The db, wal and *-shm files are deleted from the file system.
73fa68815fSdan#
74fa68815fSdan#   3. Another connection creates a new wal mode db at the same file-system
75fa68815fSdan#      location as the previous one.
76fa68815fSdan#
77fa68815fSdan#   4. The statement left unfinalized in (1) is finalized.
78fa68815fSdan#
79fa68815fSdan# The test is to ensure that the connection left open in step (1) does
80fa68815fSdan# not try to delete the wal file from the file-system as part of step
81fa68815fSdan# 4.
82fa68815fSdan#
83fa68815fSdanreset_db
84fa68815fSdandb close
85fa68815fSdan
86fa68815fSdan# Open a connection on a wal database. Write to it a bit. Then prepare
87fa68815fSdan# a statement and call sqlite3_close_v2() (so that the statement handle
88fa68815fSdan# holds the db connection open).
89fa68815fSdan#
90fa68815fSdanset ::db1 [sqlite3_open_v2 test.db SQLITE_OPEN_READWRITE ""]
91fa68815fSdando_test 2.0 {
92fa68815fSdan  lindex [
93fa68815fSdan    sqlite3_exec $::db1 {
94fa68815fSdan      PRAGMA journal_mode = wal;
95fa68815fSdan      CREATE TABLE t1(x PRIMARY KEY, y UNIQUE, z);
96fa68815fSdan      INSERT INTO t1 VALUES(1, 2, 3);
97fa68815fSdan      PRAGMA wal_checkpoint;
98fa68815fSdan    }] 0
99fa68815fSdan} {0}
100fa68815fSdanset ::stmt [sqlite3_prepare $::db1 "SELECT * FROM t1" -1 dummy]
101fa68815fSdansqlite3_close_v2 $::db1
102fa68815fSdan
103fa68815fSdan# Delete the database, wal and shm files.
104fa68815fSdan#
105fa68815fSdanforcedelete test.db test.db-wal test.db-shm
106fa68815fSdan
107fa68815fSdan# Open and populate a new database file at the same file-system location
108fa68815fSdan# as the one just deleted. Contrive a partial checkpoint on it.
109fa68815fSdan#
110fa68815fSdansqlite3 db  test.db
111fa68815fSdansqlite3 db2 test.db
112fa68815fSdando_execsql_test 2.1 {
113*2d066bbfSdan  PRAGMA auto_vacuum=OFF;
114fa68815fSdan  PRAGMA journal_mode = wal;
115fa68815fSdan  CREATE TABLE y1(a PRIMARY KEY, b UNIQUE, c);
116fa68815fSdan  INSERT INTO y1 VALUES('a', 'b', 'c');
117fa68815fSdan  INSERT INTO y1 VALUES('d', 'e', 'f');
118fa68815fSdan} {wal}
119fa68815fSdando_execsql_test -db db2 2.2 {
120fa68815fSdan  BEGIN;
121fa68815fSdan    SELECT * FROM y1;
122fa68815fSdan} {a b c d e f}
123fa68815fSdando_execsql_test 2.3 {
124fa68815fSdan  UPDATE y1 SET c='g' WHERE a='d';
125fa68815fSdan  PRAGMA wal_checkpoint;
126fa68815fSdan} {0 11 10}
127fa68815fSdando_execsql_test -db db2 2.4 {
128fa68815fSdan  COMMIT
129fa68815fSdan}
130fa68815fSdan
131fa68815fSdan# Finalize the statement handle, causing the first connection to be
132fa68815fSdan# closed. Test that this has not corrupted the database file by
133fa68815fSdan# deleting the new wal file from the file-system. If it has, this
134fa68815fSdan# test should fail with an IO or corruption error.
135fa68815fSdan#
136fa68815fSdando_test 2.5 {
137fa68815fSdan  sqlite3_finalize $::stmt
138fa68815fSdan  sqlite3 db3 test.db
139fa68815fSdan  execsql {
140fa68815fSdan    PRAGMA integrity_check;
141fa68815fSdan    SELECT * FROM y1;
142fa68815fSdan  } db3
143fa68815fSdan} {ok a b c d e g}
144b189e410Smistachkin}
1454a5bad57Sdan
146298af023Sdan
147298af023Sdanfinish_test
148