AryaWu/sqlite
0
1# 2016 September 232#3# The author disclaims copyright to this source code. In place of4# 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 focus12# of this file is the sqlite3_snapshot_xxx() APIs.13#14 15set testdir [file dirname $argv0]16source $testdir/tester.tcl17ifcapable !snapshot {finish_test; return}18set testprefix snapshot319 20# This test does not work with the inmemory_journal permutation. The reason21# is that each connection opened as part of this permutation executes22# "PRAGMA journal_mode=memory", which fails if the database is in wal mode23# and there are one or more existing connections.24if {[permutation]=="inmemory_journal"} {25 finish_test26 return27}28 29#-------------------------------------------------------------------------30# This block of tests verifies that it is not possible to wrap the wal31# file - using a writer or a "PRAGMA wal_checkpoint = TRUNCATE" - while32# there is an open snapshot transaction (transaction opened using33# sqlite3_snapshot_open()).34#35do_execsql_test 1.0 {36 CREATE TABLE t1(y);37 PRAGMA journal_mode = wal;38 INSERT INTO t1 VALUES(1);39 INSERT INTO t1 VALUES(2);40 INSERT INTO t1 VALUES(3);41 INSERT INTO t1 VALUES(4);42} {wal}43 44do_test 1.1 {45 sqlite3 db2 test.db46 sqlite3 db3 test.db47 48 execsql {SELECT * FROM sqlite_master} db249 execsql {SELECT * FROM sqlite_master} db350 51 db2 trans { set snap [sqlite3_snapshot_get_blob db2 main] }52 db2 eval { SELECT * FROM t1 }53} {1 2 3 4}54 55do_test 1.2 {56 execsql BEGIN db257 sqlite3_snapshot_open_blob db2 main $snap58 db2 eval { SELECT * FROM t1 }59} {1 2 3 4}60 61do_test 1.2 {62 execsql END db263 execsql { PRAGMA wal_checkpoint }64 65 execsql BEGIN db266 sqlite3_snapshot_open_blob db2 main $snap67 db2 eval { SELECT * FROM t1 }68} {1 2 3 4}69 70set sz [file size test.db-wal]71do_test 1.3 {72 execsql { PRAGMA wal_checkpoint = truncate }73 file size test.db-wal74} $sz75 76do_test 1.4 {77 execsql BEGIN db378 list [catch { sqlite3_snapshot_open_blob db3 main $snap } msg] $msg79} {0 {}}80 81do_test 1.5 {82 db3 eval { SELECT * FROM t1; END }83} {1 2 3 4}84 85do_test 1.6 {86 db2 eval { SELECT * FROM t1; END }87} {1 2 3 4}88 89do_test 1.7 {90 execsql { PRAGMA wal_checkpoint = truncate }91 file size test.db-wal92} 093 94do_test 1.8 {95 execsql BEGIN db396 list [catch { sqlite3_snapshot_open_blob db3 main $snap } msg] $msg97} {1 SQLITE_ERROR_SNAPSHOT}98 99db3 close100db2 close101 102#-------------------------------------------------------------------------103reset_db104do_execsql_test 2.0 {105 PRAGMA journal_mode = wal;106 CREATE TABLE t1(a, b);107 INSERT INTO t1 VALUES(1, 2);108 INSERT INTO t1 VALUES(3, 4);109} {wal}110 111sqlite3 db2 test.db112sqlite3 db3 test.db113do_execsql_test -db db2 2.0.1 {114 SELECT * FROM t1115} {1 2 3 4}116do_execsql_test -db db3 2.0.2 {117 SELECT * FROM t1118} {1 2 3 4}119 120do_execsql_test -db db2 2.2 {121 PRAGMA wal_checkpoint;122} {0 4 4}123 124do_test 2.1 {125 db eval { BEGIN }126 set snap [sqlite3_snapshot_get db main]127 set {} {}128} {}129 130do_execsql_test -db db2 2.3 {131 INSERT INTO t1 VALUES(5, 6);132} {}133 134do_test 2.2 {135 execsql { BEGIN } db3136 sqlite3_snapshot_open db3 main $snap137} {}138 139sqlite3_snapshot_free $snap140 141finish_test142 