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);
128 ALTER TABLE t1 ADD c PRIMARY KEY;
130 } {1 {Cannot add a PRIMARY KEY column}}
133 ALTER TABLE t1 ADD c UNIQUE
135 } {1 {Cannot add a UNIQUE column}}
138 ALTER TABLE t1 ADD b VARCHAR(10)
140 } {1 {duplicate column name: b}}
143 ALTER TABLE t1 ADD c NOT NULL;
145 } {1 {Cannot add a NOT NULL column with default value NULL}}
148 ALTER TABLE t1 ADD c NOT NULL DEFAULT 10;
154 CREATE TEMPORARY VIEW v1 AS SELECT * FROM t1;
157 alter table v1 add column d;
159 } {1 {Cannot add a column to a view}}
163 alter table t1 add column d DEFAULT CURRENT_TIME;
165 } {1 {Cannot add a column with non-constant default}}
168 alter table t1 add column d default (-5+1);
170 } {1 {Cannot add a column with non-constant default}}
171 do_test alter4-2.99 {
179 CREATE TEMP TABLE t1(a, b);
180 INSERT INTO t1 VALUES(1, 100);
181 INSERT INTO t1 VALUES(2, 300);
187 PRAGMA schema_version = 10;
192 ALTER TABLE t1 ADD c;
195 } {1 100 {} 2 300 {}}
196 ifcapable schema_version {
199 PRAGMA schema_version;
207 set ::DB [sqlite3 db test.db]
209 CREATE TEMP TABLE t1(a, b);
210 INSERT INTO t1 VALUES(1, 100);
211 INSERT INTO t1 VALUES(2, 300);
217 PRAGMA schema_version = 20;
222 ALTER TABLE t1 ADD c DEFAULT 'hello world';
225 } {1 100 {hello world} 2 300 {hello world}}
226 ifcapable schema_version {
229 PRAGMA schema_version;
233 do_test alter4-4.99 {
242 forcedelete test2.db-journal
244 CREATE TEMP TABLE t1(a, b);
245 INSERT INTO t1 VALUES(1, 'one');
246 INSERT INTO t1 VALUES(2, 'two');
247 ATTACH 'test2.db' AS aux;
248 CREATE TABLE aux.t1 AS SELECT * FROM t1;
249 PRAGMA aux.schema_version = 30;
250 SELECT sql FROM aux.sqlite_master;
252 } {{CREATE TABLE t1(a,b)}}
255 ALTER TABLE aux.t1 ADD COLUMN c VARCHAR(128);
256 SELECT sql FROM aux.sqlite_master;
258 } {{CREATE TABLE t1(a,b, c VARCHAR(128))}}
261 SELECT * FROM aux.t1;
263 } {1 one {} 2 two {}}
264 ifcapable schema_version {
267 PRAGMA aux.schema_version;
273 ALTER TABLE aux.t1 ADD COLUMN d DEFAULT 1000;
274 SELECT sql FROM aux.sqlite_master;
276 } {{CREATE TABLE t1(a,b, c VARCHAR(128), d DEFAULT 1000)}}
279 SELECT * FROM aux.t1;
281 } {1 one {} 1000 2 two {} 1000}
282 ifcapable schema_version {
285 PRAGMA aux.schema_version;
294 do_test alter4-5.99 {
302 #----------------------------------------------------------------
303 # Test that the table schema is correctly reloaded when a column
304 # is added to a table.
306 ifcapable trigger&&tempdb {
309 CREATE TEMP TABLE t1(a, b);
310 CREATE TEMP TABLE log(trig, a, b);
312 CREATE TRIGGER t1_a AFTER INSERT ON t1 BEGIN
313 INSERT INTO log VALUES('a', new.a, new.b);
315 CREATE TEMP TRIGGER t1_b AFTER INSERT ON t1 BEGIN
316 INSERT INTO log VALUES('b', new.a, new.b);
319 INSERT INTO t1 VALUES(1, 2);
325 ALTER TABLE t1 ADD COLUMN c DEFAULT 'c';
326 INSERT INTO t1(a, b) VALUES(3, 4);
329 } {b 1 2 a 1 2 b 3 4 a 3 4}
332 # Ticket #1183 - Make sure adding columns to large tables does not cause
333 # memory corruption (as was the case before this bug was fixed).
336 CREATE TEMP TABLE t4(c1);
342 for {set i 2} {$i < 100} {incr i} {
344 ALTER TABLE t4 ADD c$i
348 set ::sql "CREATE TABLE t4([join $cols {, }])"
353 SELECT sql FROM sqlite_temp_master WHERE name = 't4';
358 # Test that a default value equal to -1 multipied by the smallest possible
359 # 64-bit integer is correctly converted to a real.
360 do_execsql_test alter4-9.1 {
362 a INTEGER DEFAULT -9223372036854775808,
363 b INTEGER DEFAULT (-(-9223372036854775808))
365 INSERT INTO t5 DEFAULT VALUES;
368 do_execsql_test alter4-9.2 { SELECT typeof(a), a, typeof(b), b FROM t5; } {
369 integer -9223372036854775808
370 real 9.22337203685478e+18
373 do_execsql_test alter4-9.3 {
374 ALTER TABLE t5 ADD COLUMN c INTEGER DEFAULT (-(-9223372036854775808));
375 SELECT typeof(c), c FROM t5;
376 } {real 9.22337203685478e+18}
378 # Confirm that doing an ALTER TABLE on a legacy format database
379 # does not corrupt DESC indexes.
381 # Ticket https://www.sqlite.org/src/tktview/f68bf68513a1c
383 do_test alter4-10.1 {
387 PRAGMA legacy_file_format=on;
388 CREATE TABLE t1(a,b,c);
389 CREATE INDEX t1a ON t1(a DESC);
390 INSERT INTO t1 VALUES(1,2,3);
391 INSERT INTO t1 VALUES(2,3,4);
392 ALTER TABLE t1 ADD COLUMN d;
393 PRAGMA integrity_check;