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 18e91a54e1Sdan 19b0ac3e3aSdan# 20b0ac3e3aSdan# pager1-1.*: Test inter-process locking (clients in multiple processes). 21b0ac3e3aSdan# 22b0ac3e3aSdan# pager1-2.*: Test intra-process locking (multiple clients in this process). 23b0ac3e3aSdan# 24b0ac3e3aSdan# pager1-3.*: Savepoint related tests. 25b0ac3e3aSdan# 261f4cb65aSdan# pager1-4.*: Hot-journal related tests. 271f4cb65aSdan# 28de4996e2Sdan# pager1-5.*: Cases related to multi-file commits. 29de4996e2Sdan# 30146ed78bSdan# pager1-6.*: Cases related to "PRAGMA max_page_count" 31146ed78bSdan# 32153eda0aSdan# pager1-7.*: Cases specific to "PRAGMA journal_mode=TRUNCATE" 33153eda0aSdan# 34b0ac3e3aSdan 3553f04f3bSdanset a_string_counter 1 3653f04f3bSdanproc a_string {n} { 3753f04f3bSdan global a_string_counter 3853f04f3bSdan incr a_string_counter 3953f04f3bSdan string range [string repeat "${a_string_counter}." $n] 1 $n 4053f04f3bSdan} 4153f04f3bSdandb func a_string a_string 4253f04f3bSdan 43a4a9095eSdando_multiclient_test tn { 44e91a54e1Sdan 45e91a54e1Sdan # Create and populate a database table using connection [db]. Check 46e91a54e1Sdan # that connections [db2] and [db3] can see the schema and content. 47e91a54e1Sdan # 48e91a54e1Sdan do_test pager1-$tn.1 { 49e91a54e1Sdan sql1 { 50e91a54e1Sdan CREATE TABLE t1(a PRIMARY KEY, b); 51e91a54e1Sdan CREATE INDEX i1 ON t1(b); 52e91a54e1Sdan INSERT INTO t1 VALUES(1, 'one'); 53e91a54e1Sdan INSERT INTO t1 VALUES(2, 'two'); 54e91a54e1Sdan } 55e91a54e1Sdan } {} 56e91a54e1Sdan do_test pager1-$tn.2 { sql2 { SELECT * FROM t1 } } {1 one 2 two} 57e91a54e1Sdan do_test pager1-$tn.3 { sql3 { SELECT * FROM t1 } } {1 one 2 two} 58e91a54e1Sdan 59e91a54e1Sdan # Open a transaction and add a row using [db]. This puts [db] in 60e91a54e1Sdan # RESERVED state. Check that connections [db2] and [db3] can still 61e91a54e1Sdan # read the database content as it was before the transaction was 62e91a54e1Sdan # opened. [db] should see the inserted row. 63e91a54e1Sdan # 64e91a54e1Sdan do_test pager1-$tn.4 { 65e91a54e1Sdan sql1 { 66e91a54e1Sdan BEGIN; 67e91a54e1Sdan INSERT INTO t1 VALUES(3, 'three'); 68e91a54e1Sdan } 69e91a54e1Sdan } {} 70e91a54e1Sdan do_test pager1-$tn.5 { sql2 { SELECT * FROM t1 } } {1 one 2 two} 71e91a54e1Sdan do_test pager1-$tn.7 { sql1 { SELECT * FROM t1 } } {1 one 2 two 3 three} 72e91a54e1Sdan 73e91a54e1Sdan # [db] still has an open write transaction. Check that this prevents 74e91a54e1Sdan # other connections (specifically [db2]) from writing to the database. 75e91a54e1Sdan # 76e91a54e1Sdan # Even if [db2] opens a transaction first, it may not write to the 77e91a54e1Sdan # database. After the attempt to write the db within a transaction, 78e91a54e1Sdan # [db2] is left with an open transaction, but not a read-lock on 79e91a54e1Sdan # the main database. So it does not prevent [db] from committing. 80e91a54e1Sdan # 81e91a54e1Sdan do_test pager1-$tn.8 { 82e91a54e1Sdan csql2 { UPDATE t1 SET a = a + 10 } 83e91a54e1Sdan } {1 {database is locked}} 84e91a54e1Sdan do_test pager1-$tn.9 { 85e91a54e1Sdan csql2 { 86e91a54e1Sdan BEGIN; 87e91a54e1Sdan UPDATE t1 SET a = a + 10; 88e91a54e1Sdan } 89e91a54e1Sdan } {1 {database is locked}} 90e91a54e1Sdan 91e91a54e1Sdan # Have [db] commit its transactions. Check the other connections can 92e91a54e1Sdan # now see the new database content. 93e91a54e1Sdan # 94e91a54e1Sdan do_test pager1-$tn.10 { sql1 { COMMIT } } {} 95e91a54e1Sdan do_test pager1-$tn.11 { sql1 { SELECT * FROM t1 } } {1 one 2 two 3 three} 96e91a54e1Sdan do_test pager1-$tn.12 { sql2 { SELECT * FROM t1 } } {1 one 2 two 3 three} 97e91a54e1Sdan do_test pager1-$tn.13 { sql3 { SELECT * FROM t1 } } {1 one 2 two 3 three} 98e91a54e1Sdan 99e91a54e1Sdan # Check that, as noted above, [db2] really did keep an open transaction 100e91a54e1Sdan # after the attempt to write the database failed. 101e91a54e1Sdan # 102e91a54e1Sdan do_test pager1-$tn.14 { 103e91a54e1Sdan csql2 { BEGIN } 104e91a54e1Sdan } {1 {cannot start a transaction within a transaction}} 105e91a54e1Sdan do_test pager1-$tn.15 { sql2 { ROLLBACK } } {} 106e91a54e1Sdan 107e91a54e1Sdan # Have [db2] open a transaction and take a read-lock on the database. 108e91a54e1Sdan # Check that this prevents [db] from writing to the database (outside 109e91a54e1Sdan # of any transaction). After this fails, check that [db3] can read 110e91a54e1Sdan # the db (showing that [db] did not take a PENDING lock etc.) 111e91a54e1Sdan # 112e91a54e1Sdan do_test pager1-$tn.15 { 113e91a54e1Sdan sql2 { BEGIN; SELECT * FROM t1; } 114e91a54e1Sdan } {1 one 2 two 3 three} 115e91a54e1Sdan do_test pager1-$tn.16 { 116e91a54e1Sdan csql1 { UPDATE t1 SET a = a + 10 } 117e91a54e1Sdan } {1 {database is locked}} 118e91a54e1Sdan do_test pager1-$tn.17 { sql3 { SELECT * FROM t1 } } {1 one 2 two 3 three} 119e91a54e1Sdan 120e91a54e1Sdan # This time, have [db] open a transaction before writing the database. 121e91a54e1Sdan # This works - [db] gets a RESERVED lock which does not conflict with 122e91a54e1Sdan # the SHARED lock [db2] is holding. 123e91a54e1Sdan # 124e91a54e1Sdan do_test pager1-$tn.18 { 125e91a54e1Sdan sql1 { 126e91a54e1Sdan BEGIN; 127e91a54e1Sdan UPDATE t1 SET a = a + 10; 128e91a54e1Sdan } 129e91a54e1Sdan } {} 130e91a54e1Sdan do_test pager1-$tn-19 { 131e91a54e1Sdan sql1 { PRAGMA lock_status } 132e91a54e1Sdan } {main reserved temp closed} 133e91a54e1Sdan do_test pager1-$tn-20 { 134e91a54e1Sdan sql2 { PRAGMA lock_status } 135e91a54e1Sdan } {main shared temp closed} 136e91a54e1Sdan 137e91a54e1Sdan # Check that all connections can still read the database. Only [db] sees 138e91a54e1Sdan # the updated content (as the transaction has not been committed yet). 139e91a54e1Sdan # 140e91a54e1Sdan do_test pager1-$tn.21 { sql1 { SELECT * FROM t1 } } {11 one 12 two 13 three} 141e91a54e1Sdan do_test pager1-$tn.22 { sql2 { SELECT * FROM t1 } } {1 one 2 two 3 three} 142e91a54e1Sdan do_test pager1-$tn.23 { sql3 { SELECT * FROM t1 } } {1 one 2 two 3 three} 143e91a54e1Sdan 144e91a54e1Sdan # Because [db2] still has the SHARED lock, [db] is unable to commit the 145e91a54e1Sdan # transaction. If it tries, an error is returned and the connection 146e91a54e1Sdan # upgrades to a PENDING lock. 147e91a54e1Sdan # 148e91a54e1Sdan # Once this happens, [db] can read the database and see the new content, 149e91a54e1Sdan # [db2] (still holding SHARED) can still read the old content, but [db3] 150e91a54e1Sdan # (not holding any lock) is prevented by [db]'s PENDING from reading 151e91a54e1Sdan # the database. 152e91a54e1Sdan # 153e91a54e1Sdan do_test pager1-$tn.24 { csql1 { COMMIT } } {1 {database is locked}} 154e91a54e1Sdan do_test pager1-$tn-25 { 155e91a54e1Sdan sql1 { PRAGMA lock_status } 156e91a54e1Sdan } {main pending temp closed} 157e91a54e1Sdan do_test pager1-$tn.26 { sql1 { SELECT * FROM t1 } } {11 one 12 two 13 three} 158e91a54e1Sdan do_test pager1-$tn.27 { sql2 { SELECT * FROM t1 } } {1 one 2 two 3 three} 159e91a54e1Sdan do_test pager1-$tn.28 { csql3 { SELECT * FROM t1 } } {1 {database is locked}} 160e91a54e1Sdan 161e91a54e1Sdan # Have [db2] commit its read transaction, releasing the SHARED lock it 162e91a54e1Sdan # is holding. Now, neither [db2] nor [db3] may read the database (as [db] 163e91a54e1Sdan # is still holding a PENDING). 164e91a54e1Sdan # 165e91a54e1Sdan do_test pager1-$tn.29 { sql2 { COMMIT } } {} 166e91a54e1Sdan do_test pager1-$tn.30 { csql2 { SELECT * FROM t1 } } {1 {database is locked}} 167e91a54e1Sdan do_test pager1-$tn.31 { csql3 { SELECT * FROM t1 } } {1 {database is locked}} 168e91a54e1Sdan 169e91a54e1Sdan # [db] is now able to commit the transaction. Once the transaction is 170e91a54e1Sdan # committed, all three connections can read the new content. 171e91a54e1Sdan # 172e91a54e1Sdan do_test pager1-$tn.25 { sql1 { UPDATE t1 SET a = a+10 } } {} 173e91a54e1Sdan do_test pager1-$tn.26 { sql1 { COMMIT } } {} 174e91a54e1Sdan do_test pager1-$tn.27 { sql1 { SELECT * FROM t1 } } {21 one 22 two 23 three} 175e91a54e1Sdan do_test pager1-$tn.27 { sql2 { SELECT * FROM t1 } } {21 one 22 two 23 three} 176e91a54e1Sdan do_test pager1-$tn.28 { sql3 { SELECT * FROM t1 } } {21 one 22 two 23 three} 177e91a54e1Sdan} 178e91a54e1Sdan 17953f04f3bSdan#------------------------------------------------------------------------- 18053f04f3bSdan# Savepoint related test cases. 18153f04f3bSdan# 182b0ac3e3aSdando_test pager1-3.1 { 183b0ac3e3aSdan faultsim_delete_and_reopen 184b0ac3e3aSdan execsql { 185b0ac3e3aSdan CREATE TABLE t1(a PRIMARY KEY, b); 186b0ac3e3aSdan CREATE TABLE counter( 187b0ac3e3aSdan i CHECK (i<5), 188b0ac3e3aSdan u CHECK (u<10) 189b0ac3e3aSdan ); 190b0ac3e3aSdan INSERT INTO counter VALUES(0, 0); 191b0ac3e3aSdan CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN 192b0ac3e3aSdan UPDATE counter SET i = i+1; 193b0ac3e3aSdan END; 194b0ac3e3aSdan CREATE TRIGGER tr2 AFTER UPDATE ON t1 BEGIN 195b0ac3e3aSdan UPDATE counter SET u = u+1; 196b0ac3e3aSdan END; 197b0ac3e3aSdan } 198b0ac3e3aSdan execsql { SELECT * FROM counter } 199b0ac3e3aSdan} {0 0} 200b0ac3e3aSdan 201b0ac3e3aSdando_execsql_test pager1-3.2 { 202b0ac3e3aSdan BEGIN; 203b0ac3e3aSdan INSERT INTO t1 VALUES(1, randomblob(1500)); 204b0ac3e3aSdan INSERT INTO t1 VALUES(2, randomblob(1500)); 205b0ac3e3aSdan INSERT INTO t1 VALUES(3, randomblob(1500)); 206b0ac3e3aSdan SELECT * FROM counter; 207b0ac3e3aSdan} {3 0} 208b0ac3e3aSdando_catchsql_test pager1-3.3 { 209b0ac3e3aSdan INSERT INTO t1 SELECT a+3, randomblob(1500) FROM t1 210b0ac3e3aSdan} {1 {constraint failed}} 211b0ac3e3aSdando_execsql_test pager1-3.4 { SELECT * FROM counter } {3 0} 212b0ac3e3aSdando_execsql_test pager1-3.5 { SELECT a FROM t1 } {1 2 3} 213b0ac3e3aSdando_execsql_test pager1-3.6 { COMMIT } {} 214b0ac3e3aSdan 21553f04f3bSdan#------------------------------------------------------------------------- 21653f04f3bSdan# Hot journal rollback related test cases. 21753f04f3bSdan# 21853f04f3bSdan# pager1.4.1.*: Test that the pager module deletes very small invalid 21953f04f3bSdan# journal files. 22053f04f3bSdan# 22153f04f3bSdan# pager1.4.2.*: Test that if the master journal pointer at the end of a 22253f04f3bSdan# hot-journal file appears to be corrupt (checksum does not 22353f04f3bSdan# compute) the associated journal is rolled back (and no 22453f04f3bSdan# xAccess() call to check for the presence of any master 22553f04f3bSdan# journal file is made). 22653f04f3bSdan# 227de4996e2Sdan# pager1.4.3.*: Test that the contents of a hot-journal are ignored if the 228de4996e2Sdan# page-size or sector-size in the journal header appear to 229de4996e2Sdan# be invalid (too large, too small or not a power of 2). 230de4996e2Sdan# 231*e08341c6Sdan# pager1.4.4.*: Test hot-journal rollback of journal file with a master 232*e08341c6Sdan# journal pointer generated in various "PRAGMA synchronous" 233*e08341c6Sdan# modes. 234*e08341c6Sdan# 235*e08341c6Sdan# pager1.4.5.*: Test that hot-journal rollback stops if it encounters a 236*e08341c6Sdan# journal-record for which the checksum fails. 237*e08341c6Sdan# 23853f04f3bSdando_test pager1.4.1.1 { 23953f04f3bSdan faultsim_delete_and_reopen 24053f04f3bSdan execsql { 24153f04f3bSdan CREATE TABLE x(y, z); 24253f04f3bSdan INSERT INTO x VALUES(1, 2); 24353f04f3bSdan } 24453f04f3bSdan set fd [open test.db-journal w] 24553f04f3bSdan puts -nonewline $fd "helloworld" 24653f04f3bSdan close $fd 24753f04f3bSdan file exists test.db-journal 24853f04f3bSdan} {1} 24953f04f3bSdando_test pager1.4.1.2 { execsql { SELECT * FROM x } } {1 2} 25053f04f3bSdando_test pager1.4.1.3 { file exists test.db-journal } {0} 25153f04f3bSdan 25253f04f3bSdan# Set up a [testvfs] to snapshot the file-system just before SQLite 25353f04f3bSdan# deletes the master-journal to commit a multi-file transaction. 25453f04f3bSdan# 25553f04f3bSdan# In subsequent test cases, invoking [faultsim_restore_and_reopen] sets 25653f04f3bSdan# up the file system to contain two databases, two hot-journal files and 25753f04f3bSdan# a master-journal. 25853f04f3bSdan# 25953f04f3bSdando_test pager1.4.2.1 { 26053f04f3bSdan testvfs tstvfs -default 1 26153f04f3bSdan tstvfs filter xDelete 26253f04f3bSdan tstvfs script xDeleteCallback 26353f04f3bSdan proc xDeleteCallback {method file args} { 26453f04f3bSdan set file [file tail $file] 26553f04f3bSdan if { [string match *mj* $file] } { faultsim_save } 26653f04f3bSdan } 26753f04f3bSdan faultsim_delete_and_reopen 26853f04f3bSdan db func a_string a_string 26953f04f3bSdan execsql { 27053f04f3bSdan ATTACH 'test.db2' AS aux; 27153f04f3bSdan PRAGMA journal_mode = DELETE; 27253f04f3bSdan PRAGMA main.cache_size = 10; 27353f04f3bSdan PRAGMA aux.cache_size = 10; 27453f04f3bSdan CREATE TABLE t1(a UNIQUE, b UNIQUE); 27553f04f3bSdan CREATE TABLE aux.t2(a UNIQUE, b UNIQUE); 27653f04f3bSdan INSERT INTO t1 VALUES(a_string(200), a_string(300)); 27753f04f3bSdan INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1; 27853f04f3bSdan INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1; 27953f04f3bSdan INSERT INTO t2 SELECT * FROM t1; 28053f04f3bSdan BEGIN; 28153f04f3bSdan INSERT INTO t1 SELECT a_string(201), a_string(301) FROM t1; 28253f04f3bSdan INSERT INTO t1 SELECT a_string(202), a_string(302) FROM t1; 28353f04f3bSdan INSERT INTO t1 SELECT a_string(203), a_string(303) FROM t1; 28453f04f3bSdan INSERT INTO t1 SELECT a_string(204), a_string(304) FROM t1; 28553f04f3bSdan REPLACE INTO t2 SELECT * FROM t1; 28653f04f3bSdan COMMIT; 28753f04f3bSdan } 28853f04f3bSdan db close 28953f04f3bSdan tstvfs delete 29053f04f3bSdan} {} 29153f04f3bSdando_test pager1.4.2.2 { 29253f04f3bSdan faultsim_restore_and_reopen 29353f04f3bSdan execsql { 29453f04f3bSdan SELECT count(*) FROM t1; 29553f04f3bSdan PRAGMA integrity_check; 29653f04f3bSdan } 29753f04f3bSdan} {4 ok} 29853f04f3bSdando_test pager1.4.2.3 { 29953f04f3bSdan faultsim_restore_and_reopen 30053f04f3bSdan foreach f [glob test.db-mj*] { file delete -force $f } 30153f04f3bSdan execsql { 30253f04f3bSdan SELECT count(*) FROM t1; 30353f04f3bSdan PRAGMA integrity_check; 30453f04f3bSdan } 30553f04f3bSdan} {64 ok} 30653f04f3bSdando_test pager1.4.2.4 { 30753f04f3bSdan faultsim_restore_and_reopen 30853f04f3bSdan hexio_write test.db-journal [expr [file size test.db-journal]-20] 123456 30953f04f3bSdan execsql { 31053f04f3bSdan SELECT count(*) FROM t1; 31153f04f3bSdan PRAGMA integrity_check; 31253f04f3bSdan } 31353f04f3bSdan} {4 ok} 31453f04f3bSdando_test pager1.4.2.5 { 31553f04f3bSdan faultsim_restore_and_reopen 31653f04f3bSdan hexio_write test.db-journal [expr [file size test.db-journal]-20] 123456 31753f04f3bSdan foreach f [glob test.db-mj*] { file delete -force $f } 31853f04f3bSdan execsql { 31953f04f3bSdan SELECT count(*) FROM t1; 32053f04f3bSdan PRAGMA integrity_check; 32153f04f3bSdan } 32253f04f3bSdan} {4 ok} 32353f04f3bSdan 324de4996e2Sdando_test pager1.4.3.1 { 325de4996e2Sdan testvfs tstvfs -default 1 326de4996e2Sdan tstvfs filter xSync 327de4996e2Sdan tstvfs script xSyncCallback 328de4996e2Sdan proc xSyncCallback {method file args} { 329de4996e2Sdan set file [file tail $file] 330de4996e2Sdan if { 0==[string match *journal $file] } { faultsim_save } 331de4996e2Sdan } 332de4996e2Sdan faultsim_delete_and_reopen 333de4996e2Sdan execsql { 334de4996e2Sdan PRAGMA journal_mode = DELETE; 335de4996e2Sdan CREATE TABLE t1(a, b); 336de4996e2Sdan INSERT INTO t1 VALUES(1, 2); 337de4996e2Sdan INSERT INTO t1 VALUES(3, 4); 338de4996e2Sdan } 339de4996e2Sdan db close 340de4996e2Sdan tstvfs delete 341de4996e2Sdan} {} 342de4996e2Sdan 343de4996e2Sdanforeach {tn ofst value result} { 344de4996e2Sdan 2 20 31 {1 2 3 4} 345de4996e2Sdan 3 20 32 {1 2 3 4} 346de4996e2Sdan 4 20 33 {1 2 3 4} 347de4996e2Sdan 5 20 65536 {1 2 3 4} 348de4996e2Sdan 6 20 131072 {1 2 3 4} 349de4996e2Sdan 350de4996e2Sdan 7 24 511 {1 2 3 4} 351de4996e2Sdan 8 24 513 {1 2 3 4} 352de4996e2Sdan 9 24 65536 {1 2 3 4} 353de4996e2Sdan 354de4996e2Sdan 10 32 65536 {1 2} 355de4996e2Sdan} { 356de4996e2Sdan do_test pager1.4.3.$tn { 357de4996e2Sdan faultsim_restore_and_reopen 358de4996e2Sdan hexio_write test.db-journal $ofst [format %.8x $value] 359de4996e2Sdan execsql { SELECT * FROM t1 } 360de4996e2Sdan } $result 361de4996e2Sdan} 362de4996e2Sdandb close 363de4996e2Sdan 364*e08341c6Sdan# Set up a VFS that snapshots the file-system just before a master journal 365*e08341c6Sdan# file is deleted to commit a multi-file transaction. Specifically, the 366*e08341c6Sdan# file-system is saved just before the xDelete() call to remove the 367*e08341c6Sdan# master journal file from the file-system. 368*e08341c6Sdan# 369*e08341c6Sdantestvfs tv -default 1 370*e08341c6Sdantv script copy_on_mj_delete 371*e08341c6Sdanset ::mj_filename_length 0 372*e08341c6Sdanproc copy_on_mj_delete {method filename args} { 373*e08341c6Sdan if {[string match *mj* [file tail $filename]]} { 374*e08341c6Sdan set ::mj_filename_length [string length $filename] 375*e08341c6Sdan faultsim_save 376*e08341c6Sdan } 377*e08341c6Sdan return SQLITE_OK 378*e08341c6Sdan} 379*e08341c6Sdan 380*e08341c6Sdanset pwd [pwd] 381*e08341c6Sdanforeach {tn1 tcl} { 382*e08341c6Sdan 1 { set prefix "test.db" } 383*e08341c6Sdan 2 { 384*e08341c6Sdan # This test depends on the underlying VFS being able to open paths 385*e08341c6Sdan # 512 bytes in length. The idea is to create a hot-journal file that 386*e08341c6Sdan # contains a master-journal pointer so large that it could contain 387*e08341c6Sdan # a valid page record (if the file page-size is 512 bytes). So as to 388*e08341c6Sdan # make sure SQLite doesn't get confused by this. 389*e08341c6Sdan # 390*e08341c6Sdan set nPadding [expr 511 - $::mj_filename_length] 391*e08341c6Sdan 392*e08341c6Sdan # We cannot just create a really long database file name to open, as 393*e08341c6Sdan # Linux limits a single component of a path to 255 bytes by default 394*e08341c6Sdan # (and presumably other systems have limits too). So create a directory 395*e08341c6Sdan # hierarchy to work in. 396*e08341c6Sdan # 397*e08341c6Sdan set dirname "d123456789012345678901234567890/" 398*e08341c6Sdan set nDir [expr $nPadding / 32] 399*e08341c6Sdan if { $nDir } { 400*e08341c6Sdan set p [string repeat $dirname $nDir] 401*e08341c6Sdan file mkdir $p 402*e08341c6Sdan cd $p 403*e08341c6Sdan } 404*e08341c6Sdan 405*e08341c6Sdan set padding [string repeat x [expr $nPadding %32]] 406*e08341c6Sdan set prefix "test.db${padding}" 407*e08341c6Sdan } 408*e08341c6Sdan} { 409*e08341c6Sdan eval $tcl 410*e08341c6Sdan foreach {tn2 sql} { 411*e08341c6Sdan o { 412*e08341c6Sdan PRAGMA main.synchronous=OFF; 413*e08341c6Sdan PRAGMA aux.synchronous=OFF; 414*e08341c6Sdan } 415*e08341c6Sdan o512 { 416*e08341c6Sdan PRAGMA main.synchronous=OFF; 417*e08341c6Sdan PRAGMA aux.synchronous=OFF; 418*e08341c6Sdan PRAGMA main.page_size = 512; 419*e08341c6Sdan PRAGMA aux.page_size = 512; 420*e08341c6Sdan } 421*e08341c6Sdan n { 422*e08341c6Sdan PRAGMA main.synchronous=NORMAL; 423*e08341c6Sdan PRAGMA aux.synchronous=NORMAL; 424*e08341c6Sdan } 425*e08341c6Sdan f { 426*e08341c6Sdan PRAGMA main.synchronous=FULL; 427*e08341c6Sdan PRAGMA aux.synchronous=FULL; 428*e08341c6Sdan } 429*e08341c6Sdan } { 430*e08341c6Sdan 431*e08341c6Sdan set tn "${tn1}.${tn2}" 432*e08341c6Sdan 433*e08341c6Sdan # Set up a connection to have two databases, test.db (main) and 434*e08341c6Sdan # test.db2 (aux). Then run a multi-file transaction on them. The 435*e08341c6Sdan # VFS will snapshot the file-system just before the master-journal 436*e08341c6Sdan # file is deleted to commit the transaction. 437*e08341c6Sdan # 438*e08341c6Sdan tv filter xDelete 439*e08341c6Sdan do_test pager1-4.4.$tn.1 { 440*e08341c6Sdan faultsim_delete_and_reopen $prefix 441*e08341c6Sdan execsql " 442*e08341c6Sdan ATTACH '${prefix}2' AS aux; 443*e08341c6Sdan $sql 444*e08341c6Sdan CREATE TABLE a(x); 445*e08341c6Sdan CREATE TABLE aux.b(x); 446*e08341c6Sdan INSERT INTO a VALUES('double-you'); 447*e08341c6Sdan INSERT INTO a VALUES('why'); 448*e08341c6Sdan INSERT INTO a VALUES('zed'); 449*e08341c6Sdan INSERT INTO b VALUES('won'); 450*e08341c6Sdan INSERT INTO b VALUES('too'); 451*e08341c6Sdan INSERT INTO b VALUES('free'); 452*e08341c6Sdan " 453*e08341c6Sdan execsql { 454*e08341c6Sdan BEGIN; 455*e08341c6Sdan INSERT INTO a SELECT * FROM b WHERE rowid<=3; 456*e08341c6Sdan INSERT INTO b SELECT * FROM a WHERE rowid<=3; 457*e08341c6Sdan COMMIT; 458*e08341c6Sdan } 459*e08341c6Sdan } {} 460*e08341c6Sdan tv filter {} 461*e08341c6Sdan 462*e08341c6Sdan # Check that the transaction was committed successfully. 463*e08341c6Sdan # 464*e08341c6Sdan do_execsql_test pager1-4.4.$tn.2 { 465*e08341c6Sdan SELECT * FROM a 466*e08341c6Sdan } {double-you why zed won too free} 467*e08341c6Sdan do_execsql_test pager1-4.4.$tn.3 { 468*e08341c6Sdan SELECT * FROM b 469*e08341c6Sdan } {won too free double-you why zed} 470*e08341c6Sdan 471*e08341c6Sdan # Restore the file-system and reopen the databases. Check that it now 472*e08341c6Sdan # appears that the transaction was not committed (because the file-system 473*e08341c6Sdan # was restored to the state where it had not been). 474*e08341c6Sdan # 475*e08341c6Sdan do_test pager1-4.4.$tn.4 { 476*e08341c6Sdan faultsim_restore_and_reopen $prefix 477*e08341c6Sdan execsql "ATTACH '${prefix}2' AS aux" 478*e08341c6Sdan } {} 479*e08341c6Sdan do_execsql_test pager1-4.4.$tn.5 {SELECT * FROM a} {double-you why zed} 480*e08341c6Sdan do_execsql_test pager1-4.4.$tn.6 {SELECT * FROM b} {won too free} 481*e08341c6Sdan 482*e08341c6Sdan # Restore the file-system again. This time, before reopening the databases, 483*e08341c6Sdan # delete the master-journal file from the file-system. It now appears that 484*e08341c6Sdan # the transaction was committed (no master-journal file == no rollback). 485*e08341c6Sdan # 486*e08341c6Sdan do_test pager1-4.4.$tn.7 { 487*e08341c6Sdan faultsim_restore_and_reopen $prefix 488*e08341c6Sdan foreach f [glob ${prefix}-mj*] { file delete -force $f } 489*e08341c6Sdan execsql "ATTACH '${prefix}2' AS aux" 490*e08341c6Sdan } {} 491*e08341c6Sdan do_execsql_test pager1-4.4.$tn.8 { 492*e08341c6Sdan SELECT * FROM a 493*e08341c6Sdan } {double-you why zed won too free} 494*e08341c6Sdan do_execsql_test pager1-4.4.$tn.9 { 495*e08341c6Sdan SELECT * FROM b 496*e08341c6Sdan } {won too free double-you why zed} 497*e08341c6Sdan } 498*e08341c6Sdan 499*e08341c6Sdan cd $pwd 500*e08341c6Sdan} 501*e08341c6Sdandb close 502*e08341c6Sdantv delete 503*e08341c6Sdan 504de4996e2Sdan#------------------------------------------------------------------------- 505146ed78bSdan# The following tests deal with multi-file commits. 506de4996e2Sdan# 507146ed78bSdan# pager1-5.1.*: The case where a multi-file cannot be committed because 508146ed78bSdan# another connection is holding a SHARED lock on one of the 509146ed78bSdan# files. After the SHARED lock is removed, the COMMIT succeeds. 510146ed78bSdan# 511146ed78bSdan# pager1-5.2.*: Multi-file commits with journal_mode=memory. 512146ed78bSdan# 513146ed78bSdan# pager1-5.3.*: Multi-file commits with journal_mode=memory. 514146ed78bSdan# 515146ed78bSdan# pager1-5.4.*: Check that with synchronous=normal, the master-journal file 516146ed78bSdan# name is added to a journal file immediately after the last 517146ed78bSdan# journal record. But with synchronous=full, extra unused space 518146ed78bSdan# is allocated between the last journal record and the 519146ed78bSdan# master-journal file name so that the master-journal file 520146ed78bSdan# name does not lie on the same sector as the last journal file 521146ed78bSdan# record. 522146ed78bSdan# 523*e08341c6Sdan# pager1-5.5.*: Check that in journal_mode=PERSIST mode, a journal file is 524*e08341c6Sdan# truncated to zero bytes when a multi-file transaction is 525*e08341c6Sdan# committed (instead of the first couple of bytes being zeroed). 526*e08341c6Sdan# 527146ed78bSdan# 528146ed78bSdando_test pager1-5.1.1 { 529146ed78bSdan faultsim_delete_and_reopen 530146ed78bSdan execsql { 531146ed78bSdan ATTACH 'test.db2' AS aux; 532146ed78bSdan CREATE TABLE t1(a, b); 533146ed78bSdan CREATE TABLE aux.t2(a, b); 534146ed78bSdan INSERT INTO t1 VALUES(17, 'Lenin'); 535146ed78bSdan INSERT INTO t1 VALUES(22, 'Stalin'); 536146ed78bSdan INSERT INTO t1 VALUES(53, 'Khrushchev'); 537146ed78bSdan } 538146ed78bSdan} {} 539146ed78bSdando_test pager1-5.1.2 { 540146ed78bSdan execsql { 541146ed78bSdan BEGIN; 542146ed78bSdan INSERT INTO t1 VALUES(64, 'Brezhnev'); 543146ed78bSdan INSERT INTO t2 SELECT * FROM t1; 544146ed78bSdan } 545146ed78bSdan sqlite3 db2 test.db2 546146ed78bSdan execsql { 547146ed78bSdan BEGIN; 548146ed78bSdan SELECT * FROM t2; 549146ed78bSdan } db2 550146ed78bSdan} {} 551146ed78bSdando_test pager1-5.1.3 { 552146ed78bSdan catchsql COMMIT 553146ed78bSdan} {1 {database is locked}} 554146ed78bSdando_test pager1-5.1.4 { 555146ed78bSdan execsql COMMIT db2 556146ed78bSdan execsql COMMIT 557146ed78bSdan execsql { SELECT * FROM t2 } db2 558146ed78bSdan} {17 Lenin 22 Stalin 53 Khrushchev 64 Brezhnev} 559146ed78bSdando_test pager1-5.1.5 { 560146ed78bSdan db2 close 561146ed78bSdan} {} 562146ed78bSdan 563146ed78bSdando_test pager1-5.2.1 { 564146ed78bSdan execsql { 565146ed78bSdan PRAGMA journal_mode = memory; 566146ed78bSdan BEGIN; 567146ed78bSdan INSERT INTO t1 VALUES(84, 'Andropov'); 568146ed78bSdan INSERT INTO t2 VALUES(84, 'Andropov'); 569146ed78bSdan COMMIT; 570146ed78bSdan } 571146ed78bSdan} {memory} 572146ed78bSdando_test pager1-5.3.1 { 573146ed78bSdan execsql { 574146ed78bSdan PRAGMA journal_mode = off; 575146ed78bSdan BEGIN; 576146ed78bSdan INSERT INTO t1 VALUES(85, 'Gorbachev'); 577146ed78bSdan INSERT INTO t2 VALUES(85, 'Gorbachev'); 578146ed78bSdan COMMIT; 579146ed78bSdan } 580146ed78bSdan} {off} 581146ed78bSdan 582146ed78bSdando_test pager1-5.4.1 { 583146ed78bSdan db close 584146ed78bSdan testvfs tv 585146ed78bSdan sqlite3 db test.db -vfs tv 586146ed78bSdan execsql { ATTACH 'test.db2' AS aux } 587146ed78bSdan 588146ed78bSdan tv filter xDelete 589146ed78bSdan tv script max_journal_size 590146ed78bSdan tv sectorsize 512 591146ed78bSdan set ::max_journal 0 592146ed78bSdan proc max_journal_size {method args} { 593146ed78bSdan set sz 0 594146ed78bSdan catch { set sz [file size test.db-journal] } 595146ed78bSdan if {$sz > $::max_journal} { 596146ed78bSdan set ::max_journal $sz 597146ed78bSdan } 598146ed78bSdan return SQLITE_OK 599146ed78bSdan } 600146ed78bSdan execsql { 601146ed78bSdan PRAGMA journal_mode = DELETE; 602146ed78bSdan PRAGMA synchronous = NORMAL; 603146ed78bSdan BEGIN; 604146ed78bSdan INSERT INTO t1 VALUES(85, 'Gorbachev'); 605146ed78bSdan INSERT INTO t2 VALUES(85, 'Gorbachev'); 606146ed78bSdan COMMIT; 607146ed78bSdan } 608146ed78bSdan set ::max_journal 609146ed78bSdan} [expr 2615+[string length [pwd]]] 610146ed78bSdando_test pager1-5.4.2 { 611146ed78bSdan set ::max_journal 0 612146ed78bSdan execsql { 613146ed78bSdan PRAGMA synchronous = full; 614146ed78bSdan BEGIN; 615146ed78bSdan DELETE FROM t1 WHERE b = 'Lenin'; 616146ed78bSdan DELETE FROM t2 WHERE b = 'Lenin'; 617146ed78bSdan COMMIT; 618146ed78bSdan } 619146ed78bSdan set ::max_journal 620146ed78bSdan} [expr 3111+[string length [pwd]]] 621146ed78bSdandb close 622146ed78bSdantv delete 623146ed78bSdan 624146ed78bSdando_test pager1-5.5.1 { 625146ed78bSdan sqlite3 db test.db 626146ed78bSdan execsql { 627146ed78bSdan ATTACH 'test.db2' AS aux; 628146ed78bSdan PRAGMA journal_mode = PERSIST; 629146ed78bSdan CREATE TABLE t3(a, b); 630146ed78bSdan INSERT INTO t3 SELECT randomblob(1500), randomblob(1500) FROM t1; 631146ed78bSdan UPDATE t3 SET b = randomblob(1500); 632146ed78bSdan } 633146ed78bSdan expr [file size test.db-journal] > 15000 634146ed78bSdan} {1} 635146ed78bSdando_test pager1-5.5.2 { 636146ed78bSdan execsql { 637146ed78bSdan PRAGMA synchronous = full; 638146ed78bSdan BEGIN; 639146ed78bSdan DELETE FROM t1 WHERE b = 'Stalin'; 640146ed78bSdan DELETE FROM t2 WHERE b = 'Stalin'; 641146ed78bSdan COMMIT; 642146ed78bSdan } 643146ed78bSdan file size test.db-journal 644146ed78bSdan} {0} 645146ed78bSdan 646146ed78bSdan 647146ed78bSdan#------------------------------------------------------------------------- 648146ed78bSdan# The following tests work with "PRAGMA max_page_count" 649146ed78bSdan# 650146ed78bSdando_test pager1-6.1 { 651146ed78bSdan faultsim_delete_and_reopen 652146ed78bSdan execsql { 653146ed78bSdan PRAGMA max_page_count = 10; 654146ed78bSdan CREATE TABLE t2(a, b); 655146ed78bSdan CREATE TABLE t3(a, b); 656146ed78bSdan CREATE TABLE t4(a, b); 657146ed78bSdan CREATE TABLE t5(a, b); 658146ed78bSdan CREATE TABLE t6(a, b); 659146ed78bSdan CREATE TABLE t7(a, b); 660146ed78bSdan CREATE TABLE t8(a, b); 661146ed78bSdan CREATE TABLE t9(a, b); 662146ed78bSdan CREATE TABLE t10(a, b); 663146ed78bSdan } 664146ed78bSdan} {10} 665146ed78bSdando_test pager1-6.2 { 666146ed78bSdan catchsql { 667146ed78bSdan CREATE TABLE t11(a, b); 668146ed78bSdan } 669146ed78bSdan} {1 {database or disk is full}} 670de4996e2Sdan 671153eda0aSdan 672153eda0aSdan#------------------------------------------------------------------------- 673153eda0aSdan# The following tests work with "PRAGMA journal_mode=TRUNCATE" and 674153eda0aSdan# "PRAGMA locking_mode=EXCLUSIVE". 675153eda0aSdan# 676153eda0aSdan# Each test is specified with 5 variables. As follows: 677153eda0aSdan# 678153eda0aSdan# $tn: Test Number. Used as part of the [do_test] test names. 679153eda0aSdan# $sql: SQL to execute. 680153eda0aSdan# $res: Expected result of executing $sql. 681153eda0aSdan# $js: The expected size of the journal file, in bytes, after executing 682153eda0aSdan# the SQL script. Or -1 if the journal is not expected to exist. 683153eda0aSdan# $ws: The expected size of the WAL file, in bytes, after executing 684153eda0aSdan# the SQL script. Or -1 if the WAL is not expected to exist. 685153eda0aSdan# 686153eda0aSdanfaultsim_delete_and_reopen 687153eda0aSdanforeach {tn sql res js ws} [subst { 688153eda0aSdan 689153eda0aSdan 1 { 690153eda0aSdan CREATE TABLE t1(a, b); 691153eda0aSdan PRAGMA auto_vacuum=OFF; 692153eda0aSdan PRAGMA synchronous=NORMAL; 693153eda0aSdan PRAGMA page_size=1024; 694153eda0aSdan PRAGMA locking_mode=EXCLUSIVE; 695153eda0aSdan PRAGMA journal_mode=TRUNCATE; 696153eda0aSdan INSERT INTO t1 VALUES(1, 2); 697153eda0aSdan } {exclusive truncate} 0 -1 698153eda0aSdan 699153eda0aSdan 2 { 700153eda0aSdan BEGIN IMMEDIATE; 701153eda0aSdan SELECT * FROM t1; 702153eda0aSdan COMMIT; 703153eda0aSdan } {1 2} 0 -1 704153eda0aSdan 705153eda0aSdan 3 { 706153eda0aSdan BEGIN; 707153eda0aSdan SELECT * FROM t1; 708153eda0aSdan COMMIT; 709153eda0aSdan } {1 2} 0 -1 710153eda0aSdan 711153eda0aSdan 4 { PRAGMA journal_mode = WAL } wal -1 -1 712153eda0aSdan 5 { INSERT INTO t1 VALUES(3, 4) } {} -1 [wal_file_size 1 1024] 713153eda0aSdan 6 { PRAGMA locking_mode = NORMAL } normal -1 [wal_file_size 1 1024] 714153eda0aSdan 7 { INSERT INTO t1 VALUES(5, 6); } {} -1 [wal_file_size 2 1024] 715153eda0aSdan 716153eda0aSdan 8 { PRAGMA journal_mode = TRUNCATE } truncate 0 -1 717153eda0aSdan 9 { INSERT INTO t1 VALUES(7, 8) } {} 0 -1 718153eda0aSdan 10 { SELECT * FROM t1 } {1 2 3 4 5 6 7 8} 0 -1 719153eda0aSdan 720153eda0aSdan}] { 721153eda0aSdan do_execsql_test pager1-7.1.$tn.1 $sql $res 722153eda0aSdan catch { set J -1 ; set J [file size test.db-journal] } 723153eda0aSdan catch { set W -1 ; set W [file size test.db-wal] } 724153eda0aSdan do_test pager1-7.1.$tn.2 { list $J $W } [list $js $ws] 725153eda0aSdan} 726153eda0aSdan 727153eda0aSdando_test pager1-8.1 { 728153eda0aSdan faultsim_delete_and_reopen 729153eda0aSdan db close 730153eda0aSdan sqlite3 db :memory: 731153eda0aSdan execsql { 732153eda0aSdan CREATE TABLE x1(x); 733153eda0aSdan INSERT INTO x1 VALUES('Charles'); 734153eda0aSdan INSERT INTO x1 VALUES('James'); 735153eda0aSdan INSERT INTO x1 VALUES('Mary'); 736153eda0aSdan SELECT * FROM x1; 737153eda0aSdan } 738153eda0aSdan} {Charles James Mary} 739153eda0aSdando_test pager1-8.2 { 740153eda0aSdan db close 741153eda0aSdan sqlite3 db :memory: 742153eda0aSdan catchsql { SELECT * FROM x1 } 743153eda0aSdan} {1 {no such table: x1}} 744153eda0aSdan 745e91a54e1Sdanfinish_test 746e91a54e1Sdan 747