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 # This file implements regression tests for SQLite library. The focus
12 # of this file is the sqlite3_snapshot_xxx() APIs.
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
17 ifcapable !snapshot {finish_test; return}
18 set testprefix snapshot
20 # This test does not work with the inmemory_journal permutation. The reason
21 # is that each connection opened as part of this permutation executes
22 # "PRAGMA journal_mode=memory", which fails if the database is in wal mode
23 # and there are one or more existing connections.
24 if {[permutation]=="inmemory_journal"} {
31 proc snapshot_get {DB DBNAME} {
32 uplevel [list sqlite3_snapshot_get $DB $DBNAME]
34 proc snapshot_open {DB DBNAME SNAPSHOT} {
35 uplevel [list sqlite3_snapshot_open $DB $DBNAME $SNAPSHOT]
37 proc snapshot_free {SNAPSHOT} {
38 uplevel [list sqlite3_snapshot_free $SNAPSHOT]
40 proc snapshot_cmp {SNAPSHOT1 SNAPSHOT2} {
41 uplevel [list sqlite3_snapshot_cmp $SNAPSHOT1 $SNAPSHOT2]
46 proc snapshot_get {DB DBNAME} {
47 uplevel [list sqlite3_snapshot_get_blob $DB $DBNAME]
49 proc snapshot_open {DB DBNAME SNAPSHOT} {
50 uplevel [list sqlite3_snapshot_open_blob $DB $DBNAME $SNAPSHOT]
52 proc snapshot_free {SNAPSHOT} {
54 proc snapshot_cmp {SNAPSHOT1 SNAPSHOT2} {
55 uplevel [list sqlite3_snapshot_cmp_blob $SNAPSHOT1 $SNAPSHOT2]
63 #-------------------------------------------------------------------------
64 # Check some error conditions in snapshot_get(). It is an error if:
66 # 1) snapshot_get() is called on a non-WAL database, or
67 # 2) there is an open write transaction on the database.
68 # 3) the database handle is in auto-commit mode
70 do_execsql_test $tn.1.0 {
71 CREATE TABLE t1(a, b);
72 INSERT INTO t1 VALUES(1, 2);
73 INSERT INTO t1 VALUES(3, 4);
77 execsql { BEGIN; SELECT * FROM t1; }
78 list [catch { snapshot_get db main } msg] $msg
80 do_execsql_test $tn.1.1.2 COMMIT
84 PRAGMA journal_mode = WAL;
86 INSERT INTO t1 VALUES(5, 6);
87 INSERT INTO t1 VALUES(7, 8);
89 list [catch { snapshot_get db main } msg] $msg
91 do_execsql_test $tn.1.2.2 COMMIT
94 list [catch { snapshot_get db main } msg] $msg
97 db trans { set snap [snapshot_get db main] }
101 #-------------------------------------------------------------------------
102 # Check that a simple case works. Reuse the database created by the
103 # block of tests above.
105 do_execsql_test $tn.2.1.0 {
111 set snapshot [snapshot_get db main]
114 INSERT INTO t1 VALUES(9, 10);
117 } {1 2 3 4 5 6 7 8 9 10}
121 snapshot_open db main $snapshot
128 snapshot_free $snapshot
138 } {1 2 3 4 5 6 7 8 9 10}
141 set snapshot [snapshot_get db2 main]
143 INSERT INTO t1 VALUES(11, 12);
146 } {1 2 3 4 5 6 7 8 9 10 11 12}
150 snapshot_open db main $snapshot
154 } {1 2 3 4 5 6 7 8 9 10}
157 snapshot_free $snapshot
164 execsql { DELETE FROM t1 WHERE a>6 }
165 db trans { set snapshot [snapshot_get db main] }
167 INSERT INTO t1 VALUES('a', 'b');
168 INSERT INTO t1 VALUES('c', 'd');
171 } {1 2 3 4 5 6 a b c d}
174 snapshot_open db main $snapshot
175 execsql { SELECT * FROM t1 }
180 INSERT INTO t1 VALUES('x','y')
182 } {1 {database is locked}}
185 snapshot_free $snapshot
188 #-------------------------------------------------------------------------
189 # Check some errors in snapshot_open(). It is an error if:
191 # 1) the db is in auto-commit mode,
192 # 2) the db has an open (read or write) transaction,
193 # 3) the db is not a wal database,
195 # Reuse the database created by earlier tests.
197 do_execsql_test $tn.3.0.0 {
198 CREATE TABLE t2(x, y);
199 INSERT INTO t2 VALUES('a', 'b');
200 INSERT INTO t2 VALUES('c', 'd');
205 set snapshot [snapshot_get db main]
207 execsql { INSERT INTO t2 VALUES('e', 'f'); }
211 list [catch {snapshot_open db main $snapshot } msg] $msg
221 list [catch {snapshot_open db main $snapshot } msg] $msg
228 INSERT INTO t2 VALUES('g', 'h');
230 list [catch {snapshot_open db main $snapshot } msg] $msg
232 do_execsql_test $tn.3.2.4 COMMIT
235 execsql { PRAGMA journal_mode = DELETE }
237 list [catch {snapshot_open db main $snapshot } msg] $msg
240 do_test $tn.$tn.3.3.2 {
241 snapshot_free $snapshot
245 #-------------------------------------------------------------------------
246 # Check that SQLITE_BUSY_SNAPSHOT is returned if the specified snapshot
247 # no longer exists because the wal file has been checkpointed.
249 # 1. Reading a snapshot from the middle of a wal file is not possible
250 # after the wal file has been checkpointed.
252 # 2. That a snapshot from the end of a wal file can not be read once
253 # the wal file has been wrapped.
255 do_execsql_test $tn.4.1.0 {
256 PRAGMA journal_mode = wal;
257 CREATE TABLE t3(i, j);
258 INSERT INTO t3 VALUES('o', 't');
259 INSERT INTO t3 VALUES('t', 'f');
265 set snapshot [snapshot_get db main]
270 INSERT INTO t3 VALUES('f', 's');
273 snapshot_open db main $snapshot
274 execsql { SELECT * FROM t3 }
280 PRAGMA wal_checkpoint;
283 list [catch {snapshot_open db main $snapshot} msg] $msg
284 } {1 SQLITE_BUSY_SNAPSHOT}
286 snapshot_free $snapshot
292 INSERT INTO t3 VALUES('s', 'e');
293 INSERT INTO t3 VALUES('n', 't');
297 } {o t t f f s s e n t}
299 set snapshot [snapshot_get db main]
302 PRAGMA wal_checkpoint;
305 snapshot_open db main $snapshot
306 execsql { SELECT * FROM t3 }
307 } {o t t f f s s e n t}
311 INSERT INTO t3 VALUES('e', 't');
314 list [catch {snapshot_open db main $snapshot} msg] $msg
315 } {1 SQLITE_BUSY_SNAPSHOT}
317 snapshot_free $snapshot
320 #-------------------------------------------------------------------------
321 # Check that SQLITE_BUSY is returned if a checkpoint is running when
322 # sqlite3_snapshot_open() is called.
327 sqlite3 db test.db -vfs tvfs
329 do_execsql_test $tn.5.1 {
330 PRAGMA journal_mode = wal;
331 CREATE TABLE x1(x, xx, xxx);
332 INSERT INTO x1 VALUES('z', 'zz', 'zzz');
338 set ::snapshot [snapshot_get db main]
339 sqlite3 db2 test.db -vfs tvfs
341 INSERT INTO x1 VALUES('a', 'aa', 'aaa');
347 proc write_callback {args} {
348 do_test $tn.5.3.[incr ::t53] {
350 list [catch { snapshot_open db main $::snapshot } msg] $msg
356 tvfs script write_callback
357 db2 eval { PRAGMA wal_checkpoint }
361 snapshot_free $snapshot
363 #-------------------------------------------------------------------------
364 # Test that sqlite3_snapshot_get() may be called immediately after
365 # "BEGIN; PRAGMA user_version;". And that sqlite3_snapshot_open() may
366 # be called after opening the db handle and running the script
367 # "PRAGMA user_version; BEGIN".
369 do_execsql_test $tn.6.1 {
370 PRAGMA journal_mode = wal;
371 CREATE TABLE x1(x, xx, xxx);
372 INSERT INTO x1 VALUES('z', 'zz', 'zzz');
377 set ::snapshot [snapshot_get db main]
379 INSERT INTO x1 VALUES('a', 'aa', 'aaa');
385 db2 eval "PRAGMA user_version ; BEGIN"
386 snapshot_open db2 main $::snapshot
387 db2 eval { SELECT * FROM x1 }
392 db2 eval "PRAGMA application_id"
394 snapshot_open db2 main $::snapshot
395 db2 eval { SELECT * FROM x1 }
402 list [catch {snapshot_open db2 main $::snapshot} msg] $msg
405 snapshot_free $snapshot
407 #-------------------------------------------------------------------------
408 # The following tests investigate the sqlite3_snapshot_cmp() API.
411 # Compare snapshots $p1 and $p2, checking that the result is $r.
413 proc do_snapshot_cmp_test {tn p1 p2 r} {
414 uplevel [list do_test $tn.1 [list snapshot_cmp $p1 $p2] $r]
415 uplevel [list do_test $tn.2 [list snapshot_cmp $p2 $p1] [expr $r*-1]]
416 uplevel [list do_test $tn.3 [list snapshot_cmp $p1 $p1] 0]
417 uplevel [list do_test $tn.4 [list snapshot_cmp $p2 $p2] 0]
423 do_execsql_test $tn.7.1 {
424 PRAGMA journal_mode = wal;
429 execsql { BEGIN ; PRAGMA application_id }
430 set p1 [snapshot_get db main]
432 INSERT INTO t1 VALUES(10);
435 execsql { BEGIN ; PRAGMA application_id }
436 set p2 [snapshot_get db main]
440 do_snapshot_cmp_test $tn.7.1.3 $p1 $p2 -1
444 do_execsql_test $tn.7.2.1 {
445 INSERT INTO t1 VALUES(11);
446 INSERT INTO t1 VALUES(12);
447 INSERT INTO t1 VALUES(13);
449 PRAGMA application_id;
452 set p1 [snapshot_get db main]
455 INSERT INTO t1 VALUES(14);
456 PRAGMA wal_checkpoint;
458 PRAGMA application_id;
460 set p2 [snapshot_get db main]
464 do_snapshot_cmp_test $tn.7.2.3 $p1 $p2 -1
469 INSERT INTO t1 VALUES(14);
471 PRAGMA application_id;
473 set p2 [snapshot_get db main]
477 do_snapshot_cmp_test $tn.7.3.2 $p1 $p2 -1