xref: /sqlite-3.40.0/test/resetdb.test (revision 867e6de4)
1# 2018-04-28
2#
3# The author disclaims copyright to this source code.  In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
11# Test cases for SQLITE_DBCONFIG_RESET_DATABASE
12#
13
14set testdir [file dirname $argv0]
15source $testdir/tester.tcl
16set testprefix resetdb
17
18ifcapable !vtab||!compound {
19  finish_test
20  return
21}
22
23# In the "inmemory_journal" permutation, each new connection executes
24# "PRAGMA journal_mode = memory". This fails with SQLITE_BUSY if attempted
25# on a wal mode database with existing connections. For this and a few
26# other reasons, this test is not run as part of "inmemory_journal".
27#
28# Permutation "journaltest" does not support wal mode.
29#
30if {[permutation]=="inmemory_journal"
31 || [permutation]=="journaltest"
32} {
33  finish_test
34  return
35}
36
37# Create a sample database
38do_execsql_test 100 {
39  PRAGMA auto_vacuum = 0;
40  PRAGMA page_size=4096;
41  CREATE TABLE t1(a,b);
42  WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<20)
43    INSERT INTO t1(a,b) SELECT x, randomblob(300) FROM c;
44  CREATE INDEX t1a ON t1(a);
45  CREATE INDEX t1b ON t1(b);
46  SELECT sum(a), sum(length(b)) FROM t1;
47  PRAGMA integrity_check;
48  PRAGMA journal_mode;
49  PRAGMA page_count;
50} {210 6000 ok delete 8}
51
52# Verify that the same content is seen from a separate database connection
53sqlite3 db2 test.db
54do_test 110 {
55  execsql {
56    SELECT sum(a), sum(length(b)) FROM t1;
57    PRAGMA integrity_check;
58    PRAGMA journal_mode;
59    PRAGMA page_count;
60  } db2
61} {210 6000 ok delete 8}
62
63do_test 200 {
64  # Thoroughly corrupt the database file by overwriting the first
65  # page with randomness.
66  catchsql {
67    UPDATE sqlite_dbpage SET data=randomblob(4096) WHERE pgno=1;
68    PRAGMA quick_check;
69  }
70} {1 {unsupported file format}}
71do_test 201 {
72  catchsql {
73    PRAGMA quick_check;
74  } db2
75} {1 {unsupported file format}}
76
77do_test 210 {
78  # Reset the database file using SQLITE_DBCONFIG_RESET_DATABASE
79  sqlite3_db_config db RESET_DB 1
80  db eval VACUUM
81  sqlite3_db_config db RESET_DB 0
82
83  # Verify that the reset took, even on the separate database connection
84  catchsql {
85     PRAGMA page_count;
86     PRAGMA page_size;
87     PRAGMA quick_check;
88     PRAGMA journal_mode;
89  } db2
90} {0 {1 4096 ok delete}}
91
92# Delete the old connections and database and start over again
93# with a different page size and in WAL mode.
94#
95db close
96db2 close
97forcedelete test.db
98sqlite3 db test.db
99do_execsql_test 300 {
100  PRAGMA auto_vacuum = 0;
101  PRAGMA page_size=8192;
102  PRAGMA journal_mode=WAL;
103  CREATE TABLE t1(a,b);
104  WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<20)
105    INSERT INTO t1(a,b) SELECT x, randomblob(1300) FROM c;
106  CREATE INDEX t1a ON t1(a);
107  CREATE INDEX t1b ON t1(b);
108  SELECT sum(a), sum(length(b)) FROM t1;
109  PRAGMA integrity_check;
110  PRAGMA journal_mode;
111  PRAGMA page_size;
112  PRAGMA page_count;
113} {wal 210 26000 ok wal 8192 12}
114sqlite3 db2 test.db
115do_test 310 {
116  execsql {
117    SELECT sum(a), sum(length(b)) FROM t1;
118    PRAGMA integrity_check;
119    PRAGMA journal_mode;
120    PRAGMA page_size;
121    PRAGMA page_count;
122  } db2
123} {210 26000 ok wal 8192 12}
124
125# Corrupt the database again
126do_catchsql_test 320 {
127  UPDATE sqlite_dbpage SET data=randomblob(8192) WHERE pgno=1;
128  PRAGMA quick_check
129} {1 {file is not a database}}
130
131do_test 330 {
132  catchsql {
133    PRAGMA quick_check
134  } db2
135} {1 {file is not a database}}
136
137db2 cache flush         ;# Required by permutation "prepare".
138
139# Reset the database yet again.  Verify that the page size and
140# journal mode are preserved.
141#
142do_test 400 {
143  sqlite3_db_config db RESET_DB 1
144  db eval VACUUM
145  sqlite3_db_config db RESET_DB 0
146  catchsql {
147     PRAGMA page_count;
148     PRAGMA page_size;
149     PRAGMA journal_mode;
150     PRAGMA quick_check;
151  } db2
152} {0 {1 8192 wal ok}}
153db2 close
154
155
156finish_test
157