sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | # 2026 September 1 |
| 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 | # This file implements regression tests for SQLite library. The |
| 12 | # focus of this file is testing the operation of the library in |
| 13 | # "PRAGMA journal_mode=WAL" mode. |
| 14 | # |
| 15 | |
| 16 | set testdir [file dirname $argv0] |
| 17 | source $testdir/tester.tcl |
| 18 | source $testdir/lock_common.tcl |
| 19 | source $testdir/malloc_common.tcl |
| 20 | source $testdir/wal_common.tcl |
| 21 | |
| 22 | set testprefix waliter |
| 23 | |
| 24 | ifcapable !wal {finish_test ; return } |
| 25 | |
| 26 | testvfs tvfs -default 1 -fullshm 1 |
| 27 | tvfs script vfs_callback |
| 28 | tvfs filter xWrite |
| 29 | |
| 30 | proc vfs_callback {method file nm offset amt} { |
| 31 | if {[file tail $file]=="test.db" && $amt==1024} { |
| 32 | incr ::WRITE([expr $offset/1024]) 1 |
| 33 | if {[info exists ::DEBUG]} { |
| 34 | puts "write page [expr $offset/1024]" |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | sqlite3 db test.db |
| 40 | |
| 41 | do_execsql_test 1.0 { |
| 42 | PRAGMA page_size = 1024; |
| 43 | CREATE TABLE t1(a INTEGER PRIMARY KEY, b BLOB[900]); |
| 44 | WITH s(i) AS ( |
| 45 | SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000 |
| 46 | ) |
| 47 | INSERT INTO t1(b) SELECT randomblob(900) FROM s; |
| 48 | PRAGMA journal_mode = wal; |
| 49 | PRAGMA wal_autocheckpoint = 0; |
| 50 | } {wal 0} |
| 51 | |
| 52 | do_execsql_test 1.1 { |
| 53 | PRAGMA wal_autocheckpoint = 0; |
| 54 | UPDATE t1 SET b = randomblob(900); |
| 55 | UPDATE t1 SET b = randomblob(900); |
| 56 | UPDATE t1 SET b = randomblob(900); |
| 57 | UPDATE t1 SET b = randomblob(900); |
| 58 | |
| 59 | UPDATE t1 SET b = randomblob(900); |
| 60 | UPDATE t1 SET b = randomblob(900); |
| 61 | UPDATE t1 SET b = randomblob(900); |
| 62 | UPDATE t1 SET b = randomblob(900); |
| 63 | |
| 64 | UPDATE t1 SET b = randomblob(900); |
| 65 | UPDATE t1 SET b = randomblob(900); |
| 66 | } {0} |
| 67 | |
| 68 | do_test 1.2 { |
| 69 | expr {[file size test.db-shm] >= 32768*3} |
| 70 | # Forum thread 2026-10-11T08:31:48Z |
| 71 | # https://sqlite.org/forum/forumpost/471533c5f0 |
| 72 | # |
| 73 | # On "normal" systems, the file size is exactly 98304 bytes. |
| 74 | # But apparently some (obscure) systems have page size of 65536 |
| 75 | # which means that the file size will be larger. |
| 76 | } 1 |
| 77 | |
| 78 | do_execsql_test 1.3 { |
| 79 | PRAGMA wal_checkpoint |
| 80 | } {0 10000 10000} |
| 81 | # foreach k [array names WRITE] { puts "$k : $WRITE($k) writes" } |
| 82 | |
| 83 | db close |
| 84 | tvfs delete |
| 85 | |
| 86 | #------------------------------------------------------------------------- |
| 87 | reset_db |
| 88 | |
| 89 | do_execsql_test 2.0 { |
| 90 | PRAGMA page_size = 1024; |
| 91 | CREATE TABLE t1(a INTEGER PRIMARY KEY, b BLOB[900]); |
| 92 | WITH s(i) AS ( |
| 93 | SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000 |
| 94 | ) |
| 95 | INSERT INTO t1(b) SELECT randomblob(900) FROM s; |
| 96 | CREATE INDEX i1 ON t1(b); |
| 97 | PRAGMA journal_mode = wal; |
| 98 | } {wal} |
| 99 | |
| 100 | sqlite3 db2 test.db |
| 101 | |
| 102 | do_test 2.1 { |
| 103 | for {set ii 1} {$ii<4000} {incr ii} { |
| 104 | db2 eval { |
| 105 | BEGIN; |
| 106 | SELECT count(*) FROM t1; |
| 107 | } |
| 108 | execsql { |
| 109 | UPDATE t1 SET b = randomblob(900) WHERE a = ($ii%1000)+1; |
| 110 | PRAGMA wal_checkpoint; |
| 111 | } |
| 112 | db2 eval COMMIT |
| 113 | |
| 114 | if {($ii%100)==0} { |
| 115 | if {[db one {PRAGMA integrity_check}]!="ok"} { |
| 116 | error "integrity_check failure" |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | set {} {} |
| 121 | } {} |
| 122 | |
| 123 | do_execsql_test 2.2.1 { PRAGMA integrity_check } ok |
| 124 | do_test 2.2.2 { execsql { PRAGMA wal_checkpoint } ; set {} {} } {} |
| 125 | do_execsql_test 2.2.3 { PRAGMA integrity_check } ok |
| 126 | |
| 127 | |
| 128 | finish_test |