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 page_size=4096; 36 CREATE TABLE t1(a,b); 37 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<20) 38 INSERT INTO t1(a,b) SELECT x, randomblob(300) FROM c; 39 CREATE INDEX t1a ON t1(a); 40 CREATE INDEX t1b ON t1(b); 41 SELECT sum(a), sum(length(b)) FROM t1; 42 PRAGMA integrity_check; 43 PRAGMA journal_mode; 44 PRAGMA page_count; 45} {210 6000 ok delete 8} 46 47# Verify that the same content is seen from a separate database connection 48sqlite3 db2 test.db 49do_test 110 { 50 execsql { 51 SELECT sum(a), sum(length(b)) FROM t1; 52 PRAGMA integrity_check; 53 PRAGMA journal_mode; 54 PRAGMA page_count; 55 } db2 56} {210 6000 ok delete 8} 57 58do_test 200 { 59 # Thoroughly corrupt the database file by overwriting the first 60 # page with randomness. 61 catchsql { 62 UPDATE sqlite_dbpage SET data=randomblob(4096) WHERE pgno=1; 63 PRAGMA quick_check; 64 } 65} {1 {unsupported file format}} 66do_test 201 { 67 catchsql { 68 PRAGMA quick_check; 69 } db2 70} {1 {unsupported file format}} 71 72do_test 210 { 73 # Reset the database file using SQLITE_DBCONFIG_RESET_DATABASE 74 sqlite3_db_config db RESET_DB 1 75 db eval VACUUM 76 sqlite3_db_config db RESET_DB 0 77 78 # Verify that the reset took, even on the separate database connection 79 catchsql { 80 PRAGMA page_count; 81 PRAGMA page_size; 82 PRAGMA quick_check; 83 PRAGMA journal_mode; 84 } db2 85} {0 {1 4096 ok delete}} 86 87# Delete the old connections and database and start over again 88# with a different page size and in WAL mode. 89# 90db close 91db2 close 92forcedelete test.db 93sqlite3 db test.db 94do_execsql_test 300 { 95 PRAGMA page_size=8192; 96 PRAGMA journal_mode=WAL; 97 CREATE TABLE t1(a,b); 98 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<20) 99 INSERT INTO t1(a,b) SELECT x, randomblob(1300) FROM c; 100 CREATE INDEX t1a ON t1(a); 101 CREATE INDEX t1b ON t1(b); 102 SELECT sum(a), sum(length(b)) FROM t1; 103 PRAGMA integrity_check; 104 PRAGMA journal_mode; 105 PRAGMA page_size; 106 PRAGMA page_count; 107} {wal 210 26000 ok wal 8192 12} 108sqlite3 db2 test.db 109do_test 310 { 110 execsql { 111 SELECT sum(a), sum(length(b)) FROM t1; 112 PRAGMA integrity_check; 113 PRAGMA journal_mode; 114 PRAGMA page_size; 115 PRAGMA page_count; 116 } db2 117} {210 26000 ok wal 8192 12} 118 119# Corrupt the database again 120do_catchsql_test 320 { 121 UPDATE sqlite_dbpage SET data=randomblob(8192) WHERE pgno=1; 122 PRAGMA quick_check 123} {1 {file is not a database}} 124 125do_test 330 { 126 catchsql { 127 PRAGMA quick_check 128 } db2 129} {1 {file is not a database}} 130 131# Reset the database yet again. Verify that the page size and 132# journal mode are preserved. 133# 134do_test 400 { 135 sqlite3_db_config db RESET_DB 1 136 db eval VACUUM 137 sqlite3_db_config db RESET_DB 0 138 catchsql { 139 PRAGMA page_count; 140 PRAGMA page_size; 141 PRAGMA journal_mode; 142 PRAGMA quick_check; 143 } db2 144} {0 {1 8192 wal ok}} 145db2 close 146 147 148finish_test 149