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 #***********************************************************************
11 # This file implements regression tests for SQLite library. The focus
12 # of these tests is the journal mode pragma.
14 # $Id: jrnlmode.test,v 1.16 2009/06/05 17:09:12 drh Exp $
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
19 ifcapable {!pager_pragmas} {
24 if {[info exists TEMP_STORE] && $TEMP_STORE>=2} {
25 set temp_persist memory
26 set temp_delete memory
27 set temp_truncate memory
30 set temp_persist persist
31 set temp_delete delete
32 set temp_truncate truncate
36 proc temp_journal_mode {newmode} {
37 if {[info exists ::TEMP_STORE] && $::TEMP_STORE>=2} {
38 if {$newmode ne "off" && $newmode ne "memory"} {
39 execsql {PRAGMA temp.journal_mode}
40 set newmode [db one {PRAGMA temp.journal_mode}]
46 #----------------------------------------------------------------------
47 # Test cases jrnlmode-1.X test the PRAGMA logic.
49 do_test jrnlmode-1.0 {
52 PRAGMA main.journal_mode;
53 PRAGMA temp.journal_mode;
55 } [list delete delete [temp_journal_mode delete]]
56 do_test jrnlmode-1.1 {
58 PRAGMA journal_mode = persist;
61 do_test jrnlmode-1.2 {
64 PRAGMA main.journal_mode;
65 PRAGMA temp.journal_mode;
67 } [list persist persist [temp_journal_mode persist]]
68 do_test jrnlmode-1.4a {
69 # When defensive is on, unable to set journal_mode to OFF
70 sqlite3_db_config db DEFENSIVE 1
72 PRAGMA journal_mode = off;
75 do_test jrnlmode-1.4b {
76 # When defensive is on, unable to set journal_mode to OFF
77 sqlite3_db_config db DEFENSIVE 0
79 PRAGMA journal_mode = off;
82 do_test jrnlmode-1.5 {
85 PRAGMA main.journal_mode;
86 PRAGMA temp.journal_mode;
88 } [list off off [temp_journal_mode off]]
89 do_test jrnlmode-1.6 {
91 PRAGMA journal_mode = delete;
94 do_test jrnlmode-1.7 {
97 PRAGMA main.journal_mode;
98 PRAGMA Temp.journal_mode;
100 } [list delete delete [temp_journal_mode delete]]
101 do_test jrnlmode-1.7.1 {
103 PRAGMA journal_mode = truncate;
106 do_test jrnlmode-1.7.2 {
109 PRAGMA main.journal_mode;
110 PRAGMA temp.journal_mode;
112 } [list truncate truncate [temp_journal_mode truncate]]
113 do_test jrnlmode-1.8 {
115 PRAGMA journal_mode = off;
116 PRAGMA journal_mode = invalid;
120 do_test jrnlmode-1.9 {
122 PRAGMA journal_mode = PERSIST;
123 ATTACH ':memory:' as aux1;
126 PRAGMA main.journal_mode;
127 PRAGMA aux1.journal_mode;
130 do_test jrnlmode-1.10 {
132 PRAGMA main.journal_mode = OFF;
135 PRAGMA main.journal_mode;
136 PRAGMA temp.journal_mode;
137 PRAGMA aux1.journal_mode;
139 } [list off [temp_journal_mode persist] memory]
140 do_test jrnlmode-1.11 {
145 do_test jrnlmode-1.12 {
147 ATTACH ':memory:' as aux2;
150 PRAGMA main.journal_mode;
151 PRAGMA aux1.journal_mode;
152 PRAGMA aux2.journal_mode;
154 } {off memory memory}
155 do_test jrnlmode-1.13 {
156 # The journal-mode used by in-memory databases cannot be changed.
158 PRAGMA aux1.journal_mode = DELETE;
161 PRAGMA main.journal_mode;
162 PRAGMA aux1.journal_mode;
163 PRAGMA aux2.journal_mode;
165 } {off memory memory}
166 do_test jrnlmode-1.14 {
168 PRAGMA journal_mode = delete;
171 PRAGMA main.journal_mode;
172 PRAGMA temp.journal_mode;
173 PRAGMA aux1.journal_mode;
174 PRAGMA aux2.journal_mode;
176 } [list delete [temp_journal_mode delete] memory memory]
177 do_test jrnlmode-1.15 {
179 ATTACH ':memory:' as aux3;
182 PRAGMA main.journal_mode;
183 PRAGMA temp.journal_mode;
184 PRAGMA aux1.journal_mode;
185 PRAGMA aux2.journal_mode;
186 PRAGMA aux3.journal_mode;
188 } [list delete [temp_journal_mode delete] memory memory memory]
189 do_test jrnlmode-1.16 {
191 PRAGMA journal_mode = TRUNCATE;
194 PRAGMA main.journal_mode;
195 PRAGMA temp.journal_mode;
196 PRAGMA aux1.journal_mode;
197 PRAGMA aux2.journal_mode;
198 PRAGMA aux3.journal_mode;
200 } [list truncate [temp_journal_mode truncate] memory memory memory]
202 do_test jrnlmode-1.99 {
213 do_test jrnlmode-2.1 {
215 ATTACH 'test2.db' AS aux;
216 PRAGMA main.journal_mode = persist;
217 PRAGMA aux.journal_mode = persist;
218 CREATE TABLE abc(a, b, c);
219 CREATE TABLE aux.def(d, e, f);
223 INSERT INTO abc VALUES(1, 2, 3);
224 INSERT INTO def VALUES(4, 5, 6);
227 list [file exists test.db-journal] [file exists test2.db-journal]
230 do_test jrnlmode-2.2 {
231 file size test.db-journal
234 do_test jrnlmode-2.3 {
240 do_test jrnlmode-2.4 {
241 file size test.db-journal
244 do_test jrnlmode-2.5 {
250 #----------------------------------------------------------------------
251 # Test caes jrnlmode-3.X verify that ticket #3127 has been fixed.
258 do_test jrnlmode-3.1 {
260 CREATE TABLE x(n INTEGER);
261 ATTACH 'test2.db' AS a;
262 create table a.x ( n integer );
263 insert into a.x values(1);
264 insert into a.x values (2);
265 insert into a.x values (3);
266 insert into a.x values (4);
270 do_test jrnlmode-3.2 {
271 execsql { PRAGMA journal_mode=off; }
274 INSERT OR IGNORE INTO main.x SELECT * FROM a.x;
280 ifcapable autovacuum&&pragma {
284 do_test jrnlmode-4.1 {
286 PRAGMA cache_size = 1;
287 PRAGMA auto_vacuum = 1;
288 CREATE TABLE abc(a, b, c);
290 execsql { PRAGMA page_count }
293 do_test jrnlmode-4.2 {
294 execsql { PRAGMA journal_mode = off }
297 do_test jrnlmode-4.3 {
298 execsql { INSERT INTO abc VALUES(1, 2, randomblob(2000)) }
301 # This will attempt to truncate the database file. Check that this
302 # is not a problem when journal_mode=off.
303 do_test jrnlmode-4.4 {
304 execsql { DELETE FROM abc }
307 integrity_check jrnlmode-4.5
310 #------------------------------------------------------------------------
311 # The following test caes, jrnlmode-5.*, test the journal_size_limit
314 if {[atomic_batch_write test.db]==0} {
316 forcedelete test.db test2.db test3.db
319 do_test jrnlmode-5.1 {
320 execsql {pragma page_size=1024}
321 execsql {pragma journal_mode=persist}
324 do_test jrnlmode-5.2 {
325 execsql { PRAGMA journal_size_limit }
327 do_test jrnlmode-5.3 {
329 ATTACH 'test2.db' AS aux;
330 PRAGMA aux.journal_mode=persist;
331 PRAGMA aux.journal_size_limit;
334 do_test jrnlmode-5.4.1 {
335 execsql { PRAGMA aux.journal_size_limit = 999999999999 }
337 do_test jrnlmode-5.4.2 {
338 execsql { PRAGMA aux.journal_size_limit = 10240 }
340 do_test jrnlmode-5.5 {
341 execsql { PRAGMA main.journal_size_limit = 20480 }
343 do_test jrnlmode-5.6 {
344 execsql { PRAGMA journal_size_limit }
346 do_test jrnlmode-5.7 {
347 execsql { PRAGMA aux.journal_size_limit }
350 do_test jrnlmode-5.8 {
352 ATTACH 'test3.db' AS aux2;
353 PRAGMA aux2.journal_mode=persist;
357 do_test jrnlmode-5.9 {
359 CREATE TABLE main.t1(a, b, c);
360 CREATE TABLE aux.t2(a, b, c);
361 CREATE TABLE aux2.t3(a, b, c);
364 do_test jrnlmode-5.10 {
366 [file exists test.db-journal] \
367 [file exists test2.db-journal] \
368 [file exists test3.db-journal]
370 do_test jrnlmode-5.11 {
373 INSERT INTO t3 VALUES(randomblob(1000),randomblob(1000),randomblob(1000));
375 SELECT randomblob(1000),randomblob(1000),randomblob(1000) FROM t3;
377 SELECT randomblob(1000),randomblob(1000),randomblob(1000) FROM t3;
379 SELECT randomblob(1000),randomblob(1000),randomblob(1000) FROM t3;
381 SELECT randomblob(1000),randomblob(1000),randomblob(1000) FROM t3;
383 SELECT randomblob(1000),randomblob(1000),randomblob(1000) FROM t3;
384 INSERT INTO t2 SELECT * FROM t3;
385 INSERT INTO t1 SELECT * FROM t2;
389 [file exists test.db-journal] \
390 [file exists test2.db-journal] \
391 [file exists test3.db-journal] \
392 [file size test.db-journal] \
393 [file size test2.db-journal] \
394 [file size test3.db-journal]
397 do_test jrnlmode-5.12 {
400 UPDATE t1 SET a = randomblob(1000);
402 expr {[file size test.db-journal]>30000}
404 do_test jrnlmode-5.13 {
406 file size test.db-journal
409 do_test jrnlmode-5.14 {
412 UPDATE t2 SET a = randomblob(1000);
414 expr {[file size test2.db-journal]>30000}
416 do_test jrnlmode-5.15 {
418 file size test2.db-journal
421 do_test jrnlmode-5.16 {
424 UPDATE t3 SET a = randomblob(1000);
426 set journalsize [file size test3.db-journal]
427 expr {$journalsize>30000}
429 do_test jrnlmode-5.17 {
431 set sz [file size test3.db-journal]
432 expr {$sz>=$journalsize}
435 do_test jrnlmode-5.18 {
437 PRAGMA journal_size_limit = -4;
439 UPDATE t1 SET a = randomblob(1000);
441 set journalsize [file size test.db-journal]
442 expr {$journalsize>30000}
444 do_test jrnlmode-5.19 {
446 set sz [file size test.db-journal]
447 expr {$sz>=$journalsize}
450 # Test a size-limit of 0.
452 do_test jrnlmode-5.20 {
454 PRAGMA journal_size_limit = 0;
456 UPDATE t1 SET a = randomblob(1000);
459 do_test jrnlmode-5.21 {
460 expr {[file size test.db-journal] > 1024}
462 do_test jrnlmode-5.22 {
464 list [file exists test.db-journal] [file size test.db-journal]
470 if {[atomic_batch_write test.db]==0} {
471 # These tests are not run as part of the "journaltest" permutation,
472 # as the test_journal.c layer is incompatible with in-memory journaling.
473 if {[permutation] ne "journaltest"} {
475 do_test jrnlmode-6.1 {
477 PRAGMA journal_mode = truncate;
478 CREATE TABLE t4(a, b);
480 INSERT INTO t4 VALUES(1, 2);
481 PRAGMA journal_mode = memory;
483 } {truncate truncate}
484 do_test jrnlmode-6.2 {
485 file exists test.db-journal
487 do_test jrnlmode-6.3 {
493 do_test jrnlmode-6.4 {
494 file exists test.db-journal
496 do_test jrnlmode-6.5 {
498 PRAGMA journal_mode = MEMORY;
500 INSERT INTO t4 VALUES(3, 4);
502 file exists test.db-journal
504 do_test jrnlmode-6.7 {
510 do_test jrnlmode-6.8 {
511 file exists test.db-journal
513 do_test jrnlmode-6.9 {
515 PRAGMA journal_mode = DELETE;
516 BEGIN IMMEDIATE; INSERT INTO t4 VALUES(1,2); COMMIT;
518 file exists test.db-journal
526 do_test jrnlmode-7.1 {
527 foreach f [glob -nocomplain test.db*] { forcedelete $f }
530 PRAGMA journal_mode = memory;
531 PRAGMA auto_vacuum = 0;
532 PRAGMA page_size = 1024;
533 PRAGMA user_version = 5;
537 do_test jrnlmode-7.2 { file size test.db } {1024}
540 do_execsql_test jrnlmode-8.1 { PRAGMA locking_mode=EXCLUSIVE } {exclusive}
541 do_execsql_test jrnlmode-8.2 { CREATE TABLE t1(x) } {}
542 do_execsql_test jrnlmode-8.3 { INSERT INTO t1 VALUES(123) } {}
543 do_execsql_test jrnlmode-8.4 { SELECT * FROM t1 } {123}
544 do_execsql_test jrnlmode-8.5 { PRAGMA journal_mode=PERSIST } {persist}
545 do_execsql_test jrnlmode-8.6 { PRAGMA journal_mode=DELETE } {delete}
546 do_execsql_test jrnlmode-8.7 { PRAGMA journal_mode=TRUNCATE } {truncate}
547 do_execsql_test jrnlmode-8.8 { PRAGMA journal_mode=DELETE } {delete}
548 do_execsql_test jrnlmode-8.9 { CREATE TABLE t2(y) } {}
549 do_execsql_test jrnlmode-8.10 { INSERT INTO t2 VALUES(456) } {}
550 do_execsql_test jrnlmode-8.11 { SELECT * FROM t1, t2 } {123 456}
551 do_execsql_test jrnlmode-8.12 { PRAGMA locking_mode=NORMAL } {normal}
552 do_execsql_test jrnlmode-8.13 { PRAGMA journal_mode=PERSIST } {persist}
553 do_execsql_test jrnlmode-8.14 { PRAGMA journal_mode=TRUNCATE } {truncate}
554 do_execsql_test jrnlmode-8.15 { PRAGMA journal_mode=PERSIST } {persist}
555 do_execsql_test jrnlmode-8.16 { PRAGMA journal_mode=DELETE } {delete}
556 do_execsql_test jrnlmode-8.17 { PRAGMA journal_mode=TRUNCATE } {truncate}
557 do_execsql_test jrnlmode-8.18 { PRAGMA locking_mode=EXCLUSIVE } {exclusive}
558 do_execsql_test jrnlmode-8.19 { CREATE TABLE t3(z) } {}
559 do_execsql_test jrnlmode-8.20 { BEGIN IMMEDIATE } {}
560 do_execsql_test jrnlmode-8.21 { PRAGMA journal_mode=DELETE } {delete}
561 do_execsql_test jrnlmode-8.22 { COMMIT } {}
562 do_execsql_test jrnlmode-8.23 { PRAGMA journal_mode=DELETE } {delete}
563 do_execsql_test jrnlmode-8.24 { PRAGMA journal_mode=TRUNCATE } {truncate}
564 do_execsql_test jrnlmode-8.25 { PRAGMA locking_mode=NORMAL } {normal}
565 do_execsql_test jrnlmode-8.26 { CREATE TABLE t4(w) } {}
566 do_execsql_test jrnlmode-8.27 { BEGIN IMMEDIATE } {}
567 do_execsql_test jrnlmode-8.28 { PRAGMA journal_mode=DELETE } {delete}
568 do_execsql_test jrnlmode-8.29 { COMMIT } {}
569 do_execsql_test jrnlmode-8.30 { PRAGMA journal_mode=DELETE } {delete}
571 # Assertion fault on 2015-05-01
572 do_test jrnlmode-9.1 {
575 db2 eval {CREATE TEMP TABLE t(l); PRAGMA journal_mode=off;}
578 do_execsql_test jrnlmode-9.2 {
579 PRAGMA locking_mode = exclusive;
581 PRAGMA journal_mode = off;