summaryrefslogtreecommitdiffstats
path: root/test/alterdropcol.test
blob: 8674e981a33a3ab2bd58f9491ee907631ffff7a7 (plain)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# 2021 February 16
#
# 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.
#
#*************************************************************************
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix alterdropcol

# If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
ifcapable !altertable {
  finish_test
  return
}

do_execsql_test 1.0 {
  CREATE TABLE t1(a, b, c);
  CREATE VIEW v1 AS SELECT * FROM t1;

  CREATE TABLE t2(x INTEGER PRIMARY KEY, y, z UNIQUE);
  CREATE INDEX t2y ON t2(y);

  CREATE TABLE t3(q, r, s);
  CREATE INDEX t3rs ON t3(r+s);
}

do_catchsql_test 1.1 {
  ALTER TABLE nosuch DROP COLUMN z;
} {1 {no such table: nosuch}}

do_catchsql_test 1.2 {
  ALTER TABLE v1 DROP COLUMN c;
} {1 {cannot drop column from view "v1"}}

ifcapable fts5 {
  do_execsql_test 1.3.1 {
    CREATE VIRTUAL TABLE ft1 USING fts5(one, two);
  }
  do_catchsql_test 1.3.2 {
    ALTER TABLE ft1 DROP COLUMN two;
  } {1 {cannot drop column from virtual table "ft1"}}
}

do_catchsql_test 1.4 {
  ALTER TABLE sqlite_schema DROP COLUMN sql;
} {1 {table sqlite_master may not be altered}}

do_catchsql_test 1.5 {
  ALTER TABLE t1 DROP COLUMN d;
} {1 {no such column: "d"}}

do_execsql_test 1.6.1 {
  ALTER TABLE t1 DROP COLUMN b;
}
do_execsql_test 1.6.2 {
  SELECT sql FROM sqlite_schema WHERE name = 't1'
} {{CREATE TABLE t1(a, c)}}

do_execsql_test 1.7.1 {
  ALTER TABLE t1 DROP COLUMN c;
}
do_execsql_test 1.7.2 {
  SELECT sql FROM sqlite_schema WHERE name = 't1'
} {{CREATE TABLE t1(a)}}

do_catchsql_test 1.7.3 {
  ALTER TABLE t1 DROP COLUMN a;
} {1 {cannot drop column "a": no other columns exist}}


do_catchsql_test 1.8 {
  ALTER TABLE t2 DROP COLUMN z
} {1 {cannot drop UNIQUE column: "z"}}

do_catchsql_test 1.9 {
  ALTER TABLE t2 DROP COLUMN x
} {1 {cannot drop PRIMARY KEY column: "x"}}

do_catchsql_test 1.10 {
  ALTER TABLE t2 DROP COLUMN y
} {1 {error in index t2y after drop column: no such column: y}}

do_catchsql_test 1.11 {
  ALTER TABLE t3 DROP COLUMN s
} {1 {error in index t3rs after drop column: no such column: s}}

#-------------------------------------------------------------------------

foreach {tn wo} {
  1 {}
  2 {WITHOUT ROWID}
} { eval [string map [list %TN% $tn %WO% $wo] {

  reset_db
  do_execsql_test 2.%TN%.0 {
    CREATE TABLE t1(x, y INTEGER PRIMARY KEY, z) %WO% ;
    INSERT INTO t1 VALUES(1, 2, 3);
    INSERT INTO t1 VALUES(4, 5, 6);
    INSERT INTO t1 VALUES(7, 8, 9);
  }
  
  do_execsql_test 2.%TN%.1 {
    ALTER TABLE t1 DROP COLUMN x;
    SELECT * FROM t1;
  } {
    2 3  5 6  8 9
  }
  do_execsql_test 2.%TN%.2 {
    ALTER TABLE t1 DROP COLUMN z;
    SELECT * FROM t1;
  } {
    2 5 8
  }
}]}

#-------------------------------------------------------------------------
reset_db

do_execsql_test 3.0 {
  CREATE TABLE t12(a, b, c, CHECK(c>10));
  CREATE TABLE t13(a, b, c CHECK(c>10));
}
do_catchsql_test 3.1 {
  ALTER TABLE t12 DROP COLUMN c;
} {1 {error in table t12 after drop column: no such column: c}}

do_catchsql_test 3.2 {
  ALTER TABLE t13 DROP COLUMN c;
} {0 {}}

#-------------------------------------------------------------------------
# Test that generated columns can be dropped. And that other columns from
# tables that contain generated columns can be dropped.
#
foreach {tn wo vs} {
  1 ""              ""
  2 ""              VIRTUAL
  3 ""              STORED
  4 "WITHOUT ROWID" STORED
  5 "WITHOUT ROWID" VIRTUAL
} {
  reset_db

  do_execsql_test 4.$tn.0 "
    CREATE TABLE 'my table'(a, b PRIMARY KEY, c AS (a+b) $vs, d) $wo
  "
  do_execsql_test 4.$tn.1 {
    INSERT INTO "my table"(a, b, d) VALUES(1, 2, 'hello');
    INSERT INTO "my table"(a, b, d) VALUES(3, 4, 'world');

    SELECT * FROM "my table"
  } {
    1 2 3 hello
    3 4 7 world
  }

  do_execsql_test 4.$tn.2 {
    ALTER TABLE "my table" DROP COLUMN c;
  }
  do_execsql_test 4.$tn.3 {
    SELECT * FROM "my table"
  } {
    1 2 hello
    3 4 world
  }
  
  do_execsql_test 4.$tn.4 "
    CREATE TABLE x1(a, b, c PRIMARY KEY, d AS (b+c) $vs, e) $wo
  "
  do_execsql_test 4.$tn.5 {
    INSERT INTO x1(a, b, c, e) VALUES(1, 2, 3, 4);
    INSERT INTO x1(a, b, c, e) VALUES(5, 6, 7, 8);
    INSERT INTO x1(a, b, c, e) VALUES(9, 10, 11, 12);
    SELECT * FROM x1;
  } {
    1 2 3 5 4
    5 6 7 13 8
    9 10 11 21 12
  }

  do_execsql_test 4.$tn.6 {
    ALTER TABLE x1 DROP COLUMN a
  }
  do_execsql_test 4.$tn.7 {
    SELECT * FROM x1
  } {
    2 3 5 4
    6 7 13 8
    10 11 21 12
  }
  do_execsql_test 4.$tn.8 {
    ALTER TABLE x1 DROP COLUMN e
  }
  do_execsql_test 4.$tn.9 {
    SELECT * FROM x1
  } {
    2 3 5
    6 7 13
    10 11 21
  }
}

#-------------------------------------------------------------------------
reset_db
do_execsql_test 5.0 {
  CREATE TABLE p1(a PRIMARY KEY, b UNIQUE);
  CREATE TABLE c1(x, y, z REFERENCES p1(c));
  CREATE TABLE c2(x, y, z, w REFERENCES p1(b));
}
do_execsql_test 5.1 {
  ALTER TABLE c1 DROP COLUMN z;
  ALTER TABLE c2 DROP COLUMN z;
  SELECT sql FROM sqlite_schema WHERE name IN ('c1', 'c2');
} {
  {CREATE TABLE c1(x, y)} 
  {CREATE TABLE c2(x, y, w REFERENCES p1(b))}
}

do_execsql_test 5.2.1 {
  CREATE VIEW v1 AS SELECT d, e FROM p1
}
do_catchsql_test 5.2.2 {
  ALTER TABLE c1 DROP COLUMN x
} {1 {error in view v1: no such column: d}}
do_execsql_test 5.3.1 {
  DROP VIEW v1;
  CREATE VIEW v1 AS SELECT x, y FROM c1;
}
do_catchsql_test 5.3.2 {
  ALTER TABLE c1 DROP COLUMN x
} {1 {error in view v1 after drop column: no such column: x}}

do_execsql_test 5.4.1 {
  CREATE TRIGGER tr AFTER INSERT ON c1 BEGIN
    INSERT INTO p1 VALUES(new.y, new.xyz);
  END;
}
do_catchsql_test 5.4.2 {
  ALTER TABLE c1 DROP COLUMN y
} {1 {error in trigger tr: no such column: new.xyz}}
do_execsql_test 5.5.1 {
  DROP TRIGGER tr;
  CREATE TRIGGER tr AFTER INSERT ON c1 BEGIN
    INSERT INTO p1 VALUES(new.y, new.z);
  END;
}
do_catchsql_test 5.5.2 {
  ALTER TABLE c1 DROP COLUMN y
} {1 {error in trigger tr: no such column: new.z}}

# 2021-03-06 dbsqlfuzz crash-419aa525df93db6e463772c686ac6da27b46da9e
reset_db
do_catchsql_test 6.0 {
  CREATE TABLE t1(a,b,c);
  CREATE TABLE t2(x,y,z);
  PRAGMA writable_schema=ON;
  UPDATE sqlite_schema SET sql='CREATE INDEX t1b ON t1(b)' WHERE name='t2';
  PRAGMA writable_schema=OFF;
  ALTER TABLE t2 DROP COLUMN z;
} {1 {database disk image is malformed}}
reset_db
do_catchsql_test 6.1 {
  CREATE TABLE t1(a,b,c);
  CREATE TABLE t2(x,y,z);
  PRAGMA writable_schema=ON;
  UPDATE sqlite_schema SET sql='CREATE VIEW t2(x,y,z) AS SELECT b,a,c FROM t1'
   WHERE name='t2';
  PRAGMA writable_schema=OFF;
  ALTER TABLE t2 DROP COLUMN z;
} {1 {database disk image is malformed}}

# 2021-04-06 dbsqlfuzz crash-331c5c29bb76257b198f1318eef3288f9624c8ce
reset_db
do_execsql_test 7.0 {
  CREATE TABLE t1(a, b, c, PRIMARY KEY(a COLLATE nocase, a)) WITHOUT ROWID;
  INSERT INTO t1 VALUES(1, 2, 3);
  INSERT INTO t1 VALUES(4, 5, 6);
}
do_execsql_test 7.1 {
  ALTER TABLE t1 DROP COLUMN c;                
}
do_execsql_test 7.2 {
  SELECT sql FROM sqlite_schema;
} {{CREATE TABLE t1(a, b, PRIMARY KEY(a COLLATE nocase, a)) WITHOUT ROWID}}
do_execsql_test 7.3 {
  SELECT * FROM t1;
} {1 2 4 5}

reset_db
do_execsql_test 8.0 {
  CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
  PRAGMA writable_schema = 1;
  UPDATE sqlite_schema 
  SET sql = 'CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b)'
}
db close
sqlite3 db test.db
do_execsql_test 8.1 {
  ALTER TABLE t1 DROP COLUMN b;                
}
do_execsql_test 8.2 {
  SELECT sql FROM sqlite_schema;
} {{CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT)}}

#-------------------------------------------------------------------------

foreach {tn wo} {
  1 {}
  2 {WITHOUT ROWID}
} {
  reset_db
  do_execsql_test 9.$tn.0 "
    CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c) $wo;
  "
  do_execsql_test 9.$tn.1 {
    WITH s(i) AS (
        SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<50000
    )
    INSERT INTO t1(a, b, c) SELECT i, 123, 456 FROM s;
  }
  do_execsql_test 9.$tn.2 {
    ALTER TABLE t1 DROP COLUMN b;
  }

  do_execsql_test 9.$tn.3 {
    SELECT count(*), c FROM t1 GROUP BY c;
  } {50000 456}
}



finish_test