1e91a54e1Sdan# 2010 June 15 2e91a54e1Sdan# 3e91a54e1Sdan# The author disclaims copyright to this source code. In place of 4e91a54e1Sdan# a legal notice, here is a blessing: 5e91a54e1Sdan# 6e91a54e1Sdan# May you do good and not evil. 7e91a54e1Sdan# May you find forgiveness for yourself and forgive others. 8e91a54e1Sdan# May you share freely, never taking more than you give. 9e91a54e1Sdan# 10e91a54e1Sdan#*********************************************************************** 11e91a54e1Sdan# 12e91a54e1Sdan 13e91a54e1Sdanset testdir [file dirname $argv0] 14e91a54e1Sdansource $testdir/tester.tcl 15e91a54e1Sdansource $testdir/lock_common.tcl 16e91a54e1Sdansource $testdir/malloc_common.tcl 17153eda0aSdansource $testdir/wal_common.tcl 1868928b6cSdan 1968928b6cSdan# Do not use a codec for tests in this file, as the database file is 2068928b6cSdan# manipulated directly using tcl scripts (using the [hexio_write] command). 2168928b6cSdan# 2268928b6cSdando_not_use_codec 23e91a54e1Sdan 24b0ac3e3aSdan# 25b0ac3e3aSdan# pager1-1.*: Test inter-process locking (clients in multiple processes). 26b0ac3e3aSdan# 27b0ac3e3aSdan# pager1-2.*: Test intra-process locking (multiple clients in this process). 28b0ac3e3aSdan# 29b0ac3e3aSdan# pager1-3.*: Savepoint related tests. 30b0ac3e3aSdan# 311f4cb65aSdan# pager1-4.*: Hot-journal related tests. 321f4cb65aSdan# 33de4996e2Sdan# pager1-5.*: Cases related to multi-file commits. 34de4996e2Sdan# 35146ed78bSdan# pager1-6.*: Cases related to "PRAGMA max_page_count" 36146ed78bSdan# 37153eda0aSdan# pager1-7.*: Cases specific to "PRAGMA journal_mode=TRUNCATE" 38153eda0aSdan# 390e986f51Sdan# pager1-8.*: Cases using temporary and in-memory databases. 400e986f51Sdan# 41dca321aeSdan# pager1-9.*: Tests related to the backup API. 42dca321aeSdan# 43ec6ffc1aSdan# pager1-10.*: Test that the assumed file-system sector-size is limited to 44ec6ffc1aSdan# 64KB. 45ec6ffc1aSdan# 46d353331aSdan# pager1-12.*: Tests involving "PRAGMA page_size" 47d353331aSdan# 48d353331aSdan# pager1-13.*: Cases specific to "PRAGMA journal_mode=PERSIST" 49d353331aSdan# 50d353331aSdan# pager1-14.*: Cases specific to "PRAGMA journal_mode=OFF" 51ec6ffc1aSdan# 52c8ce3972Sdan# pager1-15.*: Varying sqlite3_vfs.szOsFile 53c8ce3972Sdan# 54c8ce3972Sdan# pager1-16.*: Varying sqlite3_vfs.mxPathname 55c8ce3972Sdan# 56c8ce3972Sdan# pager1-17.*: Tests related to "PRAGMA omit_readlock" 57c8ce3972Sdan# 58c8ce3972Sdan# pager1-18.*: Test that the pager layer responds correctly if the b-tree 59c8ce3972Sdan# requests an invalid page number (due to db corruption). 60c8ce3972Sdan# 61b0ac3e3aSdan 62c396d4afSdanproc recursive_select {id table {script {}}} { 63c396d4afSdan set cnt 0 64c396d4afSdan db eval "SELECT rowid, * FROM $table WHERE rowid = ($id-1)" { 65c396d4afSdan recursive_select $rowid $table $script 66c396d4afSdan incr cnt 67c396d4afSdan } 68c396d4afSdan if {$cnt==0} { eval $script } 69c396d4afSdan} 70c396d4afSdan 7153f04f3bSdanset a_string_counter 1 7253f04f3bSdanproc a_string {n} { 7353f04f3bSdan global a_string_counter 7453f04f3bSdan incr a_string_counter 7553f04f3bSdan string range [string repeat "${a_string_counter}." $n] 1 $n 7653f04f3bSdan} 7753f04f3bSdandb func a_string a_string 7853f04f3bSdan 79a4a9095eSdando_multiclient_test tn { 80e91a54e1Sdan 81e91a54e1Sdan # Create and populate a database table using connection [db]. Check 82e91a54e1Sdan # that connections [db2] and [db3] can see the schema and content. 83e91a54e1Sdan # 84e91a54e1Sdan do_test pager1-$tn.1 { 85e91a54e1Sdan sql1 { 86e91a54e1Sdan CREATE TABLE t1(a PRIMARY KEY, b); 87e91a54e1Sdan CREATE INDEX i1 ON t1(b); 88d353331aSdan INSERT INTO t1 VALUES(1, 'one'); INSERT INTO t1 VALUES(2, 'two'); 89e91a54e1Sdan } 90e91a54e1Sdan } {} 91e91a54e1Sdan do_test pager1-$tn.2 { sql2 { SELECT * FROM t1 } } {1 one 2 two} 92e91a54e1Sdan do_test pager1-$tn.3 { sql3 { SELECT * FROM t1 } } {1 one 2 two} 93e91a54e1Sdan 94e91a54e1Sdan # Open a transaction and add a row using [db]. This puts [db] in 95e91a54e1Sdan # RESERVED state. Check that connections [db2] and [db3] can still 96e91a54e1Sdan # read the database content as it was before the transaction was 97e91a54e1Sdan # opened. [db] should see the inserted row. 98e91a54e1Sdan # 99e91a54e1Sdan do_test pager1-$tn.4 { 100e91a54e1Sdan sql1 { 101e91a54e1Sdan BEGIN; 102e91a54e1Sdan INSERT INTO t1 VALUES(3, 'three'); 103e91a54e1Sdan } 104e91a54e1Sdan } {} 105e91a54e1Sdan do_test pager1-$tn.5 { sql2 { SELECT * FROM t1 } } {1 one 2 two} 106e91a54e1Sdan do_test pager1-$tn.7 { sql1 { SELECT * FROM t1 } } {1 one 2 two 3 three} 107e91a54e1Sdan 108e91a54e1Sdan # [db] still has an open write transaction. Check that this prevents 109e91a54e1Sdan # other connections (specifically [db2]) from writing to the database. 110e91a54e1Sdan # 111e91a54e1Sdan # Even if [db2] opens a transaction first, it may not write to the 112e91a54e1Sdan # database. After the attempt to write the db within a transaction, 113e91a54e1Sdan # [db2] is left with an open transaction, but not a read-lock on 114e91a54e1Sdan # the main database. So it does not prevent [db] from committing. 115e91a54e1Sdan # 116e91a54e1Sdan do_test pager1-$tn.8 { 117e91a54e1Sdan csql2 { UPDATE t1 SET a = a + 10 } 118e91a54e1Sdan } {1 {database is locked}} 119e91a54e1Sdan do_test pager1-$tn.9 { 120e91a54e1Sdan csql2 { 121e91a54e1Sdan BEGIN; 122e91a54e1Sdan UPDATE t1 SET a = a + 10; 123e91a54e1Sdan } 124e91a54e1Sdan } {1 {database is locked}} 125e91a54e1Sdan 126e91a54e1Sdan # Have [db] commit its transactions. Check the other connections can 127e91a54e1Sdan # now see the new database content. 128e91a54e1Sdan # 129e91a54e1Sdan do_test pager1-$tn.10 { sql1 { COMMIT } } {} 130e91a54e1Sdan do_test pager1-$tn.11 { sql1 { SELECT * FROM t1 } } {1 one 2 two 3 three} 131e91a54e1Sdan do_test pager1-$tn.12 { sql2 { SELECT * FROM t1 } } {1 one 2 two 3 three} 132e91a54e1Sdan do_test pager1-$tn.13 { sql3 { SELECT * FROM t1 } } {1 one 2 two 3 three} 133e91a54e1Sdan 134e91a54e1Sdan # Check that, as noted above, [db2] really did keep an open transaction 135e91a54e1Sdan # after the attempt to write the database failed. 136e91a54e1Sdan # 137e91a54e1Sdan do_test pager1-$tn.14 { 138e91a54e1Sdan csql2 { BEGIN } 139e91a54e1Sdan } {1 {cannot start a transaction within a transaction}} 140e91a54e1Sdan do_test pager1-$tn.15 { sql2 { ROLLBACK } } {} 141e91a54e1Sdan 142e91a54e1Sdan # Have [db2] open a transaction and take a read-lock on the database. 143e91a54e1Sdan # Check that this prevents [db] from writing to the database (outside 144e91a54e1Sdan # of any transaction). After this fails, check that [db3] can read 145e91a54e1Sdan # the db (showing that [db] did not take a PENDING lock etc.) 146e91a54e1Sdan # 147e91a54e1Sdan do_test pager1-$tn.15 { 148e91a54e1Sdan sql2 { BEGIN; SELECT * FROM t1; } 149e91a54e1Sdan } {1 one 2 two 3 three} 150e91a54e1Sdan do_test pager1-$tn.16 { 151e91a54e1Sdan csql1 { UPDATE t1 SET a = a + 10 } 152e91a54e1Sdan } {1 {database is locked}} 153e91a54e1Sdan do_test pager1-$tn.17 { sql3 { SELECT * FROM t1 } } {1 one 2 two 3 three} 154e91a54e1Sdan 155e91a54e1Sdan # This time, have [db] open a transaction before writing the database. 156e91a54e1Sdan # This works - [db] gets a RESERVED lock which does not conflict with 157e91a54e1Sdan # the SHARED lock [db2] is holding. 158e91a54e1Sdan # 159e91a54e1Sdan do_test pager1-$tn.18 { 160e91a54e1Sdan sql1 { 161e91a54e1Sdan BEGIN; 162e91a54e1Sdan UPDATE t1 SET a = a + 10; 163e91a54e1Sdan } 164e91a54e1Sdan } {} 165e91a54e1Sdan do_test pager1-$tn-19 { 166e91a54e1Sdan sql1 { PRAGMA lock_status } 167e91a54e1Sdan } {main reserved temp closed} 168e91a54e1Sdan do_test pager1-$tn-20 { 169e91a54e1Sdan sql2 { PRAGMA lock_status } 170e91a54e1Sdan } {main shared temp closed} 171e91a54e1Sdan 172e91a54e1Sdan # Check that all connections can still read the database. Only [db] sees 173e91a54e1Sdan # the updated content (as the transaction has not been committed yet). 174e91a54e1Sdan # 175e91a54e1Sdan do_test pager1-$tn.21 { sql1 { SELECT * FROM t1 } } {11 one 12 two 13 three} 176e91a54e1Sdan do_test pager1-$tn.22 { sql2 { SELECT * FROM t1 } } {1 one 2 two 3 three} 177e91a54e1Sdan do_test pager1-$tn.23 { sql3 { SELECT * FROM t1 } } {1 one 2 two 3 three} 178e91a54e1Sdan 179e91a54e1Sdan # Because [db2] still has the SHARED lock, [db] is unable to commit the 180e91a54e1Sdan # transaction. If it tries, an error is returned and the connection 181e91a54e1Sdan # upgrades to a PENDING lock. 182e91a54e1Sdan # 183e91a54e1Sdan # Once this happens, [db] can read the database and see the new content, 184e91a54e1Sdan # [db2] (still holding SHARED) can still read the old content, but [db3] 185e91a54e1Sdan # (not holding any lock) is prevented by [db]'s PENDING from reading 186e91a54e1Sdan # the database. 187e91a54e1Sdan # 188e91a54e1Sdan do_test pager1-$tn.24 { csql1 { COMMIT } } {1 {database is locked}} 189e91a54e1Sdan do_test pager1-$tn-25 { 190e91a54e1Sdan sql1 { PRAGMA lock_status } 191e91a54e1Sdan } {main pending temp closed} 192e91a54e1Sdan do_test pager1-$tn.26 { sql1 { SELECT * FROM t1 } } {11 one 12 two 13 three} 193e91a54e1Sdan do_test pager1-$tn.27 { sql2 { SELECT * FROM t1 } } {1 one 2 two 3 three} 194e91a54e1Sdan do_test pager1-$tn.28 { csql3 { SELECT * FROM t1 } } {1 {database is locked}} 195e91a54e1Sdan 196e91a54e1Sdan # Have [db2] commit its read transaction, releasing the SHARED lock it 197e91a54e1Sdan # is holding. Now, neither [db2] nor [db3] may read the database (as [db] 198e91a54e1Sdan # is still holding a PENDING). 199e91a54e1Sdan # 200e91a54e1Sdan do_test pager1-$tn.29 { sql2 { COMMIT } } {} 201e91a54e1Sdan do_test pager1-$tn.30 { csql2 { SELECT * FROM t1 } } {1 {database is locked}} 202e91a54e1Sdan do_test pager1-$tn.31 { csql3 { SELECT * FROM t1 } } {1 {database is locked}} 203e91a54e1Sdan 204e91a54e1Sdan # [db] is now able to commit the transaction. Once the transaction is 205e91a54e1Sdan # committed, all three connections can read the new content. 206e91a54e1Sdan # 207e91a54e1Sdan do_test pager1-$tn.25 { sql1 { UPDATE t1 SET a = a+10 } } {} 208e91a54e1Sdan do_test pager1-$tn.26 { sql1 { COMMIT } } {} 209e91a54e1Sdan do_test pager1-$tn.27 { sql1 { SELECT * FROM t1 } } {21 one 22 two 23 three} 210e91a54e1Sdan do_test pager1-$tn.27 { sql2 { SELECT * FROM t1 } } {21 one 22 two 23 three} 211e91a54e1Sdan do_test pager1-$tn.28 { sql3 { SELECT * FROM t1 } } {21 one 22 two 23 three} 212d353331aSdan 213d353331aSdan # Install a busy-handler for connection [db]. 214d353331aSdan # 215d353331aSdan set ::nbusy [list] 216d353331aSdan proc busy {n} { 217d353331aSdan lappend ::nbusy $n 218d353331aSdan if {$n>5} { sql2 COMMIT } 219d353331aSdan return 0 220d353331aSdan } 221d353331aSdan db busy busy 222d353331aSdan 223d353331aSdan do_test pager1-$tn.29 { 224d353331aSdan sql1 { BEGIN ; INSERT INTO t1 VALUES('x', 'y') } 225d353331aSdan } {} 226d353331aSdan do_test pager1-$tn.30 { 227d353331aSdan sql2 { BEGIN ; SELECT * FROM t1 } 228d353331aSdan } {21 one 22 two 23 three} 229d353331aSdan do_test pager1-$tn.31 { sql1 COMMIT } {} 230d353331aSdan do_test pager1-$tn.32 { set ::nbusy } {0 1 2 3 4 5 6} 231e91a54e1Sdan} 232e91a54e1Sdan 23353f04f3bSdan#------------------------------------------------------------------------- 23453f04f3bSdan# Savepoint related test cases. 23553f04f3bSdan# 236dca321aeSdan# pager1-3.1.2.*: Force a savepoint rollback to cause the database file 237dca321aeSdan# to grow. 238dca321aeSdan# 239273f3f0dSdan# pager1-3.1.3.*: Use a journal created in synchronous=off mode as part 240273f3f0dSdan# of a savepoint rollback. 241273f3f0dSdan# 2420e986f51Sdando_test pager1-3.1.1 { 243b0ac3e3aSdan faultsim_delete_and_reopen 244b0ac3e3aSdan execsql { 245b0ac3e3aSdan CREATE TABLE t1(a PRIMARY KEY, b); 246b0ac3e3aSdan CREATE TABLE counter( 247b0ac3e3aSdan i CHECK (i<5), 248b0ac3e3aSdan u CHECK (u<10) 249b0ac3e3aSdan ); 250b0ac3e3aSdan INSERT INTO counter VALUES(0, 0); 251b0ac3e3aSdan CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN 252b0ac3e3aSdan UPDATE counter SET i = i+1; 253b0ac3e3aSdan END; 254b0ac3e3aSdan CREATE TRIGGER tr2 AFTER UPDATE ON t1 BEGIN 255b0ac3e3aSdan UPDATE counter SET u = u+1; 256b0ac3e3aSdan END; 257b0ac3e3aSdan } 258b0ac3e3aSdan execsql { SELECT * FROM counter } 259b0ac3e3aSdan} {0 0} 260b0ac3e3aSdan 2610e986f51Sdando_execsql_test pager1-3.1.2 { 262d353331aSdan PRAGMA cache_size = 10; 263b0ac3e3aSdan BEGIN; 264b0ac3e3aSdan INSERT INTO t1 VALUES(1, randomblob(1500)); 265b0ac3e3aSdan INSERT INTO t1 VALUES(2, randomblob(1500)); 266b0ac3e3aSdan INSERT INTO t1 VALUES(3, randomblob(1500)); 267b0ac3e3aSdan SELECT * FROM counter; 268b0ac3e3aSdan} {3 0} 2690e986f51Sdando_catchsql_test pager1-3.1.3 { 270b0ac3e3aSdan INSERT INTO t1 SELECT a+3, randomblob(1500) FROM t1 271b0ac3e3aSdan} {1 {constraint failed}} 272b0ac3e3aSdando_execsql_test pager1-3.4 { SELECT * FROM counter } {3 0} 273b0ac3e3aSdando_execsql_test pager1-3.5 { SELECT a FROM t1 } {1 2 3} 274b0ac3e3aSdando_execsql_test pager1-3.6 { COMMIT } {} 275b0ac3e3aSdan 276273f3f0dSdanforeach {tn sql tcl} { 277d353331aSdan 7 { PRAGMA synchronous = NORMAL ; PRAGMA temp_store = 0 } { 278273f3f0dSdan testvfs tv -default 1 279273f3f0dSdan tv devchar safe_append 280273f3f0dSdan } 281d353331aSdan 8 { PRAGMA synchronous = NORMAL ; PRAGMA temp_store = 2 } { 282d353331aSdan testvfs tv -default 1 283d353331aSdan tv devchar sequential 284d353331aSdan } 285d353331aSdan 9 { PRAGMA synchronous = FULL } { } 286d353331aSdan 10 { PRAGMA synchronous = NORMAL } { } 287d353331aSdan 11 { PRAGMA synchronous = OFF } { } 288d353331aSdan 12 { PRAGMA synchronous = FULL ; PRAGMA fullfsync = 1 } { } 289d353331aSdan 13 { PRAGMA synchronous = FULL } { 290d353331aSdan testvfs tv -default 1 291d353331aSdan tv devchar sequential 292d353331aSdan } 2936b63ab47Sdan 14 { PRAGMA locking_mode = EXCLUSIVE } { 2946b63ab47Sdan } 295273f3f0dSdan} { 296273f3f0dSdan do_test pager1-3.$tn.1 { 297273f3f0dSdan eval $tcl 298dca321aeSdan faultsim_delete_and_reopen 299dca321aeSdan db func a_string a_string 300273f3f0dSdan execsql $sql 301dca321aeSdan execsql { 302dca321aeSdan PRAGMA auto_vacuum = 2; 303dca321aeSdan PRAGMA cache_size = 10; 304dca321aeSdan CREATE TABLE z(x INTEGER PRIMARY KEY, y); 305dca321aeSdan BEGIN; 306dca321aeSdan INSERT INTO z VALUES(NULL, a_string(800)); 307dca321aeSdan INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 2 308dca321aeSdan INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 4 309dca321aeSdan INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 8 310dca321aeSdan INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 16 311dca321aeSdan INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 32 312dca321aeSdan INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 64 313dca321aeSdan INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 128 314dca321aeSdan INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 256 315dca321aeSdan COMMIT; 316dca321aeSdan } 317dca321aeSdan execsql { PRAGMA auto_vacuum } 318dca321aeSdan } {2} 319273f3f0dSdan do_execsql_test pager1-3.$tn.2 { 320dca321aeSdan BEGIN; 321dca321aeSdan INSERT INTO z VALUES(NULL, a_string(800)); 322dca321aeSdan INSERT INTO z VALUES(NULL, a_string(800)); 323dca321aeSdan SAVEPOINT one; 324273f3f0dSdan UPDATE z SET y = NULL WHERE x>256; 325dca321aeSdan PRAGMA incremental_vacuum; 326dca321aeSdan SELECT count(*) FROM z WHERE x < 100; 327dca321aeSdan ROLLBACK TO one; 328dca321aeSdan COMMIT; 329dca321aeSdan } {99} 330dca321aeSdan 331273f3f0dSdan do_execsql_test pager1-3.$tn.3 { 332273f3f0dSdan BEGIN; 333273f3f0dSdan SAVEPOINT one; 334273f3f0dSdan UPDATE z SET y = y||x; 335273f3f0dSdan ROLLBACK TO one; 336273f3f0dSdan COMMIT; 337273f3f0dSdan SELECT count(*) FROM z; 338273f3f0dSdan } {258} 339273f3f0dSdan 340273f3f0dSdan do_execsql_test pager1-3.$tn.4 { 341273f3f0dSdan SAVEPOINT one; 342273f3f0dSdan UPDATE z SET y = y||x; 343273f3f0dSdan ROLLBACK TO one; 344273f3f0dSdan } {} 345273f3f0dSdan do_execsql_test pager1-3.$tn.5 { 346273f3f0dSdan SELECT count(*) FROM z; 347273f3f0dSdan RELEASE one; 348273f3f0dSdan PRAGMA integrity_check; 349273f3f0dSdan } {258 ok} 350273f3f0dSdan 351*78f1e538Sdan do_execsql_test pager1-3.$tn.6 { 352*78f1e538Sdan SAVEPOINT one; 353*78f1e538Sdan RELEASE one; 354*78f1e538Sdan } {} 355*78f1e538Sdan 356273f3f0dSdan db close 357273f3f0dSdan catch { tv delete } 358273f3f0dSdan} 359273f3f0dSdan 36053f04f3bSdan#------------------------------------------------------------------------- 36153f04f3bSdan# Hot journal rollback related test cases. 36253f04f3bSdan# 36353f04f3bSdan# pager1.4.1.*: Test that the pager module deletes very small invalid 36453f04f3bSdan# journal files. 36553f04f3bSdan# 36653f04f3bSdan# pager1.4.2.*: Test that if the master journal pointer at the end of a 36753f04f3bSdan# hot-journal file appears to be corrupt (checksum does not 36853f04f3bSdan# compute) the associated journal is rolled back (and no 36953f04f3bSdan# xAccess() call to check for the presence of any master 37053f04f3bSdan# journal file is made). 37153f04f3bSdan# 372de4996e2Sdan# pager1.4.3.*: Test that the contents of a hot-journal are ignored if the 373de4996e2Sdan# page-size or sector-size in the journal header appear to 374de4996e2Sdan# be invalid (too large, too small or not a power of 2). 375de4996e2Sdan# 376e08341c6Sdan# pager1.4.4.*: Test hot-journal rollback of journal file with a master 377e08341c6Sdan# journal pointer generated in various "PRAGMA synchronous" 378e08341c6Sdan# modes. 379e08341c6Sdan# 380e08341c6Sdan# pager1.4.5.*: Test that hot-journal rollback stops if it encounters a 381e08341c6Sdan# journal-record for which the checksum fails. 382e08341c6Sdan# 383ec6ffc1aSdan# pager1.4.6.*: Test that when rolling back a hot-journal that contains a 384ec6ffc1aSdan# master journal pointer, the master journal file is deleted 385ec6ffc1aSdan# after all the hot-journals that refer to it are deleted. 386ec6ffc1aSdan# 387c8ce3972Sdan# pager1.4.7.*: Test that if a hot-journal file exists but a client can 388c8ce3972Sdan# open it for reading only, the database cannot be accessed and 389c8ce3972Sdan# SQLITE_CANTOPEN is returned. 390c8ce3972Sdan# 39153f04f3bSdando_test pager1.4.1.1 { 39253f04f3bSdan faultsim_delete_and_reopen 39353f04f3bSdan execsql { 39453f04f3bSdan CREATE TABLE x(y, z); 39553f04f3bSdan INSERT INTO x VALUES(1, 2); 39653f04f3bSdan } 39753f04f3bSdan set fd [open test.db-journal w] 39853f04f3bSdan puts -nonewline $fd "helloworld" 39953f04f3bSdan close $fd 40053f04f3bSdan file exists test.db-journal 40153f04f3bSdan} {1} 40253f04f3bSdando_test pager1.4.1.2 { execsql { SELECT * FROM x } } {1 2} 40353f04f3bSdando_test pager1.4.1.3 { file exists test.db-journal } {0} 40453f04f3bSdan 40553f04f3bSdan# Set up a [testvfs] to snapshot the file-system just before SQLite 40653f04f3bSdan# deletes the master-journal to commit a multi-file transaction. 40753f04f3bSdan# 40853f04f3bSdan# In subsequent test cases, invoking [faultsim_restore_and_reopen] sets 40953f04f3bSdan# up the file system to contain two databases, two hot-journal files and 41053f04f3bSdan# a master-journal. 41153f04f3bSdan# 41253f04f3bSdando_test pager1.4.2.1 { 41353f04f3bSdan testvfs tstvfs -default 1 41453f04f3bSdan tstvfs filter xDelete 41553f04f3bSdan tstvfs script xDeleteCallback 41653f04f3bSdan proc xDeleteCallback {method file args} { 41753f04f3bSdan set file [file tail $file] 41853f04f3bSdan if { [string match *mj* $file] } { faultsim_save } 41953f04f3bSdan } 42053f04f3bSdan faultsim_delete_and_reopen 42153f04f3bSdan db func a_string a_string 42253f04f3bSdan execsql { 42353f04f3bSdan ATTACH 'test.db2' AS aux; 42453f04f3bSdan PRAGMA journal_mode = DELETE; 42553f04f3bSdan PRAGMA main.cache_size = 10; 42653f04f3bSdan PRAGMA aux.cache_size = 10; 42753f04f3bSdan CREATE TABLE t1(a UNIQUE, b UNIQUE); 42853f04f3bSdan CREATE TABLE aux.t2(a UNIQUE, b UNIQUE); 42953f04f3bSdan INSERT INTO t1 VALUES(a_string(200), a_string(300)); 43053f04f3bSdan INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1; 43153f04f3bSdan INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1; 43253f04f3bSdan INSERT INTO t2 SELECT * FROM t1; 43353f04f3bSdan BEGIN; 43453f04f3bSdan INSERT INTO t1 SELECT a_string(201), a_string(301) FROM t1; 43553f04f3bSdan INSERT INTO t1 SELECT a_string(202), a_string(302) FROM t1; 43653f04f3bSdan INSERT INTO t1 SELECT a_string(203), a_string(303) FROM t1; 43753f04f3bSdan INSERT INTO t1 SELECT a_string(204), a_string(304) FROM t1; 43853f04f3bSdan REPLACE INTO t2 SELECT * FROM t1; 43953f04f3bSdan COMMIT; 44053f04f3bSdan } 44153f04f3bSdan db close 44253f04f3bSdan tstvfs delete 44353f04f3bSdan} {} 44453f04f3bSdando_test pager1.4.2.2 { 44553f04f3bSdan faultsim_restore_and_reopen 44653f04f3bSdan execsql { 44753f04f3bSdan SELECT count(*) FROM t1; 44853f04f3bSdan PRAGMA integrity_check; 44953f04f3bSdan } 45053f04f3bSdan} {4 ok} 45153f04f3bSdando_test pager1.4.2.3 { 45253f04f3bSdan faultsim_restore_and_reopen 45353f04f3bSdan foreach f [glob test.db-mj*] { file delete -force $f } 45453f04f3bSdan execsql { 45553f04f3bSdan SELECT count(*) FROM t1; 45653f04f3bSdan PRAGMA integrity_check; 45753f04f3bSdan } 45853f04f3bSdan} {64 ok} 45953f04f3bSdando_test pager1.4.2.4 { 46053f04f3bSdan faultsim_restore_and_reopen 46153f04f3bSdan hexio_write test.db-journal [expr [file size test.db-journal]-20] 123456 46253f04f3bSdan execsql { 46353f04f3bSdan SELECT count(*) FROM t1; 46453f04f3bSdan PRAGMA integrity_check; 46553f04f3bSdan } 46653f04f3bSdan} {4 ok} 46753f04f3bSdando_test pager1.4.2.5 { 46853f04f3bSdan faultsim_restore_and_reopen 46953f04f3bSdan hexio_write test.db-journal [expr [file size test.db-journal]-20] 123456 47053f04f3bSdan foreach f [glob test.db-mj*] { file delete -force $f } 47153f04f3bSdan execsql { 47253f04f3bSdan SELECT count(*) FROM t1; 47353f04f3bSdan PRAGMA integrity_check; 47453f04f3bSdan } 47553f04f3bSdan} {4 ok} 47653f04f3bSdan 477de4996e2Sdando_test pager1.4.3.1 { 478de4996e2Sdan testvfs tstvfs -default 1 479de4996e2Sdan tstvfs filter xSync 480de4996e2Sdan tstvfs script xSyncCallback 481de4996e2Sdan proc xSyncCallback {method file args} { 482de4996e2Sdan set file [file tail $file] 483de4996e2Sdan if { 0==[string match *journal $file] } { faultsim_save } 484de4996e2Sdan } 485de4996e2Sdan faultsim_delete_and_reopen 486de4996e2Sdan execsql { 487de4996e2Sdan PRAGMA journal_mode = DELETE; 488de4996e2Sdan CREATE TABLE t1(a, b); 489de4996e2Sdan INSERT INTO t1 VALUES(1, 2); 490de4996e2Sdan INSERT INTO t1 VALUES(3, 4); 491de4996e2Sdan } 492de4996e2Sdan db close 493de4996e2Sdan tstvfs delete 494de4996e2Sdan} {} 495de4996e2Sdan 496de4996e2Sdanforeach {tn ofst value result} { 497de4996e2Sdan 2 20 31 {1 2 3 4} 498de4996e2Sdan 3 20 32 {1 2 3 4} 499de4996e2Sdan 4 20 33 {1 2 3 4} 500de4996e2Sdan 5 20 65536 {1 2 3 4} 501de4996e2Sdan 6 20 131072 {1 2 3 4} 502de4996e2Sdan 503de4996e2Sdan 7 24 511 {1 2 3 4} 504de4996e2Sdan 8 24 513 {1 2 3 4} 505de4996e2Sdan 9 24 65536 {1 2 3 4} 506de4996e2Sdan 507de4996e2Sdan 10 32 65536 {1 2} 508de4996e2Sdan} { 509de4996e2Sdan do_test pager1.4.3.$tn { 510de4996e2Sdan faultsim_restore_and_reopen 511de4996e2Sdan hexio_write test.db-journal $ofst [format %.8x $value] 512de4996e2Sdan execsql { SELECT * FROM t1 } 513de4996e2Sdan } $result 514de4996e2Sdan} 515de4996e2Sdandb close 516de4996e2Sdan 517e08341c6Sdan# Set up a VFS that snapshots the file-system just before a master journal 518e08341c6Sdan# file is deleted to commit a multi-file transaction. Specifically, the 519e08341c6Sdan# file-system is saved just before the xDelete() call to remove the 520e08341c6Sdan# master journal file from the file-system. 521e08341c6Sdan# 522e08341c6Sdantestvfs tv -default 1 523e08341c6Sdantv script copy_on_mj_delete 524e08341c6Sdanset ::mj_filename_length 0 525e08341c6Sdanproc copy_on_mj_delete {method filename args} { 526e08341c6Sdan if {[string match *mj* [file tail $filename]]} { 527e08341c6Sdan set ::mj_filename_length [string length $filename] 528e08341c6Sdan faultsim_save 529e08341c6Sdan } 530e08341c6Sdan return SQLITE_OK 531e08341c6Sdan} 532e08341c6Sdan 533e08341c6Sdanset pwd [pwd] 534e08341c6Sdanforeach {tn1 tcl} { 535e08341c6Sdan 1 { set prefix "test.db" } 536e08341c6Sdan 2 { 537e08341c6Sdan # This test depends on the underlying VFS being able to open paths 538e08341c6Sdan # 512 bytes in length. The idea is to create a hot-journal file that 539e08341c6Sdan # contains a master-journal pointer so large that it could contain 540e08341c6Sdan # a valid page record (if the file page-size is 512 bytes). So as to 541e08341c6Sdan # make sure SQLite doesn't get confused by this. 542e08341c6Sdan # 543e08341c6Sdan set nPadding [expr 511 - $::mj_filename_length] 54433d85c9cSshaneh if {$tcl_platform(platform)=="windows"} { 54533d85c9cSshaneh # TBD need to figure out how to do this correctly for Windows!!! 54633d85c9cSshaneh set nPadding [expr 255 - $::mj_filename_length] 54733d85c9cSshaneh } 548e08341c6Sdan 549e08341c6Sdan # We cannot just create a really long database file name to open, as 550e08341c6Sdan # Linux limits a single component of a path to 255 bytes by default 551e08341c6Sdan # (and presumably other systems have limits too). So create a directory 552e08341c6Sdan # hierarchy to work in. 553e08341c6Sdan # 554e08341c6Sdan set dirname "d123456789012345678901234567890/" 555e08341c6Sdan set nDir [expr $nPadding / 32] 556e08341c6Sdan if { $nDir } { 557e08341c6Sdan set p [string repeat $dirname $nDir] 558e08341c6Sdan file mkdir $p 559e08341c6Sdan cd $p 560e08341c6Sdan } 561e08341c6Sdan 562e08341c6Sdan set padding [string repeat x [expr $nPadding %32]] 563e08341c6Sdan set prefix "test.db${padding}" 564e08341c6Sdan } 565e08341c6Sdan} { 566e08341c6Sdan eval $tcl 567e08341c6Sdan foreach {tn2 sql} { 568e08341c6Sdan o { 569e08341c6Sdan PRAGMA main.synchronous=OFF; 570e08341c6Sdan PRAGMA aux.synchronous=OFF; 5713f94b609Sdan PRAGMA journal_mode = DELETE; 572e08341c6Sdan } 573e08341c6Sdan o512 { 574e08341c6Sdan PRAGMA main.synchronous=OFF; 575e08341c6Sdan PRAGMA aux.synchronous=OFF; 576e08341c6Sdan PRAGMA main.page_size = 512; 577e08341c6Sdan PRAGMA aux.page_size = 512; 5783f94b609Sdan PRAGMA journal_mode = DELETE; 579e08341c6Sdan } 580e08341c6Sdan n { 581e08341c6Sdan PRAGMA main.synchronous=NORMAL; 582e08341c6Sdan PRAGMA aux.synchronous=NORMAL; 5833f94b609Sdan PRAGMA journal_mode = DELETE; 584e08341c6Sdan } 585e08341c6Sdan f { 586e08341c6Sdan PRAGMA main.synchronous=FULL; 587e08341c6Sdan PRAGMA aux.synchronous=FULL; 5883f94b609Sdan PRAGMA journal_mode = DELETE; 589e08341c6Sdan } 590e08341c6Sdan } { 591e08341c6Sdan 592e08341c6Sdan set tn "${tn1}.${tn2}" 593e08341c6Sdan 594e08341c6Sdan # Set up a connection to have two databases, test.db (main) and 595e08341c6Sdan # test.db2 (aux). Then run a multi-file transaction on them. The 596e08341c6Sdan # VFS will snapshot the file-system just before the master-journal 597e08341c6Sdan # file is deleted to commit the transaction. 598e08341c6Sdan # 599e08341c6Sdan tv filter xDelete 600e08341c6Sdan do_test pager1-4.4.$tn.1 { 601e08341c6Sdan faultsim_delete_and_reopen $prefix 602e08341c6Sdan execsql " 603e08341c6Sdan ATTACH '${prefix}2' AS aux; 604e08341c6Sdan $sql 605e08341c6Sdan CREATE TABLE a(x); 606e08341c6Sdan CREATE TABLE aux.b(x); 607e08341c6Sdan INSERT INTO a VALUES('double-you'); 608e08341c6Sdan INSERT INTO a VALUES('why'); 609e08341c6Sdan INSERT INTO a VALUES('zed'); 610e08341c6Sdan INSERT INTO b VALUES('won'); 611e08341c6Sdan INSERT INTO b VALUES('too'); 612e08341c6Sdan INSERT INTO b VALUES('free'); 613e08341c6Sdan " 614e08341c6Sdan execsql { 615e08341c6Sdan BEGIN; 616e08341c6Sdan INSERT INTO a SELECT * FROM b WHERE rowid<=3; 617e08341c6Sdan INSERT INTO b SELECT * FROM a WHERE rowid<=3; 618e08341c6Sdan COMMIT; 619e08341c6Sdan } 620e08341c6Sdan } {} 621e08341c6Sdan tv filter {} 622e08341c6Sdan 623e08341c6Sdan # Check that the transaction was committed successfully. 624e08341c6Sdan # 625e08341c6Sdan do_execsql_test pager1-4.4.$tn.2 { 626e08341c6Sdan SELECT * FROM a 627e08341c6Sdan } {double-you why zed won too free} 628e08341c6Sdan do_execsql_test pager1-4.4.$tn.3 { 629e08341c6Sdan SELECT * FROM b 630e08341c6Sdan } {won too free double-you why zed} 631e08341c6Sdan 632e08341c6Sdan # Restore the file-system and reopen the databases. Check that it now 633e08341c6Sdan # appears that the transaction was not committed (because the file-system 634e08341c6Sdan # was restored to the state where it had not been). 635e08341c6Sdan # 636e08341c6Sdan do_test pager1-4.4.$tn.4 { 637e08341c6Sdan faultsim_restore_and_reopen $prefix 638e08341c6Sdan execsql "ATTACH '${prefix}2' AS aux" 639e08341c6Sdan } {} 640e08341c6Sdan do_execsql_test pager1-4.4.$tn.5 {SELECT * FROM a} {double-you why zed} 641e08341c6Sdan do_execsql_test pager1-4.4.$tn.6 {SELECT * FROM b} {won too free} 642e08341c6Sdan 643e08341c6Sdan # Restore the file-system again. This time, before reopening the databases, 644e08341c6Sdan # delete the master-journal file from the file-system. It now appears that 645e08341c6Sdan # the transaction was committed (no master-journal file == no rollback). 646e08341c6Sdan # 647e08341c6Sdan do_test pager1-4.4.$tn.7 { 648e08341c6Sdan faultsim_restore_and_reopen $prefix 649e08341c6Sdan foreach f [glob ${prefix}-mj*] { file delete -force $f } 650e08341c6Sdan execsql "ATTACH '${prefix}2' AS aux" 651e08341c6Sdan } {} 652e08341c6Sdan do_execsql_test pager1-4.4.$tn.8 { 653e08341c6Sdan SELECT * FROM a 654e08341c6Sdan } {double-you why zed won too free} 655e08341c6Sdan do_execsql_test pager1-4.4.$tn.9 { 656e08341c6Sdan SELECT * FROM b 657e08341c6Sdan } {won too free double-you why zed} 658e08341c6Sdan } 659e08341c6Sdan 660e08341c6Sdan cd $pwd 661e08341c6Sdan} 662e08341c6Sdandb close 663e08341c6Sdantv delete 6640e986f51Sdanfile delete -force $dirname 6650e986f51Sdan 6660e986f51Sdan 6670e986f51Sdan# Set up a VFS to make a copy of the file-system just before deleting a 6680e986f51Sdan# journal file to commit a transaction. The transaction modifies exactly 6690e986f51Sdan# two database pages (and page 1 - the change counter). 6700e986f51Sdan# 6710e986f51Sdantestvfs tv -default 1 6720e986f51Sdantv sectorsize 512 6730e986f51Sdantv script copy_on_journal_delete 6740e986f51Sdantv filter xDelete 6750e986f51Sdanproc copy_on_journal_delete {method filename args} { 6760e986f51Sdan if {[string match *journal $filename]} faultsim_save 6770e986f51Sdan return SQLITE_OK 6780e986f51Sdan} 6790e986f51Sdanfaultsim_delete_and_reopen 6800e986f51Sdando_execsql_test pager1.4.5.1 { 6813f94b609Sdan PRAGMA journal_mode = DELETE; 6820e986f51Sdan PRAGMA page_size = 1024; 6830e986f51Sdan CREATE TABLE t1(a, b); 6840e986f51Sdan CREATE TABLE t2(a, b); 6850e986f51Sdan INSERT INTO t1 VALUES('I', 'II'); 6860e986f51Sdan INSERT INTO t2 VALUES('III', 'IV'); 6870e986f51Sdan BEGIN; 6880e986f51Sdan INSERT INTO t1 VALUES(1, 2); 6890e986f51Sdan INSERT INTO t2 VALUES(3, 4); 6900e986f51Sdan COMMIT; 6913f94b609Sdan} {delete} 6920e986f51Sdantv filter {} 6930e986f51Sdan 6940e986f51Sdan# Check the transaction was committed: 6950e986f51Sdan# 6960e986f51Sdando_execsql_test pager1.4.5.2 { 6970e986f51Sdan SELECT * FROM t1; 6980e986f51Sdan SELECT * FROM t2; 6990e986f51Sdan} {I II 1 2 III IV 3 4} 7000e986f51Sdan 701ec6ffc1aSdan# Now try four tests: 7020e986f51Sdan# 7030e986f51Sdan# pager1-4.5.3: Restore the file-system. Check that the whole transaction 7040e986f51Sdan# is rolled back. 7050e986f51Sdan# 7060e986f51Sdan# pager1-4.5.4: Restore the file-system. Corrupt the first record in the 7070e986f51Sdan# journal. Check the transaction is not rolled back. 7080e986f51Sdan# 7090e986f51Sdan# pager1-4.5.5: Restore the file-system. Corrupt the second record in the 7100e986f51Sdan# journal. Check that the first record in the transaction is 7110e986f51Sdan# played back, but not the second. 7120e986f51Sdan# 71310f5a50eSdan# pager1-4.5.6: Restore the file-system. Try to open the database with a 71410f5a50eSdan# readonly connection. This should fail, as a read-only 71510f5a50eSdan# connection cannot roll back the database file. 71610f5a50eSdan# 7170e986f51Sdanfaultsim_restore_and_reopen 7180e986f51Sdando_execsql_test pager1.4.5.3 { 7190e986f51Sdan SELECT * FROM t1; 7200e986f51Sdan SELECT * FROM t2; 7210e986f51Sdan} {I II III IV} 7220e986f51Sdanfaultsim_restore_and_reopen 7230e986f51Sdanhexio_write test.db-journal [expr 512+4+1024 - 202] 0123456789ABCDEF 7240e986f51Sdando_execsql_test pager1.4.5.4 { 7250e986f51Sdan SELECT * FROM t1; 7260e986f51Sdan SELECT * FROM t2; 7270e986f51Sdan} {I II 1 2 III IV 3 4} 7280e986f51Sdanfaultsim_restore_and_reopen 7290e986f51Sdanhexio_write test.db-journal [expr 512+4+1024+4+4+1024 - 202] 0123456789ABCDEF 7300e986f51Sdando_execsql_test pager1.4.5.5 { 7310e986f51Sdan SELECT * FROM t1; 7320e986f51Sdan SELECT * FROM t2; 7330e986f51Sdan} {I II III IV 3 4} 7340e986f51Sdan 73510f5a50eSdanfaultsim_restore_and_reopen 73610f5a50eSdandb close 73710f5a50eSdansqlite3 db test.db -readonly 1 73810f5a50eSdando_catchsql_test pager1.4.5.6 { 73910f5a50eSdan SELECT * FROM t1; 74010f5a50eSdan SELECT * FROM t2; 74110f5a50eSdan} {1 {disk I/O error}} 742ec6ffc1aSdandb close 743ec6ffc1aSdan 744ec6ffc1aSdan# Snapshot the file-system just before multi-file commit. Save the name 745ec6ffc1aSdan# of the master journal file in $::mj_filename. 746ec6ffc1aSdan# 747ec6ffc1aSdantv script copy_on_mj_delete 748ec6ffc1aSdantv filter xDelete 749ec6ffc1aSdanproc copy_on_mj_delete {method filename args} { 750ec6ffc1aSdan if {[string match *mj* [file tail $filename]]} { 751ec6ffc1aSdan set ::mj_filename $filename 752ec6ffc1aSdan faultsim_save 753ec6ffc1aSdan } 754ec6ffc1aSdan return SQLITE_OK 755ec6ffc1aSdan} 756ec6ffc1aSdando_test pager1.4.6.1 { 757ec6ffc1aSdan faultsim_delete_and_reopen 758ec6ffc1aSdan execsql { 7593f94b609Sdan PRAGMA journal_mode = DELETE; 760ec6ffc1aSdan ATTACH 'test.db2' AS two; 761ec6ffc1aSdan CREATE TABLE t1(a, b); 762ec6ffc1aSdan CREATE TABLE two.t2(a, b); 763ec6ffc1aSdan INSERT INTO t1 VALUES(1, 't1.1'); 764ec6ffc1aSdan INSERT INTO t2 VALUES(1, 't2.1'); 765ec6ffc1aSdan BEGIN; 766ec6ffc1aSdan UPDATE t1 SET b = 't1.2'; 767ec6ffc1aSdan UPDATE t2 SET b = 't2.2'; 768ec6ffc1aSdan COMMIT; 769ec6ffc1aSdan } 770ec6ffc1aSdan tv filter {} 771ec6ffc1aSdan db close 772ec6ffc1aSdan} {} 773ec6ffc1aSdan 774ec6ffc1aSdanfaultsim_restore_and_reopen 775ec6ffc1aSdando_execsql_test pager1.4.6.2 { SELECT * FROM t1 } {1 t1.1} 776ec6ffc1aSdando_test pager1.4.6.3 { file exists $::mj_filename } {1} 777ec6ffc1aSdando_execsql_test pager1.4.6.4 { 778ec6ffc1aSdan ATTACH 'test.db2' AS two; 779ec6ffc1aSdan SELECT * FROM t2; 780ec6ffc1aSdan} {1 t2.1} 781ec6ffc1aSdando_test pager1.4.6.5 { file exists $::mj_filename } {0} 782ec6ffc1aSdan 783ec6ffc1aSdanfaultsim_restore_and_reopen 784ec6ffc1aSdandb close 785ec6ffc1aSdando_test pager1.4.6.8 { 786ec6ffc1aSdan set ::mj_filename1 $::mj_filename 787ec6ffc1aSdan tv filter xDelete 788ec6ffc1aSdan sqlite3 db test.db2 789ec6ffc1aSdan execsql { 7903f94b609Sdan PRAGMA journal_mode = DELETE; 791ec6ffc1aSdan ATTACH 'test.db3' AS three; 792ec6ffc1aSdan CREATE TABLE three.t3(a, b); 793ec6ffc1aSdan INSERT INTO t3 VALUES(1, 't3.1'); 794ec6ffc1aSdan BEGIN; 795ec6ffc1aSdan UPDATE t2 SET b = 't2.3'; 796ec6ffc1aSdan UPDATE t3 SET b = 't3.3'; 797ec6ffc1aSdan COMMIT; 798ec6ffc1aSdan } 799ec6ffc1aSdan expr {$::mj_filename1 != $::mj_filename} 800ec6ffc1aSdan} {1} 801ec6ffc1aSdanfaultsim_restore_and_reopen 802ec6ffc1aSdantv filter {} 803ec6ffc1aSdan 804ec6ffc1aSdan# The file-system now contains: 805ec6ffc1aSdan# 806ec6ffc1aSdan# * three databases 807ec6ffc1aSdan# * three hot-journal files 808ec6ffc1aSdan# * two master-journal files. 809ec6ffc1aSdan# 810ec6ffc1aSdan# The hot-journals associated with test.db2 and test.db3 point to 811ec6ffc1aSdan# master journal $::mj_filename. The hot-journal file associated with 812ec6ffc1aSdan# test.db points to master journal $::mj_filename1. So reading from 813ec6ffc1aSdan# test.db should delete $::mj_filename1. 814ec6ffc1aSdan# 815ec6ffc1aSdando_test pager1.4.6.9 { 816ec6ffc1aSdan lsort [glob test.db*] 817ec6ffc1aSdan} [lsort [list \ 818ec6ffc1aSdan test.db test.db2 test.db3 \ 819ec6ffc1aSdan test.db-journal test.db2-journal test.db3-journal \ 820ec6ffc1aSdan [file tail $::mj_filename] [file tail $::mj_filename1] 821ec6ffc1aSdan]] 822ec6ffc1aSdan 823ec6ffc1aSdan# The master-journal $::mj_filename1 contains pointers to test.db and 824ec6ffc1aSdan# test.db2. However the hot-journal associated with test.db2 points to 825ec6ffc1aSdan# a different master-journal. Therefore, reading from test.db only should 826ec6ffc1aSdan# be enough to cause SQLite to delete $::mj_filename1. 827ec6ffc1aSdan# 828ec6ffc1aSdando_test pager1.4.6.10 { file exists $::mj_filename } {1} 829ec6ffc1aSdando_test pager1.4.6.11 { file exists $::mj_filename1 } {1} 830ec6ffc1aSdando_execsql_test pager1.4.6.12 { SELECT * FROM t1 } {1 t1.1} 831ec6ffc1aSdando_test pager1.4.6.13 { file exists $::mj_filename } {1} 832ec6ffc1aSdando_test pager1.4.6.14 { file exists $::mj_filename1 } {0} 833ec6ffc1aSdan 834ec6ffc1aSdando_execsql_test pager1.4.6.12 { 835ec6ffc1aSdan ATTACH 'test.db2' AS two; 836ec6ffc1aSdan SELECT * FROM t2; 837ec6ffc1aSdan} {1 t2.1} 838ec6ffc1aSdando_test pager1.4.6.13 { file exists $::mj_filename } {1} 839ec6ffc1aSdando_execsql_test pager1.4.6.14 { 840ec6ffc1aSdan ATTACH 'test.db3' AS three; 841ec6ffc1aSdan SELECT * FROM t3; 842ec6ffc1aSdan} {1 t3.1} 843ec6ffc1aSdando_test pager1.4.6.15 { file exists $::mj_filename } {0} 84410f5a50eSdan 8450e986f51Sdandb close 8460e986f51Sdantv delete 847e08341c6Sdan 848c8ce3972Sdantestvfs tv -default 1 849c8ce3972Sdantv sectorsize 512 850c8ce3972Sdantv script copy_on_journal_delete 851c8ce3972Sdantv filter xDelete 852c8ce3972Sdanproc copy_on_journal_delete {method filename args} { 853c8ce3972Sdan if {[string match *journal $filename]} faultsim_save 854c8ce3972Sdan return SQLITE_OK 855c8ce3972Sdan} 856c8ce3972Sdanfaultsim_delete_and_reopen 857c8ce3972Sdando_execsql_test pager1.4.7.1 { 8583f94b609Sdan PRAGMA journal_mode = DELETE; 859c8ce3972Sdan CREATE TABLE t1(x PRIMARY KEY, y); 860c8ce3972Sdan CREATE INDEX i1 ON t1(y); 861c8ce3972Sdan INSERT INTO t1 VALUES('I', 'one'); 862c8ce3972Sdan INSERT INTO t1 VALUES('II', 'four'); 863c8ce3972Sdan INSERT INTO t1 VALUES('III', 'nine'); 864c8ce3972Sdan BEGIN; 865c8ce3972Sdan INSERT INTO t1 VALUES('IV', 'sixteen'); 866c8ce3972Sdan INSERT INTO t1 VALUES('V' , 'twentyfive'); 867c8ce3972Sdan COMMIT; 8683f94b609Sdan} {delete} 869c8ce3972Sdantv filter {} 870c8ce3972Sdandb close 871c8ce3972Sdantv delete 872c8ce3972Sdando_test pager1.4.7.2 { 873c8ce3972Sdan faultsim_restore_and_reopen 874c8ce3972Sdan catch {file attributes test.db-journal -permissions r--------} 875c8ce3972Sdan catch {file attributes test.db-journal -readonly 1} 876c8ce3972Sdan catchsql { SELECT * FROM t1 } 877c8ce3972Sdan} {1 {unable to open database file}} 878c8ce3972Sdando_test pager1.4.7.3 { 879c8ce3972Sdan db close 880c8ce3972Sdan catch {file attributes test.db-journal -permissions rw-rw-rw-} 881c8ce3972Sdan catch {file attributes test.db-journal -readonly 0} 882c8ce3972Sdan file delete test.db-journal 883c8ce3972Sdan file exists test.db-journal 884c8ce3972Sdan} {0} 885c8ce3972Sdan 886de4996e2Sdan#------------------------------------------------------------------------- 887146ed78bSdan# The following tests deal with multi-file commits. 888de4996e2Sdan# 889146ed78bSdan# pager1-5.1.*: The case where a multi-file cannot be committed because 890146ed78bSdan# another connection is holding a SHARED lock on one of the 891146ed78bSdan# files. After the SHARED lock is removed, the COMMIT succeeds. 892146ed78bSdan# 893146ed78bSdan# pager1-5.2.*: Multi-file commits with journal_mode=memory. 894146ed78bSdan# 895146ed78bSdan# pager1-5.3.*: Multi-file commits with journal_mode=memory. 896146ed78bSdan# 897146ed78bSdan# pager1-5.4.*: Check that with synchronous=normal, the master-journal file 898146ed78bSdan# name is added to a journal file immediately after the last 899146ed78bSdan# journal record. But with synchronous=full, extra unused space 900146ed78bSdan# is allocated between the last journal record and the 901146ed78bSdan# master-journal file name so that the master-journal file 902146ed78bSdan# name does not lie on the same sector as the last journal file 903146ed78bSdan# record. 904146ed78bSdan# 905e08341c6Sdan# pager1-5.5.*: Check that in journal_mode=PERSIST mode, a journal file is 906e08341c6Sdan# truncated to zero bytes when a multi-file transaction is 907e08341c6Sdan# committed (instead of the first couple of bytes being zeroed). 908e08341c6Sdan# 909146ed78bSdan# 910146ed78bSdando_test pager1-5.1.1 { 911146ed78bSdan faultsim_delete_and_reopen 912146ed78bSdan execsql { 913146ed78bSdan ATTACH 'test.db2' AS aux; 914146ed78bSdan CREATE TABLE t1(a, b); 915146ed78bSdan CREATE TABLE aux.t2(a, b); 916146ed78bSdan INSERT INTO t1 VALUES(17, 'Lenin'); 917146ed78bSdan INSERT INTO t1 VALUES(22, 'Stalin'); 918146ed78bSdan INSERT INTO t1 VALUES(53, 'Khrushchev'); 919146ed78bSdan } 920146ed78bSdan} {} 921146ed78bSdando_test pager1-5.1.2 { 922146ed78bSdan execsql { 923146ed78bSdan BEGIN; 924146ed78bSdan INSERT INTO t1 VALUES(64, 'Brezhnev'); 925146ed78bSdan INSERT INTO t2 SELECT * FROM t1; 926146ed78bSdan } 927146ed78bSdan sqlite3 db2 test.db2 928146ed78bSdan execsql { 929146ed78bSdan BEGIN; 930146ed78bSdan SELECT * FROM t2; 931146ed78bSdan } db2 932146ed78bSdan} {} 933146ed78bSdando_test pager1-5.1.3 { 934146ed78bSdan catchsql COMMIT 935146ed78bSdan} {1 {database is locked}} 936146ed78bSdando_test pager1-5.1.4 { 937146ed78bSdan execsql COMMIT db2 938146ed78bSdan execsql COMMIT 939146ed78bSdan execsql { SELECT * FROM t2 } db2 940146ed78bSdan} {17 Lenin 22 Stalin 53 Khrushchev 64 Brezhnev} 941146ed78bSdando_test pager1-5.1.5 { 942146ed78bSdan db2 close 943146ed78bSdan} {} 944146ed78bSdan 945146ed78bSdando_test pager1-5.2.1 { 946146ed78bSdan execsql { 947146ed78bSdan PRAGMA journal_mode = memory; 948146ed78bSdan BEGIN; 949146ed78bSdan INSERT INTO t1 VALUES(84, 'Andropov'); 950146ed78bSdan INSERT INTO t2 VALUES(84, 'Andropov'); 951146ed78bSdan COMMIT; 952146ed78bSdan } 953146ed78bSdan} {memory} 954146ed78bSdando_test pager1-5.3.1 { 955146ed78bSdan execsql { 956146ed78bSdan PRAGMA journal_mode = off; 957146ed78bSdan BEGIN; 958146ed78bSdan INSERT INTO t1 VALUES(85, 'Gorbachev'); 959146ed78bSdan INSERT INTO t2 VALUES(85, 'Gorbachev'); 960146ed78bSdan COMMIT; 961146ed78bSdan } 962146ed78bSdan} {off} 963146ed78bSdan 964146ed78bSdando_test pager1-5.4.1 { 965146ed78bSdan db close 966146ed78bSdan testvfs tv 967146ed78bSdan sqlite3 db test.db -vfs tv 968146ed78bSdan execsql { ATTACH 'test.db2' AS aux } 969146ed78bSdan 970146ed78bSdan tv filter xDelete 971146ed78bSdan tv script max_journal_size 972146ed78bSdan tv sectorsize 512 973146ed78bSdan set ::max_journal 0 974146ed78bSdan proc max_journal_size {method args} { 975146ed78bSdan set sz 0 976146ed78bSdan catch { set sz [file size test.db-journal] } 977146ed78bSdan if {$sz > $::max_journal} { 978146ed78bSdan set ::max_journal $sz 979146ed78bSdan } 980146ed78bSdan return SQLITE_OK 981146ed78bSdan } 982146ed78bSdan execsql { 983146ed78bSdan PRAGMA journal_mode = DELETE; 984146ed78bSdan PRAGMA synchronous = NORMAL; 985146ed78bSdan BEGIN; 986146ed78bSdan INSERT INTO t1 VALUES(85, 'Gorbachev'); 987146ed78bSdan INSERT INTO t2 VALUES(85, 'Gorbachev'); 988146ed78bSdan COMMIT; 989146ed78bSdan } 990146ed78bSdan set ::max_journal 991146ed78bSdan} [expr 2615+[string length [pwd]]] 992146ed78bSdando_test pager1-5.4.2 { 993146ed78bSdan set ::max_journal 0 994146ed78bSdan execsql { 995146ed78bSdan PRAGMA synchronous = full; 996146ed78bSdan BEGIN; 997146ed78bSdan DELETE FROM t1 WHERE b = 'Lenin'; 998146ed78bSdan DELETE FROM t2 WHERE b = 'Lenin'; 999146ed78bSdan COMMIT; 1000146ed78bSdan } 1001146ed78bSdan set ::max_journal 1002146ed78bSdan} [expr 3111+[string length [pwd]]] 1003146ed78bSdandb close 1004146ed78bSdantv delete 1005146ed78bSdan 1006146ed78bSdando_test pager1-5.5.1 { 1007146ed78bSdan sqlite3 db test.db 1008146ed78bSdan execsql { 1009146ed78bSdan ATTACH 'test.db2' AS aux; 1010146ed78bSdan PRAGMA journal_mode = PERSIST; 1011146ed78bSdan CREATE TABLE t3(a, b); 1012146ed78bSdan INSERT INTO t3 SELECT randomblob(1500), randomblob(1500) FROM t1; 1013146ed78bSdan UPDATE t3 SET b = randomblob(1500); 1014146ed78bSdan } 1015146ed78bSdan expr [file size test.db-journal] > 15000 1016146ed78bSdan} {1} 1017146ed78bSdando_test pager1-5.5.2 { 1018146ed78bSdan execsql { 1019146ed78bSdan PRAGMA synchronous = full; 1020146ed78bSdan BEGIN; 1021146ed78bSdan DELETE FROM t1 WHERE b = 'Stalin'; 1022146ed78bSdan DELETE FROM t2 WHERE b = 'Stalin'; 1023146ed78bSdan COMMIT; 1024146ed78bSdan } 1025146ed78bSdan file size test.db-journal 1026146ed78bSdan} {0} 1027146ed78bSdan 1028146ed78bSdan 1029146ed78bSdan#------------------------------------------------------------------------- 1030146ed78bSdan# The following tests work with "PRAGMA max_page_count" 1031146ed78bSdan# 1032146ed78bSdando_test pager1-6.1 { 1033146ed78bSdan faultsim_delete_and_reopen 1034146ed78bSdan execsql { 1035f43d7fceSdan PRAGMA auto_vacuum = none; 1036146ed78bSdan PRAGMA max_page_count = 10; 1037146ed78bSdan CREATE TABLE t2(a, b); 1038146ed78bSdan CREATE TABLE t3(a, b); 1039146ed78bSdan CREATE TABLE t4(a, b); 1040146ed78bSdan CREATE TABLE t5(a, b); 1041146ed78bSdan CREATE TABLE t6(a, b); 1042146ed78bSdan CREATE TABLE t7(a, b); 1043146ed78bSdan CREATE TABLE t8(a, b); 1044146ed78bSdan CREATE TABLE t9(a, b); 1045146ed78bSdan CREATE TABLE t10(a, b); 1046146ed78bSdan } 1047146ed78bSdan} {10} 1048d353331aSdando_catchsql_test pager1-6.2 { 1049d353331aSdan CREATE TABLE t11(a, b) 1050146ed78bSdan} {1 {database or disk is full}} 1051d353331aSdando_execsql_test pager1-6.4 { PRAGMA max_page_count } {10} 1052d353331aSdando_execsql_test pager1-6.5 { PRAGMA max_page_count = 15 } {15} 1053d353331aSdando_execsql_test pager1-6.6 { CREATE TABLE t11(a, b) } {} 1054d353331aSdando_execsql_test pager1-6.7 { 1055d353331aSdan BEGIN; 1056d353331aSdan INSERT INTO t11 VALUES(1, 2); 1057d353331aSdan PRAGMA max_page_count = 13; 1058d353331aSdan} {13} 1059d353331aSdando_execsql_test pager1-6.8 { 1060d353331aSdan INSERT INTO t11 VALUES(3, 4); 1061d353331aSdan PRAGMA max_page_count = 10; 1062d353331aSdan} {11} 1063d353331aSdando_execsql_test pager1-6.9 { COMMIT } {} 1064de4996e2Sdan 1065153eda0aSdan 1066153eda0aSdan#------------------------------------------------------------------------- 1067153eda0aSdan# The following tests work with "PRAGMA journal_mode=TRUNCATE" and 1068153eda0aSdan# "PRAGMA locking_mode=EXCLUSIVE". 1069153eda0aSdan# 1070153eda0aSdan# Each test is specified with 5 variables. As follows: 1071153eda0aSdan# 1072153eda0aSdan# $tn: Test Number. Used as part of the [do_test] test names. 1073153eda0aSdan# $sql: SQL to execute. 1074153eda0aSdan# $res: Expected result of executing $sql. 1075153eda0aSdan# $js: The expected size of the journal file, in bytes, after executing 1076153eda0aSdan# the SQL script. Or -1 if the journal is not expected to exist. 1077153eda0aSdan# $ws: The expected size of the WAL file, in bytes, after executing 1078153eda0aSdan# the SQL script. Or -1 if the WAL is not expected to exist. 1079153eda0aSdan# 108038e1a279Sdanifcapable wal { 1081153eda0aSdan faultsim_delete_and_reopen 1082153eda0aSdan foreach {tn sql res js ws} [subst { 1083153eda0aSdan 1084153eda0aSdan 1 { 1085153eda0aSdan CREATE TABLE t1(a, b); 1086153eda0aSdan PRAGMA auto_vacuum=OFF; 1087153eda0aSdan PRAGMA synchronous=NORMAL; 1088153eda0aSdan PRAGMA page_size=1024; 1089153eda0aSdan PRAGMA locking_mode=EXCLUSIVE; 1090153eda0aSdan PRAGMA journal_mode=TRUNCATE; 1091153eda0aSdan INSERT INTO t1 VALUES(1, 2); 1092153eda0aSdan } {exclusive truncate} 0 -1 1093153eda0aSdan 1094153eda0aSdan 2 { 1095153eda0aSdan BEGIN IMMEDIATE; 1096153eda0aSdan SELECT * FROM t1; 1097153eda0aSdan COMMIT; 1098153eda0aSdan } {1 2} 0 -1 1099153eda0aSdan 1100153eda0aSdan 3 { 1101153eda0aSdan BEGIN; 1102153eda0aSdan SELECT * FROM t1; 1103153eda0aSdan COMMIT; 1104153eda0aSdan } {1 2} 0 -1 1105153eda0aSdan 1106153eda0aSdan 4 { PRAGMA journal_mode = WAL } wal -1 -1 1107153eda0aSdan 5 { INSERT INTO t1 VALUES(3, 4) } {} -1 [wal_file_size 1 1024] 1108153eda0aSdan 6 { PRAGMA locking_mode = NORMAL } normal -1 [wal_file_size 1 1024] 1109153eda0aSdan 7 { INSERT INTO t1 VALUES(5, 6); } {} -1 [wal_file_size 2 1024] 1110153eda0aSdan 1111153eda0aSdan 8 { PRAGMA journal_mode = TRUNCATE } truncate 0 -1 1112153eda0aSdan 9 { INSERT INTO t1 VALUES(7, 8) } {} 0 -1 1113153eda0aSdan 10 { SELECT * FROM t1 } {1 2 3 4 5 6 7 8} 0 -1 1114153eda0aSdan 1115153eda0aSdan }] { 1116153eda0aSdan do_execsql_test pager1-7.1.$tn.1 $sql $res 1117153eda0aSdan catch { set J -1 ; set J [file size test.db-journal] } 1118153eda0aSdan catch { set W -1 ; set W [file size test.db-wal] } 1119153eda0aSdan do_test pager1-7.1.$tn.2 { list $J $W } [list $js $ws] 1120153eda0aSdan } 112138e1a279Sdan} 1122153eda0aSdan 112389ccf448Sdando_test pager1-7.2.1 { 112489ccf448Sdan faultsim_delete_and_reopen 112589ccf448Sdan execsql { 112689ccf448Sdan PRAGMA locking_mode = EXCLUSIVE; 112789ccf448Sdan CREATE TABLE t1(a, b); 112889ccf448Sdan BEGIN; 112989ccf448Sdan PRAGMA journal_mode = delete; 113089ccf448Sdan PRAGMA journal_mode = truncate; 113189ccf448Sdan } 113289ccf448Sdan} {exclusive delete truncate} 113389ccf448Sdando_test pager1-7.2.2 { 113489ccf448Sdan execsql { INSERT INTO t1 VALUES(1, 2) } 113589ccf448Sdan execsql { PRAGMA journal_mode = persist } 113689ccf448Sdan} {truncate} 113789ccf448Sdando_test pager1-7.2.3 { 113889ccf448Sdan execsql { COMMIT } 113989ccf448Sdan execsql { 114089ccf448Sdan PRAGMA journal_mode = persist; 114189ccf448Sdan PRAGMA journal_size_limit; 114289ccf448Sdan } 114389ccf448Sdan} {persist -1} 114489ccf448Sdan 1145d353331aSdan#------------------------------------------------------------------------- 1146d353331aSdan# The following tests, pager1-8.*, test that the special filenames 1147d353331aSdan# ":memory:" and "" open temporary databases. 1148d353331aSdan# 11490e986f51Sdanforeach {tn filename} { 11500e986f51Sdan 1 :memory: 11510e986f51Sdan 2 "" 11520e986f51Sdan} { 11530e986f51Sdan do_test pager1-8.$tn.1 { 1154153eda0aSdan faultsim_delete_and_reopen 1155153eda0aSdan db close 11560e986f51Sdan sqlite3 db $filename 1157153eda0aSdan execsql { 1158c8ce3972Sdan PRAGMA auto_vacuum = 1; 1159153eda0aSdan CREATE TABLE x1(x); 1160153eda0aSdan INSERT INTO x1 VALUES('Charles'); 1161153eda0aSdan INSERT INTO x1 VALUES('James'); 1162153eda0aSdan INSERT INTO x1 VALUES('Mary'); 1163153eda0aSdan SELECT * FROM x1; 1164153eda0aSdan } 1165153eda0aSdan } {Charles James Mary} 11660e986f51Sdan 11670e986f51Sdan do_test pager1-8.$tn.2 { 11680e986f51Sdan sqlite3 db2 $filename 11690e986f51Sdan catchsql { SELECT * FROM x1 } db2 1170153eda0aSdan } {1 {no such table: x1}} 1171153eda0aSdan 11720e986f51Sdan do_execsql_test pager1-8.$tn.3 { 11730e986f51Sdan BEGIN; 11740e986f51Sdan INSERT INTO x1 VALUES('William'); 11750e986f51Sdan INSERT INTO x1 VALUES('Anne'); 11760e986f51Sdan ROLLBACK; 11770e986f51Sdan } {} 11780e986f51Sdan} 11790e986f51Sdan 1180dca321aeSdan#------------------------------------------------------------------------- 1181dca321aeSdan# The next block of tests - pager1-9.* - deal with interactions between 1182dca321aeSdan# the pager and the backup API. Test cases: 1183dca321aeSdan# 1184dca321aeSdan# pager1-9.1.*: Test that a backup completes successfully even if the 1185dca321aeSdan# source db is written to during the backup op. 1186dca321aeSdan# 1187dca321aeSdan# pager1-9.2.*: Test that a backup completes successfully even if the 1188dca321aeSdan# source db is written to and then rolled back during a 1189dca321aeSdan# backup operation. 1190dca321aeSdan# 1191dca321aeSdando_test pager1-9.0.1 { 1192dca321aeSdan faultsim_delete_and_reopen 1193dca321aeSdan db func a_string a_string 1194dca321aeSdan execsql { 1195dca321aeSdan PRAGMA cache_size = 10; 1196dca321aeSdan BEGIN; 1197dca321aeSdan CREATE TABLE ab(a, b, UNIQUE(a, b)); 1198dca321aeSdan INSERT INTO ab VALUES( a_string(200), a_string(300) ); 1199dca321aeSdan INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab; 1200dca321aeSdan INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab; 1201dca321aeSdan INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab; 1202dca321aeSdan INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab; 1203dca321aeSdan INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab; 1204dca321aeSdan INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab; 1205dca321aeSdan INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab; 1206dca321aeSdan COMMIT; 1207dca321aeSdan } 1208dca321aeSdan} {} 1209dca321aeSdando_test pager1-9.0.2 { 1210dca321aeSdan sqlite3 db2 test.db2 1211dca321aeSdan db2 eval { PRAGMA cache_size = 10 } 1212dca321aeSdan sqlite3_backup B db2 main db main 1213dca321aeSdan list [B step 10000] [B finish] 1214dca321aeSdan} {SQLITE_DONE SQLITE_OK} 1215dca321aeSdando_test pager1-9.0.3 { 1216dca321aeSdan db one {SELECT md5sum(a, b) FROM ab} 1217dca321aeSdan} [db2 one {SELECT md5sum(a, b) FROM ab}] 1218dca321aeSdan 1219dca321aeSdando_test pager1-9.1.1 { 1220dca321aeSdan execsql { UPDATE ab SET a = a_string(201) } 1221dca321aeSdan sqlite3_backup B db2 main db main 1222dca321aeSdan B step 30 1223dca321aeSdan} {SQLITE_OK} 1224dca321aeSdando_test pager1-9.1.2 { 1225dca321aeSdan execsql { UPDATE ab SET b = a_string(301) } 1226dca321aeSdan list [B step 10000] [B finish] 1227dca321aeSdan} {SQLITE_DONE SQLITE_OK} 1228dca321aeSdando_test pager1-9.1.3 { 1229dca321aeSdan db one {SELECT md5sum(a, b) FROM ab} 1230dca321aeSdan} [db2 one {SELECT md5sum(a, b) FROM ab}] 1231dca321aeSdando_test pager1-9.1.4 { execsql { SELECT count(*) FROM ab } } {128} 1232dca321aeSdan 1233dca321aeSdando_test pager1-9.2.1 { 1234dca321aeSdan execsql { UPDATE ab SET a = a_string(202) } 1235dca321aeSdan sqlite3_backup B db2 main db main 1236dca321aeSdan B step 30 1237dca321aeSdan} {SQLITE_OK} 1238dca321aeSdando_test pager1-9.2.2 { 1239dca321aeSdan execsql { 1240dca321aeSdan BEGIN; 1241dca321aeSdan UPDATE ab SET b = a_string(301); 1242dca321aeSdan ROLLBACK; 1243dca321aeSdan } 1244dca321aeSdan list [B step 10000] [B finish] 1245dca321aeSdan} {SQLITE_DONE SQLITE_OK} 1246dca321aeSdando_test pager1-9.2.3 { 1247dca321aeSdan db one {SELECT md5sum(a, b) FROM ab} 1248dca321aeSdan} [db2 one {SELECT md5sum(a, b) FROM ab}] 1249dca321aeSdando_test pager1-9.2.4 { execsql { SELECT count(*) FROM ab } } {128} 1250ec6ffc1aSdandb close 1251ec6ffc1aSdandb2 close 1252ec6ffc1aSdan 1253c396d4afSdando_test pager1-9.3.1 { 1254c396d4afSdan testvfs tv -default 1 1255c396d4afSdan tv sectorsize 4096 1256c396d4afSdan faultsim_delete_and_reopen 1257c396d4afSdan 1258c396d4afSdan execsql { PRAGMA page_size = 1024 } 1259c396d4afSdan for {set ii 0} {$ii < 4} {incr ii} { execsql "CREATE TABLE t${ii}(a, b)" } 1260c396d4afSdan} {} 1261c396d4afSdando_test pager1-9.3.2 { 1262c396d4afSdan sqlite3 db2 test.db2 1263f412ee2eSdan 1264c396d4afSdan execsql { 1265c396d4afSdan PRAGMA page_size = 4096; 1266c396d4afSdan PRAGMA synchronous = OFF; 1267c396d4afSdan CREATE TABLE t1(a, b); 1268c396d4afSdan CREATE TABLE t2(a, b); 1269c396d4afSdan } db2 1270c396d4afSdan 1271c396d4afSdan sqlite3_backup B db2 main db main 1272c396d4afSdan B step 30 1273c396d4afSdan list [B step 10000] [B finish] 1274c396d4afSdan} {SQLITE_DONE SQLITE_OK} 1275c396d4afSdando_test pager1-9.3.3 { 1276c396d4afSdan db2 close 1277c396d4afSdan db close 1278c396d4afSdan tv delete 1279c396d4afSdan file size test.db2 1280c396d4afSdan} [file size test.db] 1281c396d4afSdan 1282f412ee2eSdando_test pager1-9.4.1 { 1283f412ee2eSdan faultsim_delete_and_reopen 1284f412ee2eSdan sqlite3 db2 test.db2 1285f412ee2eSdan execsql { 1286f412ee2eSdan PRAGMA page_size = 4096; 1287f412ee2eSdan CREATE TABLE t1(a, b); 1288f412ee2eSdan CREATE TABLE t2(a, b); 1289f412ee2eSdan } db2 1290f412ee2eSdan sqlite3_backup B db2 main db main 1291f412ee2eSdan list [B step 10000] [B finish] 1292f412ee2eSdan} {SQLITE_DONE SQLITE_OK} 1293f412ee2eSdando_test pager1-9.4.2 { 1294f412ee2eSdan list [file size test.db2] [file size test.db] 1295f412ee2eSdan} {0 0} 129633d85c9cSshanehdb2 close 1297d0b0d4dcSdan 1298ec6ffc1aSdan#------------------------------------------------------------------------- 1299ec6ffc1aSdan# Test that regardless of the value returned by xSectorSize(), the 1300ec6ffc1aSdan# minimum effective sector-size is 512 and the maximum 65536 bytes. 1301ec6ffc1aSdan# 1302ec6ffc1aSdantestvfs tv -default 1 1303ec6ffc1aSdanforeach sectorsize { 1304ec6ffc1aSdan 32 64 128 256 512 1024 2048 1305ec6ffc1aSdan 4096 8192 16384 32768 65536 131072 262144 1306ec6ffc1aSdan} { 1307ec6ffc1aSdan tv sectorsize $sectorsize 1308ec6ffc1aSdan set eff $sectorsize 1309ec6ffc1aSdan if {$sectorsize < 512} { set eff 512 } 1310ec6ffc1aSdan if {$sectorsize > 65536} { set eff 65536 } 1311ec6ffc1aSdan 1312d0b0d4dcSdan do_test pager1-10.$sectorsize.1 { 1313ec6ffc1aSdan faultsim_delete_and_reopen 1314d0b0d4dcSdan db func a_string a_string 1315ec6ffc1aSdan execsql { 1316ec6ffc1aSdan PRAGMA journal_mode = PERSIST; 1317ec6ffc1aSdan PRAGMA page_size = 1024; 1318d0b0d4dcSdan BEGIN; 1319ec6ffc1aSdan CREATE TABLE t1(a, b); 1320d0b0d4dcSdan CREATE TABLE t2(a, b); 1321d0b0d4dcSdan CREATE TABLE t3(a, b); 1322d0b0d4dcSdan COMMIT; 1323ec6ffc1aSdan } 1324ec6ffc1aSdan file size test.db-journal 1325ec6ffc1aSdan } [expr $sectorsize > 65536 ? 65536 : $sectorsize] 1326d0b0d4dcSdan 1327d0b0d4dcSdan do_test pager1-10.$sectorsize.2 { 1328d0b0d4dcSdan execsql { 1329d0b0d4dcSdan INSERT INTO t3 VALUES(a_string(300), a_string(300)); 1330d0b0d4dcSdan INSERT INTO t3 SELECT * FROM t3; /* 2 */ 1331d0b0d4dcSdan INSERT INTO t3 SELECT * FROM t3; /* 4 */ 1332d0b0d4dcSdan INSERT INTO t3 SELECT * FROM t3; /* 8 */ 1333d0b0d4dcSdan INSERT INTO t3 SELECT * FROM t3; /* 16 */ 1334d0b0d4dcSdan INSERT INTO t3 SELECT * FROM t3; /* 32 */ 1335ec6ffc1aSdan } 1336d0b0d4dcSdan } {} 1337d0b0d4dcSdan 1338d0b0d4dcSdan do_test pager1-10.$sectorsize.3 { 1339d0b0d4dcSdan db close 1340d0b0d4dcSdan sqlite3 db test.db 1341d0b0d4dcSdan execsql { 1342d0b0d4dcSdan PRAGMA cache_size = 10; 1343d0b0d4dcSdan BEGIN; 1344d0b0d4dcSdan } 1345d0b0d4dcSdan recursive_select 32 t3 {db eval "INSERT INTO t2 VALUES(1, 2)"} 1346d0b0d4dcSdan execsql { 1347d0b0d4dcSdan COMMIT; 1348d0b0d4dcSdan SELECT * FROM t2; 1349d0b0d4dcSdan } 1350d0b0d4dcSdan } {1 2} 1351d0b0d4dcSdan 1352d0b0d4dcSdan do_test pager1-10.$sectorsize.4 { 1353d0b0d4dcSdan execsql { 1354d0b0d4dcSdan CREATE TABLE t6(a, b); 1355d0b0d4dcSdan CREATE TABLE t7(a, b); 1356d0b0d4dcSdan CREATE TABLE t5(a, b); 1357d0b0d4dcSdan DROP TABLE t6; 1358d0b0d4dcSdan DROP TABLE t7; 1359d0b0d4dcSdan } 1360d0b0d4dcSdan execsql { 1361d0b0d4dcSdan BEGIN; 1362d0b0d4dcSdan CREATE TABLE t6(a, b); 1363d0b0d4dcSdan } 1364d0b0d4dcSdan recursive_select 32 t3 {db eval "INSERT INTO t5 VALUES(1, 2)"} 1365d0b0d4dcSdan execsql { 1366d0b0d4dcSdan COMMIT; 1367d0b0d4dcSdan SELECT * FROM t5; 1368d0b0d4dcSdan } 1369d0b0d4dcSdan } {1 2} 1370d0b0d4dcSdan 1371d0b0d4dcSdan} 1372d0b0d4dcSdandb close 1373d0b0d4dcSdan 1374d0b0d4dcSdantv sectorsize 4096 1375d0b0d4dcSdando_test pager1.10.x.1 { 1376d0b0d4dcSdan faultsim_delete_and_reopen 1377d0b0d4dcSdan execsql { 1378f43d7fceSdan PRAGMA auto_vacuum = none; 1379d0b0d4dcSdan PRAGMA page_size = 1024; 1380d0b0d4dcSdan CREATE TABLE t1(x); 1381d0b0d4dcSdan } 1382d0b0d4dcSdan for {set i 0} {$i<30} {incr i} { 1383d0b0d4dcSdan execsql { INSERT INTO t1 VALUES(zeroblob(900)) } 1384d0b0d4dcSdan } 1385d0b0d4dcSdan file size test.db 1386d0b0d4dcSdan} {32768} 1387d0b0d4dcSdando_test pager1.10.x.2 { 1388d0b0d4dcSdan execsql { 1389d0b0d4dcSdan CREATE TABLE t2(x); 1390d0b0d4dcSdan DROP TABLE t2; 1391d0b0d4dcSdan } 1392d0b0d4dcSdan file size test.db 1393d0b0d4dcSdan} {33792} 1394d0b0d4dcSdando_test pager1.10.x.3 { 1395d0b0d4dcSdan execsql { 1396d0b0d4dcSdan BEGIN; 1397d0b0d4dcSdan CREATE TABLE t2(x); 1398d0b0d4dcSdan } 1399d0b0d4dcSdan recursive_select 30 t1 1400d0b0d4dcSdan execsql { 1401d0b0d4dcSdan CREATE TABLE t3(x); 1402d0b0d4dcSdan COMMIT; 1403d0b0d4dcSdan } 1404d0b0d4dcSdan} {} 1405d0b0d4dcSdan 1406ec6ffc1aSdandb close 1407ec6ffc1aSdantv delete 1408ec6ffc1aSdan 1409ec6ffc1aSdantestvfs tv -default 1 1410ec6ffc1aSdanfaultsim_delete_and_reopen 1411ec6ffc1aSdandb func a_string a_string 1412ec6ffc1aSdando_execsql_test pager1-11.1 { 14133f94b609Sdan PRAGMA journal_mode = DELETE; 1414ec6ffc1aSdan PRAGMA cache_size = 10; 1415ec6ffc1aSdan BEGIN; 1416ec6ffc1aSdan CREATE TABLE zz(top PRIMARY KEY); 1417ec6ffc1aSdan INSERT INTO zz VALUES(a_string(222)); 1418ec6ffc1aSdan INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz; 1419ec6ffc1aSdan INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz; 1420ec6ffc1aSdan INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz; 1421ec6ffc1aSdan INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz; 1422ec6ffc1aSdan INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz; 1423ec6ffc1aSdan COMMIT; 1424ec6ffc1aSdan BEGIN; 1425ec6ffc1aSdan UPDATE zz SET top = a_string(345); 14263f94b609Sdan} {delete} 1427ec6ffc1aSdan 1428ec6ffc1aSdanproc lockout {method args} { return SQLITE_IOERR } 1429ec6ffc1aSdantv script lockout 1430ec6ffc1aSdantv filter {xWrite xTruncate xSync} 1431ec6ffc1aSdando_catchsql_test pager1-11.2 { COMMIT } {1 {disk I/O error}} 1432ec6ffc1aSdan 1433ec6ffc1aSdantv script {} 1434ec6ffc1aSdando_test pager1-11.3 { 1435ec6ffc1aSdan sqlite3 db2 test.db 1436ec6ffc1aSdan execsql { 1437ec6ffc1aSdan PRAGMA journal_mode = TRUNCATE; 1438ec6ffc1aSdan PRAGMA integrity_check; 1439ec6ffc1aSdan } db2 1440ec6ffc1aSdan} {truncate ok} 1441ec6ffc1aSdando_test pager1-11.4 { 1442ec6ffc1aSdan db2 close 1443ec6ffc1aSdan file size test.db-journal 1444ec6ffc1aSdan} {0} 1445ec6ffc1aSdando_execsql_test pager1-11.5 { SELECT count(*) FROM zz } {32} 1446f9b4419dSdandb close 1447f9b4419dSdantv delete 1448ec6ffc1aSdan 1449f9b4419dSdan#------------------------------------------------------------------------- 1450f9b4419dSdan# Test "PRAGMA page_size" 1451f9b4419dSdan# 1452f43d7fceSdantestvfs tv -default 1 1453f43d7fceSdantv sectorsize 1024 1454f9b4419dSdanforeach pagesize { 1455f9b4419dSdan 512 1024 2048 4096 8192 16384 32768 1456f9b4419dSdan} { 1457f9b4419dSdan faultsim_delete_and_reopen 1458f9b4419dSdan 1459f43d7fceSdan # The sector-size (according to the VFS) is 1024 bytes. So if the 1460f43d7fceSdan # page-size requested using "PRAGMA page_size" is greater than the 1461f43d7fceSdan # compile time value of SQLITE_MAX_PAGE_SIZE, then the effective 1462f43d7fceSdan # page-size remains 1024 bytes. 1463f43d7fceSdan # 1464f43d7fceSdan set eff $pagesize 1465f43d7fceSdan if {$eff > $::SQLITE_MAX_PAGE_SIZE} { set eff 1024 } 1466f43d7fceSdan 1467f9b4419dSdan do_test pager1-12.$pagesize.1 { 1468f9b4419dSdan sqlite3 db2 test.db 1469f9b4419dSdan execsql " 1470f9b4419dSdan PRAGMA page_size = $pagesize; 1471f9b4419dSdan CREATE VIEW v AS SELECT * FROM sqlite_master; 1472f9b4419dSdan " db2 1473f9b4419dSdan file size test.db 1474f43d7fceSdan } $eff 1475f9b4419dSdan do_test pager1-12.$pagesize.2 { 1476f9b4419dSdan sqlite3 db2 test.db 1477f9b4419dSdan execsql { 1478f9b4419dSdan SELECT count(*) FROM v; 1479f9b4419dSdan PRAGMA main.page_size; 1480f9b4419dSdan } db2 1481f43d7fceSdan } [list 1 $eff] 1482f9b4419dSdan do_test pager1-12.$pagesize.3 { 1483f9b4419dSdan execsql { 1484f9b4419dSdan SELECT count(*) FROM v; 1485f9b4419dSdan PRAGMA main.page_size; 1486f9b4419dSdan } 1487f43d7fceSdan } [list 1 $eff] 1488f9b4419dSdan db2 close 1489f9b4419dSdan} 1490f43d7fceSdandb close 1491f43d7fceSdantv delete 1492dca321aeSdan 1493d353331aSdan#------------------------------------------------------------------------- 1494d353331aSdan# Test specal "PRAGMA journal_mode=PERSIST" test cases. 1495d353331aSdan# 1496d353331aSdan# pager1-13.1.*: This tests a special case encountered in persistent 1497d353331aSdan# journal mode: If the journal associated with a transaction 1498d353331aSdan# is smaller than the journal file (because a previous 1499d353331aSdan# transaction left a very large non-hot journal file in the 1500d353331aSdan# file-system), then SQLite has to be careful that there is 1501d353331aSdan# not a journal-header left over from a previous transaction 1502d353331aSdan# immediately following the journal content just written. 1503d353331aSdan# If there is, and the process crashes so that the journal 1504d353331aSdan# becomes a hot-journal and must be rolled back by another 1505d353331aSdan# process, there is a danger that the other process may roll 1506d353331aSdan# back the aborted transaction, then continue copying data 1507d353331aSdan# from an older transaction from the remainder of the journal. 1508d353331aSdan# See the syncJournal() function for details. 1509d353331aSdan# 1510d353331aSdan# pager1-13.2.*: Same test as the previous. This time, throw an index into 1511d353331aSdan# the mix to make the integrity-check more likely to catch 1512d353331aSdan# errors. 1513d353331aSdan# 1514d353331aSdantestvfs tv -default 1 1515d353331aSdantv script xSyncCb 1516d353331aSdantv filter xSync 1517d353331aSdanproc xSyncCb {method filename args} { 1518d353331aSdan set t [file tail $filename] 1519d353331aSdan if {$t == "test.db"} faultsim_save 1520d353331aSdan return SQLITE_OK 1521d353331aSdan} 1522d353331aSdanfaultsim_delete_and_reopen 1523d353331aSdandb func a_string a_string 1524e91a54e1Sdan 1525d353331aSdan# The UPDATE statement at the end of this test case creates a really big 1526d353331aSdan# journal. Since the cache-size is only 10 pages, the journal contains 1527d353331aSdan# frequent journal headers. 1528d353331aSdan# 1529d353331aSdando_execsql_test pager1-13.1.1 { 1530d353331aSdan PRAGMA page_size = 1024; 1531d353331aSdan PRAGMA journal_mode = PERSIST; 1532d353331aSdan PRAGMA cache_size = 10; 1533d353331aSdan BEGIN; 1534d353331aSdan CREATE TABLE t1(a INTEGER PRIMARY KEY, b BLOB); 1535d353331aSdan INSERT INTO t1 VALUES(NULL, a_string(400)); 1536d353331aSdan INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 2 */ 1537d353331aSdan INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 4 */ 1538d353331aSdan INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 8 */ 1539d353331aSdan INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 16 */ 1540d353331aSdan INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 32 */ 1541d353331aSdan INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 64 */ 1542d353331aSdan INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 128 */ 1543d353331aSdan COMMIT; 1544d353331aSdan UPDATE t1 SET b = a_string(400); 1545d353331aSdan} {persist} 1546d353331aSdan 1547d353331aSdan# Run transactions of increasing sizes. Eventually, one (or more than one) 1548d353331aSdan# of these will write just enough content that one of the old headers created 1549d353331aSdan# by the transaction in the block above lies immediately after the content 1550d353331aSdan# journalled by the current transaction. 1551d353331aSdan# 1552d353331aSdanfor {set nUp 1} {$nUp<64} {incr nUp} { 1553d353331aSdan do_execsql_test pager1-13.1.2.$nUp.1 { 1554d353331aSdan UPDATE t1 SET b = a_string(399) WHERE a <= $nUp 1555d353331aSdan } {} 1556d353331aSdan do_execsql_test pager1-13.1.2.$nUp.2 { PRAGMA integrity_check } {ok} 1557d353331aSdan 1558d353331aSdan # Try to access the snapshot of the file-system. 1559d353331aSdan # 1560d353331aSdan sqlite3 db2 sv_test.db 1561d353331aSdan do_test pager1-13.1.2.$nUp.3 { 1562d353331aSdan execsql { SELECT sum(length(b)) FROM t1 } db2 1563d353331aSdan } [expr {128*400 - ($nUp-1)}] 1564d353331aSdan do_test pager1-13.1.2.$nUp.4 { 1565d353331aSdan execsql { PRAGMA integrity_check } db2 1566d353331aSdan } {ok} 1567d353331aSdan db2 close 1568d353331aSdan} 1569d353331aSdan 1570d353331aSdan# Same test as above. But this time with an index on the table. 1571d353331aSdan# 1572d353331aSdando_execsql_test pager1-13.2.1 { 1573d353331aSdan CREATE INDEX i1 ON t1(b); 1574d353331aSdan UPDATE t1 SET b = a_string(400); 1575d353331aSdan} {} 1576d353331aSdanfor {set nUp 1} {$nUp<64} {incr nUp} { 1577d353331aSdan do_execsql_test pager1-13.2.2.$nUp.1 { 1578d353331aSdan UPDATE t1 SET b = a_string(399) WHERE a <= $nUp 1579d353331aSdan } {} 1580d353331aSdan do_execsql_test pager1-13.2.2.$nUp.2 { PRAGMA integrity_check } {ok} 1581d353331aSdan sqlite3 db2 sv_test.db 1582d353331aSdan do_test pager1-13.2.2.$nUp.3 { 1583d353331aSdan execsql { SELECT sum(length(b)) FROM t1 } db2 1584d353331aSdan } [expr {128*400 - ($nUp-1)}] 1585d353331aSdan do_test pager1-13.2.2.$nUp.4 { 1586d353331aSdan execsql { PRAGMA integrity_check } db2 1587d353331aSdan } {ok} 1588d353331aSdan db2 close 1589d353331aSdan} 1590d353331aSdan 1591d353331aSdandb close 1592d353331aSdantv delete 1593d353331aSdan 1594d353331aSdan#------------------------------------------------------------------------- 1595d353331aSdan# Test specal "PRAGMA journal_mode=OFF" test cases. 1596d353331aSdan# 1597d353331aSdanfaultsim_delete_and_reopen 1598d353331aSdando_execsql_test pager1-14.1.1 { 1599d353331aSdan PRAGMA journal_mode = OFF; 1600d353331aSdan CREATE TABLE t1(a, b); 1601d353331aSdan BEGIN; 1602d353331aSdan INSERT INTO t1 VALUES(1, 2); 1603d353331aSdan COMMIT; 1604d353331aSdan SELECT * FROM t1; 1605d353331aSdan} {off 1 2} 1606d353331aSdando_catchsql_test pager1-14.1.2 { 1607d353331aSdan BEGIN; 1608d353331aSdan INSERT INTO t1 VALUES(3, 4); 1609d353331aSdan ROLLBACK; 1610d353331aSdan} {0 {}} 1611d353331aSdando_execsql_test pager1-14.1.3 { 1612d353331aSdan SELECT * FROM t1; 1613d353331aSdan} {1 2 3 4} 1614d353331aSdando_catchsql_test pager1-14.1.4 { 1615d353331aSdan BEGIN; 1616d353331aSdan INSERT INTO t1(rowid, a, b) SELECT a+3, b, b FROM t1; 1617d353331aSdan INSERT INTO t1(rowid, a, b) SELECT a+3, b, b FROM t1; 1618d353331aSdan} {1 {PRIMARY KEY must be unique}} 1619d353331aSdando_execsql_test pager1-14.1.5 { 1620d353331aSdan COMMIT; 1621d353331aSdan SELECT * FROM t1; 1622d353331aSdan} {1 2 3 4 2 2 4 4} 1623d353331aSdan 1624c8ce3972Sdan#------------------------------------------------------------------------- 1625c8ce3972Sdan# Test opening and closing the pager sub-system with different values 1626c8ce3972Sdan# for the sqlite3_vfs.szOsFile variable. 1627c8ce3972Sdan# 1628c8ce3972Sdanfaultsim_delete_and_reopen 1629c8ce3972Sdando_execsql_test pager1-15.0 { 1630c8ce3972Sdan CREATE TABLE tx(y, z); 1631c8ce3972Sdan INSERT INTO tx VALUES('Ayutthaya', 'Beijing'); 1632c8ce3972Sdan INSERT INTO tx VALUES('London', 'Tokyo'); 1633c8ce3972Sdan} {} 1634c8ce3972Sdandb close 1635c8ce3972Sdanfor {set i 0} {$i<513} {incr i 3} { 1636c8ce3972Sdan testvfs tv -default 1 -szosfile $i 1637c8ce3972Sdan sqlite3 db test.db 1638c8ce3972Sdan do_execsql_test pager1-15.$i.1 { 1639c8ce3972Sdan SELECT * FROM tx; 1640c8ce3972Sdan } {Ayutthaya Beijing London Tokyo} 1641c8ce3972Sdan db close 1642c8ce3972Sdan tv delete 1643c8ce3972Sdan} 1644c8ce3972Sdan 1645c8ce3972Sdan#------------------------------------------------------------------------- 1646c8ce3972Sdan# Check that it is not possible to open a database file if the full path 1647c8ce3972Sdan# to the associated journal file will be longer than sqlite3_vfs.mxPathname. 1648c8ce3972Sdan# 1649c8ce3972Sdantestvfs tv -default 1 1650c8ce3972Sdantv script xOpenCb 1651c8ce3972Sdantv filter xOpen 1652c8ce3972Sdanproc xOpenCb {method filename} { 1653c8ce3972Sdan set ::file_len [string length $filename] 1654c8ce3972Sdan} 1655c8ce3972Sdansqlite3 db test.db 1656c8ce3972Sdandb close 1657c8ce3972Sdantv delete 1658c8ce3972Sdan 1659c8ce3972Sdanfor {set ii [expr $::file_len-5]} {$ii < [expr $::file_len+20]} {incr ii} { 1660c8ce3972Sdan testvfs tv -default 1 -mxpathname $ii 1661c8ce3972Sdan 1662c8ce3972Sdan # The length of the full path to file "test.db-journal" is ($::file_len+8). 1663c8ce3972Sdan # If the configured sqlite3_vfs.mxPathname value greater than or equal to 1664c8ce3972Sdan # this, then the file can be opened. Otherwise, it cannot. 1665c8ce3972Sdan # 1666c8ce3972Sdan if {$ii >= [expr $::file_len+8]} { 1667c8ce3972Sdan set res {0 {}} 1668c8ce3972Sdan } else { 1669c8ce3972Sdan set res {1 {unable to open database file}} 1670c8ce3972Sdan } 1671c8ce3972Sdan 1672c8ce3972Sdan do_test pager1-16.1.$ii { 1673c8ce3972Sdan list [catch { sqlite3 db test.db } msg] $msg 1674c8ce3972Sdan } $res 1675c8ce3972Sdan 1676c8ce3972Sdan catch {db close} 1677c8ce3972Sdan tv delete 1678c8ce3972Sdan} 1679c8ce3972Sdan 1680c8ce3972Sdan#------------------------------------------------------------------------- 1681c8ce3972Sdan# Test "PRAGMA omit_readlock". 1682c8ce3972Sdan# 1683c8ce3972Sdan# pager1-17.$tn.1.*: Test that if a second connection has an open 1684c8ce3972Sdan# read-transaction, it is not usually possible to write 1685c8ce3972Sdan# the database. 1686c8ce3972Sdan# 1687c8ce3972Sdan# pager1-17.$tn.2.*: Test that if the second connection was opened with 1688c8ce3972Sdan# the SQLITE_OPEN_READONLY flag, and 1689c8ce3972Sdan# "PRAGMA omit_readlock = 1" is executed before attaching 1690c8ce3972Sdan# the database and opening a read-transaction on it, it is 1691c8ce3972Sdan# possible to write the db. 1692c8ce3972Sdan# 1693c8ce3972Sdan# pager1-17.$tn.3.*: Test that if the second connection was *not* opened with 1694c8ce3972Sdan# the SQLITE_OPEN_READONLY flag, executing 1695c8ce3972Sdan# "PRAGMA omit_readlock = 1" has no effect. 1696c8ce3972Sdan# 1697c8ce3972Sdando_multiclient_test tn { 1698c8ce3972Sdan do_test pager1-17.$tn.1.1 { 1699c8ce3972Sdan sql1 { 1700c8ce3972Sdan CREATE TABLE t1(a, b); 1701c8ce3972Sdan INSERT INTO t1 VALUES(1, 2); 1702c8ce3972Sdan } 1703c8ce3972Sdan sql2 { 1704c8ce3972Sdan BEGIN; 1705c8ce3972Sdan SELECT * FROM t1; 1706c8ce3972Sdan } 1707c8ce3972Sdan } {1 2} 1708c8ce3972Sdan do_test pager1-17.$tn.1.2 { 1709c8ce3972Sdan csql1 { INSERT INTO t1 VALUES(3, 4) } 1710c8ce3972Sdan } {1 {database is locked}} 1711c8ce3972Sdan do_test pager1-17.$tn.1.3 { 1712c8ce3972Sdan sql2 { COMMIT } 1713c8ce3972Sdan sql1 { INSERT INTO t1 VALUES(3, 4) } 1714c8ce3972Sdan } {} 1715c8ce3972Sdan 1716c8ce3972Sdan do_test pager1-17.$tn.2.1 { 1717c8ce3972Sdan code2 { 1718c8ce3972Sdan db2 close 1719c8ce3972Sdan sqlite3 db2 :memory: -readonly 1 1720c8ce3972Sdan } 1721c8ce3972Sdan sql2 { 1722c8ce3972Sdan PRAGMA omit_readlock = 1; 1723c8ce3972Sdan ATTACH 'test.db' AS two; 1724c8ce3972Sdan BEGIN; 1725c8ce3972Sdan SELECT * FROM t1; 1726c8ce3972Sdan } 1727c8ce3972Sdan } {1 2 3 4} 1728c8ce3972Sdan do_test pager1-17.$tn.2.2 { sql1 "INSERT INTO t1 VALUES(5, 6)" } {} 1729c8ce3972Sdan do_test pager1-17.$tn.2.3 { sql2 "SELECT * FROM t1" } {1 2 3 4} 1730c8ce3972Sdan do_test pager1-17.$tn.2.4 { sql2 "COMMIT ; SELECT * FROM t1" } {1 2 3 4 5 6} 1731c8ce3972Sdan 1732c8ce3972Sdan do_test pager1-17.$tn.3.1 { 1733c8ce3972Sdan code2 { 1734c8ce3972Sdan db2 close 1735c8ce3972Sdan sqlite3 db2 :memory: 1736c8ce3972Sdan } 1737c8ce3972Sdan sql2 { 1738c8ce3972Sdan PRAGMA omit_readlock = 1; 1739c8ce3972Sdan ATTACH 'test.db' AS two; 1740c8ce3972Sdan BEGIN; 1741c8ce3972Sdan SELECT * FROM t1; 1742c8ce3972Sdan } 1743c8ce3972Sdan } {1 2 3 4 5 6} 1744c8ce3972Sdan do_test pager1-17.$tn.3.2 { 1745c8ce3972Sdan csql1 { INSERT INTO t1 VALUES(3, 4) } 1746c8ce3972Sdan } {1 {database is locked}} 1747c8ce3972Sdan do_test pager1-17.$tn.3.3 { sql2 COMMIT } {} 1748c8ce3972Sdan} 1749c8ce3972Sdan 1750c8ce3972Sdan#------------------------------------------------------------------------- 1751c8ce3972Sdan# Test the pagers response to the b-tree layer requesting illegal page 1752c8ce3972Sdan# numbers: 1753c8ce3972Sdan# 1754c8ce3972Sdan# + The locking page, 1755c8ce3972Sdan# + Page 0, 1756c8ce3972Sdan# + A page with a page number greater than (2^31-1). 1757c8ce3972Sdan# 1758c8ce3972Sdando_test pager1-18.1 { 1759c8ce3972Sdan faultsim_delete_and_reopen 1760c8ce3972Sdan db func a_string a_string 1761c8ce3972Sdan execsql { 1762c8ce3972Sdan PRAGMA page_size = 1024; 1763c8ce3972Sdan CREATE TABLE t1(a, b); 1764c8ce3972Sdan INSERT INTO t1 VALUES(a_string(500), a_string(200)); 1765c8ce3972Sdan INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1; 1766c8ce3972Sdan INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1; 1767c8ce3972Sdan INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1; 1768c8ce3972Sdan INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1; 1769c8ce3972Sdan INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1; 1770c8ce3972Sdan INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1; 1771c8ce3972Sdan INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1; 1772c8ce3972Sdan } 1773c8ce3972Sdan} {} 1774c8ce3972Sdando_test pager1-18.2 { 1775c8ce3972Sdan set root [db one "SELECT rootpage FROM sqlite_master"] 1776c8ce3972Sdan set lockingpage [expr (0x10000/1024) + 1] 1777c8ce3972Sdan execsql { 1778c8ce3972Sdan PRAGMA writable_schema = 1; 1779c8ce3972Sdan UPDATE sqlite_master SET rootpage = $lockingpage; 1780c8ce3972Sdan } 1781c8ce3972Sdan sqlite3 db2 test.db 1782c8ce3972Sdan catchsql { SELECT count(*) FROM t1 } db2 1783c8ce3972Sdan} {1 {database disk image is malformed}} 1784c8ce3972Sdandb2 close 1785c8ce3972Sdando_test pager1-18.3 { 1786c8ce3972Sdan execsql { 1787c8ce3972Sdan CREATE TABLE t2(x); 1788c8ce3972Sdan INSERT INTO t2 VALUES(a_string(5000)); 1789c8ce3972Sdan } 1790c8ce3972Sdan set pgno [expr ([file size test.db] / 1024)-2] 1791c8ce3972Sdan hexio_write test.db [expr ($pgno-1)*1024] 00000000 1792c8ce3972Sdan sqlite3 db2 test.db 1793c8ce3972Sdan catchsql { SELECT length(x) FROM t2 } db2 1794c8ce3972Sdan} {1 {database disk image is malformed}} 1795c8ce3972Sdandb2 close 1796c8ce3972Sdando_test pager1-18.4 { 1797c8ce3972Sdan hexio_write test.db [expr ($pgno-1)*1024] 90000000 1798c8ce3972Sdan sqlite3 db2 test.db 1799c8ce3972Sdan catchsql { SELECT length(x) FROM t2 } db2 1800c8ce3972Sdan} {1 {database disk image is malformed}} 1801c8ce3972Sdandb2 close 1802c8ce3972Sdando_test pager1-18.5 { 1803c8ce3972Sdan sqlite3 db "" 1804c8ce3972Sdan execsql { 1805c8ce3972Sdan CREATE TABLE t1(a, b); 1806c8ce3972Sdan CREATE TABLE t2(a, b); 1807c8ce3972Sdan PRAGMA writable_schema = 1; 1808c8ce3972Sdan UPDATE sqlite_master SET rootpage=5 WHERE tbl_name = 't1'; 1809c8ce3972Sdan PRAGMA writable_schema = 0; 1810c8ce3972Sdan ALTER TABLE t1 RENAME TO x1; 1811c8ce3972Sdan } 1812c8ce3972Sdan catchsql { SELECT * FROM x1 } 1813ba3cbf3dSdan} {1 {database disk image is malformed}} 1814c8ce3972Sdandb close 1815c8ce3972Sdan 1816ba3cbf3dSdando_test pager1-18.6 { 1817ba3cbf3dSdan faultsim_delete_and_reopen 1818ba3cbf3dSdan db func a_string a_string 1819ba3cbf3dSdan execsql { 1820ba3cbf3dSdan PRAGMA page_size = 1024; 1821ba3cbf3dSdan CREATE TABLE t1(x); 1822ba3cbf3dSdan INSERT INTO t1 VALUES(a_string(800)); 1823ba3cbf3dSdan INSERT INTO t1 VALUES(a_string(800)); 1824ba3cbf3dSdan } 1825ba3cbf3dSdan 1826ba3cbf3dSdan set root [db one "SELECT rootpage FROM sqlite_master"] 1827ba3cbf3dSdan db close 1828ba3cbf3dSdan 1829ba3cbf3dSdan hexio_write test.db [expr ($root-1)*1024 + 8] 00000000 1830ba3cbf3dSdan sqlite3 db test.db 1831ba3cbf3dSdan catchsql { SELECT length(x) FROM t1 } 1832ba3cbf3dSdan} {1 {database disk image is malformed}} 1833ba3cbf3dSdan 1834ba3cbf3dSdando_test pager1-19.1 { 1835ba3cbf3dSdan sqlite3 db "" 1836ba3cbf3dSdan db func a_string a_string 1837ba3cbf3dSdan execsql { 1838ba3cbf3dSdan PRAGMA page_size = 512; 1839ba3cbf3dSdan PRAGMA auto_vacuum = 1; 1840ba3cbf3dSdan CREATE TABLE t1(aa, ab, ac, ad, ae, af, ag, ah, ai, aj, ak, al, am, an, 1841ba3cbf3dSdan ba, bb, bc, bd, be, bf, bg, bh, bi, bj, bk, bl, bm, bn, 1842ba3cbf3dSdan ca, cb, cc, cd, ce, cf, cg, ch, ci, cj, ck, cl, cm, cn, 1843ba3cbf3dSdan da, db, dc, dd, de, df, dg, dh, di, dj, dk, dl, dm, dn, 1844ba3cbf3dSdan ea, eb, ec, ed, ee, ef, eg, eh, ei, ej, ek, el, em, en, 1845ba3cbf3dSdan fa, fb, fc, fd, fe, ff, fg, fh, fi, fj, fk, fl, fm, fn, 1846ba3cbf3dSdan ga, gb, gc, gd, ge, gf, gg, gh, gi, gj, gk, gl, gm, gn, 1847ba3cbf3dSdan ha, hb, hc, hd, he, hf, hg, hh, hi, hj, hk, hl, hm, hn, 1848ba3cbf3dSdan ia, ib, ic, id, ie, if, ig, ih, ii, ij, ik, il, im, ix, 1849ba3cbf3dSdan ja, jb, jc, jd, je, jf, jg, jh, ji, jj, jk, jl, jm, jn, 1850ba3cbf3dSdan ka, kb, kc, kd, ke, kf, kg, kh, ki, kj, kk, kl, km, kn, 1851ba3cbf3dSdan la, lb, lc, ld, le, lf, lg, lh, li, lj, lk, ll, lm, ln, 1852ba3cbf3dSdan ma, mb, mc, md, me, mf, mg, mh, mi, mj, mk, ml, mm, mn 1853ba3cbf3dSdan ); 1854ba3cbf3dSdan CREATE TABLE t2(aa, ab, ac, ad, ae, af, ag, ah, ai, aj, ak, al, am, an, 1855ba3cbf3dSdan ba, bb, bc, bd, be, bf, bg, bh, bi, bj, bk, bl, bm, bn, 1856ba3cbf3dSdan ca, cb, cc, cd, ce, cf, cg, ch, ci, cj, ck, cl, cm, cn, 1857ba3cbf3dSdan da, db, dc, dd, de, df, dg, dh, di, dj, dk, dl, dm, dn, 1858ba3cbf3dSdan ea, eb, ec, ed, ee, ef, eg, eh, ei, ej, ek, el, em, en, 1859ba3cbf3dSdan fa, fb, fc, fd, fe, ff, fg, fh, fi, fj, fk, fl, fm, fn, 1860ba3cbf3dSdan ga, gb, gc, gd, ge, gf, gg, gh, gi, gj, gk, gl, gm, gn, 1861ba3cbf3dSdan ha, hb, hc, hd, he, hf, hg, hh, hi, hj, hk, hl, hm, hn, 1862ba3cbf3dSdan ia, ib, ic, id, ie, if, ig, ih, ii, ij, ik, il, im, ix, 1863ba3cbf3dSdan ja, jb, jc, jd, je, jf, jg, jh, ji, jj, jk, jl, jm, jn, 1864ba3cbf3dSdan ka, kb, kc, kd, ke, kf, kg, kh, ki, kj, kk, kl, km, kn, 1865ba3cbf3dSdan la, lb, lc, ld, le, lf, lg, lh, li, lj, lk, ll, lm, ln, 1866ba3cbf3dSdan ma, mb, mc, md, me, mf, mg, mh, mi, mj, mk, ml, mm, mn 1867ba3cbf3dSdan ); 1868ba3cbf3dSdan INSERT INTO t1(aa) VALUES( a_string(100000) ); 1869ba3cbf3dSdan INSERT INTO t2(aa) VALUES( a_string(100000) ); 1870ba3cbf3dSdan VACUUM; 1871ba3cbf3dSdan } 1872ba3cbf3dSdan} {} 1873ba3cbf3dSdan 18746b63ab47Sdan#------------------------------------------------------------------------- 18756b63ab47Sdan# Test a couple of special cases that come up while committing 18766b63ab47Sdan# transactions: 18776b63ab47Sdan# 18786b63ab47Sdan# pager1-20.1.*: Committing an in-memory database transaction when the 18796b63ab47Sdan# database has not been modified at all. 18806b63ab47Sdan# 18816b63ab47Sdan# pager1-20.2.*: As above, but with a normal db in exclusive-locking mode. 18826b63ab47Sdan# 18836b63ab47Sdan# pager1-20.3.*: Committing a transaction in WAL mode where the database has 18846b63ab47Sdan# been modified, but all dirty pages have been flushed to 18856b63ab47Sdan# disk before the commit. 18866b63ab47Sdan# 18876b63ab47Sdando_test pager1-20.1.1 { 18886b63ab47Sdan catch {db close} 18896b63ab47Sdan sqlite3 db :memory: 18906b63ab47Sdan execsql { 18916b63ab47Sdan CREATE TABLE one(two, three); 18926b63ab47Sdan INSERT INTO one VALUES('a', 'b'); 18936b63ab47Sdan } 18946b63ab47Sdan} {} 18956b63ab47Sdando_test pager1-20.1.2 { 18966b63ab47Sdan execsql { 18976b63ab47Sdan BEGIN EXCLUSIVE; 18986b63ab47Sdan COMMIT; 18996b63ab47Sdan } 19006b63ab47Sdan} {} 19016b63ab47Sdan 19026b63ab47Sdando_test pager1-20.2.1 { 19036b63ab47Sdan faultsim_delete_and_reopen 19046b63ab47Sdan execsql { 19056b63ab47Sdan PRAGMA locking_mode = exclusive; 19066b63ab47Sdan PRAGMA journal_mode = persist; 19076b63ab47Sdan CREATE TABLE one(two, three); 19086b63ab47Sdan INSERT INTO one VALUES('a', 'b'); 19096b63ab47Sdan } 19106b63ab47Sdan} {exclusive persist} 19116b63ab47Sdando_test pager1-20.2.2 { 19126b63ab47Sdan execsql { 19136b63ab47Sdan BEGIN EXCLUSIVE; 19146b63ab47Sdan COMMIT; 19156b63ab47Sdan } 19166b63ab47Sdan} {} 19176b63ab47Sdan 19186b63ab47Sdando_test pager1-20.3.1 { 19196b63ab47Sdan faultsim_delete_and_reopen 19206b63ab47Sdan db func a_string a_string 19216b63ab47Sdan execsql { 19226b63ab47Sdan PRAGMA cache_size = 10; 19236b63ab47Sdan PRAGMA journal_mode = wal; 19246b63ab47Sdan BEGIN; 19256b63ab47Sdan CREATE TABLE t1(x); 19266b63ab47Sdan CREATE TABLE t2(y); 19276b63ab47Sdan INSERT INTO t1 VALUES(a_string(800)); 19286b63ab47Sdan INSERT INTO t1 SELECT a_string(800) FROM t1; /* 2 */ 19296b63ab47Sdan INSERT INTO t1 SELECT a_string(800) FROM t1; /* 4 */ 19306b63ab47Sdan INSERT INTO t1 SELECT a_string(800) FROM t1; /* 8 */ 19316b63ab47Sdan INSERT INTO t1 SELECT a_string(800) FROM t1; /* 16 */ 19326b63ab47Sdan INSERT INTO t1 SELECT a_string(800) FROM t1; /* 32 */ 19336b63ab47Sdan COMMIT; 19346b63ab47Sdan } 19356b63ab47Sdan} {wal} 19366b63ab47Sdando_test pager1-20.3.2 { 19376b63ab47Sdan execsql { 19386b63ab47Sdan BEGIN; 19396b63ab47Sdan INSERT INTO t2 VALUES('xxxx'); 19406b63ab47Sdan } 1941c396d4afSdan recursive_select 32 t1 19426b63ab47Sdan execsql COMMIT 19436b63ab47Sdan} {} 19446b63ab47Sdan 194589ccf448Sdan#------------------------------------------------------------------------- 194689ccf448Sdan# Test that a WAL database may not be opened if: 194789ccf448Sdan# 194889ccf448Sdan# pager1-21.1.*: The VFS has an iVersion less than 2, or 194989ccf448Sdan# pager1-21.2.*: The VFS does not provide xShmXXX() methods. 195089ccf448Sdan# 195189ccf448Sdando_test pager1-21.0 { 195289ccf448Sdan faultsim_delete_and_reopen 195389ccf448Sdan execsql { 195489ccf448Sdan PRAGMA journal_mode = WAL; 195589ccf448Sdan CREATE TABLE ko(c DEFAULT 'abc', b DEFAULT 'def'); 195689ccf448Sdan INSERT INTO ko DEFAULT VALUES; 195789ccf448Sdan } 195889ccf448Sdan} {wal} 195989ccf448Sdando_test pager1-21.1 { 196089ccf448Sdan testvfs tv -noshm 1 196189ccf448Sdan sqlite3 db2 test.db -vfs tv 196289ccf448Sdan catchsql { SELECT * FROM ko } db2 196389ccf448Sdan} {1 {unable to open database file}} 196489ccf448Sdandb2 close 196589ccf448Sdantv delete 196689ccf448Sdando_test pager1-21.2 { 196789ccf448Sdan testvfs tv -iversion 1 196889ccf448Sdan sqlite3 db2 test.db -vfs tv 196989ccf448Sdan catchsql { SELECT * FROM ko } db2 197089ccf448Sdan} {1 {unable to open database file}} 197189ccf448Sdandb2 close 197289ccf448Sdantv delete 197389ccf448Sdan 197489ccf448Sdan#------------------------------------------------------------------------- 197589ccf448Sdan# Test that a "PRAGMA wal_checkpoint": 197689ccf448Sdan# 197789ccf448Sdan# pager1-22.1.*: is a no-op on a non-WAL db, and 197889ccf448Sdan# pager1-22.2.*: does not cause xSync calls with a synchronous=off db. 197989ccf448Sdan# 198089ccf448Sdando_test pager1-22.1.1 { 198189ccf448Sdan faultsim_delete_and_reopen 198289ccf448Sdan execsql { 198389ccf448Sdan CREATE TABLE ko(c DEFAULT 'abc', b DEFAULT 'def'); 198489ccf448Sdan INSERT INTO ko DEFAULT VALUES; 198589ccf448Sdan } 198689ccf448Sdan execsql { PRAGMA wal_checkpoint } 198789ccf448Sdan} {} 198889ccf448Sdando_test pager1-22.2.1 { 198989ccf448Sdan testvfs tv -default 1 199089ccf448Sdan tv filter xSync 199189ccf448Sdan tv script xSyncCb 199289ccf448Sdan proc xSyncCb {args} {incr ::synccount} 199389ccf448Sdan set ::synccount 0 199489ccf448Sdan sqlite3 db test.db 199589ccf448Sdan execsql { 199689ccf448Sdan PRAGMA synchronous = off; 199789ccf448Sdan PRAGMA journal_mode = WAL; 199889ccf448Sdan INSERT INTO ko DEFAULT VALUES; 199989ccf448Sdan } 200089ccf448Sdan execsql { PRAGMA wal_checkpoint } 200189ccf448Sdan set synccount 200289ccf448Sdan} {0} 200389ccf448Sdandb close 200489ccf448Sdantv delete 200589ccf448Sdan 200689ccf448Sdan#------------------------------------------------------------------------- 200789ccf448Sdan# Tests for changing journal mode. 200889ccf448Sdan# 200989ccf448Sdan# pager1-23.1.*: Test that when changing from PERSIST to DELETE mode, 201089ccf448Sdan# the journal file is deleted. 201189ccf448Sdan# 201289ccf448Sdan# pager1-23.2.*: Same test as above, but while a shared lock is held 201389ccf448Sdan# on the database file. 201489ccf448Sdan# 201589ccf448Sdan# pager1-23.3.*: Same test as above, but while a reserved lock is held 201689ccf448Sdan# on the database file. 201789ccf448Sdan# 201889ccf448Sdan# pager1-23.4.*: And, for fun, while holding an exclusive lock. 201989ccf448Sdan# 202089ccf448Sdan# pager1-23.5.*: Try to set various different journal modes with an 202189ccf448Sdan# in-memory database (only MEMORY and OFF should work). 202289ccf448Sdan# 2023d0b0d4dcSdan# pager1-23.6.*: Try to set locking_mode=normal on an in-memory database 2024d0b0d4dcSdan# (doesn't work - in-memory databases always use 2025d0b0d4dcSdan# locking_mode=exclusive). 2026d0b0d4dcSdan# 202789ccf448Sdando_test pager1-23.1.1 { 202889ccf448Sdan faultsim_delete_and_reopen 202989ccf448Sdan execsql { 203089ccf448Sdan PRAGMA journal_mode = PERSIST; 203189ccf448Sdan CREATE TABLE t1(a, b); 203289ccf448Sdan } 203389ccf448Sdan file exists test.db-journal 203489ccf448Sdan} {1} 203589ccf448Sdando_test pager1-23.1.2 { 203689ccf448Sdan execsql { PRAGMA journal_mode = DELETE } 203789ccf448Sdan file exists test.db-journal 203889ccf448Sdan} {0} 203989ccf448Sdan 204089ccf448Sdando_test pager1-23.2.1 { 204189ccf448Sdan execsql { 204289ccf448Sdan PRAGMA journal_mode = PERSIST; 204389ccf448Sdan INSERT INTO t1 VALUES('Canberra', 'ACT'); 204489ccf448Sdan } 204589ccf448Sdan db eval { SELECT * FROM t1 } { 204689ccf448Sdan db eval { PRAGMA journal_mode = DELETE } 204789ccf448Sdan } 204889ccf448Sdan execsql { PRAGMA journal_mode } 204989ccf448Sdan} {delete} 205089ccf448Sdando_test pager1-23.2.2 { 205189ccf448Sdan file exists test.db-journal 205289ccf448Sdan} {0} 205389ccf448Sdan 205489ccf448Sdando_test pager1-23.3.1 { 205589ccf448Sdan execsql { 205689ccf448Sdan PRAGMA journal_mode = PERSIST; 205789ccf448Sdan INSERT INTO t1 VALUES('Darwin', 'NT'); 205889ccf448Sdan BEGIN IMMEDIATE; 205989ccf448Sdan } 206089ccf448Sdan db eval { PRAGMA journal_mode = DELETE } 206189ccf448Sdan execsql { PRAGMA journal_mode } 206289ccf448Sdan} {delete} 206389ccf448Sdando_test pager1-23.3.2 { 206489ccf448Sdan file exists test.db-journal 206589ccf448Sdan} {0} 206689ccf448Sdando_test pager1-23.3.3 { 206789ccf448Sdan execsql COMMIT 206889ccf448Sdan} {} 206989ccf448Sdan 207089ccf448Sdando_test pager1-23.4.1 { 207189ccf448Sdan execsql { 207289ccf448Sdan PRAGMA journal_mode = PERSIST; 207389ccf448Sdan INSERT INTO t1 VALUES('Adelaide', 'SA'); 207489ccf448Sdan BEGIN EXCLUSIVE; 207589ccf448Sdan } 207689ccf448Sdan db eval { PRAGMA journal_mode = DELETE } 207789ccf448Sdan execsql { PRAGMA journal_mode } 207889ccf448Sdan} {delete} 207989ccf448Sdando_test pager1-23.4.2 { 208089ccf448Sdan file exists test.db-journal 208189ccf448Sdan} {0} 208289ccf448Sdando_test pager1-23.4.3 { 208389ccf448Sdan execsql COMMIT 208489ccf448Sdan} {} 208589ccf448Sdan 208689ccf448Sdando_test pager1-23.5.1 { 208789ccf448Sdan faultsim_delete_and_reopen 208889ccf448Sdan sqlite3 db :memory: 208989ccf448Sdan} {} 209089ccf448Sdanforeach {tn mode possible} { 209189ccf448Sdan 2 off 1 209289ccf448Sdan 3 memory 1 209389ccf448Sdan 4 persist 0 209489ccf448Sdan 5 delete 0 209589ccf448Sdan 6 wal 0 209689ccf448Sdan 7 truncate 0 209789ccf448Sdan} { 209889ccf448Sdan do_test pager1-23.5.$tn.1 { 209989ccf448Sdan execsql "PRAGMA journal_mode = off" 210089ccf448Sdan execsql "PRAGMA journal_mode = $mode" 210189ccf448Sdan } [if $possible {list $mode} {list off}] 210289ccf448Sdan do_test pager1-23.5.$tn.2 { 210389ccf448Sdan execsql "PRAGMA journal_mode = memory" 210489ccf448Sdan execsql "PRAGMA journal_mode = $mode" 210589ccf448Sdan } [if $possible {list $mode} {list memory}] 210689ccf448Sdan} 2107d0b0d4dcSdando_test pager1-23.6.1 { 210889ccf448Sdan execsql {PRAGMA locking_mode = normal} 210989ccf448Sdan} {exclusive} 2110d0b0d4dcSdando_test pager1-23.6.2 { 211189ccf448Sdan execsql {PRAGMA locking_mode = exclusive} 211289ccf448Sdan} {exclusive} 2113d0b0d4dcSdando_test pager1-23.6.3 { 211489ccf448Sdan execsql {PRAGMA locking_mode} 211589ccf448Sdan} {exclusive} 2116d0b0d4dcSdando_test pager1-23.6.4 { 211789ccf448Sdan execsql {PRAGMA main.locking_mode} 211889ccf448Sdan} {exclusive} 21196b63ab47Sdan 2120d0b0d4dcSdan#------------------------------------------------------------------------- 2121d0b0d4dcSdan# 2122d0b0d4dcSdando_test pager1-24.1.1 { 2123d0b0d4dcSdan faultsim_delete_and_reopen 2124d0b0d4dcSdan db func a_string a_string 2125d0b0d4dcSdan execsql { 2126d0b0d4dcSdan PRAGMA cache_size = 10; 2127d0b0d4dcSdan PRAGMA auto_vacuum = FULL; 2128d0b0d4dcSdan CREATE TABLE x1(x, y, z, PRIMARY KEY(y, z)); 2129d0b0d4dcSdan CREATE TABLE x2(x, y, z, PRIMARY KEY(y, z)); 2130d0b0d4dcSdan INSERT INTO x2 VALUES(a_string(400), a_string(500), a_string(600)); 2131d0b0d4dcSdan INSERT INTO x2 SELECT a_string(600), a_string(400), a_string(500) FROM x2; 2132d0b0d4dcSdan INSERT INTO x2 SELECT a_string(500), a_string(600), a_string(400) FROM x2; 2133d0b0d4dcSdan INSERT INTO x2 SELECT a_string(400), a_string(500), a_string(600) FROM x2; 2134d0b0d4dcSdan INSERT INTO x2 SELECT a_string(600), a_string(400), a_string(500) FROM x2; 2135d0b0d4dcSdan INSERT INTO x2 SELECT a_string(500), a_string(600), a_string(400) FROM x2; 2136d0b0d4dcSdan INSERT INTO x2 SELECT a_string(400), a_string(500), a_string(600) FROM x2; 2137d0b0d4dcSdan INSERT INTO x1 SELECT * FROM x2; 2138d0b0d4dcSdan } 2139d0b0d4dcSdan} {} 2140d0b0d4dcSdando_test pager1-24.1.2 { 2141d0b0d4dcSdan execsql { 2142d0b0d4dcSdan BEGIN; 2143d0b0d4dcSdan DELETE FROM x1 WHERE rowid<32; 2144d0b0d4dcSdan } 2145c396d4afSdan recursive_select 64 x2 2146d0b0d4dcSdan} {} 2147d0b0d4dcSdando_test pager1-24.1.3 { 2148d0b0d4dcSdan execsql { 2149d0b0d4dcSdan UPDATE x1 SET z = a_string(300) WHERE rowid>40; 2150d0b0d4dcSdan COMMIT; 2151d0b0d4dcSdan PRAGMA integrity_check; 2152d0b0d4dcSdan SELECT count(*) FROM x1; 2153d0b0d4dcSdan } 2154d0b0d4dcSdan} {ok 33} 2155d0b0d4dcSdan 2156d0b0d4dcSdando_test pager1-24.1.4 { 2157d0b0d4dcSdan execsql { 2158d0b0d4dcSdan DELETE FROM x1; 2159d0b0d4dcSdan INSERT INTO x1 SELECT * FROM x2; 2160d0b0d4dcSdan BEGIN; 2161d0b0d4dcSdan DELETE FROM x1 WHERE rowid<32; 2162d0b0d4dcSdan UPDATE x1 SET z = a_string(299) WHERE rowid>40; 2163d0b0d4dcSdan } 2164c396d4afSdan recursive_select 64 x2 {db eval COMMIT} 2165d0b0d4dcSdan execsql { 2166d0b0d4dcSdan PRAGMA integrity_check; 2167d0b0d4dcSdan SELECT count(*) FROM x1; 2168d0b0d4dcSdan } 2169d0b0d4dcSdan} {ok 33} 2170d0b0d4dcSdan 2171d0b0d4dcSdando_test pager1-24.1.5 { 2172d0b0d4dcSdan execsql { 2173d0b0d4dcSdan DELETE FROM x1; 2174d0b0d4dcSdan INSERT INTO x1 SELECT * FROM x2; 2175d0b0d4dcSdan } 2176c396d4afSdan recursive_select 64 x2 { db eval {CREATE TABLE x3(x, y, z)} } 2177d0b0d4dcSdan execsql { SELECT * FROM x3 } 2178d0b0d4dcSdan} {} 2179d0b0d4dcSdan 2180d0b0d4dcSdan#------------------------------------------------------------------------- 2181d0b0d4dcSdan# 2182d0b0d4dcSdando_test pager1-25-1 { 2183d0b0d4dcSdan faultsim_delete_and_reopen 2184d0b0d4dcSdan execsql { 2185d0b0d4dcSdan BEGIN; 2186d0b0d4dcSdan SAVEPOINT abc; 2187d0b0d4dcSdan CREATE TABLE t1(a, b); 2188d0b0d4dcSdan ROLLBACK TO abc; 2189d0b0d4dcSdan COMMIT; 2190d0b0d4dcSdan } 2191d0b0d4dcSdan db close 2192d0b0d4dcSdan} {} 2193d0b0d4dcSdanbreakpoint 2194d0b0d4dcSdando_test pager1-25-2 { 2195d0b0d4dcSdan faultsim_delete_and_reopen 2196d0b0d4dcSdan execsql { 2197d0b0d4dcSdan SAVEPOINT abc; 2198d0b0d4dcSdan CREATE TABLE t1(a, b); 2199d0b0d4dcSdan ROLLBACK TO abc; 2200d0b0d4dcSdan COMMIT; 2201d0b0d4dcSdan } 2202d0b0d4dcSdan db close 2203d0b0d4dcSdan} {} 22046b63ab47Sdan 2205c396d4afSdan#------------------------------------------------------------------------- 2206c396d4afSdan# Sector-size tests. 2207c396d4afSdan# 2208c396d4afSdando_test pager1-26.1 { 2209c396d4afSdan testvfs tv -default 1 2210c396d4afSdan tv sectorsize 4096 2211c396d4afSdan faultsim_delete_and_reopen 2212c396d4afSdan db func a_string a_string 2213c396d4afSdan execsql { 2214c396d4afSdan PRAGMA page_size = 512; 2215c396d4afSdan CREATE TABLE tbl(a PRIMARY KEY, b UNIQUE); 2216c396d4afSdan BEGIN; 2217c396d4afSdan INSERT INTO tbl VALUES(a_string(25), a_string(600)); 2218c396d4afSdan INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl; 2219c396d4afSdan INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl; 2220c396d4afSdan INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl; 2221c396d4afSdan INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl; 2222c396d4afSdan INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl; 2223c396d4afSdan INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl; 2224c396d4afSdan INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl; 2225c396d4afSdan COMMIT; 2226c396d4afSdan } 2227c396d4afSdan} {} 2228c396d4afSdando_execsql_test pager1-26.1 { 2229c396d4afSdan UPDATE tbl SET b = a_string(550); 2230c396d4afSdan} {} 2231c396d4afSdandb close 2232c396d4afSdantv delete 2233c396d4afSdan 2234c396d4afSdan#------------------------------------------------------------------------- 2235c396d4afSdando_test pager1.27.1 { 2236c396d4afSdan faultsim_delete_and_reopen 2237c396d4afSdan sqlite3_pager_refcounts db 2238c396d4afSdan execsql { 2239c396d4afSdan BEGIN; 2240c396d4afSdan CREATE TABLE t1(a, b); 2241c396d4afSdan } 2242c396d4afSdan sqlite3_pager_refcounts db 2243c396d4afSdan execsql COMMIT 2244c396d4afSdan} {} 2245c396d4afSdan 2246d353331aSdanfinish_test 2247