xref: /sqlite-3.40.0/test/alter2.test (revision aee18ef8)
1# 2005 February 18
2#
3# The author disclaims copyright to this source code.  In place of
4# a legal notice, here is a blessing:
5#
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.
9#
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".
15#
16# $Id: alter2.test,v 1.3 2005/03/09 12:26:51 danielk1977 Exp $
17#
18
19set testdir [file dirname $argv0]
20source $testdir/tester.tcl
21
22# We have to have pragmas in order to do this test
23ifcapable {!pragma} return
24
25# The file format change affects the way row-records stored in tables (but
26# not indices) are interpreted. Before version 3.1.3, a row-record for a
27# table with N columns was guaranteed to contain exactly N fields. As
28# of version 3.1.3, the record may contain up to N fields. In this case
29# the M fields that are present are the values for the left-most M
30# columns. The (N-M) rightmost columns contain NULL.
31#
32# If any records in the database contain less fields than their table
33# has columns, then the file-format meta value should be set to (at least) 2.
34#
35
36# This procedure sets the value of the file-format in file 'test.db'
37# to $newval. Also, the schema cookie is incremented.
38#
39proc set_file_format {newval} {
40  set bt [btree_open test.db 10 0]
41  btree_begin_transaction $bt
42  set meta [btree_get_meta $bt]
43  lset meta 2 $newval                    ;# File format
44  lset meta 1 [expr [lindex $meta 1]+1]  ;# Schema cookie
45  eval "btree_update_meta $bt $meta"
46  btree_commit $bt
47  btree_close $bt
48}
49
50# This procedure returns the value of the file-format in file 'test.db'.
51#
52proc get_file_format {{fname test.db}} {
53  set bt [btree_open $fname 10 0]
54  set meta [btree_get_meta $bt]
55  btree_close $bt
56  lindex $meta 2
57}
58
59# This procedure sets the SQL statement stored for table $tbl in the
60# sqlite_master table of file 'test.db' to $sql. Also set the file format
61# to the supplied value. This is 2 if the added column has a default that is
62# NULL, or 3 otherwise.
63#
64proc alter_table {tbl sql {file_format 2}} {
65  sqlite3 dbat test.db
66  dbat eval {
67    PRAGMA writable_schema = 1;
68    UPDATE sqlite_master SET sql = $sql WHERE name = $tbl AND type = 'table';
69    PRAGMA writable_schema = 0;
70  }
71  dbat close
72  set_file_format 2
73}
74
75#-----------------------------------------------------------------------
76# Some basic tests to make sure short rows are handled.
77#
78do_test alter2-1.1 {
79  execsql {
80    CREATE TABLE abc(a, b);
81    INSERT INTO abc VALUES(1, 2);
82    INSERT INTO abc VALUES(3, 4);
83    INSERT INTO abc VALUES(5, 6);
84  }
85} {}
86do_test alter2-1.2 {
87  # ALTER TABLE abc ADD COLUMN c;
88  alter_table abc {CREATE TABLE abc(a, b, c);}
89} {}
90do_test alter2-1.3 {
91  execsql {
92    SELECT * FROM abc;
93  }
94} {1 2 {} 3 4 {} 5 6 {}}
95do_test alter2-1.4 {
96  execsql {
97    UPDATE abc SET c = 10 WHERE a = 1;
98    SELECT * FROM abc;
99  }
100} {1 2 10 3 4 {} 5 6 {}}
101do_test alter2-1.5 {
102  execsql {
103    CREATE INDEX abc_i ON abc(c);
104  }
105} {}
106do_test alter2-1.6 {
107  execsql {
108    SELECT c FROM abc ORDER BY c;
109  }
110} {{} {} 10}
111do_test alter2-1.7 {
112  execsql {
113    SELECT * FROM abc WHERE c = 10;
114  }
115} {1 2 10}
116do_test alter2-1.8 {
117  execsql {
118    SELECT sum(a), c FROM abc GROUP BY c;
119  }
120} {8.0 {} 1.0 10}
121do_test alter2-1.9 {
122  # ALTER TABLE abc ADD COLUMN d;
123  alter_table abc {CREATE TABLE abc(a, b, c, d);}
124  execsql { SELECT * FROM abc; }
125  execsql {
126    UPDATE abc SET d = 11 WHERE c IS NULL AND a<4;
127    SELECT * FROM abc;
128  }
129} {1 2 10 {} 3 4 {} 11 5 6 {} {}}
130do_test alter2-1.10 {
131  execsql {
132    SELECT typeof(d) FROM abc;
133  }
134} {null integer null}
135do_test alter2-1.99 {
136  execsql {
137    DROP TABLE abc;
138  }
139} {}
140
141#-----------------------------------------------------------------------
142# Test that views work when the underlying table structure is changed.
143#
144ifcapable view {
145  do_test alter2-2.1 {
146    execsql {
147      CREATE TABLE abc2(a, b, c);
148      INSERT INTO abc2 VALUES(1, 2, 10);
149      INSERT INTO abc2 VALUES(3, 4, NULL);
150      INSERT INTO abc2 VALUES(5, 6, NULL);
151      CREATE VIEW abc2_v AS SELECT * FROM abc2;
152      SELECT * FROM abc2_v;
153    }
154  } {1 2 10 3 4 {} 5 6 {}}
155  do_test alter2-2.2 {
156    # ALTER TABLE abc ADD COLUMN d;
157    alter_table abc2 {CREATE TABLE abc2(a, b, c, d);}
158    execsql {
159      SELECT * FROM abc2_v;
160    }
161  } {1 2 10 {} 3 4 {} {} 5 6 {} {}}
162  do_test alter2-2.3 {
163    execsql {
164      DROP TABLE abc2;
165      DROP VIEW abc2_v;
166    }
167  } {}
168}
169
170#-----------------------------------------------------------------------
171# Test that triggers work when a short row is copied to the old.*
172# trigger pseudo-table.
173#
174ifcapable trigger {
175  do_test alter2-3.1 {
176    execsql {
177      CREATE TABLE abc3(a, b);
178      CREATE TABLE blog(o, n);
179      CREATE TRIGGER abc3_t AFTER UPDATE OF b ON abc3 BEGIN
180        INSERT INTO blog VALUES(old.b, new.b);
181      END;
182    }
183  } {}
184  do_test alter2-3.2 {
185    execsql {
186      INSERT INTO abc3 VALUES(1, 4);
187      UPDATE abc3 SET b = 2 WHERE b = 4;
188      SELECT * FROM blog;
189    }
190  } {4 2}
191  do_test alter2-3.3 {
192    execsql {
193      INSERT INTO abc3 VALUES(3, 4);
194      INSERT INTO abc3 VALUES(5, 6);
195    }
196    alter_table abc3 {CREATE TABLE abc3(a, b, c);}
197    execsql {
198      SELECT * FROM abc3;
199    }
200  } {1 2 {} 3 4 {} 5 6 {}}
201  do_test alter2-3.4 {
202    execsql {
203      UPDATE abc3 SET b = b*2 WHERE a<4;
204      SELECT * FROM abc3;
205    }
206  } {1 4 {} 3 8 {} 5 6 {}}
207  do_test alter2-3.5 {
208    execsql {
209      SELECT * FROM blog;
210    }
211  } {4 2 2 4 4 8}
212
213  do_test alter2-3.6 {
214    execsql {
215      CREATE TABLE clog(o, n);
216      CREATE TRIGGER abc3_t2 AFTER UPDATE OF c ON abc3 BEGIN
217        INSERT INTO clog VALUES(old.c, new.c);
218      END;
219      UPDATE abc3 SET c = a*2;
220      SELECT * FROM clog;
221    }
222  } {{} 2 {} 6 {} 10}
223}
224
225#---------------------------------------------------------------------
226# Check that an error occurs if the database is upgraded to a file
227# format that SQLite does not support (in this case 4). Note: The
228# file format is checked each time the schema is read, so changing the
229# file format requires incrementing the schema cookie.
230#
231do_test alter2-4.1 {
232  set_file_format 4
233} {}
234do_test alter2-4.2 {
235  catchsql {
236    SELECT * FROM sqlite_master;
237  }
238} {1 {unsupported file format}}
239do_test alter2-4.3 {
240  sqlite3_errcode $::DB
241} {SQLITE_ERROR}
242do_test alter2-4.4 {
243  db close
244  set ::DB [sqlite3 db test.db]
245  catchsql {
246    SELECT * FROM sqlite_master;
247  }
248} {1 {unsupported file format}}
249do_test alter2-4.5 {
250  sqlite3_errcode $::DB
251} {SQLITE_ERROR}
252
253#---------------------------------------------------------------------
254# Check that executing VACUUM on a file with file-format version 2
255# resets the file format to 1.
256#
257do_test alter2-5.1 {
258  set_file_format 2
259  get_file_format
260} {2}
261do_test alter2-5.2 {
262  execsql {
263    VACUUM;
264  }
265} {}
266do_test alter2-5.3 {
267  get_file_format
268} {1}
269
270#---------------------------------------------------------------------
271# Test that when a database with file-format 2 is opened, new
272# databases are still created with file-format 1.
273#
274do_test alter2-6.1 {
275  db close
276  set_file_format 2
277  set ::DB [sqlite3 db test.db]
278  get_file_format
279} {2}
280do_test alter2-6.2 {
281  file delete -force test2.db-journal
282  file delete -force test2.db
283  execsql {
284    ATTACH 'test2.db' AS aux;
285    CREATE TABLE aux.t1(a, b);
286  }
287  get_file_format test2.db
288} {1}
289do_test alter2-6.3 {
290  execsql {
291    CREATE TABLE t1(a, b);
292  }
293  get_file_format
294} {2}
295
296#---------------------------------------------------------------------
297# Test that types and values for columns added with default values
298# other than NULL work with SELECT statements.
299#
300do_test alter2-7.1 {
301  execsql {
302    DROP TABLE t1;
303    CREATE TABLE t1(a);
304    INSERT INTO t1 VALUES(1);
305    INSERT INTO t1 VALUES(2);
306    INSERT INTO t1 VALUES(3);
307    INSERT INTO t1 VALUES(4);
308    SELECT * FROM t1;
309  }
310} {1 2 3 4}
311do_test alter2-7.2 {
312  set sql {CREATE TABLE t1(a, b DEFAULT '123', c INTEGER DEFAULT '123')}
313  alter_table t1 $sql 3
314  execsql {
315    SELECT * FROM t1 LIMIT 1;
316  }
317} {1 123 123}
318do_test alter2-7.3 {
319  execsql {
320    SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
321  }
322} {1 integer 123 text 123 integer}
323do_test alter2-7.4 {
324  execsql {
325    SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
326  }
327} {1 integer 123 text 123 integer}
328do_test alter2-7.5 {
329  set sql {CREATE TABLE t1(a, b DEFAULT -123.0, c VARCHAR(10) default 5)}
330  alter_table t1 $sql 3
331  execsql {
332    SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
333  }
334} {1 integer -123.0 real 5 text}
335
336#-----------------------------------------------------------------------
337# Test that UPDATE trigger tables work with default values, and that when
338# a row is updated the default values are correctly transfered to the
339# new row.
340#
341ifcapable trigger {
342db function set_val {set ::val}
343  do_test alter2-8.1 {
344    execsql {
345      CREATE TRIGGER trig1 BEFORE UPDATE ON t1 BEGIN
346      SELECT set_val(
347          old.b||' '||typeof(old.b)||' '||old.c||' '||typeof(old.c)||' '||
348          new.b||' '||typeof(new.b)||' '||new.c||' '||typeof(new.c)
349      );
350      END;
351    }
352    list
353  } {}
354}
355do_test alter2-8.2 {
356  execsql {
357    UPDATE t1 SET c = 10 WHERE a = 1;
358    SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
359  }
360} {1 integer -123.0 real 10 text}
361ifcapable trigger {
362  do_test alter2-8.3 {
363    set ::val
364  } {-123 real 5 text -123 real 10 text}
365}
366
367#-----------------------------------------------------------------------
368# Test that DELETE trigger tables work with default values, and that when
369# a row is updated the default values are correctly transfered to the
370# new row.
371#
372ifcapable trigger {
373  do_test alter2-9.1 {
374    execsql {
375      CREATE TRIGGER trig2 BEFORE DELETE ON t1 BEGIN
376      SELECT set_val(
377          old.b||' '||typeof(old.b)||' '||old.c||' '||typeof(old.c)
378      );
379      END;
380    }
381    list
382  } {}
383  do_test alter2-9.2 {
384    execsql {
385      DELETE FROM t1 WHERE a = 2;
386    }
387    set ::val
388  } {-123 real 5 text}
389}
390
391#-----------------------------------------------------------------------
392# Test creating an index on a column added with a default value.
393#
394do_test alter2-10.1 {
395  execsql {
396    CREATE TABLE t2(a);
397    INSERT INTO t2 VALUES('a');
398    INSERT INTO t2 VALUES('b');
399    INSERT INTO t2 VALUES('c');
400    INSERT INTO t2 VALUES('d');
401  }
402  alter_table t2 {CREATE TABLE t2(a, b DEFAULT X'ABCD', c DEFAULT NULL);} 3
403  catchsql {
404    SELECT * FROM sqlite_master;
405  }
406  execsql {
407    SELECT quote(a), quote(b), quote(c) FROM t2 LIMIT 1;
408  }
409} {'a' X'ABCD' NULL}
410do_test alter2-10.2 {
411  execsql {
412    CREATE INDEX i1 ON t2(b);
413    SELECT a FROM t2 WHERE b = X'ABCD';
414  }
415} {a b c d}
416do_test alter2-10.3 {
417  execsql {
418    DELETE FROM t2 WHERE a = 'c';
419    SELECT a FROM t2 WHERE b = X'ABCD';
420  }
421} {a b d}
422do_test alter2-10.4 {
423  execsql {
424    SELECT count(b) FROM t2 WHERE b = X'ABCD';
425  }
426} {3}
427
428finish_test
429
430