Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/test/alterdropcol3.test # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / test / alterdropcol3.test` 213 lines ยท 6452 bytes Raw Blame History 1 # 2026 November 2 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 # 12 # Tests for ALTER TABLE DROP COLUMN executed while another statement on 13 # the same connection is part-way through a scan of the table being 14 # modified. 15 # 16 17 set testdir [file dirname $argv0] 18 source $testdir/tester.tcl 19 set testprefix alterdropcol3 20 21 ifcapable !altertable { 22 finish_test 23 return 24 } 25 26 # Run SELECT statement $select. After the first row is returned, run 27 # $alter. Return a list containing one element for each row returned 28 # by $select, then either "ALTER-ERROR" (if the ALTER failed) or 29 # "ALTER-OK", then "SELECT-ERROR" if the remainder of the scan failed. 30 # 31 proc scan_and_alter {select alter} { 32 set res [list] 33 set bDone 0 34 set rc [catch { 35 db eval $select x { 36 set row [list] 37 foreach c $x(*) { lappend row $x($c) } 38 lappend res $row 39 if {$bDone==0} { 40 set bDone 1 41 if {[catch { db eval $alter } msg]} { 42 lappend res ALTER-ERROR 43 } else { 44 lappend res ALTER-OK 45 } 46 } 47 } 48 } msg] 49 if {$rc} { lappend res SELECT-ERROR } 50 set res 51 } 52 53 # Check that the result of [scan_and_alter] is acceptable. $expect is 54 # the list of rows the SELECT should return if the ALTER had not been 55 # run. Return "ok" if the results are acceptable, or the actual 56 # results otherwise. 57 # 58 proc check_scan {res expect} { 59 set rows [list] 60 foreach r $res { 61 if {[string match *-* $r] && [llength $r]==1} continue 62 lappend rows $r 63 } 64 set n [llength $rows] 65 if {$rows ne [lrange $expect 0 [expr $n-1]]} { return $res } 66 if {[lsearch $res SELECT-ERROR]<0 && $n!=[llength $expect]} { return $res } 67 return ok 68 } 69 70 proc setup_t1 {{opts ""}} { 71 db eval { DROP TABLE IF EXISTS t1 } 72 db eval "CREATE TABLE t1(a PRIMARY KEY, b, c, d) $opts" 73 db eval { 74 INSERT INTO t1 VALUES('a1', 'b1', 'c1', 'd1'); 75 INSERT INTO t1 VALUES('a2', 'b2', 'c2', 'd2'); 76 INSERT INTO t1 VALUES('a3', 'b3', 'c3', 'd3'); 77 } 78 } 79 80 set EXPECT {{a1 c1 d1} {a2 c2 d2} {a3 c3 d3}} 81 82 #------------------------------------------------------------------------- 83 # 1.*: Rowid table, full table scan. Drop a column that precedes the 84 # columns being read. 85 # 86 do_test 1.0 { 87 setup_t1 88 set ::res [scan_and_alter {SELECT a, c, d FROM t1} {ALTER TABLE t1 DROP b}] 89 check_scan $::res $EXPECT 90 } {ok} 91 92 # The ALTER TABLE fails with SQLITE_LOCKED because there is another 93 # active reader on the connection, so the table is unchanged. 94 do_test 1.1 { 95 set ::res 96 } [list {a1 c1 d1} ALTER-ERROR {a2 c2 d2} {a3 c3 d3}] 97 do_execsql_test 1.2 { 98 SELECT * FROM t1 99 } {a1 b1 c1 d1 a2 b2 c2 d2 a3 b3 c3 d3} 100 101 do_test 1.3 { 102 set res [list] 103 db eval {SELECT a FROM t1} { 104 lappend res [catch { db eval {ALTER TABLE t1 DROP b} } msg] $msg 105 lappend res [sqlite3_extended_errcode db] 106 break 107 } 108 set res 109 } {1 {database table is locked} SQLITE_LOCKED} 110 111 # Once the scan has finished, the same ALTER TABLE succeeds. 112 do_execsql_test 1.4 { 113 ALTER TABLE t1 DROP b; 114 SELECT * FROM t1; 115 } {a1 c1 d1 a2 c2 d2 a3 c3 d3} 116 117 118 #------------------------------------------------------------------------- 119 # 2.*: WITHOUT ROWID table, full table scan. 120 # 121 do_test 2.0 { 122 setup_t1 "WITHOUT ROWID" 123 set ::res [scan_and_alter {SELECT a, c, d FROM t1} {ALTER TABLE t1 DROP b}] 124 check_scan $::res $EXPECT 125 } {ok} 126 127 #------------------------------------------------------------------------- 128 # 3.*: Rowid table, rows located via an index on column c (non-covering). 129 # 130 do_test 3.0 { 131 setup_t1 132 db eval { CREATE INDEX t1c ON t1(c) } 133 set ::res [scan_and_alter { 134 SELECT a, c, d FROM t1 INDEXED BY t1c WHERE c>'c' ORDER BY c 135 } {ALTER TABLE t1 DROP b}] 136 check_scan $::res $EXPECT 137 } {ok} 138 139 #------------------------------------------------------------------------- 140 # 4.*: Rowid table, rowid lookups driven by an outer loop over another 141 # table. 142 # 143 do_test 4.0 { 144 setup_t1 145 db eval { 146 DROP TABLE IF EXISTS x1; 147 CREATE TABLE x1(r INTEGER); 148 INSERT INTO x1 VALUES(1), (2), (3); 149 } 150 set ::res [scan_and_alter { 151 SELECT a, c, d FROM x1, t1 WHERE t1.rowid=x1.r 152 } {ALTER TABLE t1 DROP b}] 153 check_scan $::res $EXPECT 154 } {ok} 155 156 #------------------------------------------------------------------------- 157 # 5.*: Drop the final column, while reading a column that precedes it. 158 # Even if the scan continues, the values read should be correct. 159 # 160 do_test 5.0 { 161 setup_t1 162 set ::res [scan_and_alter {SELECT a, b, c FROM t1} {ALTER TABLE t1 DROP d}] 163 check_scan $::res {{a1 b1 c1} {a2 b2 c2} {a3 b3 c3}} 164 } {ok} 165 166 #------------------------------------------------------------------------- 167 # 6.*: Same as 1.*, but using the sqlite3_prepare/sqlite3_step API 168 # directly instead of [db eval]. 169 # 170 do_test 6.0 { 171 setup_t1 172 set ::stmt [sqlite3_prepare_v2 db {SELECT a, c, d FROM t1} -1 dummy] 173 set ::res [list] 174 sqlite3_step $::stmt 175 lappend ::res [list [sqlite3_column_text $::stmt 0] \ 176 [sqlite3_column_text $::stmt 1] \ 177 [sqlite3_column_text $::stmt 2]] 178 if {[catch {db eval {ALTER TABLE t1 DROP b}}]} { 179 lappend ::res ALTER-ERROR 180 } else { 181 lappend ::res ALTER-OK 182 } 183 while {[set rc [sqlite3_step $::stmt]]=="SQLITE_ROW"} { 184 lappend ::res [list [sqlite3_column_text $::stmt 0] \ 185 [sqlite3_column_text $::stmt 1] \ 186 [sqlite3_column_text $::stmt 2]] 187 } 188 if {$rc!="SQLITE_DONE"} { lappend ::res SELECT-ERROR } 189 sqlite3_finalize $::stmt 190 check_scan $::res $EXPECT 191 } {ok} 192 193 #------------------------------------------------------------------------- 194 # 7.*: Scanning a different table while a column is dropped from t1. 195 # The check for active readers is connection-wide, so this also 196 # fails with SQLITE_LOCKED, even though the scan could not be 197 # affected by the ALTER. 198 # 199 do_test 7.0 { 200 setup_t1 201 db eval { 202 DROP TABLE IF EXISTS t2; 203 CREATE TABLE t2(a, c, d); 204 INSERT INTO t2 SELECT a, c, d FROM t1; 205 } 206 set ::res [scan_and_alter {SELECT a, c, d FROM t2} {ALTER TABLE t1 DROP b}] 207 set ::res 208 } [list {a1 c1 d1} ALTER-ERROR {a2 c2 d2} {a3 c3 d3}] 209 do_execsql_test 7.1 { 210 SELECT * FROM t1 211 } {a1 b1 c1 d1 a2 b2 c2 d2 a3 b3 c3 d3} 212 213 finish_test