Snapshot of upstream SQLite 3.45.3
[sqlcipher.git] / ext / rbu / rbu_common.tcl
blob57db5a561fa5932b0378c62f20d7f0adf3c25a9f
1 # 2015 Aug 8
3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
13 if {![info exists testdir]} {
14 set testdir [file join [file dirname [info script]] .. .. test]
16 source $testdir/tester.tcl
18 proc if_no_rbu_support {tcl} {
19 set bOk 1
20 ifcapable !rbu { set bOk 0 }
21 if {[permutation]=="journaltest"} { set bOk 0 }
22 if {$bOk==0} {
23 set c [catch {uplevel 1 $tcl} r]
24 return -code $c $r
28 proc check_prestep_state {target state} {
29 set oal_exists [file exists $target-oal]
30 set wal_exists [file exists $target-wal]
31 set progress [rbu progress]
33 if {($progress==0 && $state!="oal" && $state!="done")
34 || ($oal_exists && $wal_exists)
35 || ($progress>0 && $state=="oal" && (!$oal_exists || $wal_exists))
36 || ($state=="move" && (!$oal_exists || $wal_exists))
37 || ($state=="checkpoint" && ($oal_exists || !$wal_exists))
38 || ($state=="done" && ($oal_exists && $progress!=0))
39 } {
40 error "B: state=$state progress=$progress oal=$oal_exists wal=$wal_exists"
44 proc check_poststep_state {rc target state} {
45 if {$rc=="SQLITE_OK" || $rc=="SQLITE_DONE"} {
46 set oal_exists [file exists $target-oal]
47 set wal_exists [file exists $target-wal]
48 if {$state=="move" && ($oal_exists || !$wal_exists)} {
49 error "A: state=$state progress=$progress oal=$oal_exists wal=$wal_exists"
54 # Run the RBU in file $rbu on target database $target until completion.
56 proc run_rbu {target rbu} {
57 sqlite3rbu rbu $target $rbu
58 while 1 {
59 set state [rbu state]
61 check_prestep_state $target $state
62 set rc [rbu step]
63 check_poststep_state $rc $target $state
65 if {$rc!="SQLITE_OK"} break
67 rbu close
70 proc step_rbu {target rbu} {
71 while 1 {
72 sqlite3rbu rbu $target $rbu
73 set state [rbu state]
74 check_prestep_state $target $state
75 set rc [rbu step]
76 check_poststep_state $rc $target $state
77 rbu close
78 if {$rc != "SQLITE_OK"} break
80 set rc
83 proc step_rbu_legacy {target rbu} {
84 while 1 {
85 sqlite3rbu rbu $target $rbu
86 set state [rbu state]
87 check_prestep_state $target $state
88 set rc [rbu step]
89 check_poststep_state $rc $target $state
90 rbu close
91 if {$rc != "SQLITE_OK"} break
92 sqlite3 tmpdb $rbu
93 tmpdb eval { DELETE FROM rbu_state WHERE k==10 }
94 tmpdb close
96 set rc
99 proc do_rbu_vacuum_test {tn step {statedb state.db}} {
100 forcedelete $statedb
101 if {$statedb=="" && $step==1} breakpoint
102 uplevel [list do_test $tn.1 [string map [list %state% $statedb %step% $step] {
103 if {%step%==0} { sqlite3rbu_vacuum rbu test.db {%state%}}
104 while 1 {
105 if {%step%==1} { sqlite3rbu_vacuum rbu test.db {%state%}}
106 set state [rbu state]
107 check_prestep_state test.db $state
108 set rc [rbu step]
109 check_poststep_state $rc test.db $state
110 if {$rc!="SQLITE_OK"} break
111 if {%step%==1} { rbu close }
113 rbu close
114 }] {SQLITE_DONE}]
116 uplevel [list do_execsql_test $tn.2 {
117 PRAGMA integrity_check
118 } ok]