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 #***********************************************************************
12 # This file contains test cases for sqlite3_db_cacheflush API.
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
17 set testprefix cacheflush
18 test_set_config_pagecache 0 0
20 # Run the supplied SQL on a copy of the database currently stored on
21 # disk in file $dbfile.
22 proc diskquery {dbfile sql} {
23 forcecopy $dbfile dq.db
25 set res [execsql $sql dq]
30 # Simplest possible test.
32 do_execsql_test 1.1.0 {
33 CREATE TABLE t1(a, b);
34 INSERT INTO t1 VALUES(1, 2);
36 INSERT INTO t1 VALUES(3, 4);
39 diskquery test.db { SELECT * FROM t1 }
42 sqlite3_db_cacheflush db
43 diskquery test.db { SELECT * FROM t1 }
46 # Test that multiple pages may be flushed to disk.
48 do_execsql_test 1.2.0 {
50 CREATE TABLE t2(a, b);
52 INSERT INTO t1 VALUES(5, 6);
53 INSERT INTO t2 VALUES('a', 'b');
62 sqlite3_db_cacheflush db
69 # Test that pages with nRef!=0 are not flushed to disk.
71 do_execsql_test 1.3.0 {
73 CREATE TABLE t3(a, b);
75 INSERT INTO t1 VALUES(7, 8);
76 INSERT INTO t2 VALUES('c', 'd');
77 INSERT INTO t3 VALUES('i', 'ii');
87 db eval { SELECT a FROM t1 } {
89 sqlite3_db_cacheflush db
97 } {1 2 3 4 5 6 a b c d i ii}
99 sqlite3_db_cacheflush db
105 } {1 2 3 4 5 6 7 8 a b c d i ii}
107 # Check that SQLITE_BUSY is returned if pages cannot be flushed due to
108 # conflicting read locks.
110 do_execsql_test 1.4.0 {
113 INSERT INTO t1 VALUES(9, 10);
126 list [catch { sqlite3_db_cacheflush db } msg] $msg
127 } {1 {database is locked}}
135 sqlite3_db_cacheflush db
139 } {1 2 3 4 5 6 7 8 9 10}
140 do_execsql_test 1.4.5 { COMMIT }
142 #-------------------------------------------------------------------------
143 # Test that ATTACHed database caches are also flushed.
146 do_execsql_test 2.1.0 {
147 ATTACH 'test.db2' AS aux;
148 CREATE TABLE aux.t4(x, y);
149 INSERT INTO t4 VALUES('A', 'B');
151 INSERT INTO t1 VALUES(11, 12);
152 INSERT INTO t4 VALUES('C', 'D');
155 diskquery test.db { SELECT * FROM t1; }
156 } {1 2 3 4 5 6 7 8 9 10}
158 diskquery test.db2 { SELECT * FROM t4; }
161 sqlite3_db_cacheflush db
162 diskquery test.db { SELECT * FROM t1; }
163 } {1 2 3 4 5 6 7 8 9 10 11 12}
165 sqlite3_db_cacheflush db
166 diskquery test.db2 { SELECT * FROM t4; }
168 do_execsql_test 2.1.5 { COMMIT }
170 # And that hitting an SQLITE_BUSY when flushing "main" does not stop
171 # SQLite from going on to flush "aux".
173 do_execsql_test 2.2.0 {
175 INSERT INTO t1 VALUES(13, 14);
176 INSERT INTO t4 VALUES('E', 'F');
179 diskquery test.db { SELECT * FROM t1; }
180 } {1 2 3 4 5 6 7 8 9 10 11 12}
182 diskquery test.db2 { SELECT * FROM t4; }
190 list [catch { sqlite3_db_cacheflush db } msg] $msg
191 } {1 {database is locked}}
193 diskquery test.db { SELECT * FROM t1; }
194 } {1 2 3 4 5 6 7 8 9 10 11 12}
196 diskquery test.db2 { SELECT * FROM t4; }
200 sqlite3_db_cacheflush db
201 diskquery test.db { SELECT * FROM t1; }
202 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14}
203 do_execsql_test 2.2.7 { COMMIT }
205 #-------------------------------------------------------------------------
206 # Test that nothing terrible happens if sqlite3_db_cacheflush() is
207 # called on an in-memory database.
213 CREATE TABLE t1(x PRIMARY KEY);
214 CREATE TABLE t2(y PRIMARY KEY);
216 INSERT INTO t1 VALUES(randomblob(100));
217 INSERT INTO t2 VALUES(randomblob(100));
218 INSERT INTO t1 VALUES(randomblob(100));
219 INSERT INTO t2 VALUES(randomblob(100));
221 sqlite3_db_cacheflush db
224 do_execsql_test 3.1 { PRAGMA integrity_check } ok
225 do_execsql_test 3.2 { COMMIT }
226 do_execsql_test 3.3 { PRAGMA integrity_check } ok
227 do_execsql_test 3.4 {
228 SELECT count(*) FROM t1;
229 SELECT count(*) FROM t2;
232 #-------------------------------------------------------------------------
233 # Test that calling sqlite3_db_cacheflush() does not interfere with
234 # savepoint transactions.
239 CREATE TABLE ta(a, aa);
240 CREATE TABLE tb(b, bb);
241 INSERT INTO ta VALUES('a', randomblob(500));
242 INSERT INTO tb VALUES('b', randomblob(500));
244 UPDATE ta SET a = 'A';
246 UPDATE tb SET b = 'B';
249 sqlite3_db_cacheflush db
260 sqlite3_db_cacheflush db
269 INSERT INTO tb VALUES('c', randomblob(10));
270 INSERT INTO tb VALUES('d', randomblob(10));
271 INSERT INTO tb VALUES('e', randomblob(10));
273 sqlite3_db_cacheflush db
283 UPDATE tb SET b = upper(b);
285 sqlite3_db_cacheflush db
296 sqlite3_db_cacheflush db
307 sqlite3_db_cacheflush db
322 test_restore_config_pagecache