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 #*************************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this script is testing that SQLite can handle a subtle
13 # file format change that may be used in the future to implement
14 # "ALTER TABLE ... ADD COLUMN".
17 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
21 # If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
22 ifcapable !altertable {
27 # Determine if there is a codec available on this test.
29 if {[catch {sqlite3 -has-codec} r] || $r} {
39 # alter3-1.*: Test that ALTER TABLE correctly modifies the CREATE TABLE sql.
40 # alter3-2.*: Test error messages.
41 # alter3-3.*: Test adding columns with default value NULL.
42 # alter3-4.*: Test adding columns with default values other than NULL.
43 # alter3-5.*: Test adding columns to tables in ATTACHed databases.
44 # alter3-6.*: Test that temp triggers are not accidentally dropped.
45 # alter3-7.*: Test that VACUUM resets the file-format.
48 # This procedure returns the value of the file-format in file 'test.db'.
50 proc get_file_format {{fname test.db}} {
51 return [hexio_get_int [hexio_read $fname 44 4]]
55 sqlite3_db_config db LEGACY_FILE_FORMAT 1
57 CREATE TABLE abc(a, b, c);
58 SELECT sql FROM sqlite_master;
60 } {{CREATE TABLE abc(a, b, c)}}
62 execsql {ALTER TABLE abc ADD d INTEGER;}
64 SELECT sql FROM sqlite_master;
66 } {{CREATE TABLE abc(a, b, c, d INTEGER)}}
68 execsql {ALTER TABLE abc ADD e}
70 SELECT sql FROM sqlite_master;
72 } {{CREATE TABLE abc(a, b, c, d INTEGER, e)}}
75 CREATE TABLE main.t1(a, b);
77 SELECT sql FROM sqlite_master WHERE tbl_name = 't1';
79 } {{CREATE TABLE t1(a, b, c)}}
82 ALTER TABLE t1 ADD d CHECK (a>d);
83 SELECT sql FROM sqlite_master WHERE tbl_name = 't1';
85 } {{CREATE TABLE t1(a, b, c, d CHECK (a>d))}}
86 ifcapable foreignkey {
89 CREATE TABLE t2(a, b, UNIQUE(a, b));
90 ALTER TABLE t2 ADD c REFERENCES t1(c) ;
91 SELECT sql FROM sqlite_master WHERE tbl_name = 't2' AND type = 'table';
93 } {{CREATE TABLE t2(a, b, c REFERENCES t1(c), UNIQUE(a, b))}}
97 CREATE TABLE t3(a, b, UNIQUE(a, b));
98 ALTER TABLE t3 ADD COLUMN c VARCHAR(10, 20);
99 SELECT sql FROM sqlite_master WHERE tbl_name = 't3' AND type = 'table';
101 } {{CREATE TABLE t3(a, b, c VARCHAR(10, 20), UNIQUE(a, b))}}
102 do_test alter3-1.99 {
104 # May not exist if foriegn-keys are omitted at compile time.
116 CREATE TABLE t1(a, b);
117 INSERT INTO t1 VALUES(1,2);
120 ALTER TABLE t1 ADD c PRIMARY KEY;
122 } {1 {Cannot add a PRIMARY KEY column}}
125 ALTER TABLE t1 ADD c UNIQUE
127 } {1 {Cannot add a UNIQUE column}}
130 ALTER TABLE t1 ADD b VARCHAR(10)
132 } {1 {duplicate column name: b}}
135 ALTER TABLE t1 ADD c NOT NULL;
137 } {1 {Cannot add a NOT NULL column with default value NULL}}
140 ALTER TABLE t1 ADD c NOT NULL DEFAULT 10;
146 CREATE VIEW v1 AS SELECT * FROM t1;
149 alter table v1 add column d;
151 } {1 {Cannot add a column to a view}}
155 alter table t1 add column d DEFAULT CURRENT_TIME;
157 } {1 {Cannot add a column with non-constant default}}
158 do_test alter3-2.99 {
166 CREATE TABLE t1(a, b);
167 INSERT INTO t1 VALUES(1, 100);
168 INSERT INTO t1 VALUES(2, 300);
174 PRAGMA schema_version = 10;
179 ALTER TABLE t1 ADD c;
182 } {1 100 {} 2 300 {}}
188 ifcapable schema_version {
191 PRAGMA schema_version;
199 set ::DB [sqlite3 db test.db]
200 sqlite3_db_config db LEGACY_FILE_FORMAT 1
202 CREATE TABLE t1(a, b);
203 INSERT INTO t1 VALUES(1, 100);
204 INSERT INTO t1 VALUES(2, 300);
210 PRAGMA schema_version = 20;
215 ALTER TABLE t1 ADD c DEFAULT 'hello world';
218 } {1 100 {hello world} 2 300 {hello world}}
224 ifcapable schema_version {
227 PRAGMA schema_version;
231 do_test alter3-4.99 {
240 forcedelete test2.db-journal
242 CREATE TABLE t1(a, b);
243 INSERT INTO t1 VALUES(1, 'one');
244 INSERT INTO t1 VALUES(2, 'two');
245 ATTACH 'test2.db' AS aux;
246 CREATE TABLE aux.t1 AS SELECT * FROM t1;
247 PRAGMA aux.schema_version = 30;
248 SELECT sql FROM aux.sqlite_master;
250 } {{CREATE TABLE t1(a,b)}}
253 ALTER TABLE aux.t1 ADD COLUMN c VARCHAR(128);
254 SELECT sql FROM aux.sqlite_master;
256 } {{CREATE TABLE t1(a,b, c VARCHAR(128))}}
259 SELECT * FROM aux.t1;
261 } {1 one {} 2 two {}}
262 ifcapable schema_version {
265 PRAGMA aux.schema_version;
271 list [get_file_format test2.db] [get_file_format]
276 ALTER TABLE aux.t1 ADD COLUMN d DEFAULT 1000;
277 SELECT sql FROM aux.sqlite_master;
279 } {{CREATE TABLE t1(a,b, c VARCHAR(128), d DEFAULT 1000)}}
282 SELECT * FROM aux.t1;
284 } {1 one {} 1000 2 two {} 1000}
285 ifcapable schema_version {
288 PRAGMA aux.schema_version;
297 do_test alter3-5.99 {
305 #----------------------------------------------------------------
306 # Test that the table schema is correctly reloaded when a column
307 # is added to a table.
309 ifcapable trigger&&tempdb {
312 CREATE TABLE t1(a, b);
313 CREATE TABLE log(trig, a, b);
315 CREATE TRIGGER t1_a AFTER INSERT ON t1 BEGIN
316 INSERT INTO log VALUES('a', new.a, new.b);
318 CREATE TEMP TRIGGER t1_b AFTER INSERT ON t1 BEGIN
319 INSERT INTO log VALUES('b', new.a, new.b);
322 INSERT INTO t1 VALUES(1, 2);
328 ALTER TABLE t1 ADD COLUMN c DEFAULT 'c';
329 INSERT INTO t1(a, b) VALUES(3, 4);
332 } {b 1 2 a 1 2 b 3 4 a 3 4}
345 CREATE TABLE abc(a, b, c);
346 ALTER TABLE abc ADD d DEFAULT NULL;
352 ALTER TABLE abc ADD e DEFAULT 10;
358 ALTER TABLE abc ADD f DEFAULT NULL;
371 # Ticket #1183 - Make sure adding columns to large tables does not cause
372 # memory corruption (as was the case before this bug was fixed).
381 for {set i 2} {$i < 100} {incr i} {
383 ALTER TABLE t4 ADD c$i
387 set ::sql "CREATE TABLE t4([join $cols {, }])"
392 SELECT sql FROM sqlite_master WHERE name = 't4';
396 # 2021-07-20: Add support for detecting CHECK and NOT NULL constraint
397 # violations in ALTER TABLE ADD COLUMN
400 do_execsql_test alter3-9.1 {
401 CREATE TABLE t1(a,b);
402 INSERT INTO t1 VALUES(1, 2), ('null!',NULL), (3,4);
404 do_catchsql_test alter3-9.2 {
405 ALTER TABLE t1 ADD COLUMN c CHECK(a!=1);
406 } {1 {CHECK constraint failed}}
407 do_catchsql_test alter3-9.3 {
408 ALTER TABLE t1 ADD COLUMN c CHECK(a!=3);
409 } {1 {CHECK constraint failed}}
410 do_catchsql_test alter3-9.4 {
411 ALTER TABLE t1 ADD COLUMN c CHECK(a!=2);
413 do_catchsql_test alter3-9.5 {
414 ALTER TABLE t1 ADD COLUMN d AS (b+1) NOT NULL;
415 } {1 {NOT NULL constraint failed}}
416 do_catchsql_test alter3-9.6 {
417 ALTER TABLE t1 ADD COLUMN d AS (b+1) NOT NULL CHECK(a!=1);
418 } {1 {CHECK constraint failed}}
419 do_catchsql_test alter3-9.7 {
420 ALTER TABLE t1 ADD COLUMN d AS (b+1) NOT NULL CHECK(a!=3);
421 } {1 {NOT NULL constraint failed}}
423 do_execsql_test alter3-9.10 {
424 CREATE TEMP TABLE t0(m,n);
425 INSERT INTO t0 VALUES(1, 2), ('null!',NULL), (3,4);
426 ATTACH ':memory:' AS aux1;
427 CREATE TABLE aux1.t2(x,y);
428 INSERT INTO t2 VALUES(1, 2), ('null!',NULL), (3,4);
430 do_catchsql_test alter3-9.11 {
431 ALTER TABLE t0 ADD COLUMN xtra1 AS (n+1) NOT NULL CHECK(m!=1);
432 } {1 {CHECK constraint failed}}
433 do_catchsql_test alter3-9.12 {
434 ALTER TABLE t0 ADD COLUMN xtra1 AS (n+1) NOT NULL CHECK(m!=3);
435 } {1 {NOT NULL constraint failed}}
436 do_catchsql_test alter3-9.13 {
437 ALTER TABLE t2 ADD COLUMN xtra1 AS (y+1) NOT NULL CHECK(x!=1);
438 } {1 {CHECK constraint failed}}
439 do_catchsql_test alter3-9.14 {
440 ALTER TABLE t2 ADD COLUMN xtra1 AS (y+1) NOT NULL CHECK(x!=3);
441 } {1 {NOT NULL constraint failed}}