interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / atomic3.test

134 lines · 3903 bytes Raw Blame History
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
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20set ::testprefix atomic3
21
22ifcapable !batchatomicwrite {
23 finish_test
24 return
25}
26
27do_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}
33db 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#
42proc 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}
50testvfs batvfs
51batvfs devchar batch_atomic
52batvfs script ba_vfs_callback
53batvfs 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#
59sqlite3 db test.db -vfs batvfs
60do_execsql_test 1.1 { SELECT count(*) FROM t1 } 2
61set ::ba_log [list]
62do_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#
67set iDbWrite -1 ;# Index of first write to db.
68set iJrnlSync -1 ;# Index of first sync of journal file,
69
70set i 0
71foreach {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}
76do_test 1.3 {
77 expr {$iJrnlSync>=0 && $iJrnlSync<$iDbWrite}
78} 1
79do_execsql_test 1.4 { PRAGMA integrity_check } ok
80
81db close
82batvfs 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#
90proc 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}
101testvfs batvfs2
102batvfs2 devchar batch_atomic
103batvfs2 script ba_vfs_callback2
104batvfs2 filter {xFileControl xWrite xSync}
105
106sqlite3 db test.db -vfs batvfs2
107set ::ba_log [list]
108do_execsql_test 2.1 {
109 UPDATE t1 SET y = randomblob(800);
110 INSERT INTO t1 VALUES(3, randomblob(800));
111} {}
112
113do_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
123do_execsql_test 2.3 { PRAGMA integrity_check } ok
124db close
125
126sqlite3 db test.db
127do_execsql_test 2.4 {
128 SELECT count(*) FROM t1;
129 PRAGMA integrity_check;
130} {3 ok}
131db close
132batvfs2 delete
133
134finish_test