1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
# 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
|