3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # Test cases for SQLITE_DBCONFIG_RESET_DATABASE
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
16 set testprefix resetdb
20 ifcapable !vtab||!compound {
25 # In the "inmemory_journal" permutation, each new connection executes
26 # "PRAGMA journal_mode = memory". This fails with SQLITE_BUSY if attempted
27 # on a wal mode database with existing connections. For this and a few
28 # other reasons, this test is not run as part of "inmemory_journal".
30 # Permutation "journaltest" does not support wal mode.
32 if {[permutation]=="inmemory_journal"
33 || [permutation]=="journaltest"
39 # Create a sample database
41 PRAGMA auto_vacuum = 0;
42 PRAGMA page_size=4096;
44 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<20)
45 INSERT INTO t1(a,b) SELECT x, randomblob(300) FROM c;
46 CREATE INDEX t1a ON t1(a);
47 CREATE INDEX t1b ON t1(b);
48 SELECT sum(a), sum(length(b)) FROM t1;
49 PRAGMA integrity_check;
52 } {210 6000 ok delete 8}
54 # Verify that the same content is seen from a separate database connection
58 SELECT sum(a), sum(length(b)) FROM t1;
59 PRAGMA integrity_check;
63 } {210 6000 ok delete 8}
66 # Thoroughly corrupt the database file by overwriting the first
67 # page with randomness.
69 UPDATE sqlite_dbpage SET data=randomblob(4096) WHERE pgno=1;
72 } {1 {unsupported file format}}
77 } {1 {unsupported file format}}
80 # Reset the database file using SQLITE_DBCONFIG_RESET_DATABASE
81 sqlite3_db_config db RESET_DB 1
83 sqlite3_db_config db RESET_DB 0
85 # Verify that the reset took, even on the separate database connection
92 } {0 {1 4096 ok delete}}
94 # Delete the old connections and database and start over again
95 # with a different page size and in WAL mode.
101 do_execsql_test 300 {
102 PRAGMA auto_vacuum = 0;
103 PRAGMA page_size=8192;
104 PRAGMA journal_mode=WAL;
105 CREATE TABLE t1(a,b);
106 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<20)
107 INSERT INTO t1(a,b) SELECT x, randomblob(1300) FROM c;
108 CREATE INDEX t1a ON t1(a);
109 CREATE INDEX t1b ON t1(b);
110 SELECT sum(a), sum(length(b)) FROM t1;
111 PRAGMA integrity_check;
115 } {wal 210 26000 ok wal 8192 12}
119 SELECT sum(a), sum(length(b)) FROM t1;
120 PRAGMA integrity_check;
125 } {210 26000 ok wal 8192 12}
127 # Corrupt the database again
128 do_catchsql_test 320 {
129 UPDATE sqlite_dbpage SET data=randomblob(8192) WHERE pgno=1;
131 } {1 {file is not a database}}
137 } {1 {file is not a database}}
139 db2 cache flush ;# Required by permutation "prepare".
141 # Reset the database yet again. Verify that the page size and
142 # journal mode are preserved.
145 sqlite3_db_config db RESET_DB 1
147 sqlite3_db_config db RESET_DB 0
154 } {0 {1 8192 wal ok}}
157 # Reset the database yet again. This time immediately after it is closed
158 # and reopened. So that the VACUUM is the first statement run.
164 sqlite3_prepare db "SELECT 1 FROM sqlite_master LIMIT 1" -1 tail
166 sqlite3_db_config db RESET_DB 1
168 sqlite3_db_config db RESET_DB 0
176 } {0 {1 8192 wal ok}}
179 #-------------------------------------------------------------------------
182 do_execsql_test 600 {
183 PRAGMA journal_mode = wal;
185 INSERT INTO t1 VALUES(1), (2), (3), (4);
188 do_execsql_test -db db2 610 {
194 db2 eval {SELECT a FROM t1} {
197 sqlite3_db_config db RESET_DB 1
199 sqlite3_db_config db RESET_DB 0
206 do_execsql_test -db db2 630 {
207 SELECT * FROM sqlite_master
210 #-------------------------------------------------------------------------
214 do_execsql_test 700 {
215 PRAGMA page_size=512;
216 PRAGMA auto_vacuum = 0;
217 CREATE TABLE t1(a,b,c);
218 CREATE INDEX t1a ON t1(a);
219 CREATE INDEX t1bc ON t1(b,c);
220 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<10)
221 INSERT INTO t1(a,b,c) SELECT x, randomblob(100),randomblob(100) FROM c;
223 PRAGMA integrity_check;
226 if {[nonzero_reserved_bytes]} {
231 do_execsql_test 710 {
232 UPDATE sqlite_dbpage SET data=
233 X'53514C69746520666F726D61742033000200030100402020000000000000001300000000000000000000000300000004000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000D00000003017C0001D801AC017C00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002E03061715110145696E6465787431626374310443524541544520494E4445582074316263204F4E20743128622C63292A0206171311013F696E64657874316174310343524541544520494E44455820743161204F4E20743128612926010617111101397461626C657431743102435245415445205441424C4520743128612C622C6329' WHERE pgno=1;
236 do_execsql_test 720 {
237 PRAGMA integrity_check;
241 sqlite3_db_config db RESET_DB 1
243 sqlite3_db_config db RESET_DB 0
246 do_execsql_test 740 {
248 PRAGMA integrity_check;