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
18 ifcapable !vtab||!compound {
23 # Create a sample database
25 PRAGMA page_size=4096;
27 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<20)
28 INSERT INTO t1(a,b) SELECT x, randomblob(300) FROM c;
29 CREATE INDEX t1a ON t1(a);
30 CREATE INDEX t1b ON t1(b);
31 SELECT sum(a), sum(length(b)) FROM t1;
32 PRAGMA integrity_check;
35 } {210 6000 ok delete 8}
37 # Verify that the same content is seen from a separate database connection
41 SELECT sum(a), sum(length(b)) FROM t1;
42 PRAGMA integrity_check;
46 } {210 6000 ok delete 8}
49 # Thoroughly corrupt the database file by overwriting the first
50 # page with randomness.
52 UPDATE sqlite_dbpage SET data=randomblob(4096) WHERE pgno=1;
55 } {1 {unsupported file format}}
60 } {1 {unsupported file format}}
63 # Reset the database file using SQLITE_DBCONFIG_RESET_DATABASE
64 sqlite3_db_config db RESET_DB 1
66 sqlite3_db_config db RESET_DB 0
68 # Verify that the reset took, even on the separate database connection
75 } {0 {1 4096 ok delete}}
77 # Delete the old connections and database and start over again
78 # with a different page size and in WAL mode.
85 PRAGMA page_size=8192;
86 PRAGMA journal_mode=WAL;
88 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<20)
89 INSERT INTO t1(a,b) SELECT x, randomblob(1300) FROM c;
90 CREATE INDEX t1a ON t1(a);
91 CREATE INDEX t1b ON t1(b);
92 SELECT sum(a), sum(length(b)) FROM t1;
93 PRAGMA integrity_check;
97 } {wal 210 26000 ok wal 8192 12}
101 SELECT sum(a), sum(length(b)) FROM t1;
102 PRAGMA integrity_check;
107 } {210 26000 ok wal 8192 12}
109 # Corrupt the database again
110 do_catchsql_test 320 {
111 UPDATE sqlite_dbpage SET data=randomblob(8192) WHERE pgno=1;
113 } {1 {file is not a database}}
119 } {1 {file is not a database}}
121 # Reset the database yet again. Verify that the page size and
122 # journal mode are preserved.
125 sqlite3_db_config db RESET_DB 1
127 sqlite3_db_config db RESET_DB 0
134 } {0 {1 8192 wal ok}}