3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
15 source $testdir/lock_common.tcl
16 source $testdir/malloc_common.tcl
17 source $testdir/wal_common.tcl
19 # Do not use a codec for tests in this file, as the database file is
20 # manipulated directly using tcl scripts (using the [hexio_write] command).
25 # pager1-1.*: Test inter-process locking (clients in multiple processes).
27 # pager1-2.*: Test intra-process locking (multiple clients in this process).
29 # pager1-3.*: Savepoint related tests.
31 # pager1-4.*: Hot-journal related tests.
33 # pager1-5.*: Cases related to multi-file commits.
35 # pager1-6.*: Cases related to "PRAGMA max_page_count"
37 # pager1-7.*: Cases specific to "PRAGMA journal_mode=TRUNCATE"
39 # pager1-8.*: Cases using temporary and in-memory databases.
41 # pager1-9.*: Tests related to the backup API.
43 # pager1-10.*: Test that the assumed file-system sector-size is limited to
46 # pager1-12.*: Tests involving "PRAGMA page_size"
48 # pager1-13.*: Cases specific to "PRAGMA journal_mode=PERSIST"
50 # pager1-14.*: Cases specific to "PRAGMA journal_mode=OFF"
52 # pager1-15.*: Varying sqlite3_vfs.szOsFile
54 # pager1-16.*: Varying sqlite3_vfs.mxPathname
56 # pager1-17.*: Tests related to "PRAGMA omit_readlock"
58 # pager1-18.*: Test that the pager layer responds correctly if the b-tree
59 # requests an invalid page number (due to db corruption).
62 proc recursive_select {id table {script {}}} {
64 db eval "SELECT rowid, * FROM $table WHERE rowid = ($id-1)" {
65 recursive_select $rowid $table $script
68 if {$cnt==0} { eval $script }
71 set a_string_counter 1
73 global a_string_counter
75 string range [string repeat "${a_string_counter}." $n] 1 $n
77 db func a_string a_string
79 do_multiclient_test tn {
81 # Create and populate a database table using connection [db]. Check
82 # that connections [db2] and [db3] can see the schema and content.
84 do_test pager1-$tn.1 {
86 CREATE TABLE t1(a PRIMARY KEY, b);
87 CREATE INDEX i1 ON t1(b);
88 INSERT INTO t1 VALUES(1, 'one'); INSERT INTO t1 VALUES(2, 'two');
91 do_test pager1-$tn.2 { sql2 { SELECT * FROM t1 } } {1 one 2 two}
92 do_test pager1-$tn.3 { sql3 { SELECT * FROM t1 } } {1 one 2 two}
94 # Open a transaction and add a row using [db]. This puts [db] in
95 # RESERVED state. Check that connections [db2] and [db3] can still
96 # read the database content as it was before the transaction was
97 # opened. [db] should see the inserted row.
99 do_test pager1-$tn.4 {
102 INSERT INTO t1 VALUES(3, 'three');
105 do_test pager1-$tn.5 { sql2 { SELECT * FROM t1 } } {1 one 2 two}
106 do_test pager1-$tn.7 { sql1 { SELECT * FROM t1 } } {1 one 2 two 3 three}
108 # [db] still has an open write transaction. Check that this prevents
109 # other connections (specifically [db2]) from writing to the database.
111 # Even if [db2] opens a transaction first, it may not write to the
112 # database. After the attempt to write the db within a transaction,
113 # [db2] is left with an open transaction, but not a read-lock on
114 # the main database. So it does not prevent [db] from committing.
116 do_test pager1-$tn.8 {
117 csql2 { UPDATE t1 SET a = a + 10 }
118 } {1 {database is locked}}
119 do_test pager1-$tn.9 {
122 UPDATE t1 SET a = a + 10;
124 } {1 {database is locked}}
126 # Have [db] commit its transactions. Check the other connections can
127 # now see the new database content.
129 do_test pager1-$tn.10 { sql1 { COMMIT } } {}
130 do_test pager1-$tn.11 { sql1 { SELECT * FROM t1 } } {1 one 2 two 3 three}
131 do_test pager1-$tn.12 { sql2 { SELECT * FROM t1 } } {1 one 2 two 3 three}
132 do_test pager1-$tn.13 { sql3 { SELECT * FROM t1 } } {1 one 2 two 3 three}
134 # Check that, as noted above, [db2] really did keep an open transaction
135 # after the attempt to write the database failed.
137 do_test pager1-$tn.14 {
139 } {1 {cannot start a transaction within a transaction}}
140 do_test pager1-$tn.15 { sql2 { ROLLBACK } } {}
142 # Have [db2] open a transaction and take a read-lock on the database.
143 # Check that this prevents [db] from writing to the database (outside
144 # of any transaction). After this fails, check that [db3] can read
145 # the db (showing that [db] did not take a PENDING lock etc.)
147 do_test pager1-$tn.15 {
148 sql2 { BEGIN; SELECT * FROM t1; }
149 } {1 one 2 two 3 three}
150 do_test pager1-$tn.16 {
151 csql1 { UPDATE t1 SET a = a + 10 }
152 } {1 {database is locked}}
153 do_test pager1-$tn.17 { sql3 { SELECT * FROM t1 } } {1 one 2 two 3 three}
155 # This time, have [db] open a transaction before writing the database.
156 # This works - [db] gets a RESERVED lock which does not conflict with
157 # the SHARED lock [db2] is holding.
159 do_test pager1-$tn.18 {
162 UPDATE t1 SET a = a + 10;
165 do_test pager1-$tn-19 {
166 sql1 { PRAGMA lock_status }
167 } {main reserved temp closed}
168 do_test pager1-$tn-20 {
169 sql2 { PRAGMA lock_status }
170 } {main shared temp closed}
172 # Check that all connections can still read the database. Only [db] sees
173 # the updated content (as the transaction has not been committed yet).
175 do_test pager1-$tn.21 { sql1 { SELECT * FROM t1 } } {11 one 12 two 13 three}
176 do_test pager1-$tn.22 { sql2 { SELECT * FROM t1 } } {1 one 2 two 3 three}
177 do_test pager1-$tn.23 { sql3 { SELECT * FROM t1 } } {1 one 2 two 3 three}
179 # Because [db2] still has the SHARED lock, [db] is unable to commit the
180 # transaction. If it tries, an error is returned and the connection
181 # upgrades to a PENDING lock.
183 # Once this happens, [db] can read the database and see the new content,
184 # [db2] (still holding SHARED) can still read the old content, but [db3]
185 # (not holding any lock) is prevented by [db]'s PENDING from reading
188 do_test pager1-$tn.24 { csql1 { COMMIT } } {1 {database is locked}}
189 do_test pager1-$tn-25 {
190 sql1 { PRAGMA lock_status }
191 } {main pending temp closed}
192 do_test pager1-$tn.26 { sql1 { SELECT * FROM t1 } } {11 one 12 two 13 three}
193 do_test pager1-$tn.27 { sql2 { SELECT * FROM t1 } } {1 one 2 two 3 three}
194 do_test pager1-$tn.28 { csql3 { SELECT * FROM t1 } } {1 {database is locked}}
196 # Have [db2] commit its read transaction, releasing the SHARED lock it
197 # is holding. Now, neither [db2] nor [db3] may read the database (as [db]
198 # is still holding a PENDING).
200 do_test pager1-$tn.29 { sql2 { COMMIT } } {}
201 do_test pager1-$tn.30 { csql2 { SELECT * FROM t1 } } {1 {database is locked}}
202 do_test pager1-$tn.31 { csql3 { SELECT * FROM t1 } } {1 {database is locked}}
204 # [db] is now able to commit the transaction. Once the transaction is
205 # committed, all three connections can read the new content.
207 do_test pager1-$tn.25 { sql1 { UPDATE t1 SET a = a+10 } } {}
208 do_test pager1-$tn.26 { sql1 { COMMIT } } {}
209 do_test pager1-$tn.27 { sql1 { SELECT * FROM t1 } } {21 one 22 two 23 three}
210 do_test pager1-$tn.27 { sql2 { SELECT * FROM t1 } } {21 one 22 two 23 three}
211 do_test pager1-$tn.28 { sql3 { SELECT * FROM t1 } } {21 one 22 two 23 three}
213 # Install a busy-handler for connection [db].
218 if {$n>5} { sql2 COMMIT }
223 do_test pager1-$tn.29 {
224 sql1 { BEGIN ; INSERT INTO t1 VALUES('x', 'y') }
226 do_test pager1-$tn.30 {
227 sql2 { BEGIN ; SELECT * FROM t1 }
228 } {21 one 22 two 23 three}
229 do_test pager1-$tn.31 { sql1 COMMIT } {}
230 do_test pager1-$tn.32 { set ::nbusy } {0 1 2 3 4 5 6}
233 #-------------------------------------------------------------------------
234 # Savepoint related test cases.
236 # pager1-3.1.2.*: Force a savepoint rollback to cause the database file
239 # pager1-3.1.3.*: Use a journal created in synchronous=off mode as part
240 # of a savepoint rollback.
242 do_test pager1-3.1.1 {
243 faultsim_delete_and_reopen
245 CREATE TABLE t1(a PRIMARY KEY, b);
246 CREATE TABLE counter(
250 INSERT INTO counter VALUES(0, 0);
251 CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
252 UPDATE counter SET i = i+1;
254 CREATE TRIGGER tr2 AFTER UPDATE ON t1 BEGIN
255 UPDATE counter SET u = u+1;
258 execsql { SELECT * FROM counter }
261 do_execsql_test pager1-3.1.2 {
262 PRAGMA cache_size = 10;
264 INSERT INTO t1 VALUES(1, randomblob(1500));
265 INSERT INTO t1 VALUES(2, randomblob(1500));
266 INSERT INTO t1 VALUES(3, randomblob(1500));
267 SELECT * FROM counter;
269 do_catchsql_test pager1-3.1.3 {
270 INSERT INTO t1 SELECT a+3, randomblob(1500) FROM t1
271 } {1 {constraint failed}}
272 do_execsql_test pager1-3.4 { SELECT * FROM counter } {3 0}
273 do_execsql_test pager1-3.5 { SELECT a FROM t1 } {1 2 3}
274 do_execsql_test pager1-3.6 { COMMIT } {}
276 foreach {tn sql tcl} {
277 7 { PRAGMA synchronous = NORMAL ; PRAGMA temp_store = 0 } {
278 testvfs tv -default 1
279 tv devchar safe_append
281 8 { PRAGMA synchronous = NORMAL ; PRAGMA temp_store = 2 } {
282 testvfs tv -default 1
283 tv devchar sequential
285 9 { PRAGMA synchronous = FULL } { }
286 10 { PRAGMA synchronous = NORMAL } { }
287 11 { PRAGMA synchronous = OFF } { }
288 12 { PRAGMA synchronous = FULL ; PRAGMA fullfsync = 1 } { }
289 13 { PRAGMA synchronous = FULL } {
290 testvfs tv -default 1
291 tv devchar sequential
293 14 { PRAGMA locking_mode = EXCLUSIVE } {
296 do_test pager1-3.$tn.1 {
298 faultsim_delete_and_reopen
299 db func a_string a_string
302 PRAGMA auto_vacuum = 2;
303 PRAGMA cache_size = 10;
304 CREATE TABLE z(x INTEGER PRIMARY KEY, y);
306 INSERT INTO z VALUES(NULL, a_string(800));
307 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 2
308 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 4
309 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 8
310 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 16
311 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 32
312 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 64
313 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 128
314 INSERT INTO z SELECT NULL, a_string(800) FROM z; -- 256
317 execsql { PRAGMA auto_vacuum }
319 do_execsql_test pager1-3.$tn.2 {
321 INSERT INTO z VALUES(NULL, a_string(800));
322 INSERT INTO z VALUES(NULL, a_string(800));
324 UPDATE z SET y = NULL WHERE x>256;
325 PRAGMA incremental_vacuum;
326 SELECT count(*) FROM z WHERE x < 100;
331 do_execsql_test pager1-3.$tn.3 {
334 UPDATE z SET y = y||x;
337 SELECT count(*) FROM z;
340 do_execsql_test pager1-3.$tn.4 {
342 UPDATE z SET y = y||x;
345 do_execsql_test pager1-3.$tn.5 {
346 SELECT count(*) FROM z;
348 PRAGMA integrity_check;
351 do_execsql_test pager1-3.$tn.6 {
360 #-------------------------------------------------------------------------
361 # Hot journal rollback related test cases.
363 # pager1.4.1.*: Test that the pager module deletes very small invalid
366 # pager1.4.2.*: Test that if the master journal pointer at the end of a
367 # hot-journal file appears to be corrupt (checksum does not
368 # compute) the associated journal is rolled back (and no
369 # xAccess() call to check for the presence of any master
370 # journal file is made).
372 # pager1.4.3.*: Test that the contents of a hot-journal are ignored if the
373 # page-size or sector-size in the journal header appear to
374 # be invalid (too large, too small or not a power of 2).
376 # pager1.4.4.*: Test hot-journal rollback of journal file with a master
377 # journal pointer generated in various "PRAGMA synchronous"
380 # pager1.4.5.*: Test that hot-journal rollback stops if it encounters a
381 # journal-record for which the checksum fails.
383 # pager1.4.6.*: Test that when rolling back a hot-journal that contains a
384 # master journal pointer, the master journal file is deleted
385 # after all the hot-journals that refer to it are deleted.
387 # pager1.4.7.*: Test that if a hot-journal file exists but a client can
388 # open it for reading only, the database cannot be accessed and
389 # SQLITE_CANTOPEN is returned.
391 do_test pager1.4.1.1 {
392 faultsim_delete_and_reopen
394 CREATE TABLE x(y, z);
395 INSERT INTO x VALUES(1, 2);
397 set fd [open test.db-journal w]
398 puts -nonewline $fd "helloworld"
400 file exists test.db-journal
402 do_test pager1.4.1.2 { execsql { SELECT * FROM x } } {1 2}
403 do_test pager1.4.1.3 { file exists test.db-journal } {0}
405 # Set up a [testvfs] to snapshot the file-system just before SQLite
406 # deletes the master-journal to commit a multi-file transaction.
408 # In subsequent test cases, invoking [faultsim_restore_and_reopen] sets
409 # up the file system to contain two databases, two hot-journal files and
412 do_test pager1.4.2.1 {
413 testvfs tstvfs -default 1
414 tstvfs filter xDelete
415 tstvfs script xDeleteCallback
416 proc xDeleteCallback {method file args} {
417 set file [file tail $file]
418 if { [string match *mj* $file] } { faultsim_save }
420 faultsim_delete_and_reopen
421 db func a_string a_string
423 ATTACH 'test.db2' AS aux;
424 PRAGMA journal_mode = DELETE;
425 PRAGMA main.cache_size = 10;
426 PRAGMA aux.cache_size = 10;
427 CREATE TABLE t1(a UNIQUE, b UNIQUE);
428 CREATE TABLE aux.t2(a UNIQUE, b UNIQUE);
429 INSERT INTO t1 VALUES(a_string(200), a_string(300));
430 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
431 INSERT INTO t1 SELECT a_string(200), a_string(300) FROM t1;
432 INSERT INTO t2 SELECT * FROM t1;
434 INSERT INTO t1 SELECT a_string(201), a_string(301) FROM t1;
435 INSERT INTO t1 SELECT a_string(202), a_string(302) FROM t1;
436 INSERT INTO t1 SELECT a_string(203), a_string(303) FROM t1;
437 INSERT INTO t1 SELECT a_string(204), a_string(304) FROM t1;
438 REPLACE INTO t2 SELECT * FROM t1;
444 do_test pager1.4.2.2 {
445 faultsim_restore_and_reopen
447 SELECT count(*) FROM t1;
448 PRAGMA integrity_check;
451 do_test pager1.4.2.3 {
452 faultsim_restore_and_reopen
453 foreach f [glob test.db-mj*] { file delete -force $f }
455 SELECT count(*) FROM t1;
456 PRAGMA integrity_check;
459 do_test pager1.4.2.4 {
460 faultsim_restore_and_reopen
461 hexio_write test.db-journal [expr [file size test.db-journal]-20] 123456
463 SELECT count(*) FROM t1;
464 PRAGMA integrity_check;
467 do_test pager1.4.2.5 {
468 faultsim_restore_and_reopen
469 hexio_write test.db-journal [expr [file size test.db-journal]-20] 123456
470 foreach f [glob test.db-mj*] { file delete -force $f }
472 SELECT count(*) FROM t1;
473 PRAGMA integrity_check;
477 do_test pager1.4.3.1 {
478 testvfs tstvfs -default 1
480 tstvfs script xSyncCallback
481 proc xSyncCallback {method file args} {
482 set file [file tail $file]
483 if { 0==[string match *journal $file] } { faultsim_save }
485 faultsim_delete_and_reopen
487 PRAGMA journal_mode = DELETE;
488 CREATE TABLE t1(a, b);
489 INSERT INTO t1 VALUES(1, 2);
490 INSERT INTO t1 VALUES(3, 4);
496 foreach {tn ofst value result} {
501 6 20 131072 {1 2 3 4}
505 9 24 131072 {1 2 3 4}
509 do_test pager1.4.3.$tn {
510 faultsim_restore_and_reopen
511 hexio_write test.db-journal $ofst [format %.8x $value]
512 execsql { SELECT * FROM t1 }
517 # Set up a VFS that snapshots the file-system just before a master journal
518 # file is deleted to commit a multi-file transaction. Specifically, the
519 # file-system is saved just before the xDelete() call to remove the
520 # master journal file from the file-system.
522 testvfs tv -default 1
523 tv script copy_on_mj_delete
524 set ::mj_filename_length 0
525 proc copy_on_mj_delete {method filename args} {
526 if {[string match *mj* [file tail $filename]]} {
527 set ::mj_filename_length [string length $filename]
535 1 { set prefix "test.db" }
537 # This test depends on the underlying VFS being able to open paths
538 # 512 bytes in length. The idea is to create a hot-journal file that
539 # contains a master-journal pointer so large that it could contain
540 # a valid page record (if the file page-size is 512 bytes). So as to
541 # make sure SQLite doesn't get confused by this.
543 set nPadding [expr 511 - $::mj_filename_length]
544 if {$tcl_platform(platform)=="windows"} {
545 # TBD need to figure out how to do this correctly for Windows!!!
546 set nPadding [expr 255 - $::mj_filename_length]
549 # We cannot just create a really long database file name to open, as
550 # Linux limits a single component of a path to 255 bytes by default
551 # (and presumably other systems have limits too). So create a directory
552 # hierarchy to work in.
554 set dirname "d123456789012345678901234567890/"
555 set nDir [expr $nPadding / 32]
557 set p [string repeat $dirname $nDir]
562 set padding [string repeat x [expr $nPadding %32]]
563 set prefix "test.db${padding}"
569 PRAGMA main.synchronous=OFF;
570 PRAGMA aux.synchronous=OFF;
571 PRAGMA journal_mode = DELETE;
574 PRAGMA main.synchronous=OFF;
575 PRAGMA aux.synchronous=OFF;
576 PRAGMA main.page_size = 512;
577 PRAGMA aux.page_size = 512;
578 PRAGMA journal_mode = DELETE;
581 PRAGMA main.synchronous=NORMAL;
582 PRAGMA aux.synchronous=NORMAL;
583 PRAGMA journal_mode = DELETE;
586 PRAGMA main.synchronous=FULL;
587 PRAGMA aux.synchronous=FULL;
588 PRAGMA journal_mode = DELETE;
592 set tn "${tn1}.${tn2}"
594 # Set up a connection to have two databases, test.db (main) and
595 # test.db2 (aux). Then run a multi-file transaction on them. The
596 # VFS will snapshot the file-system just before the master-journal
597 # file is deleted to commit the transaction.
600 do_test pager1-4.4.$tn.1 {
601 faultsim_delete_and_reopen $prefix
603 ATTACH '${prefix}2' AS aux;
606 CREATE TABLE aux.b(x);
607 INSERT INTO a VALUES('double-you');
608 INSERT INTO a VALUES('why');
609 INSERT INTO a VALUES('zed');
610 INSERT INTO b VALUES('won');
611 INSERT INTO b VALUES('too');
612 INSERT INTO b VALUES('free');
616 INSERT INTO a SELECT * FROM b WHERE rowid<=3;
617 INSERT INTO b SELECT * FROM a WHERE rowid<=3;
623 # Check that the transaction was committed successfully.
625 do_execsql_test pager1-4.4.$tn.2 {
627 } {double-you why zed won too free}
628 do_execsql_test pager1-4.4.$tn.3 {
630 } {won too free double-you why zed}
632 # Restore the file-system and reopen the databases. Check that it now
633 # appears that the transaction was not committed (because the file-system
634 # was restored to the state where it had not been).
636 do_test pager1-4.4.$tn.4 {
637 faultsim_restore_and_reopen $prefix
638 execsql "ATTACH '${prefix}2' AS aux"
640 do_execsql_test pager1-4.4.$tn.5 {SELECT * FROM a} {double-you why zed}
641 do_execsql_test pager1-4.4.$tn.6 {SELECT * FROM b} {won too free}
643 # Restore the file-system again. This time, before reopening the databases,
644 # delete the master-journal file from the file-system. It now appears that
645 # the transaction was committed (no master-journal file == no rollback).
647 do_test pager1-4.4.$tn.7 {
648 faultsim_restore_and_reopen $prefix
649 foreach f [glob ${prefix}-mj*] { file delete -force $f }
650 execsql "ATTACH '${prefix}2' AS aux"
652 do_execsql_test pager1-4.4.$tn.8 {
654 } {double-you why zed won too free}
655 do_execsql_test pager1-4.4.$tn.9 {
657 } {won too free double-you why zed}
664 file delete -force $dirname
667 # Set up a VFS to make a copy of the file-system just before deleting a
668 # journal file to commit a transaction. The transaction modifies exactly
669 # two database pages (and page 1 - the change counter).
671 testvfs tv -default 1
673 tv script copy_on_journal_delete
675 proc copy_on_journal_delete {method filename args} {
676 if {[string match *journal $filename]} faultsim_save
679 faultsim_delete_and_reopen
680 do_execsql_test pager1.4.5.1 {
681 PRAGMA journal_mode = DELETE;
682 PRAGMA page_size = 1024;
683 CREATE TABLE t1(a, b);
684 CREATE TABLE t2(a, b);
685 INSERT INTO t1 VALUES('I', 'II');
686 INSERT INTO t2 VALUES('III', 'IV');
688 INSERT INTO t1 VALUES(1, 2);
689 INSERT INTO t2 VALUES(3, 4);
694 # Check the transaction was committed:
696 do_execsql_test pager1.4.5.2 {
699 } {I II 1 2 III IV 3 4}
701 # Now try four tests:
703 # pager1-4.5.3: Restore the file-system. Check that the whole transaction
706 # pager1-4.5.4: Restore the file-system. Corrupt the first record in the
707 # journal. Check the transaction is not rolled back.
709 # pager1-4.5.5: Restore the file-system. Corrupt the second record in the
710 # journal. Check that the first record in the transaction is
711 # played back, but not the second.
713 # pager1-4.5.6: Restore the file-system. Try to open the database with a
714 # readonly connection. This should fail, as a read-only
715 # connection cannot roll back the database file.
717 faultsim_restore_and_reopen
718 do_execsql_test pager1.4.5.3 {
722 faultsim_restore_and_reopen
723 hexio_write test.db-journal [expr 512+4+1024 - 202] 0123456789ABCDEF
724 do_execsql_test pager1.4.5.4 {
727 } {I II 1 2 III IV 3 4}
728 faultsim_restore_and_reopen
729 hexio_write test.db-journal [expr 512+4+1024+4+4+1024 - 202] 0123456789ABCDEF
730 do_execsql_test pager1.4.5.5 {
735 faultsim_restore_and_reopen
737 sqlite3 db test.db -readonly 1
738 do_catchsql_test pager1.4.5.6 {
741 } {1 {disk I/O error}}
744 # Snapshot the file-system just before multi-file commit. Save the name
745 # of the master journal file in $::mj_filename.
747 tv script copy_on_mj_delete
749 proc copy_on_mj_delete {method filename args} {
750 if {[string match *mj* [file tail $filename]]} {
751 set ::mj_filename $filename
756 do_test pager1.4.6.1 {
757 faultsim_delete_and_reopen
759 PRAGMA journal_mode = DELETE;
760 ATTACH 'test.db2' AS two;
761 CREATE TABLE t1(a, b);
762 CREATE TABLE two.t2(a, b);
763 INSERT INTO t1 VALUES(1, 't1.1');
764 INSERT INTO t2 VALUES(1, 't2.1');
766 UPDATE t1 SET b = 't1.2';
767 UPDATE t2 SET b = 't2.2';
774 faultsim_restore_and_reopen
775 do_execsql_test pager1.4.6.2 { SELECT * FROM t1 } {1 t1.1}
776 do_test pager1.4.6.3 { file exists $::mj_filename } {1}
777 do_execsql_test pager1.4.6.4 {
778 ATTACH 'test.db2' AS two;
781 do_test pager1.4.6.5 { file exists $::mj_filename } {0}
783 faultsim_restore_and_reopen
785 do_test pager1.4.6.8 {
786 set ::mj_filename1 $::mj_filename
790 PRAGMA journal_mode = DELETE;
791 ATTACH 'test.db3' AS three;
792 CREATE TABLE three.t3(a, b);
793 INSERT INTO t3 VALUES(1, 't3.1');
795 UPDATE t2 SET b = 't2.3';
796 UPDATE t3 SET b = 't3.3';
799 expr {$::mj_filename1 != $::mj_filename}
801 faultsim_restore_and_reopen
804 # The file-system now contains:
807 # * three hot-journal files
808 # * two master-journal files.
810 # The hot-journals associated with test.db2 and test.db3 point to
811 # master journal $::mj_filename. The hot-journal file associated with
812 # test.db points to master journal $::mj_filename1. So reading from
813 # test.db should delete $::mj_filename1.
815 do_test pager1.4.6.9 {
816 lsort [glob test.db*]
818 test.db test.db2 test.db3 \
819 test.db-journal test.db2-journal test.db3-journal \
820 [file tail $::mj_filename] [file tail $::mj_filename1]
823 # The master-journal $::mj_filename1 contains pointers to test.db and
824 # test.db2. However the hot-journal associated with test.db2 points to
825 # a different master-journal. Therefore, reading from test.db only should
826 # be enough to cause SQLite to delete $::mj_filename1.
828 do_test pager1.4.6.10 { file exists $::mj_filename } {1}
829 do_test pager1.4.6.11 { file exists $::mj_filename1 } {1}
830 do_execsql_test pager1.4.6.12 { SELECT * FROM t1 } {1 t1.1}
831 do_test pager1.4.6.13 { file exists $::mj_filename } {1}
832 do_test pager1.4.6.14 { file exists $::mj_filename1 } {0}
834 do_execsql_test pager1.4.6.12 {
835 ATTACH 'test.db2' AS two;
838 do_test pager1.4.6.13 { file exists $::mj_filename } {1}
839 do_execsql_test pager1.4.6.14 {
840 ATTACH 'test.db3' AS three;
843 do_test pager1.4.6.15 { file exists $::mj_filename } {0}
848 testvfs tv -default 1
850 tv script copy_on_journal_delete
852 proc copy_on_journal_delete {method filename args} {
853 if {[string match *journal $filename]} faultsim_save
856 faultsim_delete_and_reopen
857 do_execsql_test pager1.4.7.1 {
858 PRAGMA journal_mode = DELETE;
859 CREATE TABLE t1(x PRIMARY KEY, y);
860 CREATE INDEX i1 ON t1(y);
861 INSERT INTO t1 VALUES('I', 'one');
862 INSERT INTO t1 VALUES('II', 'four');
863 INSERT INTO t1 VALUES('III', 'nine');
865 INSERT INTO t1 VALUES('IV', 'sixteen');
866 INSERT INTO t1 VALUES('V' , 'twentyfive');
872 do_test pager1.4.7.2 {
873 faultsim_restore_and_reopen
874 catch {file attributes test.db-journal -permissions r--------}
875 catch {file attributes test.db-journal -readonly 1}
876 catchsql { SELECT * FROM t1 }
877 } {1 {unable to open database file}}
878 do_test pager1.4.7.3 {
880 catch {file attributes test.db-journal -permissions rw-rw-rw-}
881 catch {file attributes test.db-journal -readonly 0}
882 file delete test.db-journal
883 file exists test.db-journal
886 #-------------------------------------------------------------------------
887 # The following tests deal with multi-file commits.
889 # pager1-5.1.*: The case where a multi-file cannot be committed because
890 # another connection is holding a SHARED lock on one of the
891 # files. After the SHARED lock is removed, the COMMIT succeeds.
893 # pager1-5.2.*: Multi-file commits with journal_mode=memory.
895 # pager1-5.3.*: Multi-file commits with journal_mode=memory.
897 # pager1-5.4.*: Check that with synchronous=normal, the master-journal file
898 # name is added to a journal file immediately after the last
899 # journal record. But with synchronous=full, extra unused space
900 # is allocated between the last journal record and the
901 # master-journal file name so that the master-journal file
902 # name does not lie on the same sector as the last journal file
905 # pager1-5.5.*: Check that in journal_mode=PERSIST mode, a journal file is
906 # truncated to zero bytes when a multi-file transaction is
907 # committed (instead of the first couple of bytes being zeroed).
910 do_test pager1-5.1.1 {
911 faultsim_delete_and_reopen
913 ATTACH 'test.db2' AS aux;
914 CREATE TABLE t1(a, b);
915 CREATE TABLE aux.t2(a, b);
916 INSERT INTO t1 VALUES(17, 'Lenin');
917 INSERT INTO t1 VALUES(22, 'Stalin');
918 INSERT INTO t1 VALUES(53, 'Khrushchev');
921 do_test pager1-5.1.2 {
924 INSERT INTO t1 VALUES(64, 'Brezhnev');
925 INSERT INTO t2 SELECT * FROM t1;
933 do_test pager1-5.1.3 {
935 } {1 {database is locked}}
936 do_test pager1-5.1.4 {
939 execsql { SELECT * FROM t2 } db2
940 } {17 Lenin 22 Stalin 53 Khrushchev 64 Brezhnev}
941 do_test pager1-5.1.5 {
945 do_test pager1-5.2.1 {
947 PRAGMA journal_mode = memory;
949 INSERT INTO t1 VALUES(84, 'Andropov');
950 INSERT INTO t2 VALUES(84, 'Andropov');
954 do_test pager1-5.3.1 {
956 PRAGMA journal_mode = off;
958 INSERT INTO t1 VALUES(85, 'Gorbachev');
959 INSERT INTO t2 VALUES(85, 'Gorbachev');
964 do_test pager1-5.4.1 {
967 sqlite3 db test.db -vfs tv
968 execsql { ATTACH 'test.db2' AS aux }
971 tv script max_journal_size
974 proc max_journal_size {method args} {
976 catch { set sz [file size test.db-journal] }
977 if {$sz > $::max_journal} {
978 set ::max_journal $sz
983 PRAGMA journal_mode = DELETE;
984 PRAGMA synchronous = NORMAL;
986 INSERT INTO t1 VALUES(85, 'Gorbachev');
987 INSERT INTO t2 VALUES(85, 'Gorbachev');
991 } [expr 2615+[string length [pwd]]]
992 do_test pager1-5.4.2 {
995 PRAGMA synchronous = full;
997 DELETE FROM t1 WHERE b = 'Lenin';
998 DELETE FROM t2 WHERE b = 'Lenin';
1002 } [expr 3111+[string length [pwd]]]
1006 do_test pager1-5.5.1 {
1009 ATTACH 'test.db2' AS aux;
1010 PRAGMA journal_mode = PERSIST;
1011 CREATE TABLE t3(a, b);
1012 INSERT INTO t3 SELECT randomblob(1500), randomblob(1500) FROM t1;
1013 UPDATE t3 SET b = randomblob(1500);
1015 expr [file size test.db-journal] > 15000
1017 do_test pager1-5.5.2 {
1019 PRAGMA synchronous = full;
1021 DELETE FROM t1 WHERE b = 'Stalin';
1022 DELETE FROM t2 WHERE b = 'Stalin';
1025 file size test.db-journal
1029 #-------------------------------------------------------------------------
1030 # The following tests work with "PRAGMA max_page_count"
1032 do_test pager1-6.1 {
1033 faultsim_delete_and_reopen
1035 PRAGMA auto_vacuum = none;
1036 PRAGMA max_page_count = 10;
1037 CREATE TABLE t2(a, b);
1038 CREATE TABLE t3(a, b);
1039 CREATE TABLE t4(a, b);
1040 CREATE TABLE t5(a, b);
1041 CREATE TABLE t6(a, b);
1042 CREATE TABLE t7(a, b);
1043 CREATE TABLE t8(a, b);
1044 CREATE TABLE t9(a, b);
1045 CREATE TABLE t10(a, b);
1048 do_catchsql_test pager1-6.2 {
1049 CREATE TABLE t11(a, b)
1050 } {1 {database or disk is full}}
1051 do_execsql_test pager1-6.4 { PRAGMA max_page_count } {10}
1052 do_execsql_test pager1-6.5 { PRAGMA max_page_count = 15 } {15}
1053 do_execsql_test pager1-6.6 { CREATE TABLE t11(a, b) } {}
1054 do_execsql_test pager1-6.7 {
1056 INSERT INTO t11 VALUES(1, 2);
1057 PRAGMA max_page_count = 13;
1059 do_execsql_test pager1-6.8 {
1060 INSERT INTO t11 VALUES(3, 4);
1061 PRAGMA max_page_count = 10;
1063 do_execsql_test pager1-6.9 { COMMIT } {}
1065 do_execsql_test pager1-6.10 { PRAGMA max_page_count = 10 } {11}
1066 do_execsql_test pager1-6.11 { SELECT * FROM t11 } {1 2 3 4}
1067 do_execsql_test pager1-6.12 { PRAGMA max_page_count } {11}
1070 #-------------------------------------------------------------------------
1071 # The following tests work with "PRAGMA journal_mode=TRUNCATE" and
1072 # "PRAGMA locking_mode=EXCLUSIVE".
1074 # Each test is specified with 5 variables. As follows:
1076 # $tn: Test Number. Used as part of the [do_test] test names.
1077 # $sql: SQL to execute.
1078 # $res: Expected result of executing $sql.
1079 # $js: The expected size of the journal file, in bytes, after executing
1080 # the SQL script. Or -1 if the journal is not expected to exist.
1081 # $ws: The expected size of the WAL file, in bytes, after executing
1082 # the SQL script. Or -1 if the WAL is not expected to exist.
1085 faultsim_delete_and_reopen
1086 foreach {tn sql res js ws} [subst {
1089 CREATE TABLE t1(a, b);
1090 PRAGMA auto_vacuum=OFF;
1091 PRAGMA synchronous=NORMAL;
1092 PRAGMA page_size=1024;
1093 PRAGMA locking_mode=EXCLUSIVE;
1094 PRAGMA journal_mode=TRUNCATE;
1095 INSERT INTO t1 VALUES(1, 2);
1096 } {exclusive truncate} 0 -1
1110 4 { PRAGMA journal_mode = WAL } wal -1 -1
1111 5 { INSERT INTO t1 VALUES(3, 4) } {} -1 [wal_file_size 1 1024]
1112 6 { PRAGMA locking_mode = NORMAL } exclusive -1 [wal_file_size 1 1024]
1113 7 { INSERT INTO t1 VALUES(5, 6); } {} -1 [wal_file_size 2 1024]
1115 8 { PRAGMA journal_mode = TRUNCATE } truncate 0 -1
1116 9 { INSERT INTO t1 VALUES(7, 8) } {} 0 -1
1117 10 { SELECT * FROM t1 } {1 2 3 4 5 6 7 8} 0 -1
1120 do_execsql_test pager1-7.1.$tn.1 $sql $res
1121 catch { set J -1 ; set J [file size test.db-journal] }
1122 catch { set W -1 ; set W [file size test.db-wal] }
1123 do_test pager1-7.1.$tn.2 { list $J $W } [list $js $ws]
1127 do_test pager1-7.2.1 {
1128 faultsim_delete_and_reopen
1130 PRAGMA locking_mode = EXCLUSIVE;
1131 CREATE TABLE t1(a, b);
1133 PRAGMA journal_mode = delete;
1134 PRAGMA journal_mode = truncate;
1136 } {exclusive delete truncate}
1137 do_test pager1-7.2.2 {
1138 execsql { INSERT INTO t1 VALUES(1, 2) }
1139 execsql { PRAGMA journal_mode = persist }
1141 do_test pager1-7.2.3 {
1144 PRAGMA journal_mode = persist;
1145 PRAGMA journal_size_limit;
1149 #-------------------------------------------------------------------------
1150 # The following tests, pager1-8.*, test that the special filenames
1151 # ":memory:" and "" open temporary databases.
1153 foreach {tn filename} {
1157 do_test pager1-8.$tn.1 {
1158 faultsim_delete_and_reopen
1160 sqlite3 db $filename
1162 PRAGMA auto_vacuum = 1;
1164 INSERT INTO x1 VALUES('Charles');
1165 INSERT INTO x1 VALUES('James');
1166 INSERT INTO x1 VALUES('Mary');
1169 } {Charles James Mary}
1171 do_test pager1-8.$tn.2 {
1172 sqlite3 db2 $filename
1173 catchsql { SELECT * FROM x1 } db2
1174 } {1 {no such table: x1}}
1176 do_execsql_test pager1-8.$tn.3 {
1178 INSERT INTO x1 VALUES('William');
1179 INSERT INTO x1 VALUES('Anne');
1184 #-------------------------------------------------------------------------
1185 # The next block of tests - pager1-9.* - deal with interactions between
1186 # the pager and the backup API. Test cases:
1188 # pager1-9.1.*: Test that a backup completes successfully even if the
1189 # source db is written to during the backup op.
1191 # pager1-9.2.*: Test that a backup completes successfully even if the
1192 # source db is written to and then rolled back during a
1195 do_test pager1-9.0.1 {
1196 faultsim_delete_and_reopen
1197 db func a_string a_string
1199 PRAGMA cache_size = 10;
1201 CREATE TABLE ab(a, b, UNIQUE(a, b));
1202 INSERT INTO ab VALUES( a_string(200), a_string(300) );
1203 INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
1204 INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
1205 INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
1206 INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
1207 INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
1208 INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
1209 INSERT INTO ab SELECT a_string(200), a_string(300) FROM ab;
1213 do_test pager1-9.0.2 {
1214 sqlite3 db2 test.db2
1215 db2 eval { PRAGMA cache_size = 10 }
1216 sqlite3_backup B db2 main db main
1217 list [B step 10000] [B finish]
1218 } {SQLITE_DONE SQLITE_OK}
1219 do_test pager1-9.0.3 {
1220 db one {SELECT md5sum(a, b) FROM ab}
1221 } [db2 one {SELECT md5sum(a, b) FROM ab}]
1223 do_test pager1-9.1.1 {
1224 execsql { UPDATE ab SET a = a_string(201) }
1225 sqlite3_backup B db2 main db main
1228 do_test pager1-9.1.2 {
1229 execsql { UPDATE ab SET b = a_string(301) }
1230 list [B step 10000] [B finish]
1231 } {SQLITE_DONE SQLITE_OK}
1232 do_test pager1-9.1.3 {
1233 db one {SELECT md5sum(a, b) FROM ab}
1234 } [db2 one {SELECT md5sum(a, b) FROM ab}]
1235 do_test pager1-9.1.4 { execsql { SELECT count(*) FROM ab } } {128}
1237 do_test pager1-9.2.1 {
1238 execsql { UPDATE ab SET a = a_string(202) }
1239 sqlite3_backup B db2 main db main
1242 do_test pager1-9.2.2 {
1245 UPDATE ab SET b = a_string(301);
1248 list [B step 10000] [B finish]
1249 } {SQLITE_DONE SQLITE_OK}
1250 do_test pager1-9.2.3 {
1251 db one {SELECT md5sum(a, b) FROM ab}
1252 } [db2 one {SELECT md5sum(a, b) FROM ab}]
1253 do_test pager1-9.2.4 { execsql { SELECT count(*) FROM ab } } {128}
1257 do_test pager1-9.3.1 {
1258 testvfs tv -default 1
1260 faultsim_delete_and_reopen
1262 execsql { PRAGMA page_size = 1024 }
1263 for {set ii 0} {$ii < 4} {incr ii} { execsql "CREATE TABLE t${ii}(a, b)" }
1265 do_test pager1-9.3.2 {
1266 sqlite3 db2 test.db2
1269 PRAGMA page_size = 4096;
1270 PRAGMA synchronous = OFF;
1271 CREATE TABLE t1(a, b);
1272 CREATE TABLE t2(a, b);
1275 sqlite3_backup B db2 main db main
1277 list [B step 10000] [B finish]
1278 } {SQLITE_DONE SQLITE_OK}
1279 do_test pager1-9.3.3 {
1284 } [file size test.db]
1286 do_test pager1-9.4.1 {
1287 faultsim_delete_and_reopen
1288 sqlite3 db2 test.db2
1290 PRAGMA page_size = 4096;
1291 CREATE TABLE t1(a, b);
1292 CREATE TABLE t2(a, b);
1294 sqlite3_backup B db2 main db main
1295 list [B step 10000] [B finish]
1296 } {SQLITE_DONE SQLITE_OK}
1297 do_test pager1-9.4.2 {
1298 list [file size test.db2] [file size test.db]
1302 #-------------------------------------------------------------------------
1303 # Test that regardless of the value returned by xSectorSize(), the
1304 # minimum effective sector-size is 512 and the maximum 65536 bytes.
1306 testvfs tv -default 1
1307 foreach sectorsize {
1308 32 64 128 256 512 1024 2048
1309 4096 8192 16384 32768 65536 131072 262144
1311 tv sectorsize $sectorsize
1313 if {$sectorsize < 512} { set eff 512 }
1314 if {$sectorsize > 65536} { set eff 65536 }
1316 do_test pager1-10.$sectorsize.1 {
1317 faultsim_delete_and_reopen
1318 db func a_string a_string
1320 PRAGMA journal_mode = PERSIST;
1321 PRAGMA page_size = 1024;
1323 CREATE TABLE t1(a, b);
1324 CREATE TABLE t2(a, b);
1325 CREATE TABLE t3(a, b);
1328 file size test.db-journal
1329 } [expr $sectorsize > 65536 ? 65536 : $sectorsize]
1331 do_test pager1-10.$sectorsize.2 {
1333 INSERT INTO t3 VALUES(a_string(300), a_string(300));
1334 INSERT INTO t3 SELECT * FROM t3; /* 2 */
1335 INSERT INTO t3 SELECT * FROM t3; /* 4 */
1336 INSERT INTO t3 SELECT * FROM t3; /* 8 */
1337 INSERT INTO t3 SELECT * FROM t3; /* 16 */
1338 INSERT INTO t3 SELECT * FROM t3; /* 32 */
1342 do_test pager1-10.$sectorsize.3 {
1346 PRAGMA cache_size = 10;
1349 recursive_select 32 t3 {db eval "INSERT INTO t2 VALUES(1, 2)"}
1356 do_test pager1-10.$sectorsize.4 {
1358 CREATE TABLE t6(a, b);
1359 CREATE TABLE t7(a, b);
1360 CREATE TABLE t5(a, b);
1366 CREATE TABLE t6(a, b);
1368 recursive_select 32 t3 {db eval "INSERT INTO t5 VALUES(1, 2)"}
1379 do_test pager1.10.x.1 {
1380 faultsim_delete_and_reopen
1382 PRAGMA auto_vacuum = none;
1383 PRAGMA page_size = 1024;
1386 for {set i 0} {$i<30} {incr i} {
1387 execsql { INSERT INTO t1 VALUES(zeroblob(900)) }
1391 do_test pager1.10.x.2 {
1398 do_test pager1.10.x.3 {
1403 recursive_select 30 t1
1413 testvfs tv -default 1
1414 faultsim_delete_and_reopen
1415 db func a_string a_string
1416 do_execsql_test pager1-11.1 {
1417 PRAGMA journal_mode = DELETE;
1418 PRAGMA cache_size = 10;
1420 CREATE TABLE zz(top PRIMARY KEY);
1421 INSERT INTO zz VALUES(a_string(222));
1422 INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz;
1423 INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz;
1424 INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz;
1425 INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz;
1426 INSERT INTO zz SELECT a_string((SELECT 222+max(rowid) FROM zz)) FROM zz;
1429 UPDATE zz SET top = a_string(345);
1432 proc lockout {method args} { return SQLITE_IOERR }
1434 tv filter {xWrite xTruncate xSync}
1435 do_catchsql_test pager1-11.2 { COMMIT } {1 {disk I/O error}}
1438 do_test pager1-11.3 {
1441 PRAGMA journal_mode = TRUNCATE;
1442 PRAGMA integrity_check;
1445 do_test pager1-11.4 {
1447 file exists test.db-journal
1449 do_execsql_test pager1-11.5 { SELECT count(*) FROM zz } {32}
1453 #-------------------------------------------------------------------------
1454 # Test "PRAGMA page_size"
1456 testvfs tv -default 1
1459 512 1024 2048 4096 8192 16384 32768
1461 faultsim_delete_and_reopen
1463 # The sector-size (according to the VFS) is 1024 bytes. So if the
1464 # page-size requested using "PRAGMA page_size" is greater than the
1465 # compile time value of SQLITE_MAX_PAGE_SIZE, then the effective
1466 # page-size remains 1024 bytes.
1469 if {$eff > $::SQLITE_MAX_PAGE_SIZE} { set eff 1024 }
1471 do_test pager1-12.$pagesize.1 {
1474 PRAGMA page_size = $pagesize;
1475 CREATE VIEW v AS SELECT * FROM sqlite_master;
1479 do_test pager1-12.$pagesize.2 {
1482 SELECT count(*) FROM v;
1483 PRAGMA main.page_size;
1486 do_test pager1-12.$pagesize.3 {
1488 SELECT count(*) FROM v;
1489 PRAGMA main.page_size;
1497 #-------------------------------------------------------------------------
1498 # Test specal "PRAGMA journal_mode=PERSIST" test cases.
1500 # pager1-13.1.*: This tests a special case encountered in persistent
1501 # journal mode: If the journal associated with a transaction
1502 # is smaller than the journal file (because a previous
1503 # transaction left a very large non-hot journal file in the
1504 # file-system), then SQLite has to be careful that there is
1505 # not a journal-header left over from a previous transaction
1506 # immediately following the journal content just written.
1507 # If there is, and the process crashes so that the journal
1508 # becomes a hot-journal and must be rolled back by another
1509 # process, there is a danger that the other process may roll
1510 # back the aborted transaction, then continue copying data
1511 # from an older transaction from the remainder of the journal.
1512 # See the syncJournal() function for details.
1514 # pager1-13.2.*: Same test as the previous. This time, throw an index into
1515 # the mix to make the integrity-check more likely to catch
1518 testvfs tv -default 1
1521 proc xSyncCb {method filename args} {
1522 set t [file tail $filename]
1523 if {$t == "test.db"} faultsim_save
1526 faultsim_delete_and_reopen
1527 db func a_string a_string
1529 # The UPDATE statement at the end of this test case creates a really big
1530 # journal. Since the cache-size is only 10 pages, the journal contains
1531 # frequent journal headers.
1533 do_execsql_test pager1-13.1.1 {
1534 PRAGMA page_size = 1024;
1535 PRAGMA journal_mode = PERSIST;
1536 PRAGMA cache_size = 10;
1538 CREATE TABLE t1(a INTEGER PRIMARY KEY, b BLOB);
1539 INSERT INTO t1 VALUES(NULL, a_string(400));
1540 INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 2 */
1541 INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 4 */
1542 INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 8 */
1543 INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 16 */
1544 INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 32 */
1545 INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 64 */
1546 INSERT INTO t1 SELECT NULL, a_string(400) FROM t1; /* 128 */
1548 UPDATE t1 SET b = a_string(400);
1551 # Run transactions of increasing sizes. Eventually, one (or more than one)
1552 # of these will write just enough content that one of the old headers created
1553 # by the transaction in the block above lies immediately after the content
1554 # journalled by the current transaction.
1556 for {set nUp 1} {$nUp<64} {incr nUp} {
1557 do_execsql_test pager1-13.1.2.$nUp.1 {
1558 UPDATE t1 SET b = a_string(399) WHERE a <= $nUp
1560 do_execsql_test pager1-13.1.2.$nUp.2 { PRAGMA integrity_check } {ok}
1562 # Try to access the snapshot of the file-system.
1564 sqlite3 db2 sv_test.db
1565 do_test pager1-13.1.2.$nUp.3 {
1566 execsql { SELECT sum(length(b)) FROM t1 } db2
1567 } [expr {128*400 - ($nUp-1)}]
1568 do_test pager1-13.1.2.$nUp.4 {
1569 execsql { PRAGMA integrity_check } db2
1574 # Same test as above. But this time with an index on the table.
1576 do_execsql_test pager1-13.2.1 {
1577 CREATE INDEX i1 ON t1(b);
1578 UPDATE t1 SET b = a_string(400);
1580 for {set nUp 1} {$nUp<64} {incr nUp} {
1581 do_execsql_test pager1-13.2.2.$nUp.1 {
1582 UPDATE t1 SET b = a_string(399) WHERE a <= $nUp
1584 do_execsql_test pager1-13.2.2.$nUp.2 { PRAGMA integrity_check } {ok}
1585 sqlite3 db2 sv_test.db
1586 do_test pager1-13.2.2.$nUp.3 {
1587 execsql { SELECT sum(length(b)) FROM t1 } db2
1588 } [expr {128*400 - ($nUp-1)}]
1589 do_test pager1-13.2.2.$nUp.4 {
1590 execsql { PRAGMA integrity_check } db2
1598 #-------------------------------------------------------------------------
1599 # Test specal "PRAGMA journal_mode=OFF" test cases.
1601 faultsim_delete_and_reopen
1602 do_execsql_test pager1-14.1.1 {
1603 PRAGMA journal_mode = OFF;
1604 CREATE TABLE t1(a, b);
1606 INSERT INTO t1 VALUES(1, 2);
1610 do_catchsql_test pager1-14.1.2 {
1612 INSERT INTO t1 VALUES(3, 4);
1615 do_execsql_test pager1-14.1.3 {
1618 do_catchsql_test pager1-14.1.4 {
1620 INSERT INTO t1(rowid, a, b) SELECT a+3, b, b FROM t1;
1621 INSERT INTO t1(rowid, a, b) SELECT a+3, b, b FROM t1;
1622 } {1 {PRIMARY KEY must be unique}}
1623 do_execsql_test pager1-14.1.5 {
1628 #-------------------------------------------------------------------------
1629 # Test opening and closing the pager sub-system with different values
1630 # for the sqlite3_vfs.szOsFile variable.
1632 faultsim_delete_and_reopen
1633 do_execsql_test pager1-15.0 {
1634 CREATE TABLE tx(y, z);
1635 INSERT INTO tx VALUES('Ayutthaya', 'Beijing');
1636 INSERT INTO tx VALUES('London', 'Tokyo');
1639 for {set i 0} {$i<513} {incr i 3} {
1640 testvfs tv -default 1 -szosfile $i
1642 do_execsql_test pager1-15.$i.1 {
1644 } {Ayutthaya Beijing London Tokyo}
1649 #-------------------------------------------------------------------------
1650 # Check that it is not possible to open a database file if the full path
1651 # to the associated journal file will be longer than sqlite3_vfs.mxPathname.
1653 testvfs tv -default 1
1656 proc xOpenCb {method filename} {
1657 set ::file_len [string length $filename]
1663 for {set ii [expr $::file_len-5]} {$ii < [expr $::file_len+20]} {incr ii} {
1664 testvfs tv -default 1 -mxpathname $ii
1666 # The length of the full path to file "test.db-journal" is ($::file_len+8).
1667 # If the configured sqlite3_vfs.mxPathname value greater than or equal to
1668 # this, then the file can be opened. Otherwise, it cannot.
1670 if {$ii >= [expr $::file_len+8]} {
1673 set res {1 {unable to open database file}}
1676 do_test pager1-16.1.$ii {
1677 list [catch { sqlite3 db test.db } msg] $msg
1684 #-------------------------------------------------------------------------
1685 # Test "PRAGMA omit_readlock".
1687 # pager1-17.$tn.1.*: Test that if a second connection has an open
1688 # read-transaction, it is not usually possible to write
1691 # pager1-17.$tn.2.*: Test that if the second connection was opened with
1692 # the SQLITE_OPEN_READONLY flag, and
1693 # "PRAGMA omit_readlock = 1" is executed before attaching
1694 # the database and opening a read-transaction on it, it is
1695 # possible to write the db.
1697 # pager1-17.$tn.3.*: Test that if the second connection was *not* opened with
1698 # the SQLITE_OPEN_READONLY flag, executing
1699 # "PRAGMA omit_readlock = 1" has no effect.
1701 do_multiclient_test tn {
1702 do_test pager1-17.$tn.1.1 {
1704 CREATE TABLE t1(a, b);
1705 INSERT INTO t1 VALUES(1, 2);
1712 do_test pager1-17.$tn.1.2 {
1713 csql1 { INSERT INTO t1 VALUES(3, 4) }
1714 } {1 {database is locked}}
1715 do_test pager1-17.$tn.1.3 {
1717 sql1 { INSERT INTO t1 VALUES(3, 4) }
1720 do_test pager1-17.$tn.2.1 {
1723 sqlite3 db2 :memory: -readonly 1
1726 PRAGMA omit_readlock = 1;
1727 ATTACH 'test.db' AS two;
1732 do_test pager1-17.$tn.2.2 { sql1 "INSERT INTO t1 VALUES(5, 6)" } {}
1733 do_test pager1-17.$tn.2.3 { sql2 "SELECT * FROM t1" } {1 2 3 4}
1734 do_test pager1-17.$tn.2.4 { sql2 "COMMIT ; SELECT * FROM t1" } {1 2 3 4 5 6}
1736 do_test pager1-17.$tn.3.1 {
1739 sqlite3 db2 :memory:
1742 PRAGMA omit_readlock = 1;
1743 ATTACH 'test.db' AS two;
1748 do_test pager1-17.$tn.3.2 {
1749 csql1 { INSERT INTO t1 VALUES(3, 4) }
1750 } {1 {database is locked}}
1751 do_test pager1-17.$tn.3.3 { sql2 COMMIT } {}
1754 #-------------------------------------------------------------------------
1755 # Test the pagers response to the b-tree layer requesting illegal page
1758 # + The locking page,
1760 # + A page with a page number greater than (2^31-1).
1762 do_test pager1-18.1 {
1763 faultsim_delete_and_reopen
1764 db func a_string a_string
1766 PRAGMA page_size = 1024;
1767 CREATE TABLE t1(a, b);
1768 INSERT INTO t1 VALUES(a_string(500), a_string(200));
1769 INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
1770 INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
1771 INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
1772 INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
1773 INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
1774 INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
1775 INSERT INTO t1 SELECT a_string(500), a_string(200) FROM t1;
1778 do_test pager1-18.2 {
1779 set root [db one "SELECT rootpage FROM sqlite_master"]
1780 set lockingpage [expr (0x10000/1024) + 1]
1782 PRAGMA writable_schema = 1;
1783 UPDATE sqlite_master SET rootpage = $lockingpage;
1786 catchsql { SELECT count(*) FROM t1 } db2
1787 } {1 {database disk image is malformed}}
1789 do_test pager1-18.3 {
1792 INSERT INTO t2 VALUES(a_string(5000));
1794 set pgno [expr ([file size test.db] / 1024)-2]
1795 hexio_write test.db [expr ($pgno-1)*1024] 00000000
1797 catchsql { SELECT length(x) FROM t2 } db2
1798 } {1 {database disk image is malformed}}
1800 do_test pager1-18.4 {
1801 hexio_write test.db [expr ($pgno-1)*1024] 90000000
1803 catchsql { SELECT length(x) FROM t2 } db2
1804 } {1 {database disk image is malformed}}
1806 do_test pager1-18.5 {
1809 CREATE TABLE t1(a, b);
1810 CREATE TABLE t2(a, b);
1811 PRAGMA writable_schema = 1;
1812 UPDATE sqlite_master SET rootpage=5 WHERE tbl_name = 't1';
1813 PRAGMA writable_schema = 0;
1814 ALTER TABLE t1 RENAME TO x1;
1816 catchsql { SELECT * FROM x1 }
1817 } {1 {database disk image is malformed}}
1820 do_test pager1-18.6 {
1821 faultsim_delete_and_reopen
1822 db func a_string a_string
1824 PRAGMA page_size = 1024;
1826 INSERT INTO t1 VALUES(a_string(800));
1827 INSERT INTO t1 VALUES(a_string(800));
1830 set root [db one "SELECT rootpage FROM sqlite_master"]
1833 hexio_write test.db [expr ($root-1)*1024 + 8] 00000000
1835 catchsql { SELECT length(x) FROM t1 }
1836 } {1 {database disk image is malformed}}
1838 do_test pager1-19.1 {
1840 db func a_string a_string
1842 PRAGMA page_size = 512;
1843 PRAGMA auto_vacuum = 1;
1844 CREATE TABLE t1(aa, ab, ac, ad, ae, af, ag, ah, ai, aj, ak, al, am, an,
1845 ba, bb, bc, bd, be, bf, bg, bh, bi, bj, bk, bl, bm, bn,
1846 ca, cb, cc, cd, ce, cf, cg, ch, ci, cj, ck, cl, cm, cn,
1847 da, db, dc, dd, de, df, dg, dh, di, dj, dk, dl, dm, dn,
1848 ea, eb, ec, ed, ee, ef, eg, eh, ei, ej, ek, el, em, en,
1849 fa, fb, fc, fd, fe, ff, fg, fh, fi, fj, fk, fl, fm, fn,
1850 ga, gb, gc, gd, ge, gf, gg, gh, gi, gj, gk, gl, gm, gn,
1851 ha, hb, hc, hd, he, hf, hg, hh, hi, hj, hk, hl, hm, hn,
1852 ia, ib, ic, id, ie, if, ig, ih, ii, ij, ik, il, im, ix,
1853 ja, jb, jc, jd, je, jf, jg, jh, ji, jj, jk, jl, jm, jn,
1854 ka, kb, kc, kd, ke, kf, kg, kh, ki, kj, kk, kl, km, kn,
1855 la, lb, lc, ld, le, lf, lg, lh, li, lj, lk, ll, lm, ln,
1856 ma, mb, mc, md, me, mf, mg, mh, mi, mj, mk, ml, mm, mn
1858 CREATE TABLE t2(aa, ab, ac, ad, ae, af, ag, ah, ai, aj, ak, al, am, an,
1859 ba, bb, bc, bd, be, bf, bg, bh, bi, bj, bk, bl, bm, bn,
1860 ca, cb, cc, cd, ce, cf, cg, ch, ci, cj, ck, cl, cm, cn,
1861 da, db, dc, dd, de, df, dg, dh, di, dj, dk, dl, dm, dn,
1862 ea, eb, ec, ed, ee, ef, eg, eh, ei, ej, ek, el, em, en,
1863 fa, fb, fc, fd, fe, ff, fg, fh, fi, fj, fk, fl, fm, fn,
1864 ga, gb, gc, gd, ge, gf, gg, gh, gi, gj, gk, gl, gm, gn,
1865 ha, hb, hc, hd, he, hf, hg, hh, hi, hj, hk, hl, hm, hn,
1866 ia, ib, ic, id, ie, if, ig, ih, ii, ij, ik, il, im, ix,
1867 ja, jb, jc, jd, je, jf, jg, jh, ji, jj, jk, jl, jm, jn,
1868 ka, kb, kc, kd, ke, kf, kg, kh, ki, kj, kk, kl, km, kn,
1869 la, lb, lc, ld, le, lf, lg, lh, li, lj, lk, ll, lm, ln,
1870 ma, mb, mc, md, me, mf, mg, mh, mi, mj, mk, ml, mm, mn
1872 INSERT INTO t1(aa) VALUES( a_string(100000) );
1873 INSERT INTO t2(aa) VALUES( a_string(100000) );
1878 #-------------------------------------------------------------------------
1879 # Test a couple of special cases that come up while committing
1882 # pager1-20.1.*: Committing an in-memory database transaction when the
1883 # database has not been modified at all.
1885 # pager1-20.2.*: As above, but with a normal db in exclusive-locking mode.
1887 # pager1-20.3.*: Committing a transaction in WAL mode where the database has
1888 # been modified, but all dirty pages have been flushed to
1889 # disk before the commit.
1891 do_test pager1-20.1.1 {
1895 CREATE TABLE one(two, three);
1896 INSERT INTO one VALUES('a', 'b');
1899 do_test pager1-20.1.2 {
1906 do_test pager1-20.2.1 {
1907 faultsim_delete_and_reopen
1909 PRAGMA locking_mode = exclusive;
1910 PRAGMA journal_mode = persist;
1911 CREATE TABLE one(two, three);
1912 INSERT INTO one VALUES('a', 'b');
1914 } {exclusive persist}
1915 do_test pager1-20.2.2 {
1923 do_test pager1-20.3.1 {
1924 faultsim_delete_and_reopen
1925 db func a_string a_string
1927 PRAGMA cache_size = 10;
1928 PRAGMA journal_mode = wal;
1932 INSERT INTO t1 VALUES(a_string(800));
1933 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 2 */
1934 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 4 */
1935 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 8 */
1936 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 16 */
1937 INSERT INTO t1 SELECT a_string(800) FROM t1; /* 32 */
1941 do_test pager1-20.3.2 {
1944 INSERT INTO t2 VALUES('xxxx');
1946 recursive_select 32 t1
1951 #-------------------------------------------------------------------------
1952 # Test that a WAL database may not be opened if:
1954 # pager1-21.1.*: The VFS has an iVersion less than 2, or
1955 # pager1-21.2.*: The VFS does not provide xShmXXX() methods.
1958 do_test pager1-21.0 {
1959 faultsim_delete_and_reopen
1961 PRAGMA journal_mode = WAL;
1962 CREATE TABLE ko(c DEFAULT 'abc', b DEFAULT 'def');
1963 INSERT INTO ko DEFAULT VALUES;
1966 do_test pager1-21.1 {
1968 sqlite3 db2 test.db -vfs tv
1969 catchsql { SELECT * FROM ko } db2
1970 } {1 {unable to open database file}}
1973 do_test pager1-21.2 {
1974 testvfs tv -iversion 1
1975 sqlite3 db2 test.db -vfs tv
1976 catchsql { SELECT * FROM ko } db2
1977 } {1 {unable to open database file}}
1982 #-------------------------------------------------------------------------
1983 # Test that a "PRAGMA wal_checkpoint":
1985 # pager1-22.1.*: is a no-op on a non-WAL db, and
1986 # pager1-22.2.*: does not cause xSync calls with a synchronous=off db.
1988 do_test pager1-22.1.1 {
1989 faultsim_delete_and_reopen
1991 CREATE TABLE ko(c DEFAULT 'abc', b DEFAULT 'def');
1992 INSERT INTO ko DEFAULT VALUES;
1994 execsql { PRAGMA wal_checkpoint }
1996 do_test pager1-22.2.1 {
1997 testvfs tv -default 1
2000 proc xSyncCb {args} {incr ::synccount}
2004 PRAGMA synchronous = off;
2005 PRAGMA journal_mode = WAL;
2006 INSERT INTO ko DEFAULT VALUES;
2008 execsql { PRAGMA wal_checkpoint }
2014 #-------------------------------------------------------------------------
2015 # Tests for changing journal mode.
2017 # pager1-23.1.*: Test that when changing from PERSIST to DELETE mode,
2018 # the journal file is deleted.
2020 # pager1-23.2.*: Same test as above, but while a shared lock is held
2021 # on the database file.
2023 # pager1-23.3.*: Same test as above, but while a reserved lock is held
2024 # on the database file.
2026 # pager1-23.4.*: And, for fun, while holding an exclusive lock.
2028 # pager1-23.5.*: Try to set various different journal modes with an
2029 # in-memory database (only MEMORY and OFF should work).
2031 # pager1-23.6.*: Try to set locking_mode=normal on an in-memory database
2032 # (doesn't work - in-memory databases always use
2033 # locking_mode=exclusive).
2035 do_test pager1-23.1.1 {
2036 faultsim_delete_and_reopen
2038 PRAGMA journal_mode = PERSIST;
2039 CREATE TABLE t1(a, b);
2041 file exists test.db-journal
2043 do_test pager1-23.1.2 {
2044 execsql { PRAGMA journal_mode = DELETE }
2045 file exists test.db-journal
2048 do_test pager1-23.2.1 {
2050 PRAGMA journal_mode = PERSIST;
2051 INSERT INTO t1 VALUES('Canberra', 'ACT');
2053 db eval { SELECT * FROM t1 } {
2054 db eval { PRAGMA journal_mode = DELETE }
2056 execsql { PRAGMA journal_mode }
2058 do_test pager1-23.2.2 {
2059 file exists test.db-journal
2062 do_test pager1-23.3.1 {
2064 PRAGMA journal_mode = PERSIST;
2065 INSERT INTO t1 VALUES('Darwin', 'NT');
2068 db eval { PRAGMA journal_mode = DELETE }
2069 execsql { PRAGMA journal_mode }
2071 do_test pager1-23.3.2 {
2072 file exists test.db-journal
2074 do_test pager1-23.3.3 {
2078 do_test pager1-23.4.1 {
2080 PRAGMA journal_mode = PERSIST;
2081 INSERT INTO t1 VALUES('Adelaide', 'SA');
2084 db eval { PRAGMA journal_mode = DELETE }
2085 execsql { PRAGMA journal_mode }
2087 do_test pager1-23.4.2 {
2088 file exists test.db-journal
2090 do_test pager1-23.4.3 {
2094 do_test pager1-23.5.1 {
2095 faultsim_delete_and_reopen
2098 foreach {tn mode possible} {
2106 do_test pager1-23.5.$tn.1 {
2107 execsql "PRAGMA journal_mode = off"
2108 execsql "PRAGMA journal_mode = $mode"
2109 } [if $possible {list $mode} {list off}]
2110 do_test pager1-23.5.$tn.2 {
2111 execsql "PRAGMA journal_mode = memory"
2112 execsql "PRAGMA journal_mode = $mode"
2113 } [if $possible {list $mode} {list memory}]
2115 do_test pager1-23.6.1 {
2116 execsql {PRAGMA locking_mode = normal}
2118 do_test pager1-23.6.2 {
2119 execsql {PRAGMA locking_mode = exclusive}
2121 do_test pager1-23.6.3 {
2122 execsql {PRAGMA locking_mode}
2124 do_test pager1-23.6.4 {
2125 execsql {PRAGMA main.locking_mode}
2128 #-------------------------------------------------------------------------
2130 do_test pager1-24.1.1 {
2131 faultsim_delete_and_reopen
2132 db func a_string a_string
2134 PRAGMA cache_size = 10;
2135 PRAGMA auto_vacuum = FULL;
2136 CREATE TABLE x1(x, y, z, PRIMARY KEY(y, z));
2137 CREATE TABLE x2(x, y, z, PRIMARY KEY(y, z));
2138 INSERT INTO x2 VALUES(a_string(400), a_string(500), a_string(600));
2139 INSERT INTO x2 SELECT a_string(600), a_string(400), a_string(500) FROM x2;
2140 INSERT INTO x2 SELECT a_string(500), a_string(600), a_string(400) FROM x2;
2141 INSERT INTO x2 SELECT a_string(400), a_string(500), a_string(600) FROM x2;
2142 INSERT INTO x2 SELECT a_string(600), a_string(400), a_string(500) FROM x2;
2143 INSERT INTO x2 SELECT a_string(500), a_string(600), a_string(400) FROM x2;
2144 INSERT INTO x2 SELECT a_string(400), a_string(500), a_string(600) FROM x2;
2145 INSERT INTO x1 SELECT * FROM x2;
2148 do_test pager1-24.1.2 {
2151 DELETE FROM x1 WHERE rowid<32;
2153 recursive_select 64 x2
2155 do_test pager1-24.1.3 {
2157 UPDATE x1 SET z = a_string(300) WHERE rowid>40;
2159 PRAGMA integrity_check;
2160 SELECT count(*) FROM x1;
2164 do_test pager1-24.1.4 {
2167 INSERT INTO x1 SELECT * FROM x2;
2169 DELETE FROM x1 WHERE rowid<32;
2170 UPDATE x1 SET z = a_string(299) WHERE rowid>40;
2172 recursive_select 64 x2 {db eval COMMIT}
2174 PRAGMA integrity_check;
2175 SELECT count(*) FROM x1;
2179 do_test pager1-24.1.5 {
2182 INSERT INTO x1 SELECT * FROM x2;
2184 recursive_select 64 x2 { db eval {CREATE TABLE x3(x, y, z)} }
2185 execsql { SELECT * FROM x3 }
2188 #-------------------------------------------------------------------------
2190 do_test pager1-25-1 {
2191 faultsim_delete_and_reopen
2195 CREATE TABLE t1(a, b);
2202 do_test pager1-25-2 {
2203 faultsim_delete_and_reopen
2206 CREATE TABLE t1(a, b);
2213 #-------------------------------------------------------------------------
2214 # Sector-size tests.
2216 do_test pager1-26.1 {
2217 testvfs tv -default 1
2219 faultsim_delete_and_reopen
2220 db func a_string a_string
2222 PRAGMA page_size = 512;
2223 CREATE TABLE tbl(a PRIMARY KEY, b UNIQUE);
2225 INSERT INTO tbl VALUES(a_string(25), a_string(600));
2226 INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
2227 INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
2228 INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
2229 INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
2230 INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
2231 INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
2232 INSERT INTO tbl SELECT a_string(25), a_string(600) FROM tbl;
2236 do_execsql_test pager1-26.1 {
2237 UPDATE tbl SET b = a_string(550);
2242 #-------------------------------------------------------------------------
2244 do_test pager1.27.1 {
2245 faultsim_delete_and_reopen
2246 sqlite3_pager_refcounts db
2249 CREATE TABLE t1(a, b);
2251 sqlite3_pager_refcounts db
2255 #-------------------------------------------------------------------------
2256 # Test that attempting to open a write-transaction with
2257 # locking_mode=exclusive in WAL mode fails if there are other clients on
2258 # the same database.
2262 do_multiclient_test tn {
2263 do_test pager1-28.$tn.1 {
2265 PRAGMA journal_mode = WAL;
2266 CREATE TABLE t1(a, b);
2267 INSERT INTO t1 VALUES('a', 'b');
2270 do_test pager1-28.$tn.2 { sql2 { SELECT * FROM t1 } } {a b}
2272 do_test pager1-28.$tn.3 { sql1 { PRAGMA locking_mode=exclusive } } {exclusive}
2273 do_test pager1-28.$tn.4 {
2274 csql1 { BEGIN; INSERT INTO t1 VALUES('c', 'd'); }
2275 } {1 {database is locked}}
2276 code2 { db2 close ; sqlite3 db2 test.db }
2277 do_test pager1-28.$tn.4 {
2278 sql1 { INSERT INTO t1 VALUES('c', 'd'); COMMIT }
2283 #-------------------------------------------------------------------------
2284 # Normally, when changing from journal_mode=PERSIST to DELETE the pager
2285 # attempts to delete the journal file. However, if it cannot obtain a
2286 # RESERVED lock on the database file, this step is skipped.
2288 do_multiclient_test tn {
2289 do_test pager1-28.$tn.1 {
2291 PRAGMA journal_mode = PERSIST;
2292 CREATE TABLE t1(a, b);
2293 INSERT INTO t1 VALUES('a', 'b');
2296 do_test pager1-28.$tn.2 { file exists test.db-journal } 1
2297 do_test pager1-28.$tn.3 { sql1 { PRAGMA journal_mode = DELETE } } delete
2298 do_test pager1-28.$tn.4 { file exists test.db-journal } 0
2300 do_test pager1-28.$tn.5 {
2302 PRAGMA journal_mode = PERSIST;
2303 INSERT INTO t1 VALUES('c', 'd');
2306 do_test pager1-28.$tn.6 { file exists test.db-journal } 1
2307 do_test pager1-28.$tn.7 {
2308 sql2 { BEGIN; INSERT INTO t1 VALUES('e', 'f'); }
2310 do_test pager1-28.$tn.8 { file exists test.db-journal } 1
2311 do_test pager1-28.$tn.9 { sql1 { PRAGMA journal_mode = DELETE } } delete
2312 do_test pager1-28.$tn.10 { file exists test.db-journal } 1
2314 do_test pager1-28.$tn.11 { sql2 COMMIT } {}
2315 do_test pager1-28.$tn.12 { file exists test.db-journal } 0
2317 do_test pager1-28-$tn.13 {
2318 code1 { set channel [db incrblob -readonly t1 a 2] }
2320 PRAGMA journal_mode = PERSIST;
2321 INSERT INTO t1 VALUES('g', 'h');
2324 do_test pager1-28.$tn.14 { file exists test.db-journal } 1
2325 do_test pager1-28.$tn.15 {
2326 sql2 { BEGIN; INSERT INTO t1 VALUES('e', 'f'); }
2328 do_test pager1-28.$tn.16 { sql1 { PRAGMA journal_mode = DELETE } } delete
2329 do_test pager1-28.$tn.17 { file exists test.db-journal } 1
2331 do_test pager1-28.$tn.17 { csql2 { COMMIT } } {1 {database is locked}}
2332 do_test pager1-28-$tn.18 { code1 { read $channel } } c
2333 do_test pager1-28-$tn.19 { code1 { close $channel } } {}
2334 do_test pager1-28.$tn.20 { sql2 { COMMIT } } {}
2337 do_test pager1-29.1 {
2338 faultsim_delete_and_reopen
2340 PRAGMA page_size = 1024;
2341 PRAGMA auto_vacuum = full;
2342 PRAGMA locking_mode=exclusive;
2343 CREATE TABLE t1(a, b);
2344 INSERT INTO t1 VALUES(1, 2);
2348 do_test pager1-29.2 {
2350 PRAGMA page_size = 4096;
2356 #-------------------------------------------------------------------------
2357 # Test that if an empty database file (size 0 bytes) is opened in
2358 # exclusive-locking mode, any journal file is deleted from the file-system
2359 # without being rolled back. And that the RESERVED lock obtained while
2360 # doing this is not released.
2362 do_test pager1-30.1 {
2365 file delete test.db-journal
2366 set fd [open test.db-journal w]
2367 seek $fd [expr 512+1032*2]
2368 puts -nonewline $fd x
2373 PRAGMA locking_mode=EXCLUSIVE;
2374 SELECT count(*) FROM sqlite_master;
2377 } {exclusive 0 main reserved temp closed}
2379 #-------------------------------------------------------------------------
2380 # Test that if the "page-size" field in a journal-header is 0, the journal
2381 # file can still be rolled back. This is required for backward compatibility -
2382 # versions of SQLite prior to 3.5.8 always set this field to zero.
2384 do_test pager1-31.1 {
2385 faultsim_delete_and_reopen
2387 PRAGMA cache_size = 10;
2388 PRAGMA page_size = 1024;
2389 CREATE TABLE t1(x, y, UNIQUE(x, y));
2390 INSERT INTO t1 VALUES(randomblob(1500), randomblob(1500));
2391 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2392 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2393 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2394 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2395 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2396 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2397 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2398 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2399 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2400 INSERT INTO t1 SELECT randomblob(1500), randomblob(1500) FROM t1;
2402 UPDATE t1 SET y = randomblob(1499);
2404 file copy test.db test.db2
2405 file copy test.db-journal test.db2-journal
2407 hexio_write test.db2-journal 24 00000000
2408 sqlite3 db2 test.db2
2409 execsql { PRAGMA integrity_check } db2