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".
16 # $Id: alter4.test,v 1.1 2009/02/02 18:03:22 drh Exp $
19 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
23 # If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
24 ifcapable !altertable {
33 # alter4-1.*: Test that ALTER TABLE correctly modifies the CREATE TABLE sql.
34 # alter4-2.*: Test error messages.
35 # alter4-3.*: Test adding columns with default value NULL.
36 # alter4-4.*: Test adding columns with default values other than NULL.
37 # alter4-5.*: Test adding columns to tables in ATTACHed databases.
38 # alter4-6.*: Test that temp triggers are not accidentally dropped.
39 # alter4-7.*: Test that VACUUM resets the file-format.
44 CREATE TEMP TABLE abc(a, b, c);
45 SELECT sql FROM sqlite_temp_master;
47 } {{CREATE TABLE abc(a, b, c)}}
50 SELECT sql FROM temp.sqlite_master;
52 } {{CREATE TABLE abc(a, b, c)}}
54 execsql {ALTER TABLE abc ADD d INTEGER;}
56 SELECT sql FROM sqlite_temp_master;
58 } {{CREATE TABLE abc(a, b, c, d INTEGER)}}
61 SELECT sql FROM temp.sqlite_master;
63 } {{CREATE TABLE abc(a, b, c, d INTEGER)}}
65 execsql {ALTER TABLE abc ADD e}
67 SELECT sql FROM sqlite_temp_master;
69 } {{CREATE TABLE abc(a, b, c, d INTEGER, e)}}
72 SELECT sql FROM temp.sqlite_master;
74 } {{CREATE TABLE abc(a, b, c, d INTEGER, e)}}
77 CREATE TABLE temp.t1(a, b);
79 SELECT sql FROM sqlite_temp_master WHERE tbl_name = 't1';
81 } {{CREATE TABLE t1(a, b, c)}}
84 SELECT sql FROM temp.sqlite_master WHERE tbl_name = 't1';
86 } {{CREATE TABLE t1(a, b, c)}}
89 ALTER TABLE t1 ADD d CHECK (a>d);
90 SELECT sql FROM sqlite_temp_master WHERE tbl_name = 't1';
92 } {{CREATE TABLE t1(a, b, c, d CHECK (a>d))}}
93 ifcapable foreignkey {
96 CREATE TEMP TABLE t2(a, b, UNIQUE(a, b));
97 ALTER TABLE t2 ADD c REFERENCES t1(c) ;
98 SELECT sql FROM sqlite_temp_master
99 WHERE tbl_name = 't2' AND type = 'table';
101 } {{CREATE TABLE t2(a, b, c REFERENCES t1(c), UNIQUE(a, b))}}
105 CREATE TEMPORARY TABLE t3(a, b, UNIQUE(a, b));
106 ALTER TABLE t3 ADD COLUMN c VARCHAR(10, 20);
107 SELECT sql FROM sqlite_temp_master
108 WHERE tbl_name = 't3' AND type = 'table';
110 } {{CREATE TABLE t3(a, b, c VARCHAR(10, 20), UNIQUE(a, b))}}
111 do_test alter4-1.99 {
113 # May not exist if foriegn-keys are omitted at compile time.
125 CREATE TABLE temp.t1(a, b);
126 INSERT INTO t1 VALUES(1,2);
129 ALTER TABLE t1 ADD c PRIMARY KEY;
131 } {1 {Cannot add a PRIMARY KEY column}}
134 ALTER TABLE t1 ADD c UNIQUE
136 } {1 {Cannot add a UNIQUE column}}
139 ALTER TABLE t1 ADD b VARCHAR(10)
141 } {1 {duplicate column name: b}}
144 ALTER TABLE t1 ADD c NOT NULL;
146 } {1 {Cannot add a NOT NULL column with default value NULL}}
149 ALTER TABLE t1 ADD c NOT NULL DEFAULT 10;
155 CREATE TEMPORARY VIEW v1 AS SELECT * FROM t1;
158 alter table v1 add column d;
160 } {1 {Cannot add a column to a view}}
164 alter table t1 add column d DEFAULT CURRENT_TIME;
166 } {1 {Cannot add a column with non-constant default}}
169 alter table t1 add column d default (-5+1);
171 } {1 {Cannot add a column with non-constant default}}
172 do_test alter4-2.99 {
180 CREATE TEMP TABLE t1(a, b);
181 INSERT INTO t1 VALUES(1, 100);
182 INSERT INTO t1 VALUES(2, 300);
188 PRAGMA schema_version = 10;
193 ALTER TABLE t1 ADD c;
196 } {1 100 {} 2 300 {}}
197 ifcapable schema_version {
200 PRAGMA schema_version;
208 set ::DB [sqlite3 db test.db]
210 CREATE TEMP TABLE t1(a, b);
211 INSERT INTO t1 VALUES(1, 100);
212 INSERT INTO t1 VALUES(2, 300);
218 PRAGMA schema_version = 20;
223 ALTER TABLE t1 ADD c DEFAULT 'hello world';
226 } {1 100 {hello world} 2 300 {hello world}}
227 ifcapable schema_version {
230 PRAGMA schema_version;
234 do_test alter4-4.99 {
243 forcedelete test2.db-journal
245 CREATE TEMP TABLE t1(a, b);
246 INSERT INTO t1 VALUES(1, 'one');
247 INSERT INTO t1 VALUES(2, 'two');
248 ATTACH 'test2.db' AS aux;
249 CREATE TABLE aux.t1 AS SELECT * FROM t1;
250 PRAGMA aux.schema_version = 30;
251 SELECT sql FROM aux.sqlite_master;
253 } {{CREATE TABLE t1(a,b)}}
256 ALTER TABLE aux.t1 ADD COLUMN c VARCHAR(128);
257 SELECT sql FROM aux.sqlite_master;
259 } {{CREATE TABLE t1(a,b, c VARCHAR(128))}}
262 SELECT * FROM aux.t1;
264 } {1 one {} 2 two {}}
265 ifcapable schema_version {
268 PRAGMA aux.schema_version;
274 ALTER TABLE aux.t1 ADD COLUMN d DEFAULT 1000;
275 SELECT sql FROM aux.sqlite_master;
277 } {{CREATE TABLE t1(a,b, c VARCHAR(128), d DEFAULT 1000)}}
280 SELECT * FROM aux.t1;
282 } {1 one {} 1000 2 two {} 1000}
283 ifcapable schema_version {
286 PRAGMA aux.schema_version;
295 do_test alter4-5.99 {
303 #----------------------------------------------------------------
304 # Test that the table schema is correctly reloaded when a column
305 # is added to a table.
307 ifcapable trigger&&tempdb {
310 CREATE TEMP TABLE t1(a, b);
311 CREATE TEMP TABLE log(trig, a, b);
313 CREATE TRIGGER t1_a AFTER INSERT ON t1 BEGIN
314 INSERT INTO log VALUES('a', new.a, new.b);
316 CREATE TEMP TRIGGER t1_b AFTER INSERT ON t1 BEGIN
317 INSERT INTO log VALUES('b', new.a, new.b);
320 INSERT INTO t1 VALUES(1, 2);
321 SELECT * FROM log ORDER BY trig, a, b;
326 ALTER TABLE t1 ADD COLUMN c DEFAULT 'c';
327 INSERT INTO t1(a, b) VALUES(3, 4);
328 SELECT * FROM log ORDER BY trig, a, b;
330 } {a 1 2 a 3 4 b 1 2 b 3 4}
333 # Ticket #1183 - Make sure adding columns to large tables does not cause
334 # memory corruption (as was the case before this bug was fixed).
337 CREATE TEMP TABLE t4(c1);
343 for {set i 2} {$i < 100} {incr i} {
345 ALTER TABLE t4 ADD c$i
349 set ::sql "CREATE TABLE t4([join $cols {, }])"
354 SELECT sql FROM sqlite_temp_master WHERE name = 't4';
359 # Test that a default value equal to -1 multipied by the smallest possible
360 # 64-bit integer is correctly converted to a real.
361 do_execsql_test alter4-9.1 {
363 a INTEGER DEFAULT -9223372036854775808,
364 b INTEGER DEFAULT (-(-9223372036854775808))
366 INSERT INTO t5 DEFAULT VALUES;
369 do_execsql_test alter4-9.2 { SELECT typeof(a), a, typeof(b), b FROM t5; } {
370 integer -9223372036854775808
371 real 9.22337203685478e+18
374 do_execsql_test alter4-9.3 {
375 ALTER TABLE t5 ADD COLUMN c INTEGER DEFAULT (-(-9223372036854775808));
376 SELECT typeof(c), c FROM t5;
377 } {real 9.22337203685478e+18}
379 # Confirm that doing an ALTER TABLE on a legacy format database
380 # does not corrupt DESC indexes.
382 # Ticket https://www.sqlite.org/src/tktview/f68bf68513a1c
384 do_test alter4-10.1 {
387 sqlite3_db_config db LEGACY_FILE_FORMAT 1
389 CREATE TABLE t1(a,b,c);
390 CREATE INDEX t1a ON t1(a DESC);
391 INSERT INTO t1 VALUES(1,2,3);
392 INSERT INTO t1 VALUES(2,3,4);
393 ALTER TABLE t1 ADD COLUMN d;
394 PRAGMA integrity_check;
399 do_execsql_test alter4-11.0 {
400 CREATE TABLE t1(c INTEGER PRIMARY KEY, d);
401 INSERT INTO t1(c,d) VALUES(1,2);
402 PRAGMA foreign_keys = on;
403 ALTER TABLE t1 ADD COLUMN e;
406 do_execsql_test alter4-11.1 {
407 ALTER TABLE t1 ADD COLUMN f REFERENCES t1;
410 do_catchsql_test alter4-11.2 {
411 ALTER TABLE t1 ADD COLUMN g REFERENCES t1 DEFAULT 4;
412 } {1 {Cannot add a REFERENCES column with non-NULL default value}}
414 do_catchsql_test alter4-11.3 {
415 ALTER TABLE t2 ADD COLUMN g;
416 } {1 {no such table: t2}}
419 do_execsql_test alter4-11.4 {
420 CREATE VIRTUAL TABLE fff USING fts5(f);
422 do_catchsql_test alter4-11.2 {
423 ALTER TABLE fff ADD COLUMN g;
424 } {1 {virtual tables may not be altered}}