blob: 1db08b5ec9cd6735b5df5e147ea268c2a12234cf [file] [log] [blame]
dand3f8f942010-04-13 11:35:01 +00001# 2010 April 13
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# This file implements regression tests for SQLite library. The
12# focus of this file is testing the operation of the library in
13# "PRAGMA journal_mode=WAL" mode.
14#
dan7c246102010-04-12 19:00:29 +000015
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
dane264d982010-04-14 18:06:50 +000018source $testdir/lock_common.tcl
dana4a90952010-06-15 19:07:42 +000019source $testdir/malloc_common.tcl
dan10f5a502010-06-23 15:55:43 +000020source $testdir/wal_common.tcl
dan7c246102010-04-12 19:00:29 +000021
dan5cf53532010-05-01 16:40:20 +000022ifcapable !wal {finish_test ; return }
23
dan7c246102010-04-12 19:00:29 +000024proc reopen_db {} {
danb9bf16b2010-04-14 11:23:30 +000025 catch { db close }
dan31f98fc2010-04-27 05:42:32 +000026 file delete -force test.db test.db-wal test.db-wal-summary
dan7c246102010-04-12 19:00:29 +000027 sqlite3_wal db test.db
dan7c246102010-04-12 19:00:29 +000028}
29
dan67032392010-04-17 15:42:43 +000030set ::blobcnt 0
31proc blob {nByte} {
32 incr ::blobcnt
33 return [string range [string repeat "${::blobcnt}x" $nByte] 1 $nByte]
34}
35
dan7c246102010-04-12 19:00:29 +000036proc sqlite3_wal {args} {
37 eval sqlite3 $args
dan65bddc12010-05-07 12:49:22 +000038 [lindex $args 0] eval { PRAGMA auto_vacuum = 0 }
dane04dc882010-04-20 18:53:15 +000039 [lindex $args 0] eval { PRAGMA page_size = 1024 }
dan7c246102010-04-12 19:00:29 +000040 [lindex $args 0] eval { PRAGMA journal_mode = wal }
dan67032392010-04-17 15:42:43 +000041 [lindex $args 0] eval { PRAGMA synchronous = normal }
42 [lindex $args 0] function blob blob
dan7c246102010-04-12 19:00:29 +000043}
44
dan3de777f2010-04-17 12:31:37 +000045proc log_deleted {logfile} {
46 return [expr [file exists $logfile]==0]
47}
48
dan7c246102010-04-12 19:00:29 +000049#
50# These are 'warm-body' tests used while developing the WAL code. They
51# serve to prove that a few really simple cases work:
52#
53# wal-1.*: Read and write the database.
54# wal-2.*: Test MVCC with one reader, one writer.
55# wal-3.*: Test transaction rollback.
56# wal-4.*: Test savepoint/statement rollback.
57# wal-5.*: Test the temp database.
58# wal-6.*: Test creating databases with different page sizes.
59#
dan998ad212010-05-07 06:59:08 +000060#
dan4bcc4982010-08-16 19:23:02 +000061#
dan7c246102010-04-12 19:00:29 +000062do_test wal-0.1 {
dan65bddc12010-05-07 12:49:22 +000063 execsql { PRAGMA auto_vacuum = 0 }
dan67032392010-04-17 15:42:43 +000064 execsql { PRAGMA synchronous = normal }
dan7c246102010-04-12 19:00:29 +000065 execsql { PRAGMA journal_mode = wal }
66} {wal}
dane04dc882010-04-20 18:53:15 +000067do_test wal-0.2 {
68 file size test.db
69} {1024}
dan7c246102010-04-12 19:00:29 +000070
71do_test wal-1.0 {
72 execsql {
73 BEGIN;
74 CREATE TABLE t1(a, b);
75 }
dane04dc882010-04-20 18:53:15 +000076 list [file exists test.db-journal] \
77 [file exists test.db-wal] \
78 [file size test.db]
79} {0 1 1024}
dan7c246102010-04-12 19:00:29 +000080do_test wal-1.1 {
81 execsql COMMIT
82 list [file exists test.db-journal] [file exists test.db-wal]
83} {0 1}
84do_test wal-1.2 {
85 # There are now two pages in the log.
86 file size test.db-wal
dan10f5a502010-06-23 15:55:43 +000087} [wal_file_size 2 1024]
dan7c246102010-04-12 19:00:29 +000088
89do_test wal-1.3 {
90 execsql { SELECT * FROM sqlite_master }
91} {table t1 t1 2 {CREATE TABLE t1(a, b)}}
92
93do_test wal-1.4 {
94 execsql { INSERT INTO t1 VALUES(1, 2) }
95 execsql { INSERT INTO t1 VALUES(3, 4) }
96 execsql { INSERT INTO t1 VALUES(5, 6) }
97 execsql { INSERT INTO t1 VALUES(7, 8) }
98 execsql { INSERT INTO t1 VALUES(9, 10) }
99} {}
100
101do_test wal-1.5 {
102 execsql { SELECT * FROM t1 }
103} {1 2 3 4 5 6 7 8 9 10}
104
105do_test wal-2.1 {
106 sqlite3_wal db2 ./test.db
107 execsql { BEGIN; SELECT * FROM t1 } db2
108} {1 2 3 4 5 6 7 8 9 10}
109
110do_test wal-2.2 {
111 execsql { INSERT INTO t1 VALUES(11, 12) }
112 execsql { SELECT * FROM t1 }
113} {1 2 3 4 5 6 7 8 9 10 11 12}
114
115do_test wal-2.3 {
116 execsql { SELECT * FROM t1 } db2
117} {1 2 3 4 5 6 7 8 9 10}
118
119do_test wal-2.4 {
120 execsql { INSERT INTO t1 VALUES(13, 14) }
121 execsql { SELECT * FROM t1 }
122} {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
123
124do_test wal-2.5 {
125 execsql { SELECT * FROM t1 } db2
126} {1 2 3 4 5 6 7 8 9 10}
127
128do_test wal-2.6 {
129 execsql { COMMIT; SELECT * FROM t1 } db2
130} {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
131
132do_test wal-3.1 {
133 execsql { BEGIN; DELETE FROM t1 }
134 execsql { SELECT * FROM t1 }
135} {}
136do_test wal-3.2 {
137 execsql { SELECT * FROM t1 } db2
138} {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
139do_test wal-3.3 {
140 execsql { ROLLBACK }
141 execsql { SELECT * FROM t1 }
142} {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
143db2 close
144
dan74d6cd82010-04-24 18:44:05 +0000145#-------------------------------------------------------------------------
146# The following tests, wal-4.*, test that savepoints work with WAL
147# databases.
148#
dan7c246102010-04-12 19:00:29 +0000149do_test wal-4.1 {
150 execsql {
151 DELETE FROM t1;
152 BEGIN;
153 INSERT INTO t1 VALUES('a', 'b');
154 SAVEPOINT sp;
155 INSERT INTO t1 VALUES('c', 'd');
156 SELECT * FROM t1;
157 }
158} {a b c d}
159do_test wal-4.2 {
160 execsql {
161 ROLLBACK TO sp;
162 SELECT * FROM t1;
163 }
164} {a b}
165do_test wal-4.3 {
166 execsql {
167 COMMIT;
168 SELECT * FROM t1;
169 }
170} {a b}
171
dan4cd78b42010-04-26 16:57:10 +0000172do_test wal-4.4.1 {
dan74d6cd82010-04-24 18:44:05 +0000173 db close
174 sqlite3 db test.db
175 db func blob blob
176 list [execsql { SELECT * FROM t1 }] [file size test.db-wal]
177} {{a b} 0}
dan4cd78b42010-04-26 16:57:10 +0000178do_test wal-4.4.2 {
dan74d6cd82010-04-24 18:44:05 +0000179 execsql { PRAGMA cache_size = 10 }
180 execsql {
181 CREATE TABLE t2(a, b);
182 INSERT INTO t2 VALUES(blob(400), blob(400));
183 SAVEPOINT tr;
184 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 2 */
185 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 4 */
186 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 8 */
187 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 16 */
188 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 32 */
189 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 2 */
190 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 4 */
191 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 8 */
192 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 16 */
193 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 32 */
194 SELECT count(*) FROM t2;
195 }
196} {32}
dan4cd78b42010-04-26 16:57:10 +0000197do_test wal-4.4.3 {
dan74d6cd82010-04-24 18:44:05 +0000198 execsql { ROLLBACK TO tr }
199} {}
dan4cd78b42010-04-26 16:57:10 +0000200do_test wal-4.4.4 {
dan74d6cd82010-04-24 18:44:05 +0000201 set logsize [file size test.db-wal]
202 execsql {
203 INSERT INTO t1 VALUES('x', 'y');
204 RELEASE tr;
205 }
206 expr { $logsize == [file size test.db-wal] }
207} {1}
dan4cd78b42010-04-26 16:57:10 +0000208do_test wal-4.4.5 {
dan74d6cd82010-04-24 18:44:05 +0000209 execsql { SELECT count(*) FROM t2 }
210} {1}
dan4cd78b42010-04-26 16:57:10 +0000211do_test wal-4.4.6 {
dan74d6cd82010-04-24 18:44:05 +0000212 file copy -force test.db test2.db
213 file copy -force test.db-wal test2.db-wal
214 sqlite3 db2 test2.db
215 execsql { SELECT count(*) FROM t2 ; SELECT count(*) FROM t1 } db2
216} {1 2}
dan4cd78b42010-04-26 16:57:10 +0000217do_test wal-4.4.7 {
dan74d6cd82010-04-24 18:44:05 +0000218 execsql { PRAGMA integrity_check } db2
219} {ok}
220db2 close
221
dan4cd78b42010-04-26 16:57:10 +0000222do_test wal-4.5.1 {
223 reopen_db
224 db func blob blob
225 execsql {
226 PRAGMA journal_mode = WAL;
227 CREATE TABLE t1(a, b);
228 INSERT INTO t1 VALUES('a', 'b');
229 }
230 sqlite3 db test.db
231 db func blob blob
232 list [execsql { SELECT * FROM t1 }] [file size test.db-wal]
233} {{a b} 0}
234do_test wal-4.5.2 {
235 execsql { PRAGMA cache_size = 10 }
236 execsql {
237 CREATE TABLE t2(a, b);
238 BEGIN;
239 INSERT INTO t2 VALUES(blob(400), blob(400));
240 SAVEPOINT tr;
241 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 2 */
242 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 4 */
243 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 8 */
244 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 16 */
245 INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 32 */
246 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 2 */
247 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 4 */
248 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 8 */
249 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 16 */
250 INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 32 */
251 SELECT count(*) FROM t2;
252 }
253} {32}
254do_test wal-4.5.3 {
dan4cd78b42010-04-26 16:57:10 +0000255 execsql { ROLLBACK TO tr }
256} {}
257do_test wal-4.5.4 {
258 set logsize [file size test.db-wal]
dan4cd78b42010-04-26 16:57:10 +0000259 execsql {
260 INSERT INTO t1 VALUES('x', 'y');
261 RELEASE tr;
262 COMMIT;
263 }
264 expr { $logsize == [file size test.db-wal] }
265} {1}
266do_test wal-4.5.5 {
267 execsql { SELECT count(*) FROM t2 ; SELECT count(*) FROM t1 }
268} {1 2}
269do_test wal-4.5.6 {
270 file copy -force test.db test2.db
271 file copy -force test.db-wal test2.db-wal
272 sqlite3 db2 test2.db
dan4cd78b42010-04-26 16:57:10 +0000273 execsql { SELECT count(*) FROM t2 ; SELECT count(*) FROM t1 } db2
274} {1 2}
275do_test wal-4.5.7 {
276 execsql { PRAGMA integrity_check } db2
277} {ok}
278db2 close
279
danb7d53f52010-05-06 17:28:08 +0000280do_test wal-4.6.1 {
281 execsql {
282 DELETE FROM t2;
283 PRAGMA wal_checkpoint;
284 BEGIN;
285 INSERT INTO t2 VALUES('w', 'x');
286 SAVEPOINT save;
287 INSERT INTO t2 VALUES('y', 'z');
288 ROLLBACK TO save;
289 COMMIT;
290 SELECT * FROM t2;
291 }
292} {w x}
293
dan4cd78b42010-04-26 16:57:10 +0000294
dan74d6cd82010-04-24 18:44:05 +0000295reopen_db
dan7c246102010-04-12 19:00:29 +0000296do_test wal-5.1 {
297 execsql {
298 CREATE TEMP TABLE t2(a, b);
299 INSERT INTO t2 VALUES(1, 2);
300 }
301} {}
302do_test wal-5.2 {
303 execsql {
304 BEGIN;
305 INSERT INTO t2 VALUES(3, 4);
306 SELECT * FROM t2;
307 }
308} {1 2 3 4}
309do_test wal-5.3 {
310 execsql {
311 ROLLBACK;
312 SELECT * FROM t2;
313 }
314} {1 2}
315do_test wal-5.4 {
316 execsql {
317 CREATE TEMP TABLE t3(x UNIQUE);
318 BEGIN;
319 INSERT INTO t2 VALUES(3, 4);
320 INSERT INTO t3 VALUES('abc');
321 }
322 catchsql { INSERT INTO t3 VALUES('abc') }
323} {1 {column x is not unique}}
324do_test wal-5.5 {
325 execsql {
326 COMMIT;
327 SELECT * FROM t2;
328 }
329} {1 2 3 4}
330db close
331
dan7c246102010-04-12 19:00:29 +0000332foreach sector {512 4096} {
333 sqlite3_simulate_device -sectorsize $sector
334 foreach pgsz {512 1024 2048 4096} {
335 file delete -force test.db test.db-wal
336 do_test wal-6.$sector.$pgsz.1 {
dane04dc882010-04-20 18:53:15 +0000337 sqlite3 db test.db -vfs devsym
dan7c246102010-04-12 19:00:29 +0000338 execsql "
dane04dc882010-04-20 18:53:15 +0000339 PRAGMA page_size = $pgsz;
dan65bddc12010-05-07 12:49:22 +0000340 PRAGMA auto_vacuum = 0;
dane04dc882010-04-20 18:53:15 +0000341 PRAGMA journal_mode = wal;
dan7c246102010-04-12 19:00:29 +0000342 "
343 execsql "
344 CREATE TABLE t1(a, b);
345 INSERT INTO t1 VALUES(1, 2);
346 "
347 db close
348 file size test.db
349 } [expr $pgsz*2]
350
351 do_test wal-6.$sector.$pgsz.2 {
dan3de777f2010-04-17 12:31:37 +0000352 log_deleted test.db-wal
353 } {1}
dan7c246102010-04-12 19:00:29 +0000354 }
355}
356
357do_test wal-7.1 {
358 file delete -force test.db test.db-wal
359 sqlite3_wal db test.db
360 execsql {
361 PRAGMA page_size = 1024;
362 CREATE TABLE t1(a, b);
363 INSERT INTO t1 VALUES(1, 2);
364 }
dan7c246102010-04-12 19:00:29 +0000365 list [file size test.db] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000366} [list 1024 [wal_file_size 3 1024]]
dan7c246102010-04-12 19:00:29 +0000367do_test wal-7.2 {
dan5a299f92010-05-03 11:05:08 +0000368 execsql { PRAGMA wal_checkpoint }
dan7c246102010-04-12 19:00:29 +0000369 list [file size test.db] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000370} [list 2048 [wal_file_size 3 1024]]
dan7c246102010-04-12 19:00:29 +0000371
dan7c246102010-04-12 19:00:29 +0000372# Execute some transactions in auto-vacuum mode to test database file
373# truncation.
374#
danb9bf16b2010-04-14 11:23:30 +0000375do_test wal-8.1 {
dan7c246102010-04-12 19:00:29 +0000376 reopen_db
dane04dc882010-04-20 18:53:15 +0000377 catch { db close }
378 file delete -force test.db test.db-wal
379
380 sqlite3 db test.db
381 db function blob blob
dan7c246102010-04-12 19:00:29 +0000382 execsql {
383 PRAGMA auto_vacuum = 1;
dane04dc882010-04-20 18:53:15 +0000384 PRAGMA journal_mode = wal;
dan7c246102010-04-12 19:00:29 +0000385 PRAGMA auto_vacuum;
386 }
dane04dc882010-04-20 18:53:15 +0000387} {wal 1}
danb9bf16b2010-04-14 11:23:30 +0000388do_test wal-8.2 {
dan7c246102010-04-12 19:00:29 +0000389 execsql {
390 PRAGMA page_size = 1024;
391 CREATE TABLE t1(x);
dan67032392010-04-17 15:42:43 +0000392 INSERT INTO t1 VALUES(blob(900));
393 INSERT INTO t1 VALUES(blob(900));
394 INSERT INTO t1 SELECT blob(900) FROM t1; /* 4 */
395 INSERT INTO t1 SELECT blob(900) FROM t1; /* 8 */
396 INSERT INTO t1 SELECT blob(900) FROM t1; /* 16 */
397 INSERT INTO t1 SELECT blob(900) FROM t1; /* 32 */
398 INSERT INTO t1 SELECT blob(900) FROM t1; /* 64 */
dan5a299f92010-05-03 11:05:08 +0000399 PRAGMA wal_checkpoint;
dan7c246102010-04-12 19:00:29 +0000400 }
401 file size test.db
dan80a15262010-04-13 11:45:31 +0000402} [expr 68*1024]
danb9bf16b2010-04-14 11:23:30 +0000403do_test wal-8.3 {
dan7c246102010-04-12 19:00:29 +0000404 execsql {
405 DELETE FROM t1 WHERE rowid<54;
dan5a299f92010-05-03 11:05:08 +0000406 PRAGMA wal_checkpoint;
dan7c246102010-04-12 19:00:29 +0000407 }
408 file size test.db
409} [expr 14*1024]
410
411# Run some "warm-body" tests to ensure that log-summary files with more
412# than 256 entries (log summaries that contain index blocks) work Ok.
413#
danb9bf16b2010-04-14 11:23:30 +0000414do_test wal-9.1 {
dan7c246102010-04-12 19:00:29 +0000415 reopen_db
416 execsql {
dan7c246102010-04-12 19:00:29 +0000417 CREATE TABLE t1(x PRIMARY KEY);
dan67032392010-04-17 15:42:43 +0000418 INSERT INTO t1 VALUES(blob(900));
419 INSERT INTO t1 VALUES(blob(900));
420 INSERT INTO t1 SELECT blob(900) FROM t1; /* 4 */
421 INSERT INTO t1 SELECT blob(900) FROM t1; /* 8 */
422 INSERT INTO t1 SELECT blob(900) FROM t1; /* 16 */
423 INSERT INTO t1 SELECT blob(900) FROM t1; /* 32 */
424 INSERT INTO t1 SELECT blob(900) FROM t1; /* 64 */
425 INSERT INTO t1 SELECT blob(900) FROM t1; /* 128 */
426 INSERT INTO t1 SELECT blob(900) FROM t1; /* 256 */
dan7c246102010-04-12 19:00:29 +0000427 }
428 file size test.db
dane04dc882010-04-20 18:53:15 +0000429} 1024
danb9bf16b2010-04-14 11:23:30 +0000430do_test wal-9.2 {
dan7c246102010-04-12 19:00:29 +0000431 sqlite3_wal db2 test.db
432 execsql {PRAGMA integrity_check } db2
433} {ok}
434
danb9bf16b2010-04-14 11:23:30 +0000435do_test wal-9.3 {
dan7c246102010-04-12 19:00:29 +0000436 file delete -force test2.db test2.db-wal
437 file copy test.db test2.db
438 file copy test.db-wal test2.db-wal
439 sqlite3_wal db3 test2.db
440 execsql {PRAGMA integrity_check } db3
441} {ok}
442db3 close
443
danb9bf16b2010-04-14 11:23:30 +0000444do_test wal-9.4 {
dan5a299f92010-05-03 11:05:08 +0000445 execsql { PRAGMA wal_checkpoint }
dan7c246102010-04-12 19:00:29 +0000446 db2 close
447 sqlite3_wal db2 test.db
448 execsql {PRAGMA integrity_check } db2
449} {ok}
450
dan80a15262010-04-13 11:45:31 +0000451foreach handle {db db2 db3} { catch { $handle close } }
452unset handle
453
danb9bf16b2010-04-14 11:23:30 +0000454#-------------------------------------------------------------------------
455# The following block of tests - wal-10.* - test that the WAL locking
dane264d982010-04-14 18:06:50 +0000456# scheme works in simple cases. This block of tests is run twice. Once
457# using multiple connections in the address space of the current process,
458# and once with all connections except one running in external processes.
danb9bf16b2010-04-14 11:23:30 +0000459#
dana4a90952010-06-15 19:07:42 +0000460do_multiclient_test tn {
dane264d982010-04-14 18:06:50 +0000461
462 # Initialize the database schema and contents.
463 #
464 do_test wal-10.$tn.1 {
465 execsql {
dana4a90952010-06-15 19:07:42 +0000466 PRAGMA journal_mode = wal;
dane264d982010-04-14 18:06:50 +0000467 CREATE TABLE t1(a, b);
468 INSERT INTO t1 VALUES(1, 2);
danb9bf16b2010-04-14 11:23:30 +0000469 SELECT * FROM t1;
dane264d982010-04-14 18:06:50 +0000470 }
dana4a90952010-06-15 19:07:42 +0000471 } {wal 1 2}
danb9bf16b2010-04-14 11:23:30 +0000472
dane264d982010-04-14 18:06:50 +0000473 # Open a transaction and write to the database using [db]. Check that [db2]
474 # is still able to read the snapshot before the transaction was opened.
475 #
476 do_test wal-10.$tn.2 {
477 execsql { BEGIN; INSERT INTO t1 VALUES(3, 4); }
478 sql2 {SELECT * FROM t1}
479 } {1 2}
480
481 # Have [db] commit the transaction. Check that [db2] is now seeing the
482 # new, updated snapshot.
483 #
484 do_test wal-10.$tn.3 {
485 execsql { COMMIT }
486 sql2 {SELECT * FROM t1}
487 } {1 2 3 4}
488
489 # Have [db2] open a read transaction. Then write to the db via [db]. Check
490 # that [db2] is still seeing the original snapshot. Then read with [db3].
491 # [db3] should see the newly committed data.
492 #
493 do_test wal-10.$tn.4 {
494 sql2 { BEGIN ; SELECT * FROM t1}
495 } {1 2 3 4}
496 do_test wal-10.$tn.5 {
497 execsql { INSERT INTO t1 VALUES(5, 6); }
498 sql2 {SELECT * FROM t1}
499 } {1 2 3 4}
500 do_test wal-10.$tn.6 {
501 sql3 {SELECT * FROM t1}
502 } {1 2 3 4 5 6}
503 do_test wal-10.$tn.7 {
504 sql2 COMMIT
505 } {}
506
507 # Have [db2] open a write transaction. Then attempt to write to the
508 # database via [db]. This should fail (writer lock cannot be obtained).
509 #
510 # Then open a read-transaction with [db]. Commit the [db2] transaction
511 # to disk. Verify that [db] still cannot write to the database (because
512 # it is reading an old snapshot).
513 #
514 # Close the current [db] transaction. Open a new one. [db] can now write
515 # to the database (as it is not locked and [db] is reading the latest
516 # snapshot).
517 #
518 do_test wal-10.$tn.7 {
519 sql2 { BEGIN; INSERT INTO t1 VALUES(7, 8) ; }
520 catchsql { INSERT INTO t1 VALUES(9, 10) }
521 } {1 {database is locked}}
522 do_test wal-10.$tn.8 {
523 execsql { BEGIN ; SELECT * FROM t1 }
524 } {1 2 3 4 5 6}
525 do_test wal-10.$tn.9 {
526 sql2 COMMIT
527 catchsql { INSERT INTO t1 VALUES(9, 10) }
528 } {1 {database is locked}}
529 do_test wal-10.$tn.10 {
drh7e263722010-05-20 21:21:09 +0000530 execsql { COMMIT }
531 execsql { BEGIN }
532 execsql { INSERT INTO t1 VALUES(9, 10) }
533 execsql { COMMIT }
dane264d982010-04-14 18:06:50 +0000534 execsql { SELECT * FROM t1 }
535 } {1 2 3 4 5 6 7 8 9 10}
536
537 # Open a read transaction with [db2]. Check that this prevents [db] from
538 # checkpointing the database. But not from writing to it.
539 #
540 do_test wal-10.$tn.11 {
541 sql2 { BEGIN; SELECT * FROM t1 }
542 } {1 2 3 4 5 6 7 8 9 10}
543 do_test wal-10.$tn.12 {
dan5a299f92010-05-03 11:05:08 +0000544 catchsql { PRAGMA wal_checkpoint }
drh34116ea2010-05-31 12:30:52 +0000545 } {0 {}} ;# Reader no longer block checkpoints
dane264d982010-04-14 18:06:50 +0000546 do_test wal-10.$tn.13 {
547 execsql { INSERT INTO t1 VALUES(11, 12) }
548 sql2 {SELECT * FROM t1}
549 } {1 2 3 4 5 6 7 8 9 10}
550
drh34116ea2010-05-31 12:30:52 +0000551 # Writers do not block checkpoints any more either.
dane264d982010-04-14 18:06:50 +0000552 #
dane264d982010-04-14 18:06:50 +0000553 do_test wal-10.$tn.14 {
drh34116ea2010-05-31 12:30:52 +0000554 catchsql { PRAGMA wal_checkpoint }
555 } {0 {}}
danb9bf16b2010-04-14 11:23:30 +0000556
drha2ac9df2010-05-31 13:11:49 +0000557 # The following series of test cases used to verify another blocking
558 # case in WAL - a case which no longer blocks.
dane264d982010-04-14 18:06:50 +0000559 #
dane264d982010-04-14 18:06:50 +0000560 do_test wal-10.$tn.15 {
drh34116ea2010-05-31 12:30:52 +0000561 sql2 { COMMIT; BEGIN; SELECT * FROM t1; }
dane264d982010-04-14 18:06:50 +0000562 } {1 2 3 4 5 6 7 8 9 10 11 12}
563 do_test wal-10.$tn.16 {
dan5a299f92010-05-03 11:05:08 +0000564 catchsql { PRAGMA wal_checkpoint }
drha2ac9df2010-05-31 13:11:49 +0000565 } {0 {}}
dan49320f82010-04-14 18:50:08 +0000566 do_test wal-10.$tn.17 {
dan5a299f92010-05-03 11:05:08 +0000567 execsql { PRAGMA wal_checkpoint }
dane264d982010-04-14 18:06:50 +0000568 } {}
dan49320f82010-04-14 18:50:08 +0000569 do_test wal-10.$tn.18 {
drha2ac9df2010-05-31 13:11:49 +0000570 sql3 { BEGIN; SELECT * FROM t1 }
dane264d982010-04-14 18:06:50 +0000571 } {1 2 3 4 5 6 7 8 9 10 11 12}
dan49320f82010-04-14 18:50:08 +0000572 do_test wal-10.$tn.19 {
dane264d982010-04-14 18:06:50 +0000573 catchsql { INSERT INTO t1 VALUES(13, 14) }
drha2ac9df2010-05-31 13:11:49 +0000574 } {0 {}}
dan49320f82010-04-14 18:50:08 +0000575 do_test wal-10.$tn.20 {
dane264d982010-04-14 18:06:50 +0000576 execsql { SELECT * FROM t1 }
drha2ac9df2010-05-31 13:11:49 +0000577 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
dan49320f82010-04-14 18:50:08 +0000578 do_test wal-10.$tn.21 {
dane264d982010-04-14 18:06:50 +0000579 sql3 COMMIT
drha2ac9df2010-05-31 13:11:49 +0000580 sql2 COMMIT
dane264d982010-04-14 18:06:50 +0000581 } {}
dan49320f82010-04-14 18:50:08 +0000582 do_test wal-10.$tn.22 {
dane264d982010-04-14 18:06:50 +0000583 execsql { SELECT * FROM t1 }
584 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
danb9bf16b2010-04-14 11:23:30 +0000585
drha2ac9df2010-05-31 13:11:49 +0000586 # Another series of tests that used to demonstrate blocking behavior
587 # but which now work.
dan49320f82010-04-14 18:50:08 +0000588 #
589 do_test wal-10.$tn.23 {
dan5a299f92010-05-03 11:05:08 +0000590 execsql { PRAGMA wal_checkpoint }
dan49320f82010-04-14 18:50:08 +0000591 } {}
592 do_test wal-10.$tn.24 {
593 sql2 { BEGIN; SELECT * FROM t1; }
594 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
595 do_test wal-10.$tn.25 {
dan5a299f92010-05-03 11:05:08 +0000596 execsql { PRAGMA wal_checkpoint }
dan49320f82010-04-14 18:50:08 +0000597 } {}
598 do_test wal-10.$tn.26 {
599 catchsql { INSERT INTO t1 VALUES(15, 16) }
drha2ac9df2010-05-31 13:11:49 +0000600 } {0 {}}
dan49320f82010-04-14 18:50:08 +0000601 do_test wal-10.$tn.27 {
drha2ac9df2010-05-31 13:11:49 +0000602 sql3 { INSERT INTO t1 VALUES(17, 18) }
dan49320f82010-04-14 18:50:08 +0000603 } {}
604 do_test wal-10.$tn.28 {
605 code3 {
606 set ::STMT [sqlite3_prepare db3 "SELECT * FROM t1" -1 TAIL]
607 sqlite3_step $::STMT
608 }
dan49320f82010-04-14 18:50:08 +0000609 execsql { SELECT * FROM t1 }
drha2ac9df2010-05-31 13:11:49 +0000610 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18}
dan49320f82010-04-14 18:50:08 +0000611 do_test wal-10.$tn.29 {
drha2ac9df2010-05-31 13:11:49 +0000612 execsql { INSERT INTO t1 VALUES(19, 20) }
dan5a299f92010-05-03 11:05:08 +0000613 catchsql { PRAGMA wal_checkpoint }
drha2ac9df2010-05-31 13:11:49 +0000614 } {0 {}}
dan49320f82010-04-14 18:50:08 +0000615 do_test wal-10.$tn.30 {
616 code3 { sqlite3_finalize $::STMT }
dan5a299f92010-05-03 11:05:08 +0000617 execsql { PRAGMA wal_checkpoint }
dan49320f82010-04-14 18:50:08 +0000618 } {}
619
620 # At one point, if a reader failed to upgrade to a writer because it
621 # was reading an old snapshot, the write-locks were not being released.
622 # Test that this bug has been fixed.
623 #
624 do_test wal-10.$tn.31 {
drha2ac9df2010-05-31 13:11:49 +0000625 sql2 COMMIT
dan49320f82010-04-14 18:50:08 +0000626 execsql { BEGIN ; SELECT * FROM t1 }
drha2ac9df2010-05-31 13:11:49 +0000627 sql2 { INSERT INTO t1 VALUES(21, 22) }
628 catchsql { INSERT INTO t1 VALUES(23, 24) }
dan49320f82010-04-14 18:50:08 +0000629 } {1 {database is locked}}
630 do_test wal-10.$tn.32 {
631 # This statement would fail when the bug was present.
drha2ac9df2010-05-31 13:11:49 +0000632 sql2 { INSERT INTO t1 VALUES(23, 24) }
dan49320f82010-04-14 18:50:08 +0000633 } {}
634 do_test wal-10.$tn.33 {
635 execsql { SELECT * FROM t1 ; COMMIT }
drha2ac9df2010-05-31 13:11:49 +0000636 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20}
dan49320f82010-04-14 18:50:08 +0000637 do_test wal-10.$tn.34 {
638 execsql { SELECT * FROM t1 }
drha2ac9df2010-05-31 13:11:49 +0000639 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24}
dan49320f82010-04-14 18:50:08 +0000640
dan8b348af2010-04-27 18:43:16 +0000641 # Test that if a checkpointer cannot obtain the required locks, it
642 # releases all locks before returning a busy error.
643 #
644 do_test wal-10.$tn.35 {
645 execsql {
646 DELETE FROM t1;
647 INSERT INTO t1 VALUES('a', 'b');
648 INSERT INTO t1 VALUES('c', 'd');
649 }
650 sql2 {
651 BEGIN;
652 SELECT * FROM t1;
653 }
654 } {a b c d}
dan8b348af2010-04-27 18:43:16 +0000655 do_test wal-10.$tn.36 {
dan5a299f92010-05-03 11:05:08 +0000656 catchsql { PRAGMA wal_checkpoint }
drha2ac9df2010-05-31 13:11:49 +0000657 } {0 {}}
dan8b348af2010-04-27 18:43:16 +0000658 do_test wal-10.$tn.36 {
659 sql3 { INSERT INTO t1 VALUES('e', 'f') }
660 sql2 { SELECT * FROM t1 }
661 } {a b c d}
662 do_test wal-10.$tn.37 {
663 sql2 COMMIT
dan5a299f92010-05-03 11:05:08 +0000664 execsql { PRAGMA wal_checkpoint }
dan8b348af2010-04-27 18:43:16 +0000665 } {}
dane264d982010-04-14 18:06:50 +0000666}
667
dan4cc6fb62010-04-15 16:45:34 +0000668#-------------------------------------------------------------------------
669# This block of tests, wal-11.*, test that nothing goes terribly wrong
670# if frames must be written to the log file before a transaction is
671# committed (in order to free up memory).
672#
673do_test wal-11.1 {
674 reopen_db
675 execsql {
676 PRAGMA cache_size = 10;
677 PRAGMA page_size = 1024;
678 CREATE TABLE t1(x PRIMARY KEY);
679 }
680 list [expr [file size test.db]/1024] [expr [file size test.db-wal]/1044]
dane04dc882010-04-20 18:53:15 +0000681} {1 3}
dan4cc6fb62010-04-15 16:45:34 +0000682do_test wal-11.2 {
dan5a299f92010-05-03 11:05:08 +0000683 execsql { PRAGMA wal_checkpoint }
dan97a31352010-04-16 13:59:31 +0000684 list [expr [file size test.db]/1024] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000685} [list 3 [wal_file_size 3 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000686do_test wal-11.3 {
dan67032392010-04-17 15:42:43 +0000687 execsql { INSERT INTO t1 VALUES( blob(900) ) }
dan97a31352010-04-16 13:59:31 +0000688 list [expr [file size test.db]/1024] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000689} [list 3 [wal_file_size 4 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000690
691do_test wal-11.4 {
692 execsql {
693 BEGIN;
dan67032392010-04-17 15:42:43 +0000694 INSERT INTO t1 SELECT blob(900) FROM t1; -- 2
695 INSERT INTO t1 SELECT blob(900) FROM t1; -- 4
696 INSERT INTO t1 SELECT blob(900) FROM t1; -- 8
697 INSERT INTO t1 SELECT blob(900) FROM t1; -- 16
dan4cc6fb62010-04-15 16:45:34 +0000698 }
dan97a31352010-04-16 13:59:31 +0000699 list [expr [file size test.db]/1024] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000700} [list 3 [wal_file_size 32 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000701do_test wal-11.5 {
702 execsql {
703 SELECT count(*) FROM t1;
704 PRAGMA integrity_check;
705 }
706} {16 ok}
707do_test wal-11.6 {
708 execsql COMMIT
dan97a31352010-04-16 13:59:31 +0000709 list [expr [file size test.db]/1024] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000710} [list 3 [wal_file_size 41 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000711do_test wal-11.7 {
712 execsql {
713 SELECT count(*) FROM t1;
714 PRAGMA integrity_check;
715 }
716} {16 ok}
717do_test wal-11.8 {
dan5a299f92010-05-03 11:05:08 +0000718 execsql { PRAGMA wal_checkpoint }
dan97a31352010-04-16 13:59:31 +0000719 list [expr [file size test.db]/1024] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000720} [list 37 [wal_file_size 41 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000721do_test wal-11.9 {
722 db close
dan3de777f2010-04-17 12:31:37 +0000723 list [expr [file size test.db]/1024] [log_deleted test.db-wal]
724} {37 1}
dan67032392010-04-17 15:42:43 +0000725sqlite3_wal db test.db
dan4cc6fb62010-04-15 16:45:34 +0000726do_test wal-11.10 {
727 execsql {
728 PRAGMA cache_size = 10;
729 BEGIN;
dan67032392010-04-17 15:42:43 +0000730 INSERT INTO t1 SELECT blob(900) FROM t1; -- 32
dan4cc6fb62010-04-15 16:45:34 +0000731 SELECT count(*) FROM t1;
732 }
dan97a31352010-04-16 13:59:31 +0000733 list [expr [file size test.db]/1024] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000734} [list 37 [wal_file_size 37 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000735do_test wal-11.11 {
736 execsql {
737 SELECT count(*) FROM t1;
738 ROLLBACK;
739 SELECT count(*) FROM t1;
740 }
741} {32 16}
742do_test wal-11.12 {
dan97a31352010-04-16 13:59:31 +0000743 list [expr [file size test.db]/1024] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000744} [list 37 [wal_file_size 37 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000745do_test wal-11.13 {
746 execsql {
dan67032392010-04-17 15:42:43 +0000747 INSERT INTO t1 VALUES( blob(900) );
dan4cc6fb62010-04-15 16:45:34 +0000748 SELECT count(*) FROM t1;
749 PRAGMA integrity_check;
750 }
751} {17 ok}
752do_test wal-11.14 {
dan97a31352010-04-16 13:59:31 +0000753 list [expr [file size test.db]/1024] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000754} [list 37 [wal_file_size 37 1024]]
dan4cc6fb62010-04-15 16:45:34 +0000755
756
dan4a4b01d2010-04-16 11:30:18 +0000757#-------------------------------------------------------------------------
dan97a31352010-04-16 13:59:31 +0000758# This block of tests, wal-12.*, tests the fix for a problem that
759# could occur if a log that is a prefix of an older log is written
760# into a reused log file.
dan4a4b01d2010-04-16 11:30:18 +0000761#
762reopen_db
763do_test wal-12.1 {
764 execsql {
765 PRAGMA page_size = 1024;
766 CREATE TABLE t1(x, y);
767 CREATE TABLE t2(x, y);
768 INSERT INTO t1 VALUES('A', 1);
769 }
dan97a31352010-04-16 13:59:31 +0000770 list [expr [file size test.db]/1024] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +0000771} [list 1 [wal_file_size 5 1024]]
dan4a4b01d2010-04-16 11:30:18 +0000772do_test wal-12.2 {
773 db close
dane04dc882010-04-20 18:53:15 +0000774 sqlite3 db test.db
dan4a4b01d2010-04-16 11:30:18 +0000775 execsql {
dane04dc882010-04-20 18:53:15 +0000776 PRAGMA synchronous = normal;
dan4a4b01d2010-04-16 11:30:18 +0000777 UPDATE t1 SET y = 0 WHERE x = 'A';
778 }
779 list [expr [file size test.db]/1024] [expr [file size test.db-wal]/1044]
780} {3 1}
781do_test wal-12.3 {
782 execsql { INSERT INTO t2 VALUES('B', 1) }
783 list [expr [file size test.db]/1024] [expr [file size test.db-wal]/1044]
784} {3 2}
dan4a4b01d2010-04-16 11:30:18 +0000785do_test wal-12.4 {
786 file copy -force test.db test2.db
787 file copy -force test.db-wal test2.db-wal
788 sqlite3_wal db2 test2.db
dan4a4b01d2010-04-16 11:30:18 +0000789 execsql { SELECT * FROM t2 } db2
790} {B 1}
791db2 close
dan4a4b01d2010-04-16 11:30:18 +0000792do_test wal-12.5 {
793 execsql {
dan5a299f92010-05-03 11:05:08 +0000794 PRAGMA wal_checkpoint;
dan4a4b01d2010-04-16 11:30:18 +0000795 UPDATE t2 SET y = 2 WHERE x = 'B';
dan5a299f92010-05-03 11:05:08 +0000796 PRAGMA wal_checkpoint;
dan4a4b01d2010-04-16 11:30:18 +0000797 UPDATE t1 SET y = 1 WHERE x = 'A';
dan5a299f92010-05-03 11:05:08 +0000798 PRAGMA wal_checkpoint;
dan4a4b01d2010-04-16 11:30:18 +0000799 UPDATE t1 SET y = 0 WHERE x = 'A';
800 SELECT * FROM t2;
801 }
802} {B 2}
dance4f05f2010-04-22 19:14:13 +0000803do_test wal-12.6 {
dan4a4b01d2010-04-16 11:30:18 +0000804 file copy -force test.db test2.db
805 file copy -force test.db-wal test2.db-wal
806 sqlite3_wal db2 test2.db
807 execsql { SELECT * FROM t2 } db2
808} {B 2}
809db2 close
dance4f05f2010-04-22 19:14:13 +0000810db close
811
812#-------------------------------------------------------------------------
813# Test large log summaries.
814#
dan8d6ad1c2010-05-04 10:36:20 +0000815# In this case "large" usually means a log file that requires a wal-index
816# mapping larger than 64KB (the default initial allocation). A 64KB wal-index
817# is large enough for a log file that contains approximately 13100 frames.
818# So the following tests create logs containing at least this many frames.
819#
820# wal-13.1.*: This test case creates a very large log file within the
821# file-system (around 200MB). The log file does not contain
822# any valid frames. Test that the database file can still be
823# opened and queried, and that the invalid log file causes no
824# problems.
825#
826# wal-13.2.*: Test that a process may create a large log file and query
827# the database (including the log file that it itself created).
828#
829# wal-13.3.*: Test that if a very large log file is created, and then a
830# second connection is opened on the database file, it is possible
831# to query the database (and the very large log) using the
832# second connection.
833#
834# wal-13.4.*: Same test as wal-13.3.*. Except in this case the second
835# connection is opened by an external process.
836#
dan31f98fc2010-04-27 05:42:32 +0000837do_test wal-13.1.1 {
dance4f05f2010-04-22 19:14:13 +0000838 list [file exists test.db] [file exists test.db-wal]
839} {1 0}
dan31f98fc2010-04-27 05:42:32 +0000840do_test wal-13.1.2 {
dance4f05f2010-04-22 19:14:13 +0000841 set fd [open test.db-wal w]
842 seek $fd [expr 200*1024*1024]
843 puts $fd ""
844 close $fd
845 sqlite3 db test.db
846 execsql { SELECT * FROM t2 }
847} {B 2}
dan31f98fc2010-04-27 05:42:32 +0000848do_test wal-13.1.3 {
dance4f05f2010-04-22 19:14:13 +0000849 db close
850 file exists test.db-wal
851} {0}
dan8d6ad1c2010-05-04 10:36:20 +0000852
853do_test wal-13.2.1 {
dance4f05f2010-04-22 19:14:13 +0000854 sqlite3 db test.db
855 execsql { SELECT count(*) FROM t2 }
856} {1}
dan8d6ad1c2010-05-04 10:36:20 +0000857do_test wal-13.2.2 {
dan65bddc12010-05-07 12:49:22 +0000858 db function blob blob
dan8d6ad1c2010-05-04 10:36:20 +0000859 for {set i 0} {$i < 16} {incr i} {
dan65bddc12010-05-07 12:49:22 +0000860 execsql { INSERT INTO t2 SELECT blob(400), blob(400) FROM t2 }
dance4f05f2010-04-22 19:14:13 +0000861 }
862 execsql { SELECT count(*) FROM t2 }
dan8d6ad1c2010-05-04 10:36:20 +0000863} [expr int(pow(2, 16))]
dan65bddc12010-05-07 12:49:22 +0000864do_test wal-13.2.3 {
dan10f5a502010-06-23 15:55:43 +0000865 expr [file size test.db-wal] > [wal_file_size 33000 1024]
danc6315a42010-05-07 13:52:42 +0000866} 1
dance4f05f2010-04-22 19:14:13 +0000867
dana4a90952010-06-15 19:07:42 +0000868do_multiclient_test tn {
869 incr tn 2
dan31f98fc2010-04-27 05:42:32 +0000870
871 do_test wal-13.$tn.0 {
dana4a90952010-06-15 19:07:42 +0000872 sql1 {
dan31f98fc2010-04-27 05:42:32 +0000873 PRAGMA journal_mode = WAL;
874 CREATE TABLE t1(x);
dan8d6ad1c2010-05-04 10:36:20 +0000875 INSERT INTO t1 SELECT randomblob(800);
dan31f98fc2010-04-27 05:42:32 +0000876 }
dana4a90952010-06-15 19:07:42 +0000877 sql1 { SELECT count(*) FROM t1 }
dan31f98fc2010-04-27 05:42:32 +0000878 } {1}
879
880 for {set ii 1} {$ii<16} {incr ii} {
881 do_test wal-13.$tn.$ii.a {
dana4a90952010-06-15 19:07:42 +0000882 sql2 { INSERT INTO t1 SELECT randomblob(800) FROM t1 }
883 sql2 { SELECT count(*) FROM t1 }
dan31f98fc2010-04-27 05:42:32 +0000884 } [expr (1<<$ii)]
885 do_test wal-13.$tn.$ii.b {
dana4a90952010-06-15 19:07:42 +0000886 sql1 { SELECT count(*) FROM t1 }
dan31f98fc2010-04-27 05:42:32 +0000887 } [expr (1<<$ii)]
888 do_test wal-13.$tn.$ii.c {
dana4a90952010-06-15 19:07:42 +0000889 sql1 { SELECT count(*) FROM t1 }
dan31f98fc2010-04-27 05:42:32 +0000890 } [expr (1<<$ii)]
drh1b48aa42010-04-30 17:28:35 +0000891 do_test wal-13.$tn.$ii.d {
dana4a90952010-06-15 19:07:42 +0000892 sql1 { PRAGMA integrity_check }
drh1b48aa42010-04-30 17:28:35 +0000893 } {ok}
dan31f98fc2010-04-27 05:42:32 +0000894 }
dan31f98fc2010-04-27 05:42:32 +0000895}
dan4a4b01d2010-04-16 11:30:18 +0000896
dan31c03902010-04-29 14:51:33 +0000897#-------------------------------------------------------------------------
898# Check a fun corruption case has been fixed.
899#
900# The problem was that after performing a checkpoint using a connection
901# that had an out-of-date pager-cache, the next time the connection was
902# used it did not realize the cache was out-of-date and proceeded to
903# operate with an inconsistent cache. Leading to corruption.
904#
dan31c03902010-04-29 14:51:33 +0000905catch { db close }
906catch { db2 close }
907catch { db3 close }
908file delete -force test.db test.db-wal
909sqlite3 db test.db
910sqlite3 db2 test.db
dan31c03902010-04-29 14:51:33 +0000911do_test wal-14 {
912 execsql {
913 PRAGMA journal_mode = WAL;
914 CREATE TABLE t1(a PRIMARY KEY, b);
915 INSERT INTO t1 VALUES(randomblob(10), randomblob(100));
916 INSERT INTO t1 SELECT randomblob(10), randomblob(100) FROM t1;
917 INSERT INTO t1 SELECT randomblob(10), randomblob(100) FROM t1;
918 INSERT INTO t1 SELECT randomblob(10), randomblob(100) FROM t1;
919 }
920
921 db2 eval {
922 INSERT INTO t1 SELECT randomblob(10), randomblob(100);
923 INSERT INTO t1 SELECT randomblob(10), randomblob(100);
924 INSERT INTO t1 SELECT randomblob(10), randomblob(100);
925 INSERT INTO t1 SELECT randomblob(10), randomblob(100);
926 }
927
dan5a299f92010-05-03 11:05:08 +0000928 # After executing the "PRAGMA wal_checkpoint", connection [db] was being
dan31c03902010-04-29 14:51:33 +0000929 # left with an inconsistent cache. Running the CREATE INDEX statement
930 # in this state led to database corruption.
931 catchsql {
dan5a299f92010-05-03 11:05:08 +0000932 PRAGMA wal_checkpoint;
dan31c03902010-04-29 14:51:33 +0000933 CREATE INDEX i1 on t1(b);
934 }
935
936 db2 eval { PRAGMA integrity_check }
937} {ok}
938
dan185cca62010-04-29 14:58:53 +0000939catch { db close }
940catch { db2 close }
dan87c1fe12010-05-03 12:14:15 +0000941
942#-------------------------------------------------------------------------
943# The following block of tests - wal-15.* - focus on testing the
944# implementation of the sqlite3_wal_checkpoint() interface.
945#
946file delete -force test.db test.db-wal
947sqlite3 db test.db
948do_test wal-15.1 {
949 execsql {
dan65bddc12010-05-07 12:49:22 +0000950 PRAGMA auto_vacuum = 0;
dan87c1fe12010-05-03 12:14:15 +0000951 PRAGMA page_size = 1024;
952 PRAGMA journal_mode = WAL;
953 }
954 execsql {
955 CREATE TABLE t1(a, b);
956 INSERT INTO t1 VALUES(1, 2);
957 }
958} {}
959
960# Test that an error is returned if the database name is not recognized
961#
962do_test wal-15.2.1 {
963 sqlite3_wal_checkpoint db aux
964} {SQLITE_ERROR}
965do_test wal-15.2.2 {
966 sqlite3_errcode db
967} {SQLITE_ERROR}
968do_test wal-15.2.3 {
969 sqlite3_errmsg db
970} {unknown database: aux}
971
972# Test that an error is returned if an attempt is made to checkpoint
973# if a transaction is open on the database.
974#
975do_test wal-15.3.1 {
976 execsql {
977 BEGIN;
978 INSERT INTO t1 VALUES(3, 4);
979 }
980 sqlite3_wal_checkpoint db main
981} {SQLITE_LOCKED}
982do_test wal-15.3.2 {
983 sqlite3_errcode db
984} {SQLITE_LOCKED}
985do_test wal-15.3.3 {
986 sqlite3_errmsg db
987} {database table is locked}
988
dandcb11692010-05-31 14:18:45 +0000989# Earlier versions returned an error is returned if the db cannot be
990# checkpointed because of locks held by another connection. Check that
991# this is no longer the case.
dan87c1fe12010-05-03 12:14:15 +0000992#
993sqlite3 db2 test.db
994do_test wal-15.4.1 {
995 execsql {
996 BEGIN;
997 SELECT * FROM t1;
998 } db2
999} {1 2}
1000do_test wal-15.4.2 {
1001 execsql { COMMIT }
1002 sqlite3_wal_checkpoint db
dandcb11692010-05-31 14:18:45 +00001003} {SQLITE_OK}
dan87c1fe12010-05-03 12:14:15 +00001004do_test wal-15.4.3 {
1005 sqlite3_errmsg db
dandcb11692010-05-31 14:18:45 +00001006} {not an error}
dan87c1fe12010-05-03 12:14:15 +00001007
1008# After [db2] drops its lock, [db] may checkpoint the db.
1009#
1010do_test wal-15.4.4 {
1011 execsql { COMMIT } db2
1012 sqlite3_wal_checkpoint db
1013} {SQLITE_OK}
1014do_test wal-15.4.5 {
1015 sqlite3_errmsg db
1016} {not an error}
1017do_test wal-15.4.6 {
1018 file size test.db
1019} [expr 1024*2]
1020
1021catch { db2 close }
1022catch { db close }
danaf0cfd32010-05-03 15:58:50 +00001023
1024#-------------------------------------------------------------------------
1025# The following block of tests - wal-16.* - test that if a NULL pointer or
1026# an empty string is passed as the second argument of the wal_checkpoint()
1027# API, an attempt is made to checkpoint all attached databases.
1028#
1029foreach {tn ckpt_cmd ckpt_res ckpt_main ckpt_aux} {
1030 1 {sqlite3_wal_checkpoint db} SQLITE_OK 1 1
1031 2 {sqlite3_wal_checkpoint db ""} SQLITE_OK 1 1
1032 3 {db eval "PRAGMA wal_checkpoint"} {} 1 1
1033
1034 4 {sqlite3_wal_checkpoint db main} SQLITE_OK 1 0
1035 5 {sqlite3_wal_checkpoint db aux} SQLITE_OK 0 1
1036 6 {sqlite3_wal_checkpoint db temp} SQLITE_OK 0 0
1037 7 {db eval "PRAGMA main.wal_checkpoint"} {} 1 0
1038 8 {db eval "PRAGMA aux.wal_checkpoint"} {} 0 1
1039 9 {db eval "PRAGMA temp.wal_checkpoint"} {} 0 0
1040} {
1041 do_test wal-16.$tn.1 {
1042 file delete -force test2.db test2.db-wal test2.db-journal
1043 file delete -force test.db test.db-wal test.db-journal
1044
1045 sqlite3 db test.db
1046 execsql {
1047 ATTACH 'test2.db' AS aux;
dan65bddc12010-05-07 12:49:22 +00001048 PRAGMA main.auto_vacuum = 0;
1049 PRAGMA aux.auto_vacuum = 0;
danaf0cfd32010-05-03 15:58:50 +00001050 PRAGMA main.journal_mode = WAL;
1051 PRAGMA aux.journal_mode = WAL;
1052 PRAGMA synchronous = NORMAL;
1053 }
1054 } {wal wal}
1055
1056 do_test wal-16.$tn.2 {
1057 execsql {
1058 CREATE TABLE main.t1(a, b, PRIMARY KEY(a, b));
1059 CREATE TABLE aux.t2(a, b, PRIMARY KEY(a, b));
1060
1061 INSERT INTO t2 VALUES(1, randomblob(1000));
1062 INSERT INTO t2 VALUES(2, randomblob(1000));
1063 INSERT INTO t1 SELECT * FROM t2;
1064 }
1065
1066 list [file size test.db] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +00001067 } [list [expr 1*1024] [wal_file_size 10 1024]]
danaf0cfd32010-05-03 15:58:50 +00001068 do_test wal-16.$tn.3 {
1069 list [file size test2.db] [file size test2.db-wal]
dan10f5a502010-06-23 15:55:43 +00001070 } [list [expr 1*1024] [wal_file_size 16 1024]]
danaf0cfd32010-05-03 15:58:50 +00001071
1072 do_test wal-16.$tn.4 [list eval $ckpt_cmd] $ckpt_res
1073
1074 do_test wal-16.$tn.5 {
1075 list [file size test.db] [file size test.db-wal]
dan10f5a502010-06-23 15:55:43 +00001076 } [list [expr ($ckpt_main ? 7 : 1)*1024] [wal_file_size 10 1024]]
danaf0cfd32010-05-03 15:58:50 +00001077
1078 do_test wal-16.$tn.6 {
1079 list [file size test2.db] [file size test2.db-wal]
dan10f5a502010-06-23 15:55:43 +00001080 } [list [expr ($ckpt_aux ? 7 : 1)*1024] [wal_file_size 16 1024]]
danaf0cfd32010-05-03 15:58:50 +00001081
1082 catch { db close }
1083}
1084
dan8d6ad1c2010-05-04 10:36:20 +00001085#-------------------------------------------------------------------------
1086# The following tests - wal-17.* - attempt to verify that the correct
1087# number of "padding" frames are appended to the log file when a transaction
1088# is committed in synchronous=FULL mode.
1089#
1090# Do this by creating a database that uses 512 byte pages. Then writing
1091# a transaction that modifies 171 pages. In synchronous=NORMAL mode, this
1092# produces a log file of:
1093#
dan10f5a502010-06-23 15:55:43 +00001094# 32 + (24+512)*171 = 90312 bytes.
dan8d6ad1c2010-05-04 10:36:20 +00001095#
1096# Slightly larger than 11*8192 = 90112 bytes.
1097#
1098# Run the test using various different sector-sizes. In each case, the
1099# WAL code should write the 90300 bytes of log file containing the
1100# transaction, then append as may frames as are required to extend the
1101# log file so that no part of the next transaction will be written into
1102# a disk-sector used by transaction just committed.
1103#
1104set old_pending_byte [sqlite3_test_control_pending_byte 0x10000000]
1105catch { db close }
dan10f5a502010-06-23 15:55:43 +00001106foreach {tn sectorsize logsize} "
1107 1 128 [wal_file_size 172 512]
1108 2 256 [wal_file_size 172 512]
1109 3 512 [wal_file_size 172 512]
1110 4 1024 [wal_file_size 172 512]
1111 5 2048 [wal_file_size 172 512]
1112 6 4096 [wal_file_size 176 512]
1113 7 8192 [wal_file_size 184 512]
1114" {
dan8d6ad1c2010-05-04 10:36:20 +00001115 file delete -force test.db test.db-wal test.db-journal
1116 sqlite3_simulate_device -sectorsize $sectorsize
1117 sqlite3 db test.db -vfs devsym
1118
1119 do_test wal-17.$tn.1 {
1120 execsql {
1121 PRAGMA auto_vacuum = 0;
1122 PRAGMA page_size = 512;
1123 PRAGMA journal_mode = WAL;
1124 PRAGMA synchronous = FULL;
1125 }
1126 execsql {
1127 BEGIN;
1128 CREATE TABLE t(x);
1129 }
1130 for {set i 0} {$i<166} {incr i} {
1131 execsql { INSERT INTO t VALUES(randomblob(400)) }
1132 }
1133 execsql COMMIT
1134
1135 file size test.db-wal
1136 } $logsize
1137
1138 do_test wal-17.$tn.2 {
1139 file size test.db
1140 } 512
1141
1142 do_test wal-17.$tn.3 {
1143 db close
1144 file size test.db
1145 } [expr 512*171]
1146}
1147sqlite3_test_control_pending_byte $old_pending_byte
1148
danb6e099a2010-05-04 14:47:39 +00001149#-------------------------------------------------------------------------
1150# This test - wal-18.* - verifies a couple of specific conditions that
1151# may be encountered while recovering a log file are handled correctly:
1152#
1153# wal-18.1.* When the first 32-bits of a frame checksum is correct but
1154# the second 32-bits are false, and
1155#
1156# wal-18.2.* When the page-size field that occurs at the start of a log
1157# file is a power of 2 greater than 16384 or smaller than 512.
1158#
1159file delete -force test.db test.db-wal test.db-journal
1160do_test wal-18.0 {
1161 sqlite3 db test.db
1162 execsql {
1163 PRAGMA page_size = 1024;
1164 PRAGMA auto_vacuum = 0;
1165 PRAGMA journal_mode = WAL;
1166 PRAGMA synchronous = OFF;
1167
1168 CREATE TABLE t1(a, b, UNIQUE(a, b));
1169 INSERT INTO t1 VALUES(0, 0);
1170 PRAGMA wal_checkpoint;
1171
1172 INSERT INTO t1 VALUES(1, 2); -- frames 1 and 2
1173 INSERT INTO t1 VALUES(3, 4); -- frames 3 and 4
1174 INSERT INTO t1 VALUES(5, 6); -- frames 5 and 6
1175 }
1176
1177 file copy -force test.db testX.db
1178 file copy -force test.db-wal testX.db-wal
1179 db close
1180 list [file size testX.db] [file size testX.db-wal]
dan10f5a502010-06-23 15:55:43 +00001181} [list [expr 3*1024] [wal_file_size 6 1024]]
danb6e099a2010-05-04 14:47:39 +00001182
danc9e46652010-05-06 13:36:47 +00001183unset -nocomplain nFrame result
danb6e099a2010-05-04 14:47:39 +00001184foreach {nFrame result} {
1185 0 {0 0}
1186 1 {0 0}
1187 2 {0 0 1 2}
1188 3 {0 0 1 2}
1189 4 {0 0 1 2 3 4}
1190 5 {0 0 1 2 3 4}
1191 6 {0 0 1 2 3 4 5 6}
1192} {
1193 do_test wal-18.1.$nFrame {
1194 file copy -force testX.db test.db
1195 file copy -force testX.db-wal test.db-wal
1196
drh23ea97b2010-05-20 16:45:58 +00001197 hexio_write test.db-wal [expr 24 + $nFrame*(24+1024) + 20] 00000000
danb6e099a2010-05-04 14:47:39 +00001198
1199 sqlite3 db test.db
1200 execsql {
1201 SELECT * FROM t1;
1202 PRAGMA integrity_check;
1203 }
1204 } [concat $result ok]
1205 db close
1206}
1207
1208proc randomblob {pgsz} {
1209 sqlite3 rbdb :memory:
1210 set blob [rbdb one {SELECT randomblob($pgsz)}]
1211 rbdb close
1212 set blob
1213}
1214
1215proc logcksum {ckv1 ckv2 blob} {
1216 upvar $ckv1 c1
1217 upvar $ckv2 c2
1218
danb8fd6c22010-05-24 10:39:36 +00001219 set scanpattern I*
1220 if {$::tcl_platform(byteOrder) eq "littleEndian"} {
1221 set scanpattern i*
1222 }
danb6e099a2010-05-04 14:47:39 +00001223
danb8fd6c22010-05-24 10:39:36 +00001224 binary scan $blob $scanpattern values
drh4c1cb6a2010-05-19 19:09:37 +00001225 foreach {v1 v2} $values {
1226 set c1 [expr {($c1 + $v1 + $c2)&0xFFFFFFFF}]
1227 set c2 [expr {($c2 + $v2 + $c1)&0xFFFFFFFF}]
1228 }
danb6e099a2010-05-04 14:47:39 +00001229}
1230
1231file copy -force test.db testX.db
1232foreach {tn pgsz works} {
1233 1 128 0
1234 2 256 0
1235 3 512 1
1236 4 1024 1
1237 5 2048 1
1238 6 4096 1
1239 7 8192 1
1240 8 16384 1
1241 9 32768 1
drhb2eced52010-08-12 02:41:12 +00001242 10 65536 1
1243 11 131072 0
drh4c1cb6a2010-05-19 19:09:37 +00001244 11 1016 0
danb6e099a2010-05-04 14:47:39 +00001245} {
1246
dan65bddc12010-05-07 12:49:22 +00001247 if {$::SQLITE_MAX_PAGE_SIZE < $pgsz} {
1248 set works 0
1249 }
1250
danb6e099a2010-05-04 14:47:39 +00001251 for {set pg 1} {$pg <= 3} {incr pg} {
1252 file copy -force testX.db test.db
1253 file delete -force test.db-wal
1254
1255 # Check that the database now exists and consists of three pages. And
1256 # that there is no associated wal file.
1257 #
1258 do_test wal-18.2.$tn.$pg.1 { file exists test.db-wal } 0
1259 do_test wal-18.2.$tn.$pg.2 { file exists test.db } 1
1260 do_test wal-18.2.$tn.$pg.3 { file size test.db } [expr 1024*3]
1261
1262 do_test wal-18.2.$tn.$pg.4 {
1263
1264 # Create a wal file that contains a single frame (database page
1265 # number $pg) with the commit flag set. The frame checksum is
1266 # correct, but the contents of the database page are corrupt.
1267 #
1268 # The page-size in the log file header is set to $pgsz. If the
1269 # WAL code considers $pgsz to be a valid SQLite database file page-size,
1270 # the database will be corrupt (because the garbage frame contents
1271 # will be treated as valid content). If $pgsz is invalid (too small
1272 # or too large), the db will not be corrupt as the log file will
1273 # be ignored.
1274 #
drh7e263722010-05-20 21:21:09 +00001275 set walhdr [binary format IIIIII 931071618 3007000 $pgsz 1234 22 23]
danb6e099a2010-05-04 14:47:39 +00001276 set framebody [randomblob $pgsz]
drh7e263722010-05-20 21:21:09 +00001277 set framehdr [binary format IIII $pg 5 22 23]
1278 set c1 0
1279 set c2 0
dan71d89912010-05-24 13:57:42 +00001280 logcksum c1 c2 $walhdr
dan10f5a502010-06-23 15:55:43 +00001281
1282 append walhdr [binary format II $c1 $c2]
dan71d89912010-05-24 13:57:42 +00001283 logcksum c1 c2 [string range $framehdr 0 7]
danb6e099a2010-05-04 14:47:39 +00001284 logcksum c1 c2 $framebody
drh7e263722010-05-20 21:21:09 +00001285 set framehdr [binary format IIIIII $pg 5 22 23 $c1 $c2]
danb8fd6c22010-05-24 10:39:36 +00001286
danb6e099a2010-05-04 14:47:39 +00001287 set fd [open test.db-wal w]
1288 fconfigure $fd -encoding binary -translation binary
1289 puts -nonewline $fd $walhdr
1290 puts -nonewline $fd $framehdr
1291 puts -nonewline $fd $framebody
1292 close $fd
1293
1294 file size test.db-wal
dan10f5a502010-06-23 15:55:43 +00001295 } [wal_file_size 1 $pgsz]
danb6e099a2010-05-04 14:47:39 +00001296
1297 do_test wal-18.2.$tn.$pg.5 {
1298 sqlite3 db test.db
1299 set rc [catch { db one {PRAGMA integrity_check} } msg]
1300 expr { $rc!=0 || $msg!="ok" }
1301 } $works
1302
1303 db close
1304 }
1305}
1306
danb7d53f52010-05-06 17:28:08 +00001307#-------------------------------------------------------------------------
1308# The following test - wal-19.* - fixes a bug that was present during
1309# development.
1310#
1311# When a database connection in WAL mode is closed, it attempts an
1312# EXCLUSIVE lock on the database file. If the lock is obtained, the
1313# connection knows that it is the last connection to disconnect from
1314# the database, so it runs a checkpoint operation. The bug was that
1315# the connection was not updating its private copy of the wal-index
1316# header before doing so, meaning that it could checkpoint an old
1317# snapshot.
1318#
1319do_test wal-19.1 {
1320 file delete -force test.db test.db-wal test.db-journal
1321 sqlite3 db test.db
1322 sqlite3 db2 test.db
1323 execsql {
1324 PRAGMA journal_mode = WAL;
1325 CREATE TABLE t1(a, b);
1326 INSERT INTO t1 VALUES(1, 2);
1327 INSERT INTO t1 VALUES(3, 4);
1328 }
1329 execsql { SELECT * FROM t1 } db2
1330} {1 2 3 4}
1331do_test wal-19.2 {
1332 execsql {
1333 INSERT INTO t1 VALUES(5, 6);
1334 SELECT * FROM t1;
1335 }
1336} {1 2 3 4 5 6}
1337do_test wal-19.3 {
1338 db close
1339 db2 close
1340 file exists test.db-wal
1341} {0}
1342do_test wal-19.4 {
1343 # When the bug was present, the following was returning {1 2 3 4} only,
1344 # as [db2] had an out-of-date copy of the wal-index header when it was
1345 # closed.
1346 #
1347 sqlite3 db test.db
1348 execsql { SELECT * FROM t1 }
1349} {1 2 3 4 5 6}
1350
dan998ad212010-05-07 06:59:08 +00001351#-------------------------------------------------------------------------
1352# This test - wal-20.* - uses two connections. One in this process and
1353# the other in an external process. The procedure is:
1354#
1355# 1. Using connection 1, create the database schema.
1356#
1357# 2. Using connection 2 (in an external process), add so much
1358# data to the database without checkpointing that a wal-index
1359# larger than 64KB is required.
1360#
1361# 3. Using connection 1, checkpoint the database. Make sure all
1362# the data is present and the database is not corrupt.
1363#
1364# At one point, SQLite was failing to grow the mapping of the wal-index
1365# file in step 3 and the checkpoint was corrupting the database file.
1366#
1367do_test wal-20.1 {
shaneha10069d2010-05-11 02:46:16 +00001368 catch {db close}
dan998ad212010-05-07 06:59:08 +00001369 file delete -force test.db test.db-wal test.db-journal
1370 sqlite3 db test.db
1371 execsql {
1372 PRAGMA journal_mode = WAL;
1373 CREATE TABLE t1(x);
1374 INSERT INTO t1 VALUES(randomblob(900));
1375 SELECT count(*) FROM t1;
1376 }
1377} {wal 1}
1378do_test wal-20.2 {
1379 set ::buddy [launch_testfixture]
1380 testfixture $::buddy {
1381 sqlite3 db test.db
1382 db transaction { db eval {
1383 PRAGMA wal_autocheckpoint = 0;
1384 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2 */
1385 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4 */
1386 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8 */
1387 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16 */
1388 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 32 */
1389 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 64 */
1390 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 128 */
1391 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 256 */
1392 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 512 */
1393 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 1024 */
1394 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2048 */
1395 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4096 */
1396 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8192 */
1397 INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16384 */
1398 } }
1399 }
1400} {0}
1401do_test wal-20.3 {
1402 close $::buddy
1403 execsql {
1404 PRAGMA wal_checkpoint;
1405 SELECT count(*) FROM t1;
1406 }
1407} {16384}
1408do_test wal-20.4 {
1409 db close
1410 sqlite3 db test.db
1411 execsql { SELECT count(*) FROM t1 }
1412} {16384}
1413integrity_check wal-20.5
1414
danaf0cfd32010-05-03 15:58:50 +00001415catch { db2 close }
1416catch { db close }
dan6e6bd562010-06-02 18:59:03 +00001417
1418do_test wal-21.1 {
dan10f5a502010-06-23 15:55:43 +00001419 faultsim_delete_and_reopen
dan6e6bd562010-06-02 18:59:03 +00001420 execsql {
1421 PRAGMA journal_mode = WAL;
1422 CREATE TABLE t1(a, b);
1423 INSERT INTO t1 VALUES(1, 2);
1424 INSERT INTO t1 VALUES(3, 4);
1425 INSERT INTO t1 VALUES(5, 6);
1426 INSERT INTO t1 VALUES(7, 8);
1427 INSERT INTO t1 VALUES(9, 10);
1428 INSERT INTO t1 VALUES(11, 12);
1429 }
1430} {wal}
1431do_test wal-21.2 {
1432 execsql {
1433 PRAGMA cache_size = 10;
1434 PRAGMA wal_checkpoint;
1435 BEGIN;
1436 SAVEPOINT s;
1437 INSERT INTO t1 SELECT randomblob(900), randomblob(900) FROM t1;
1438 ROLLBACK TO s;
1439 COMMIT;
1440 SELECT * FROM t1;
1441 }
1442} {1 2 3 4 5 6 7 8 9 10 11 12}
1443do_test wal-21.3 {
1444 execsql { PRAGMA integrity_check }
1445} {ok}
1446
dan4bcc4982010-08-16 19:23:02 +00001447#-------------------------------------------------------------------------
1448# Test reading and writing of databases with different page-sizes.
1449#
1450foreach pgsz {512 1024 2048 4096 8192 16384 32768 65536} {
1451 do_multiclient_test tn [string map [list %PGSZ% $pgsz] {
daneb8763d2010-08-17 14:52:22 +00001452 do_test wal-22.%PGSZ%.$tn.1 {
dan4bcc4982010-08-16 19:23:02 +00001453 sql1 {
1454 PRAGMA main.page_size = %PGSZ%;
1455 PRAGMA auto_vacuum = 0;
1456 PRAGMA journal_mode = WAL;
1457 CREATE TABLE t1(x UNIQUE);
1458 INSERT INTO t1 SELECT randomblob(800);
1459 INSERT INTO t1 SELECT randomblob(800);
1460 INSERT INTO t1 SELECT randomblob(800);
1461 }
1462 } {wal}
daneb8763d2010-08-17 14:52:22 +00001463 do_test wal-22.%PGSZ%.$tn.2 { sql2 { PRAGMA integrity_check } } {ok}
1464 do_test wal-22.%PGSZ%.$tn.3 {
dan4bcc4982010-08-16 19:23:02 +00001465 sql1 {PRAGMA wal_checkpoint}
1466 expr {[file size test.db] % %PGSZ%}
1467 } {0}
1468 }]
1469}
1470
daneb8763d2010-08-17 14:52:22 +00001471#-------------------------------------------------------------------------
1472# Test that when 1 or more pages are recovered from a WAL file,
1473# sqlite3_log() is invoked to report this to the user.
1474#
shaneh35cf9082010-08-18 14:54:03 +00001475set walfile [file nativename [file join [pwd] test.db-wal]]
daneb8763d2010-08-17 14:52:22 +00001476catch {db close}
1477file delete -force test.db
1478do_test wal-23.1 {
1479 faultsim_delete_and_reopen
1480 execsql {
1481 CREATE TABLE t1(a, b);
1482 PRAGMA journal_mode = WAL;
1483 INSERT INTO t1 VALUES(1, 2);
1484 INSERT INTO t1 VALUES(3, 4);
1485 }
1486 faultsim_save_and_close
1487
1488 sqlite3_shutdown
1489 test_sqlite3_log [list lappend ::log]
1490 set ::log [list]
1491 sqlite3 db test.db
1492 execsql { SELECT * FROM t1 }
1493} {1 2 3 4}
1494do_test wal-23.2 { set ::log } {}
1495
1496do_test wal-23.3 {
1497 db close
1498 set ::log [list]
1499 faultsim_restore_and_reopen
1500 execsql { SELECT * FROM t1 }
1501} {1 2 3 4}
dan5a9e07e2010-08-18 15:25:17 +00001502set nPage [expr 2+$AUTOVACUUM]
daneb8763d2010-08-17 14:52:22 +00001503do_test wal-23.4 {
1504 set ::log
dan5a9e07e2010-08-18 15:25:17 +00001505} [list SQLITE_OK "Recovered $nPage frames from WAL file $walfile"]
daneb8763d2010-08-17 14:52:22 +00001506
1507db close
1508sqlite3_shutdown
1509test_sqlite3_log
1510sqlite3_initialize
1511
dan7c246102010-04-12 19:00:29 +00001512finish_test