1# 2018-04-28 2# 3# The author disclaims copyright to this source code. In place of 4# a legal notice, here is a blessing: 5# 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. 9# 10#*********************************************************************** 11# Test cases for SQLITE_DBCONFIG_RESET_DATABASE 12# 13 14set testdir [file dirname $argv0] 15source $testdir/tester.tcl 16set testprefix resetdb 17 18ifcapable !vtab||!compound { 19 finish_test 20 return 21} 22 23# In the "inmemory_journal" permutation, each new connection executes 24# "PRAGMA journal_mode = memory". This fails with SQLITE_BUSY if attempted 25# on a wal mode database with existing connections. For this and a few 26# other reasons, this test is not run as part of "inmemory_journal". 27# 28if {[permutation]=="inmemory_journal"} { 29 finish_test 30 return 31} 32 33# Create a sample database 34do_execsql_test 100 { 35 PRAGMA auto_vacuum = 0; 36 PRAGMA page_size=4096; 37 CREATE TABLE t1(a,b); 38 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<20) 39 INSERT INTO t1(a,b) SELECT x, randomblob(300) FROM c; 40 CREATE INDEX t1a ON t1(a); 41 CREATE INDEX t1b ON t1(b); 42 SELECT sum(a), sum(length(b)) FROM t1; 43 PRAGMA integrity_check; 44 PRAGMA journal_mode; 45 PRAGMA page_count; 46} {210 6000 ok delete 8} 47 48# Verify that the same content is seen from a separate database connection 49sqlite3 db2 test.db 50do_test 110 { 51 execsql { 52 SELECT sum(a), sum(length(b)) FROM t1; 53 PRAGMA integrity_check; 54 PRAGMA journal_mode; 55 PRAGMA page_count; 56 } db2 57} {210 6000 ok delete 8} 58 59do_test 200 { 60 # Thoroughly corrupt the database file by overwriting the first 61 # page with randomness. 62 catchsql { 63 UPDATE sqlite_dbpage SET data=randomblob(4096) WHERE pgno=1; 64 PRAGMA quick_check; 65 } 66} {1 {unsupported file format}} 67do_test 201 { 68 catchsql { 69 PRAGMA quick_check; 70 } db2 71} {1 {unsupported file format}} 72 73do_test 210 { 74 # Reset the database file using SQLITE_DBCONFIG_RESET_DATABASE 75 sqlite3_db_config db RESET_DB 1 76 db eval VACUUM 77 sqlite3_db_config db RESET_DB 0 78 79 # Verify that the reset took, even on the separate database connection 80 catchsql { 81 PRAGMA page_count; 82 PRAGMA page_size; 83 PRAGMA quick_check; 84 PRAGMA journal_mode; 85 } db2 86} {0 {1 4096 ok delete}} 87 88# Delete the old connections and database and start over again 89# with a different page size and in WAL mode. 90# 91db close 92db2 close 93forcedelete test.db 94sqlite3 db test.db 95do_execsql_test 300 { 96 PRAGMA auto_vacuum = 0; 97 PRAGMA page_size=8192; 98 PRAGMA journal_mode=WAL; 99 CREATE TABLE t1(a,b); 100 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<20) 101 INSERT INTO t1(a,b) SELECT x, randomblob(1300) FROM c; 102 CREATE INDEX t1a ON t1(a); 103 CREATE INDEX t1b ON t1(b); 104 SELECT sum(a), sum(length(b)) FROM t1; 105 PRAGMA integrity_check; 106 PRAGMA journal_mode; 107 PRAGMA page_size; 108 PRAGMA page_count; 109} {wal 210 26000 ok wal 8192 12} 110sqlite3 db2 test.db 111do_test 310 { 112 execsql { 113 SELECT sum(a), sum(length(b)) FROM t1; 114 PRAGMA integrity_check; 115 PRAGMA journal_mode; 116 PRAGMA page_size; 117 PRAGMA page_count; 118 } db2 119} {210 26000 ok wal 8192 12} 120 121# Corrupt the database again 122do_catchsql_test 320 { 123 UPDATE sqlite_dbpage SET data=randomblob(8192) WHERE pgno=1; 124 PRAGMA quick_check 125} {1 {file is not a database}} 126 127do_test 330 { 128 catchsql { 129 PRAGMA quick_check 130 } db2 131} {1 {file is not a database}} 132 133# Reset the database yet again. Verify that the page size and 134# journal mode are preserved. 135# 136do_test 400 { 137 sqlite3_db_config db RESET_DB 1 138 db eval VACUUM 139 sqlite3_db_config db RESET_DB 0 140 catchsql { 141 PRAGMA page_count; 142 PRAGMA page_size; 143 PRAGMA journal_mode; 144 PRAGMA quick_check; 145 } db2 146} {0 {1 8192 wal ok}} 147db2 close 148 149 150finish_test 151