Correct output for the fullkey column of json_each() when the total JSON
[sqlite.git] / test / resetdb.test
blob05e456a8d091a7c17c64044ff2f94ab73f038eed
1 # 2018-04-28
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 {
19   finish_test
20   return
23 # Create a sample database
24 do_execsql_test 100 {
25   PRAGMA page_size=4096;
26   CREATE TABLE t1(a,b);
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;
33   PRAGMA journal_mode;
34   PRAGMA page_count;
35 } {210 6000 ok delete 8}
37 # Verify that the same content is seen from a separate database connection
38 sqlite3 db2 test.db
39 do_test 110 {
40   execsql {
41     SELECT sum(a), sum(length(b)) FROM t1;
42     PRAGMA integrity_check;
43     PRAGMA journal_mode;
44     PRAGMA page_count;
45   } db2
46 } {210 6000 ok delete 8}
48 do_test 200 {
49   # Thoroughly corrupt the database file by overwriting the first
50   # page with randomness.
51   catchsql {
52     UPDATE sqlite_dbpage SET data=randomblob(4096) WHERE pgno=1;
53     PRAGMA quick_check;
54   }
55 } {1 {unsupported file format}}
56 do_test 201 {
57   catchsql {
58     PRAGMA quick_check;
59   } db2
60 } {1 {unsupported file format}}
62 do_test 210 {
63   # Reset the database file using SQLITE_DBCONFIG_RESET_DATABASE
64   sqlite3_db_config db RESET_DB 1
65   db eval VACUUM
66   sqlite3_db_config db RESET_DB 0
68   # Verify that the reset took, even on the separate database connection
69   catchsql {
70      PRAGMA page_count;
71      PRAGMA page_size;
72      PRAGMA quick_check;
73      PRAGMA journal_mode;
74   } db2
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.
80 db close
81 db2 close
82 forcedelete test.db
83 sqlite3 db test.db
84 do_execsql_test 300 {
85   PRAGMA page_size=8192;
86   PRAGMA journal_mode=WAL;
87   CREATE TABLE t1(a,b);
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;
94   PRAGMA journal_mode;
95   PRAGMA page_size;
96   PRAGMA page_count;
97 } {wal 210 26000 ok wal 8192 12}
98 sqlite3 db2 test.db
99 do_test 310 {
100   execsql {
101     SELECT sum(a), sum(length(b)) FROM t1;
102     PRAGMA integrity_check;
103     PRAGMA journal_mode;
104     PRAGMA page_size;
105     PRAGMA page_count;
106   } db2
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;
112   PRAGMA quick_check
113 } {1 {file is not a database}}
115 do_test 330 {
116   catchsql {
117     PRAGMA quick_check
118   } db2
119 } {1 {file is not a database}}
121 # Reset the database yet again.  Verify that the page size and
122 # journal mode are preserved.
124 do_test 400 {
125   sqlite3_db_config db RESET_DB 1
126   db eval VACUUM
127   sqlite3_db_config db RESET_DB 0
128   catchsql {
129      PRAGMA page_count;
130      PRAGMA page_size;
131      PRAGMA journal_mode;
132      PRAGMA quick_check;
133   } db2
134 } {0 {1 8192 wal ok}}
135 db2 close
138 finish_test