xref: /sqlite-3.40.0/test/snapshot3.test (revision 6ab91a7a)
121f2bafdSdan# 2016 September 23
221f2bafdSdan#
321f2bafdSdan# The author disclaims copyright to this source code.  In place of
421f2bafdSdan# a legal notice, here is a blessing:
521f2bafdSdan#
621f2bafdSdan#    May you do good and not evil.
721f2bafdSdan#    May you find forgiveness for yourself and forgive others.
821f2bafdSdan#    May you share freely, never taking more than you give.
921f2bafdSdan#
1021f2bafdSdan#***********************************************************************
1121f2bafdSdan# This file implements regression tests for SQLite library. The focus
1221f2bafdSdan# of this file is the sqlite3_snapshot_xxx() APIs.
1321f2bafdSdan#
1421f2bafdSdan
1521f2bafdSdanset testdir [file dirname $argv0]
1621f2bafdSdansource $testdir/tester.tcl
1721f2bafdSdanifcapable !snapshot {finish_test; return}
1821f2bafdSdanset testprefix snapshot3
1921f2bafdSdan
2021f2bafdSdan# This test does not work with the inmemory_journal permutation. The reason
2121f2bafdSdan# is that each connection opened as part of this permutation executes
2221f2bafdSdan# "PRAGMA journal_mode=memory", which fails if the database is in wal mode
2321f2bafdSdan# and there are one or more existing connections.
2421f2bafdSdanif {[permutation]=="inmemory_journal"} {
2521f2bafdSdan  finish_test
2621f2bafdSdan  return
2721f2bafdSdan}
2821f2bafdSdan
2921f2bafdSdan#-------------------------------------------------------------------------
3021f2bafdSdan# This block of tests verifies that it is not possible to wrap the wal
3121f2bafdSdan# file - using a writer or a "PRAGMA wal_checkpoint = TRUNCATE" - while
3221f2bafdSdan# there is an open snapshot transaction (transaction opened using
3321f2bafdSdan# sqlite3_snapshot_open()).
3421f2bafdSdan#
3521f2bafdSdando_execsql_test 1.0 {
3621f2bafdSdan  CREATE TABLE t1(y);
3721f2bafdSdan  PRAGMA journal_mode = wal;
3821f2bafdSdan  INSERT INTO t1 VALUES(1);
3921f2bafdSdan  INSERT INTO t1 VALUES(2);
4021f2bafdSdan  INSERT INTO t1 VALUES(3);
4121f2bafdSdan  INSERT INTO t1 VALUES(4);
4221f2bafdSdan} {wal}
4321f2bafdSdan
4421f2bafdSdando_test 1.1 {
4521f2bafdSdan  sqlite3 db2 test.db
4621f2bafdSdan  sqlite3 db3 test.db
4721f2bafdSdan
4821f2bafdSdan  execsql {SELECT * FROM sqlite_master} db2
4921f2bafdSdan  execsql {SELECT * FROM sqlite_master} db3
5021f2bafdSdan
5121f2bafdSdan  db2 trans { set snap [sqlite3_snapshot_get_blob db2 main] }
5221f2bafdSdan  db2 eval { SELECT * FROM t1 }
5321f2bafdSdan} {1 2 3 4}
5421f2bafdSdan
5521f2bafdSdando_test 1.2 {
5621f2bafdSdan  execsql BEGIN db2
5721f2bafdSdan  sqlite3_snapshot_open_blob db2 main $snap
5821f2bafdSdan  db2 eval { SELECT * FROM t1 }
5921f2bafdSdan} {1 2 3 4}
6021f2bafdSdan
6121f2bafdSdando_test 1.2 {
6221f2bafdSdan  execsql END db2
6321f2bafdSdan  execsql { PRAGMA wal_checkpoint }
6421f2bafdSdan
6521f2bafdSdan  execsql BEGIN db2
6621f2bafdSdan  sqlite3_snapshot_open_blob db2 main $snap
6721f2bafdSdan  db2 eval { SELECT * FROM t1 }
6821f2bafdSdan} {1 2 3 4}
6921f2bafdSdan
7021f2bafdSdanset sz [file size test.db-wal]
7121f2bafdSdando_test 1.3 {
7221f2bafdSdan  execsql { PRAGMA wal_checkpoint = truncate }
7321f2bafdSdan  file size test.db-wal
7421f2bafdSdan} $sz
7521f2bafdSdan
7621f2bafdSdando_test 1.4 {
7721f2bafdSdan  execsql BEGIN db3
7821f2bafdSdan  list [catch { sqlite3_snapshot_open_blob db3 main $snap } msg] $msg
7921f2bafdSdan} {0 {}}
8021f2bafdSdan
8121f2bafdSdando_test 1.5 {
8221f2bafdSdan  db3 eval { SELECT * FROM t1; END }
8321f2bafdSdan} {1 2 3 4}
8421f2bafdSdan
8521f2bafdSdando_test 1.6 {
8621f2bafdSdan  db2 eval { SELECT * FROM t1; END }
8721f2bafdSdan} {1 2 3 4}
8821f2bafdSdan
8921f2bafdSdando_test 1.7 {
9021f2bafdSdan  execsql { PRAGMA wal_checkpoint = truncate }
9121f2bafdSdan  file size test.db-wal
9221f2bafdSdan} 0
9321f2bafdSdan
9421f2bafdSdando_test 1.8 {
9521f2bafdSdan  execsql BEGIN db3
9621f2bafdSdan  list [catch { sqlite3_snapshot_open_blob db3 main $snap } msg] $msg
97*8d4b7a3fSdan} {1 SQLITE_ERROR_SNAPSHOT}
9821f2bafdSdan
9921f2bafdSdanfinish_test
100