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 # Update - it is no longer an error to have a read-transaction open,
222 # provided there are no active SELECT statements.
224 db eval "SELECT * FROM t2" {
225 set res [list [catch {snapshot_open db main $snapshot } msg] $msg]
231 snapshot_open db main $snapshot
238 INSERT INTO t2 VALUES('g', 'h');
240 list [catch {snapshot_open db main $snapshot } msg] $msg
242 do_execsql_test $tn.3.2.4 COMMIT
245 execsql { PRAGMA journal_mode = DELETE }
247 list [catch {snapshot_open db main $snapshot } msg] $msg
251 execsql { COMMIT ; BEGIN ; SELECT * FROM t2 }
252 list [catch {snapshot_open db main $snapshot } msg] $msg
255 do_test $tn.$tn.3.3.2 {
256 snapshot_free $snapshot
260 #-------------------------------------------------------------------------
261 # Check that SQLITE_ERROR_SNAPSHOT is returned if the specified snapshot
262 # no longer exists because the wal file has been checkpointed.
264 # 1. Reading a snapshot from the middle of a wal file is not possible
265 # after the wal file has been checkpointed.
267 # 2. That a snapshot from the end of a wal file can not be read once
268 # the wal file has been wrapped.
270 do_execsql_test $tn.4.1.0 {
271 PRAGMA journal_mode = wal;
272 CREATE TABLE t3(i, j);
273 INSERT INTO t3 VALUES('o', 't');
274 INSERT INTO t3 VALUES('t', 'f');
280 set snapshot [snapshot_get db main]
285 INSERT INTO t3 VALUES('f', 's');
288 snapshot_open db main $snapshot
289 execsql { SELECT * FROM t3 }
295 PRAGMA wal_checkpoint;
298 list [catch {snapshot_open db main $snapshot} msg] $msg
299 } {1 SQLITE_ERROR_SNAPSHOT}
301 snapshot_free $snapshot
307 INSERT INTO t3 VALUES('s', 'e');
308 INSERT INTO t3 VALUES('n', 't');
312 } {o t t f f s s e n t}
314 set snapshot [snapshot_get db main]
317 PRAGMA wal_checkpoint;
320 snapshot_open db main $snapshot
321 execsql { SELECT * FROM t3 }
322 } {o t t f f s s e n t}
326 INSERT INTO t3 VALUES('e', 't');
329 list [catch {snapshot_open db main $snapshot} msg] $msg
330 } {1 SQLITE_ERROR_SNAPSHOT}
332 snapshot_free $snapshot
335 #-------------------------------------------------------------------------
336 # Check that SQLITE_BUSY is returned if a checkpoint is running when
337 # sqlite3_snapshot_open() is called.
342 sqlite3 db test.db -vfs tvfs
344 do_execsql_test $tn.5.1 {
345 PRAGMA journal_mode = wal;
346 CREATE TABLE x1(x, xx, xxx);
347 INSERT INTO x1 VALUES('z', 'zz', 'zzz');
353 set ::snapshot [snapshot_get db main]
354 sqlite3 db2 test.db -vfs tvfs
356 INSERT INTO x1 VALUES('a', 'aa', 'aaa');
362 proc write_callback {args} {
363 do_test $tn.5.3.[incr ::t53] {
365 list [catch { snapshot_open db main $::snapshot } msg] $msg
371 tvfs script write_callback
372 db2 eval { PRAGMA wal_checkpoint }
376 snapshot_free $snapshot
378 #-------------------------------------------------------------------------
379 # Test that sqlite3_snapshot_get() may be called immediately after
380 # "BEGIN; PRAGMA user_version;". And that sqlite3_snapshot_open() may
381 # be called after opening the db handle and running the script
382 # "PRAGMA user_version; BEGIN".
384 do_execsql_test $tn.6.1 {
385 PRAGMA journal_mode = wal;
386 CREATE TABLE x1(x, xx, xxx);
387 INSERT INTO x1 VALUES('z', 'zz', 'zzz');
392 set ::snapshot [snapshot_get db main]
394 INSERT INTO x1 VALUES('a', 'aa', 'aaa');
400 db2 eval "PRAGMA user_version ; BEGIN"
401 snapshot_open db2 main $::snapshot
402 db2 eval { SELECT * FROM x1 }
407 db2 eval "PRAGMA application_id"
409 snapshot_open db2 main $::snapshot
410 db2 eval { SELECT * FROM x1 }
417 list [catch {snapshot_open db2 main $::snapshot} msg] $msg
420 snapshot_free $snapshot
422 #-------------------------------------------------------------------------
423 # The following tests investigate the sqlite3_snapshot_cmp() API.
426 # Compare snapshots $p1 and $p2, checking that the result is $r.
428 proc do_snapshot_cmp_test {tn p1 p2 r} {
429 uplevel [list do_test $tn.1 [list snapshot_cmp $p1 $p2] $r]
430 uplevel [list do_test $tn.2 [list snapshot_cmp $p2 $p1] [expr $r*-1]]
431 uplevel [list do_test $tn.3 [list snapshot_cmp $p1 $p1] 0]
432 uplevel [list do_test $tn.4 [list snapshot_cmp $p2 $p2] 0]
438 do_execsql_test $tn.7.1 {
439 PRAGMA journal_mode = wal;
444 execsql { BEGIN ; PRAGMA application_id }
445 set p1 [snapshot_get db main]
447 INSERT INTO t1 VALUES(10);
450 execsql { BEGIN ; PRAGMA application_id }
451 set p2 [snapshot_get db main]
455 do_snapshot_cmp_test $tn.7.1.3 $p1 $p2 -1
459 do_execsql_test $tn.7.2.1 {
460 INSERT INTO t1 VALUES(11);
461 INSERT INTO t1 VALUES(12);
462 INSERT INTO t1 VALUES(13);
464 PRAGMA application_id;
467 set p1 [snapshot_get db main]
470 INSERT INTO t1 VALUES(14);
471 PRAGMA wal_checkpoint;
473 PRAGMA application_id;
475 set p2 [snapshot_get db main]
479 do_snapshot_cmp_test $tn.7.2.3 $p1 $p2 -1
484 INSERT INTO t1 VALUES(14);
486 PRAGMA application_id;
488 set p2 [snapshot_get db main]
492 do_snapshot_cmp_test $tn.7.3.2 $p1 $p2 -1