# 2026 November 2 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: # # May you do good and not evil. # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #************************************************************************* # # Tests for ALTER TABLE DROP COLUMN executed while another statement on # the same connection is part-way through a scan of the table being # modified. # set testdir [file dirname $argv0] source $testdir/tester.tcl set testprefix alterdropcol3 ifcapable !altertable { finish_test return } # Run SELECT statement $select. After the first row is returned, run # $alter. Return a list containing one element for each row returned # by $select, then either "ALTER-ERROR" (if the ALTER failed) or # "ALTER-OK", then "SELECT-ERROR" if the remainder of the scan failed. # proc scan_and_alter {select alter} { set res [list] set bDone 0 set rc [catch { db eval $select x { set row [list] foreach c $x(*) { lappend row $x($c) } lappend res $row if {$bDone==0} { set bDone 1 if {[catch { db eval $alter } msg]} { lappend res ALTER-ERROR } else { lappend res ALTER-OK } } } } msg] if {$rc} { lappend res SELECT-ERROR } set res } # Check that the result of [scan_and_alter] is acceptable. $expect is # the list of rows the SELECT should return if the ALTER had not been # run. Return "ok" if the results are acceptable, or the actual # results otherwise. # proc check_scan {res expect} { set rows [list] foreach r $res { if {[string match *-* $r] && [llength $r]==1} continue lappend rows $r } set n [llength $rows] if {$rows ne [lrange $expect 0 [expr $n-1]]} { return $res } if {[lsearch $res SELECT-ERROR]<0 && $n!=[llength $expect]} { return $res } return ok } proc setup_t1 {{opts ""}} { db eval { DROP TABLE IF EXISTS t1 } db eval "CREATE TABLE t1(a PRIMARY KEY, b, c, d) $opts" db eval { INSERT INTO t1 VALUES('a1', 'b1', 'c1', 'd1'); INSERT INTO t1 VALUES('a2', 'b2', 'c2', 'd2'); INSERT INTO t1 VALUES('a3', 'b3', 'c3', 'd3'); } } set EXPECT {{a1 c1 d1} {a2 c2 d2} {a3 c3 d3}} #------------------------------------------------------------------------- # 1.*: Rowid table, full table scan. Drop a column that precedes the # columns being read. # do_test 1.0 { setup_t1 set ::res [scan_and_alter {SELECT a, c, d FROM t1} {ALTER TABLE t1 DROP b}] check_scan $::res $EXPECT } {ok} # The ALTER TABLE fails with SQLITE_LOCKED because there is another # active reader on the connection, so the table is unchanged. do_test 1.1 { set ::res } [list {a1 c1 d1} ALTER-ERROR {a2 c2 d2} {a3 c3 d3}] do_execsql_test 1.2 { SELECT * FROM t1 } {a1 b1 c1 d1 a2 b2 c2 d2 a3 b3 c3 d3} do_test 1.3 { set res [list] db eval {SELECT a FROM t1} { lappend res [catch { db eval {ALTER TABLE t1 DROP b} } msg] $msg lappend res [sqlite3_extended_errcode db] break } set res } {1 {database table is locked} SQLITE_LOCKED} # Once the scan has finished, the same ALTER TABLE succeeds. do_execsql_test 1.4 { ALTER TABLE t1 DROP b; SELECT * FROM t1; } {a1 c1 d1 a2 c2 d2 a3 c3 d3} #------------------------------------------------------------------------- # 2.*: WITHOUT ROWID table, full table scan. # do_test 2.0 { setup_t1 "WITHOUT ROWID" set ::res [scan_and_alter {SELECT a, c, d FROM t1} {ALTER TABLE t1 DROP b}] check_scan $::res $EXPECT } {ok} #------------------------------------------------------------------------- # 3.*: Rowid table, rows located via an index on column c (non-covering). # do_test 3.0 { setup_t1 db eval { CREATE INDEX t1c ON t1(c) } set ::res [scan_and_alter { SELECT a, c, d FROM t1 INDEXED BY t1c WHERE c>'c' ORDER BY c } {ALTER TABLE t1 DROP b}] check_scan $::res $EXPECT } {ok} #------------------------------------------------------------------------- # 4.*: Rowid table, rowid lookups driven by an outer loop over another # table. # do_test 4.0 { setup_t1 db eval { DROP TABLE IF EXISTS x1; CREATE TABLE x1(r INTEGER); INSERT INTO x1 VALUES(1), (2), (3); } set ::res [scan_and_alter { SELECT a, c, d FROM x1, t1 WHERE t1.rowid=x1.r } {ALTER TABLE t1 DROP b}] check_scan $::res $EXPECT } {ok} #------------------------------------------------------------------------- # 5.*: Drop the final column, while reading a column that precedes it. # Even if the scan continues, the values read should be correct. # do_test 5.0 { setup_t1 set ::res [scan_and_alter {SELECT a, b, c FROM t1} {ALTER TABLE t1 DROP d}] check_scan $::res {{a1 b1 c1} {a2 b2 c2} {a3 b3 c3}} } {ok} #------------------------------------------------------------------------- # 6.*: Same as 1.*, but using the sqlite3_prepare/sqlite3_step API # directly instead of [db eval]. # do_test 6.0 { setup_t1 set ::stmt [sqlite3_prepare_v2 db {SELECT a, c, d FROM t1} -1 dummy] set ::res [list] sqlite3_step $::stmt lappend ::res [list [sqlite3_column_text $::stmt 0] \ [sqlite3_column_text $::stmt 1] \ [sqlite3_column_text $::stmt 2]] if {[catch {db eval {ALTER TABLE t1 DROP b}}]} { lappend ::res ALTER-ERROR } else { lappend ::res ALTER-OK } while {[set rc [sqlite3_step $::stmt]]=="SQLITE_ROW"} { lappend ::res [list [sqlite3_column_text $::stmt 0] \ [sqlite3_column_text $::stmt 1] \ [sqlite3_column_text $::stmt 2]] } if {$rc!="SQLITE_DONE"} { lappend ::res SELECT-ERROR } sqlite3_finalize $::stmt check_scan $::res $EXPECT } {ok} #------------------------------------------------------------------------- # 7.*: Scanning a different table while a column is dropped from t1. # The check for active readers is connection-wide, so this also # fails with SQLITE_LOCKED, even though the scan could not be # affected by the ALTER. # do_test 7.0 { setup_t1 db eval { DROP TABLE IF EXISTS t2; CREATE TABLE t2(a, c, d); INSERT INTO t2 SELECT a, c, d FROM t1; } set ::res [scan_and_alter {SELECT a, c, d FROM t2} {ALTER TABLE t1 DROP b}] set ::res } [list {a1 c1 d1} ALTER-ERROR {a2 c2 d2} {a3 c3 d3}] do_execsql_test 7.1 { SELECT * FROM t1 } {a1 b1 c1 d1 a2 b2 c2 d2 a3 b3 c3 d3} finish_test