xref: /sqlite-3.40.0/test/resetdb.test (revision cdf88760)
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# Create a sample database
24do_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}
36
37# Verify that the same content is seen from a separate database connection
38sqlite3 db2 test.db
39do_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}
47
48do_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}}
56do_test 201 {
57  catchsql {
58    PRAGMA quick_check;
59  } db2
60} {1 {unsupported file format}}
61
62do_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
67
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}}
76
77# Delete the old connections and database and start over again
78# with a different page size and in WAL mode.
79#
80db close
81db2 close
82forcedelete test.db
83sqlite3 db test.db
84do_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}
98sqlite3 db2 test.db
99do_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}
108
109# Corrupt the database again
110do_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}}
114
115do_test 330 {
116  catchsql {
117    PRAGMA quick_check
118  } db2
119} {1 {file is not a database}}
120
121# Reset the database yet again.  Verify that the page size and
122# journal mode are preserved.
123#
124do_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}}
135db2 close
136
137
138finish_test
139