| 1 |
# 2016 October 31 |
| 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 SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE |
| 13 |
# option. |
| 14 |
# |
| 15 |
|
| 16 |
set testdir [file dirname $argv0] |
| 17 |
source $testdir/tester.tcl |
| 18 |
source $testdir/lock_common.tcl |
| 19 |
source $testdir/malloc_common.tcl |
| 20 |
source $testdir/wal_common.tcl |
| 21 |
ifcapable !wal {finish_test ; return } |
| 22 |
if {[permutation]=="journaltest" || [permutation]=="inmemory_journal"} { |
| 23 |
finish_test |
| 24 |
return |
| 25 |
} |
| 26 |
|
| 27 |
set testprefix nockpt |
| 28 |
|
| 29 |
do_execsql_test 1.0 { |
| 30 |
PRAGMA auto_vacuum=OFF; |
| 31 |
PRAGMA page_size = 1024; |
| 32 |
PRAGMA journal_mode = wal; |
| 33 |
CREATE TABLE c1(x, y, z); |
| 34 |
INSERT INTO c1 VALUES(1, 2, 3); |
| 35 |
} {wal} |
| 36 |
|
| 37 |
do_test 1.1 { file exists test.db-wal } 1 |
| 38 |
do_test 1.2 { file size test.db-wal } [wal_file_size 3 1024] |
| 39 |
do_test 1.3 { db close } {} |
| 40 |
do_test 1.4 { file exists test.db-wal } 0 |
| 41 |
|
| 42 |
sqlite3 db test.db |
| 43 |
do_execsql_test 1.5 { |
| 44 |
INSERT INTO c1 VALUES(4, 5, 6); |
| 45 |
INSERT INTO c1 VALUES(7, 8, 9); |
| 46 |
} |
| 47 |
do_test 1.6 { file exists test.db-wal } 1 |
| 48 |
do_test 1.7 { sqlite3_db_config db NO_CKPT_ON_CLOSE 1 } {1} |
| 49 |
do_test 1.8 { file size test.db-wal } [wal_file_size 2 1024] |
| 50 |
do_test 1.9 { db close } {} |
| 51 |
do_test 1.10 { file exists test.db-wal } 1 |
| 52 |
do_test 1.11 { file size test.db-wal } [wal_file_size 2 1024] |
| 53 |
|
| 54 |
sqlite3 db test.db |
| 55 |
do_execsql_test 1.12 { |
| 56 |
SELECT * FROM c1 |
| 57 |
} {1 2 3 4 5 6 7 8 9} |
| 58 |
|
| 59 |
do_execsql_test 1.13 { PRAGMA main.journal_mode } {wal} |
| 60 |
do_test 1.14 { sqlite3_db_config db NO_CKPT_ON_CLOSE 1 } {1} |
| 61 |
do_execsql_test 1.14 { PRAGMA main.journal_mode = delete } {delete} |
| 62 |
do_test 1.15 { file exists test.db-wal } {0} |
| 63 |
|
| 64 |
if {$::tcl_platform(platform) ne "windows"} { |
| 65 |
#------------------------------------------------------------------------- |
| 66 |
# Test an unusual scenario: |
| 67 |
# |
| 68 |
# 1. A wal mode db is opened and written. Then sqlite3_close_v2() used |
| 69 |
# to close the db handle while there is still an unfinalized |
| 70 |
# statement (so the db handle stays open). |
| 71 |
# |
| 72 |
# 2. The db, wal and *-shm files are deleted from the file system. |
| 73 |
# |
| 74 |
# 3. Another connection creates a new wal mode db at the same file-system |
| 75 |
# location as the previous one. |
| 76 |
# |
| 77 |
# 4. The statement left unfinalized in (1) is finalized. |
| 78 |
# |
| 79 |
# The test is to ensure that the connection left open in step (1) does |
| 80 |
# not try to delete the wal file from the file-system as part of step |
| 81 |
# 4. |
| 82 |
# |
| 83 |
reset_db |
| 84 |
db close |
| 85 |
|
| 86 |
# Open a connection on a wal database. Write to it a bit. Then prepare |
| 87 |
# a statement and call sqlite3_close_v2() (so that the statement handle |
| 88 |
# holds the db connection open). |
| 89 |
# |
| 90 |
set ::db1 [sqlite3_open_v2 test.db SQLITE_OPEN_READWRITE ""] |
| 91 |
do_test 2.0 { |
| 92 |
lindex [ |
| 93 |
sqlite3_exec $::db1 { |
| 94 |
PRAGMA journal_mode = wal; |
| 95 |
CREATE TABLE t1(x PRIMARY KEY, y UNIQUE, z); |
| 96 |
INSERT INTO t1 VALUES(1, 2, 3); |
| 97 |
PRAGMA wal_checkpoint; |
| 98 |
}] 0 |
| 99 |
} {0} |
| 100 |
set ::stmt [sqlite3_prepare $::db1 "SELECT * FROM t1" -1 dummy] |
| 101 |
sqlite3_close_v2 $::db1 |
| 102 |
|
| 103 |
# Delete the database, wal and shm files. |
| 104 |
# |
| 105 |
catch {forcedelete test.db} |
| 106 |
catch {forcedelete test.db-wal} |
| 107 |
catch {forcedelete test.db-shm} |
| 108 |
|
| 109 |
# Open and populate a new database file at the same file-system location |
| 110 |
# as the one just deleted. Contrive a partial checkpoint on it. |
| 111 |
# |
| 112 |
sqlite3 db test.db |
| 113 |
sqlite3 db2 test.db |
| 114 |
do_execsql_test 2.1 { |
| 115 |
PRAGMA auto_vacuum=OFF; |
| 116 |
PRAGMA journal_mode = wal; |
| 117 |
CREATE TABLE y1(a PRIMARY KEY, b UNIQUE, c); |
| 118 |
INSERT INTO y1 VALUES('a', 'b', 'c'); |
| 119 |
INSERT INTO y1 VALUES('d', 'e', 'f'); |
| 120 |
} {wal} |
| 121 |
do_execsql_test -db db2 2.2 { |
| 122 |
BEGIN; |
| 123 |
SELECT * FROM y1; |
| 124 |
} {a b c d e f} |
| 125 |
do_execsql_test 2.3 { |
| 126 |
UPDATE y1 SET c='g' WHERE a='d'; |
| 127 |
PRAGMA wal_checkpoint; |
| 128 |
} {0 11 10} |
| 129 |
do_execsql_test -db db2 2.4 { |
| 130 |
COMMIT |
| 131 |
} |
| 132 |
|
| 133 |
# Finalize the statement handle, causing the first connection to be |
| 134 |
# closed. Test that this has not corrupted the database file by |
| 135 |
# deleting the new wal file from the file-system. If it has, this |
| 136 |
# test should fail with an IO or corruption error. |
| 137 |
# |
| 138 |
do_test 2.5 { |
| 139 |
sqlite3_finalize $::stmt |
| 140 |
sqlite3 db3 test.db |
| 141 |
execsql { |
| 142 |
PRAGMA integrity_check; |
| 143 |
SELECT * FROM y1; |
| 144 |
} db3 |
| 145 |
} {ok a b c d e g} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
finish_test |