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-11-11 |
| 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 that if a batch-atomic commit fails |
| 13 | # with SQLITE_IOERR and SQLite falls back to a legacy rollback-journal |
| 14 | # commit, the journal file is synced before any database pages are |
| 15 | # overwritten. |
| 16 | # |
| 17 | |
| 18 | set testdir [file dirname $argv0] |
| 19 | source $testdir/tester.tcl |
| 20 | set ::testprefix atomic3 |
| 21 | |
| 22 | ifcapable !batchatomicwrite { |
| 23 | finish_test |
| 24 | return |
| 25 | } |
| 26 | |
| 27 | do_execsql_test 1.0 { |
| 28 | PRAGMA journal_mode = delete; |
| 29 | CREATE TABLE t1(x, y); |
| 30 | INSERT INTO t1 VALUES(1, randomblob(800)); |
| 31 | INSERT INTO t1 VALUES(2, randomblob(800)); |
| 32 | } {delete} |
| 33 | db close |
| 34 | |
| 35 | # Create a testvfs named "batvfs" that: |
| 36 | # |
| 37 | # * Claims to support batch-atomic-writes, but |
| 38 | # * Always returns SQLITE_IOERR to a BEGIN_ATOMIC_WRITE xFileControl, and |
| 39 | # * Populates global variable ::ba_log with a log of all xWrite and |
| 40 | # xSync calls made. |
| 41 | # |
| 42 | proc ba_vfs_callback {method file args} { |
| 43 | if {$method=="xFileControl"} { |
| 44 | if {[lindex $args 0]=="BEGIN_ATOMIC_WRITE"} { return SQLITE_IOERR } |
| 45 | return SQLITE_OK |
| 46 | } |
| 47 | lappend ::ba_log $method [file tail $file] |
| 48 | return SQLITE_OK |
| 49 | } |
| 50 | testvfs batvfs |
| 51 | batvfs devchar batch_atomic |
| 52 | batvfs script ba_vfs_callback |
| 53 | batvfs filter {xFileControl xWrite xSync} |
| 54 | |
| 55 | # Write a transaction to the db via "batvfs". Then check that after the |
| 56 | # attempt to do an atomic write failed, SQLite created, populated and synced |
| 57 | # the journal file before writing any pages to the db. |
| 58 | # |
| 59 | sqlite3 db test.db -vfs batvfs |
| 60 | do_execsql_test 1.1 { SELECT count(*) FROM t1 } 2 |
| 61 | set ::ba_log [list] |
| 62 | do_execsql_test 1.2 { UPDATE t1 SET y = randomblob(800) } {} |
| 63 | |
| 64 | # Find the first write to the database file and the first sync of the |
| 65 | # journal file. The journal sync must come first. |
| 66 | # |
| 67 | set iDbWrite -1 ;# Index of first write to db. |
| 68 | set iJrnlSync -1 ;# Index of first sync of journal file, |
| 69 | |
| 70 | set i 0 |
| 71 | foreach {m f} $::ba_log { |
| 72 | if {$iDbWrite<0 && $m=="xWrite" && $f=="test.db"} { set iDbWrite $i } |
| 73 | if {$iJrnlSync<0 && $m=="xSync" && $f=="test.db-journal"} {set iJrnlSync $i} |
| 74 | incr i |
| 75 | } |
| 76 | do_test 1.3 { |
| 77 | expr {$iJrnlSync>=0 && $iJrnlSync<$iDbWrite} |
| 78 | } 1 |
| 79 | do_execsql_test 1.4 { PRAGMA integrity_check } ok |
| 80 | |
| 81 | db close |
| 82 | batvfs delete |
| 83 | |
| 84 | #------------------------------------------------------------------------- |
| 85 | # Test a successful batch-atomic commit. Create a testvfs named "batvfs2" |
| 86 | # that claims to support batch-atomic-writes and reports success for |
| 87 | # BEGIN_ATOMIC_WRITE and COMMIT_ATOMIC_WRITE. Check that the transaction |
| 88 | # is committed without writing to or syncing a journal file. |
| 89 | # |
| 90 | proc ba_vfs_callback2 {method file args} { |
| 91 | if {$method=="xFileControl"} { |
| 92 | set op [lindex $args 0] |
| 93 | if {$op=="BEGIN_ATOMIC_WRITE" || $op=="COMMIT_ATOMIC_WRITE"} { |
| 94 | return SQLITE_OMIT |
| 95 | } |
| 96 | return SQLITE_OK |
| 97 | } |
| 98 | lappend ::ba_log $method [file tail $file] |
| 99 | return SQLITE_OK |
| 100 | } |
| 101 | testvfs batvfs2 |
| 102 | batvfs2 devchar batch_atomic |
| 103 | batvfs2 script ba_vfs_callback2 |
| 104 | batvfs2 filter {xFileControl xWrite xSync} |
| 105 | |
| 106 | sqlite3 db test.db -vfs batvfs2 |
| 107 | set ::ba_log [list] |
| 108 | do_execsql_test 2.1 { |
| 109 | UPDATE t1 SET y = randomblob(800); |
| 110 | INSERT INTO t1 VALUES(3, randomblob(800)); |
| 111 | } {} |
| 112 | |
| 113 | do_test 2.2 { |
| 114 | set nDbWrite 0 |
| 115 | set nJrnl 0 |
| 116 | foreach {m f} $::ba_log { |
| 117 | if {$m=="xWrite" && $f=="test.db"} { incr nDbWrite } |
| 118 | if {$f=="test.db-journal"} { incr nJrnl } |
| 119 | } |
| 120 | list [expr {$nDbWrite>0}] $nJrnl |
| 121 | } {1 0} |
| 122 | |
| 123 | do_execsql_test 2.3 { PRAGMA integrity_check } ok |
| 124 | db close |
| 125 | |
| 126 | sqlite3 db test.db |
| 127 | do_execsql_test 2.4 { |
| 128 | SELECT count(*) FROM t1; |
| 129 | PRAGMA integrity_check; |
| 130 | } {3 ok} |
| 131 | db close |
| 132 | batvfs2 delete |
| 133 | |
| 134 | finish_test |