summaryrefslogtreecommitdiffstats
path: root/test/resetdb.test
blob: bdb835411017b40fe1c039ca6518ab389ade3855 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# 2018-04-28
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# Test cases for SQLITE_DBCONFIG_RESET_DATABASE
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix resetdb

do_not_use_codec

ifcapable !vtab||!compound {
  finish_test
  return
}

# In the "inmemory_journal" permutation, each new connection executes 
# "PRAGMA journal_mode = memory". This fails with SQLITE_BUSY if attempted
# on a wal mode database with existing connections. For this and a few
# other reasons, this test is not run as part of "inmemory_journal".
#
# Permutation "journaltest" does not support wal mode.
#
if {[permutation]=="inmemory_journal"
 || [permutation]=="journaltest"
} {
  finish_test
  return
}

# Create a sample database
do_execsql_test 100 {
  PRAGMA auto_vacuum = 0;
  PRAGMA page_size=4096;
  CREATE TABLE t1(a,b);
  WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<20)
    INSERT INTO t1(a,b) SELECT x, randomblob(300) FROM c;
  CREATE INDEX t1a ON t1(a);
  CREATE INDEX t1b ON t1(b);
  SELECT sum(a), sum(length(b)) FROM t1;
  PRAGMA integrity_check;
  PRAGMA journal_mode;
  PRAGMA page_count;
} {210 6000 ok delete 8}

# Verify that the same content is seen from a separate database connection
sqlite3 db2 test.db
do_test 110 {
  execsql {
    SELECT sum(a), sum(length(b)) FROM t1;
    PRAGMA integrity_check;
    PRAGMA journal_mode;
    PRAGMA page_count;
  } db2
} {210 6000 ok delete 8}

do_test 200 {
  # Thoroughly corrupt the database file by overwriting the first
  # page with randomness.
  sqlite3_db_config db DEFENSIVE 0
  catchsql {
    UPDATE sqlite_dbpage SET data=randomblob(4096) WHERE pgno=1;
    PRAGMA quick_check;
  }
} {1 {file is not a database}}
do_test 201 {
  catchsql {
    PRAGMA quick_check;
  } db2
} {1 {file is not a database}}

do_test 210 {
  # Reset the database file using SQLITE_DBCONFIG_RESET_DATABASE
  sqlite3_db_config db RESET_DB 1
  db eval VACUUM
  sqlite3_db_config db RESET_DB 0

  # If using sqlite3_prepare() instead of _v2() or _v3(), the block 
  # below raises an SQLITE_SCHEMA error. The following fixes this.
  if {[permutation]=="prepare"} { catchsql "SELECT * FROM sqlite_master" db2 }

  # Verify that the reset took, even on the separate database connection
  catchsql {
     PRAGMA page_count;
     PRAGMA page_size;
     PRAGMA quick_check;
     PRAGMA journal_mode;
  } db2
} {0 {1 4096 ok delete}}

# Delete the old connections and database and start over again
# with a different page size and in WAL mode.
#
db close
db2 close
forcedelete test.db
sqlite3 db test.db
do_execsql_test 300 {
  PRAGMA auto_vacuum = 0;
  PRAGMA page_size=8192;
  PRAGMA journal_mode=WAL;
  CREATE TABLE t1(a,b);
  WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<20)
    INSERT INTO t1(a,b) SELECT x, randomblob(1300) FROM c;
  CREATE INDEX t1a ON t1(a);
  CREATE INDEX t1b ON t1(b);
  SELECT sum(a), sum(length(b)) FROM t1;
  PRAGMA integrity_check;
  PRAGMA journal_mode;
  PRAGMA page_size;
  PRAGMA page_count;
} {wal 210 26000 ok wal 8192 12}
sqlite3 db2 test.db
do_test 310 {
  execsql {
    SELECT sum(a), sum(length(b)) FROM t1;
    PRAGMA integrity_check;
    PRAGMA journal_mode;
    PRAGMA page_size;
    PRAGMA page_count;
  } db2
} {210 26000 ok wal 8192 12}

# Corrupt the database again
sqlite3_db_config db DEFENSIVE 0
do_catchsql_test 320 {
  UPDATE sqlite_dbpage SET data=randomblob(8192) WHERE pgno=1;
  PRAGMA quick_check
} {1 {file is not a database}}

do_test 330 {
  catchsql {
    PRAGMA quick_check
  } db2
} {1 {file is not a database}}

db2 cache flush         ;# Required by permutation "prepare".

# Reset the database yet again.  Verify that the page size and
# journal mode are preserved.
#
do_test 400 {
  sqlite3_db_config db RESET_DB 1
  db eval VACUUM
  sqlite3_db_config db RESET_DB 0
  catchsql {
     PRAGMA page_count;
     PRAGMA page_size;
     PRAGMA journal_mode;
     PRAGMA quick_check;
  } db2
} {0 {1 8192 wal ok}}
db2 close

# Reset the database yet again. This time immediately after it is closed
# and reopened. So that the VACUUM is the first statement run.
#
db close
sqlite3 db test.db
do_test 500 {
  sqlite3_finalize [
    sqlite3_prepare db "SELECT 1 FROM sqlite_master LIMIT 1" -1 tail
  ]
  sqlite3_db_config db RESET_DB 1
  db eval VACUUM
  sqlite3_db_config db RESET_DB 0
  sqlite3 db2 test.db
  catchsql {
     PRAGMA page_count;
     PRAGMA page_size;
     PRAGMA journal_mode;
     PRAGMA quick_check;
  } db2
} {0 {1 8192 wal ok}}
db2 close

#-------------------------------------------------------------------------
reset_db
sqlite3 db2 test.db
do_execsql_test 600 {
  PRAGMA journal_mode = wal;
  CREATE TABLE t1(a);
  INSERT INTO t1 VALUES(1), (2), (3), (4);
} {wal}

do_execsql_test -db db2 610 {
  SELECT * FROM t1
} {1 2 3 4}

do_test 620 {
  set res [list]
  db2 eval {SELECT a FROM t1} {
    lappend res $a
    if {$a==3} {
      sqlite3_db_config db RESET_DB 1
      db eval VACUUM
      sqlite3_db_config db RESET_DB 0
    }
  }

  set res
} {1 2 3 4}

do_execsql_test -db db2 630 {
  SELECT * FROM sqlite_master
} {}

#-------------------------------------------------------------------------
db2 close
reset_db

do_execsql_test 700 {
  PRAGMA page_size=512;
  PRAGMA auto_vacuum = 0;
  CREATE TABLE t1(a,b,c);
  CREATE INDEX t1a ON t1(a);
  CREATE INDEX t1bc ON t1(b,c);
  WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<10)
    INSERT INTO t1(a,b,c) SELECT x, randomblob(100),randomblob(100) FROM c;
  PRAGMA page_count;
  PRAGMA integrity_check;
} {19 ok}

if {[nonzero_reserved_bytes]} {
  finish_test
  return
}

sqlite3_db_config db DEFENSIVE 0
do_execsql_test 710 {
  UPDATE sqlite_dbpage SET data=
    X'53514C69746520666F726D61742033000200030100402020000000000000001300000000000000000000000300000004000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000D00000003017C0001D801AC017C00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002E03061715110145696E6465787431626374310443524541544520494E4445582074316263204F4E20743128622C63292A0206171311013F696E64657874316174310343524541544520494E44455820743161204F4E20743128612926010617111101397461626C657431743102435245415445205441424C4520743128612C622C6329' WHERE pgno=1;
}

do_execsql_test 720 {
  PRAGMA integrity_check;
} {ok}

do_test 730 {
  sqlite3_db_config db RESET_DB 1
  db eval VACUUM
  sqlite3_db_config db RESET_DB 0
} {0}

do_execsql_test 740 {
  PRAGMA page_count;
  PRAGMA integrity_check;
} {1 ok}

finish_test