xref: /sqlite-3.40.0/test/alter2.test (revision 6ab91a7a)
136963fdcSdanielk1977# 2005 February 18
236963fdcSdanielk1977#
336963fdcSdanielk1977# The author disclaims copyright to this source code.  In place of
436963fdcSdanielk1977# a legal notice, here is a blessing:
536963fdcSdanielk1977#
636963fdcSdanielk1977#    May you do good and not evil.
736963fdcSdanielk1977#    May you find forgiveness for yourself and forgive others.
836963fdcSdanielk1977#    May you share freely, never taking more than you give.
936963fdcSdanielk1977#
1036963fdcSdanielk1977#*************************************************************************
1136963fdcSdanielk1977# This file implements regression tests for SQLite library.  The
1236963fdcSdanielk1977# focus of this script is testing that SQLite can handle a subtle
1336963fdcSdanielk1977# file format change that may be used in the future to implement
1436963fdcSdanielk1977# "ALTER TABLE ... ADD COLUMN".
1536963fdcSdanielk1977#
162943c372Sdanielk1977# $Id: alter2.test,v 1.14 2009/04/07 14:14:22 danielk1977 Exp $
1736963fdcSdanielk1977#
1836963fdcSdanielk1977
1936963fdcSdanielk1977set testdir [file dirname $argv0]
2036963fdcSdanielk1977source $testdir/tester.tcl
2136963fdcSdanielk1977
22bf21627bSdrh# We have to have pragmas in order to do this test
23bf21627bSdrhifcapable {!pragma} return
24bf21627bSdrh
2568928b6cSdan# Do not use a codec for tests in this file, as the database file is
2668928b6cSdan# manipulated directly using tcl scripts. See proc [set_file_format].
2768928b6cSdan#
2868928b6cSdando_not_use_codec
2968928b6cSdan
3036963fdcSdanielk1977# The file format change affects the way row-records stored in tables (but
3136963fdcSdanielk1977# not indices) are interpreted. Before version 3.1.3, a row-record for a
3236963fdcSdanielk1977# table with N columns was guaranteed to contain exactly N fields. As
3336963fdcSdanielk1977# of version 3.1.3, the record may contain up to N fields. In this case
3436963fdcSdanielk1977# the M fields that are present are the values for the left-most M
3536963fdcSdanielk1977# columns. The (N-M) rightmost columns contain NULL.
3636963fdcSdanielk1977#
3736963fdcSdanielk1977# If any records in the database contain less fields than their table
3836963fdcSdanielk1977# has columns, then the file-format meta value should be set to (at least) 2.
3936963fdcSdanielk1977#
4036963fdcSdanielk1977
4136963fdcSdanielk1977# This procedure sets the value of the file-format in file 'test.db'
4236963fdcSdanielk1977# to $newval. Also, the schema cookie is incremented.
4336963fdcSdanielk1977#
4436963fdcSdanielk1977proc set_file_format {newval} {
45bb8a279eSdrh  hexio_write test.db 44 [hexio_render_int32 $newval]
46bb8a279eSdrh  set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
47bb8a279eSdrh  incr schemacookie
48bb8a279eSdrh  hexio_write test.db 40 [hexio_render_int32 $schemacookie]
49bb8a279eSdrh  return {}
5036963fdcSdanielk1977}
5136963fdcSdanielk1977
5236963fdcSdanielk1977# This procedure returns the value of the file-format in file 'test.db'.
5336963fdcSdanielk1977#
5436963fdcSdanielk1977proc get_file_format {{fname test.db}} {
55bb8a279eSdrh  return [hexio_get_int [hexio_read $fname 44 4]]
5636963fdcSdanielk1977}
5736963fdcSdanielk1977
5836963fdcSdanielk1977# This procedure sets the SQL statement stored for table $tbl in the
59aee18ef8Sdanielk1977# sqlite_master table of file 'test.db' to $sql. Also set the file format
60aee18ef8Sdanielk1977# to the supplied value. This is 2 if the added column has a default that is
61aee18ef8Sdanielk1977# NULL, or 3 otherwise.
6236963fdcSdanielk1977#
63aee18ef8Sdanielk1977proc alter_table {tbl sql {file_format 2}} {
6436963fdcSdanielk1977  sqlite3 dbat test.db
654152e677Sdanielk1977  set s [string map {' ''} $sql]
664152e677Sdanielk1977  set t [string map {' ''} $tbl]
67*6ab91a7aSdrh  sqlite3_db_config dbat DEFENSIVE 0
684152e677Sdanielk1977  dbat eval [subst {
6936963fdcSdanielk1977    PRAGMA writable_schema = 1;
704152e677Sdanielk1977    UPDATE sqlite_master SET sql = '$s' WHERE name = '$t' AND type = 'table';
7136963fdcSdanielk1977    PRAGMA writable_schema = 0;
724152e677Sdanielk1977  }]
7336963fdcSdanielk1977  dbat close
7436963fdcSdanielk1977  set_file_format 2
7536963fdcSdanielk1977}
7636963fdcSdanielk1977
77545f587fSdrh# Create bogus application-defined functions for functions used
78545f587fSdrh# internally by ALTER TABLE, to ensure that ALTER TABLE falls back
79545f587fSdrh# to the built-in functions.
80545f587fSdrh#
81545f587fSdrhproc failing_app_func {args} {error "bad function"}
82545f587fSdrhdo_test alter2-1.0 {
83545f587fSdrh  db func substr failing_app_func
84545f587fSdrh  db func like failing_app_func
85545f587fSdrh  db func sqlite_rename_table failing_app_func
86545f587fSdrh  db func sqlite_rename_trigger failing_app_func
87545f587fSdrh  db func sqlite_rename_parent failing_app_func
88545f587fSdrh  catchsql {SELECT substr('abcdefg',1,3)}
89545f587fSdrh} {1 {bad function}}
90545f587fSdrh
91545f587fSdrh
9236963fdcSdanielk1977#-----------------------------------------------------------------------
9336963fdcSdanielk1977# Some basic tests to make sure short rows are handled.
9436963fdcSdanielk1977#
95*6ab91a7aSdrhsqlite3_db_config db DEFENSIVE 0
9636963fdcSdanielk1977do_test alter2-1.1 {
9736963fdcSdanielk1977  execsql {
9836963fdcSdanielk1977    CREATE TABLE abc(a, b);
9936963fdcSdanielk1977    INSERT INTO abc VALUES(1, 2);
10036963fdcSdanielk1977    INSERT INTO abc VALUES(3, 4);
10136963fdcSdanielk1977    INSERT INTO abc VALUES(5, 6);
10236963fdcSdanielk1977  }
10336963fdcSdanielk1977} {}
10436963fdcSdanielk1977do_test alter2-1.2 {
10536963fdcSdanielk1977  # ALTER TABLE abc ADD COLUMN c;
10636963fdcSdanielk1977  alter_table abc {CREATE TABLE abc(a, b, c);}
10736963fdcSdanielk1977} {}
10836963fdcSdanielk1977do_test alter2-1.3 {
10936963fdcSdanielk1977  execsql {
11036963fdcSdanielk1977    SELECT * FROM abc;
11136963fdcSdanielk1977  }
11236963fdcSdanielk1977} {1 2 {} 3 4 {} 5 6 {}}
11336963fdcSdanielk1977do_test alter2-1.4 {
11436963fdcSdanielk1977  execsql {
11536963fdcSdanielk1977    UPDATE abc SET c = 10 WHERE a = 1;
11636963fdcSdanielk1977    SELECT * FROM abc;
11736963fdcSdanielk1977  }
11836963fdcSdanielk1977} {1 2 10 3 4 {} 5 6 {}}
11936963fdcSdanielk1977do_test alter2-1.5 {
12036963fdcSdanielk1977  execsql {
12136963fdcSdanielk1977    CREATE INDEX abc_i ON abc(c);
12236963fdcSdanielk1977  }
12336963fdcSdanielk1977} {}
12436963fdcSdanielk1977do_test alter2-1.6 {
12536963fdcSdanielk1977  execsql {
12636963fdcSdanielk1977    SELECT c FROM abc ORDER BY c;
12736963fdcSdanielk1977  }
12836963fdcSdanielk1977} {{} {} 10}
12936963fdcSdanielk1977do_test alter2-1.7 {
13036963fdcSdanielk1977  execsql {
13136963fdcSdanielk1977    SELECT * FROM abc WHERE c = 10;
13236963fdcSdanielk1977  }
13336963fdcSdanielk1977} {1 2 10}
13436963fdcSdanielk1977do_test alter2-1.8 {
13536963fdcSdanielk1977  execsql {
13636963fdcSdanielk1977    SELECT sum(a), c FROM abc GROUP BY c;
13736963fdcSdanielk1977  }
1385de872d5Sdrh} {8 {} 1 10}
13936963fdcSdanielk1977do_test alter2-1.9 {
14036963fdcSdanielk1977  # ALTER TABLE abc ADD COLUMN d;
14136963fdcSdanielk1977  alter_table abc {CREATE TABLE abc(a, b, c, d);}
142c431fd55Sdan  if {[permutation] == "prepare"} { db cache flush }
14336963fdcSdanielk1977  execsql { SELECT * FROM abc; }
14436963fdcSdanielk1977  execsql {
14536963fdcSdanielk1977    UPDATE abc SET d = 11 WHERE c IS NULL AND a<4;
14636963fdcSdanielk1977    SELECT * FROM abc;
14736963fdcSdanielk1977  }
14836963fdcSdanielk1977} {1 2 10 {} 3 4 {} 11 5 6 {} {}}
14936963fdcSdanielk1977do_test alter2-1.10 {
15036963fdcSdanielk1977  execsql {
15136963fdcSdanielk1977    SELECT typeof(d) FROM abc;
15236963fdcSdanielk1977  }
15336963fdcSdanielk1977} {null integer null}
15436963fdcSdanielk1977do_test alter2-1.99 {
15536963fdcSdanielk1977  execsql {
15636963fdcSdanielk1977    DROP TABLE abc;
15736963fdcSdanielk1977  }
15836963fdcSdanielk1977} {}
15936963fdcSdanielk1977
16036963fdcSdanielk1977#-----------------------------------------------------------------------
16136963fdcSdanielk1977# Test that views work when the underlying table structure is changed.
16236963fdcSdanielk1977#
16336963fdcSdanielk1977ifcapable view {
16436963fdcSdanielk1977  do_test alter2-2.1 {
16536963fdcSdanielk1977    execsql {
16636963fdcSdanielk1977      CREATE TABLE abc2(a, b, c);
16736963fdcSdanielk1977      INSERT INTO abc2 VALUES(1, 2, 10);
16836963fdcSdanielk1977      INSERT INTO abc2 VALUES(3, 4, NULL);
16936963fdcSdanielk1977      INSERT INTO abc2 VALUES(5, 6, NULL);
17036963fdcSdanielk1977      CREATE VIEW abc2_v AS SELECT * FROM abc2;
17136963fdcSdanielk1977      SELECT * FROM abc2_v;
17236963fdcSdanielk1977    }
17336963fdcSdanielk1977  } {1 2 10 3 4 {} 5 6 {}}
17436963fdcSdanielk1977  do_test alter2-2.2 {
17536963fdcSdanielk1977    # ALTER TABLE abc ADD COLUMN d;
17636963fdcSdanielk1977    alter_table abc2 {CREATE TABLE abc2(a, b, c, d);}
17736963fdcSdanielk1977    execsql {
17836963fdcSdanielk1977      SELECT * FROM abc2_v;
17936963fdcSdanielk1977    }
18036963fdcSdanielk1977  } {1 2 10 {} 3 4 {} {} 5 6 {} {}}
18136963fdcSdanielk1977  do_test alter2-2.3 {
18236963fdcSdanielk1977    execsql {
18336963fdcSdanielk1977      DROP TABLE abc2;
18436963fdcSdanielk1977      DROP VIEW abc2_v;
18536963fdcSdanielk1977    }
18636963fdcSdanielk1977  } {}
18736963fdcSdanielk1977}
18836963fdcSdanielk1977
18936963fdcSdanielk1977#-----------------------------------------------------------------------
19036963fdcSdanielk1977# Test that triggers work when a short row is copied to the old.*
19136963fdcSdanielk1977# trigger pseudo-table.
19236963fdcSdanielk1977#
19336963fdcSdanielk1977ifcapable trigger {
19436963fdcSdanielk1977  do_test alter2-3.1 {
19536963fdcSdanielk1977    execsql {
19636963fdcSdanielk1977      CREATE TABLE abc3(a, b);
19736963fdcSdanielk1977      CREATE TABLE blog(o, n);
19836963fdcSdanielk1977      CREATE TRIGGER abc3_t AFTER UPDATE OF b ON abc3 BEGIN
19936963fdcSdanielk1977        INSERT INTO blog VALUES(old.b, new.b);
20036963fdcSdanielk1977      END;
20136963fdcSdanielk1977    }
20236963fdcSdanielk1977  } {}
20336963fdcSdanielk1977  do_test alter2-3.2 {
20436963fdcSdanielk1977    execsql {
20536963fdcSdanielk1977      INSERT INTO abc3 VALUES(1, 4);
20636963fdcSdanielk1977      UPDATE abc3 SET b = 2 WHERE b = 4;
20736963fdcSdanielk1977      SELECT * FROM blog;
20836963fdcSdanielk1977    }
20936963fdcSdanielk1977  } {4 2}
21036963fdcSdanielk1977  do_test alter2-3.3 {
21136963fdcSdanielk1977    execsql {
21236963fdcSdanielk1977      INSERT INTO abc3 VALUES(3, 4);
21336963fdcSdanielk1977      INSERT INTO abc3 VALUES(5, 6);
21436963fdcSdanielk1977    }
21536963fdcSdanielk1977    alter_table abc3 {CREATE TABLE abc3(a, b, c);}
21636963fdcSdanielk1977    execsql {
21736963fdcSdanielk1977      SELECT * FROM abc3;
21836963fdcSdanielk1977    }
21936963fdcSdanielk1977  } {1 2 {} 3 4 {} 5 6 {}}
22036963fdcSdanielk1977  do_test alter2-3.4 {
22136963fdcSdanielk1977    execsql {
22236963fdcSdanielk1977      UPDATE abc3 SET b = b*2 WHERE a<4;
22336963fdcSdanielk1977      SELECT * FROM abc3;
22436963fdcSdanielk1977    }
22536963fdcSdanielk1977  } {1 4 {} 3 8 {} 5 6 {}}
22636963fdcSdanielk1977  do_test alter2-3.5 {
22736963fdcSdanielk1977    execsql {
22836963fdcSdanielk1977      SELECT * FROM blog;
22936963fdcSdanielk1977    }
23036963fdcSdanielk1977  } {4 2 2 4 4 8}
23136963fdcSdanielk1977
23236963fdcSdanielk1977  do_test alter2-3.6 {
23336963fdcSdanielk1977    execsql {
23436963fdcSdanielk1977      CREATE TABLE clog(o, n);
23536963fdcSdanielk1977      CREATE TRIGGER abc3_t2 AFTER UPDATE OF c ON abc3 BEGIN
23636963fdcSdanielk1977        INSERT INTO clog VALUES(old.c, new.c);
23736963fdcSdanielk1977      END;
23836963fdcSdanielk1977      UPDATE abc3 SET c = a*2;
23936963fdcSdanielk1977      SELECT * FROM clog;
24036963fdcSdanielk1977    }
24136963fdcSdanielk1977  } {{} 2 {} 6 {} 10}
2422943c372Sdanielk1977} else {
2432943c372Sdanielk1977  execsql { CREATE TABLE abc3(a, b); }
24436963fdcSdanielk1977}
24536963fdcSdanielk1977
24636963fdcSdanielk1977#---------------------------------------------------------------------
24736963fdcSdanielk1977# Check that an error occurs if the database is upgraded to a file
2485de872d5Sdrh# format that SQLite does not support (in this case 5). Note: The
24936963fdcSdanielk1977# file format is checked each time the schema is read, so changing the
25036963fdcSdanielk1977# file format requires incrementing the schema cookie.
25136963fdcSdanielk1977#
25236963fdcSdanielk1977do_test alter2-4.1 {
253bb8a279eSdrh  db close
2545de872d5Sdrh  set_file_format 5
255cb354603Sdan  catch { sqlite3 db test.db }
256cb354603Sdan  set {} {}
25736963fdcSdanielk1977} {}
25836963fdcSdanielk1977do_test alter2-4.2 {
2598e556520Sdanielk1977  # We have to run two queries here because the Tcl interface uses
2608e556520Sdanielk1977  # sqlite3_prepare_v2(). In this case, the first query encounters an
2618e556520Sdanielk1977  # SQLITE_SCHEMA error. Then, when trying to recompile the statement, the
2628e556520Sdanielk1977  # "unsupported file format" error is encountered. So the error code
2638e556520Sdanielk1977  # returned is SQLITE_SCHEMA, not SQLITE_ERROR as required by the following
2648e556520Sdanielk1977  # test case.
2658e556520Sdanielk1977  #
2668e556520Sdanielk1977  # When the query is attempted a second time, the same error message is
2678e556520Sdanielk1977  # returned but the error code is SQLITE_ERROR, because the unsupported
2688e556520Sdanielk1977  # file format was detected during a call to sqlite3_prepare(), not
2698e556520Sdanielk1977  # sqlite3_step().
2708e556520Sdanielk1977  #
2718e556520Sdanielk1977  catchsql { SELECT * FROM sqlite_master; }
2728e556520Sdanielk1977  catchsql { SELECT * FROM sqlite_master; }
27336963fdcSdanielk1977} {1 {unsupported file format}}
27436963fdcSdanielk1977do_test alter2-4.3 {
275bb8a279eSdrh  sqlite3_errcode db
27636963fdcSdanielk1977} {SQLITE_ERROR}
27736963fdcSdanielk1977do_test alter2-4.4 {
278dddca286Sdrh  set ::DB [sqlite3_connection_pointer db]
27936963fdcSdanielk1977  catchsql {
28036963fdcSdanielk1977    SELECT * FROM sqlite_master;
28136963fdcSdanielk1977  }
28236963fdcSdanielk1977} {1 {unsupported file format}}
28336963fdcSdanielk1977do_test alter2-4.5 {
284bb8a279eSdrh  sqlite3_errcode db
28536963fdcSdanielk1977} {SQLITE_ERROR}
28636963fdcSdanielk1977
28736963fdcSdanielk1977#---------------------------------------------------------------------
28836963fdcSdanielk1977# Check that executing VACUUM on a file with file-format version 2
28936963fdcSdanielk1977# resets the file format to 1.
29036963fdcSdanielk1977#
291c797d4dcSdrhset default_file_format [expr $SQLITE_DEFAULT_FILE_FORMAT==4 ? 4 : 1]
2924152e677Sdanielk1977ifcapable vacuum {
29336963fdcSdanielk1977  do_test alter2-5.1 {
29436963fdcSdanielk1977    set_file_format 2
295bb8a279eSdrh    db close
296bb8a279eSdrh    sqlite3 db test.db
2974aa2bfe6Sdrh    execsql {SELECT 1 FROM sqlite_master LIMIT 1;}
29836963fdcSdanielk1977    get_file_format
29936963fdcSdanielk1977  } {2}
30036963fdcSdanielk1977  do_test alter2-5.2 {
3012943c372Sdanielk1977    execsql { VACUUM }
30236963fdcSdanielk1977  } {}
30336963fdcSdanielk1977  do_test alter2-5.3 {
30436963fdcSdanielk1977    get_file_format
305cd1aa900Sdanielk1977  } $default_file_format
3064152e677Sdanielk1977}
30736963fdcSdanielk1977
30836963fdcSdanielk1977#---------------------------------------------------------------------
30936963fdcSdanielk1977# Test that when a database with file-format 2 is opened, new
31036963fdcSdanielk1977# databases are still created with file-format 1.
31136963fdcSdanielk1977#
31236963fdcSdanielk1977do_test alter2-6.1 {
31336963fdcSdanielk1977  db close
31436963fdcSdanielk1977  set_file_format 2
315dddca286Sdrh  sqlite3 db test.db
31636963fdcSdanielk1977  get_file_format
31736963fdcSdanielk1977} {2}
3185a8f9374Sdanielk1977ifcapable attach {
31936963fdcSdanielk1977  do_test alter2-6.2 {
320fda06befSmistachkin    forcedelete test2.db-journal
321fda06befSmistachkin    forcedelete test2.db
32236963fdcSdanielk1977    execsql {
32336963fdcSdanielk1977      ATTACH 'test2.db' AS aux;
32436963fdcSdanielk1977      CREATE TABLE aux.t1(a, b);
32536963fdcSdanielk1977    }
32636963fdcSdanielk1977    get_file_format test2.db
327cd1aa900Sdanielk1977  } $default_file_format
3285a8f9374Sdanielk1977}
32936963fdcSdanielk1977do_test alter2-6.3 {
33036963fdcSdanielk1977  execsql {
33136963fdcSdanielk1977    CREATE TABLE t1(a, b);
33236963fdcSdanielk1977  }
33336963fdcSdanielk1977  get_file_format
33436963fdcSdanielk1977} {2}
33536963fdcSdanielk1977
336aee18ef8Sdanielk1977#---------------------------------------------------------------------
337aee18ef8Sdanielk1977# Test that types and values for columns added with default values
338aee18ef8Sdanielk1977# other than NULL work with SELECT statements.
339aee18ef8Sdanielk1977#
340aee18ef8Sdanielk1977do_test alter2-7.1 {
341aee18ef8Sdanielk1977  execsql {
342aee18ef8Sdanielk1977    DROP TABLE t1;
343aee18ef8Sdanielk1977    CREATE TABLE t1(a);
344aee18ef8Sdanielk1977    INSERT INTO t1 VALUES(1);
345aee18ef8Sdanielk1977    INSERT INTO t1 VALUES(2);
346aee18ef8Sdanielk1977    INSERT INTO t1 VALUES(3);
347aee18ef8Sdanielk1977    INSERT INTO t1 VALUES(4);
348aee18ef8Sdanielk1977    SELECT * FROM t1;
349aee18ef8Sdanielk1977  }
350aee18ef8Sdanielk1977} {1 2 3 4}
351aee18ef8Sdanielk1977do_test alter2-7.2 {
352aee18ef8Sdanielk1977  set sql {CREATE TABLE t1(a, b DEFAULT '123', c INTEGER DEFAULT '123')}
353aee18ef8Sdanielk1977  alter_table t1 $sql 3
354aee18ef8Sdanielk1977  execsql {
355aee18ef8Sdanielk1977    SELECT * FROM t1 LIMIT 1;
356aee18ef8Sdanielk1977  }
357aee18ef8Sdanielk1977} {1 123 123}
358aee18ef8Sdanielk1977do_test alter2-7.3 {
359aee18ef8Sdanielk1977  execsql {
360aee18ef8Sdanielk1977    SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
361aee18ef8Sdanielk1977  }
362aee18ef8Sdanielk1977} {1 integer 123 text 123 integer}
363aee18ef8Sdanielk1977do_test alter2-7.4 {
364aee18ef8Sdanielk1977  execsql {
365aee18ef8Sdanielk1977    SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
366aee18ef8Sdanielk1977  }
367aee18ef8Sdanielk1977} {1 integer 123 text 123 integer}
368aee18ef8Sdanielk1977do_test alter2-7.5 {
369aee18ef8Sdanielk1977  set sql {CREATE TABLE t1(a, b DEFAULT -123.0, c VARCHAR(10) default 5)}
370aee18ef8Sdanielk1977  alter_table t1 $sql 3
371aee18ef8Sdanielk1977  execsql {
372aee18ef8Sdanielk1977    SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
373aee18ef8Sdanielk1977  }
3745de872d5Sdrh} {1 integer -123 integer 5 text}
375aee18ef8Sdanielk1977
376aee18ef8Sdanielk1977#-----------------------------------------------------------------------
377aee18ef8Sdanielk1977# Test that UPDATE trigger tables work with default values, and that when
378aee18ef8Sdanielk1977# a row is updated the default values are correctly transfered to the
379aee18ef8Sdanielk1977# new row.
380aee18ef8Sdanielk1977#
381aee18ef8Sdanielk1977ifcapable trigger {
382aee18ef8Sdanielk1977db function set_val {set ::val}
383aee18ef8Sdanielk1977  do_test alter2-8.1 {
384aee18ef8Sdanielk1977    execsql {
385aee18ef8Sdanielk1977      CREATE TRIGGER trig1 BEFORE UPDATE ON t1 BEGIN
386aee18ef8Sdanielk1977      SELECT set_val(
387aee18ef8Sdanielk1977          old.b||' '||typeof(old.b)||' '||old.c||' '||typeof(old.c)||' '||
388aee18ef8Sdanielk1977          new.b||' '||typeof(new.b)||' '||new.c||' '||typeof(new.c)
389aee18ef8Sdanielk1977      );
390aee18ef8Sdanielk1977      END;
391aee18ef8Sdanielk1977    }
392aee18ef8Sdanielk1977    list
393aee18ef8Sdanielk1977  } {}
394aee18ef8Sdanielk1977}
395aee18ef8Sdanielk1977do_test alter2-8.2 {
396aee18ef8Sdanielk1977  execsql {
397aee18ef8Sdanielk1977    UPDATE t1 SET c = 10 WHERE a = 1;
398aee18ef8Sdanielk1977    SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
399aee18ef8Sdanielk1977  }
4005de872d5Sdrh} {1 integer -123 integer 10 text}
401aee18ef8Sdanielk1977ifcapable trigger {
402aee18ef8Sdanielk1977  do_test alter2-8.3 {
403aee18ef8Sdanielk1977    set ::val
4045de872d5Sdrh  } {-123 integer 5 text -123 integer 10 text}
405aee18ef8Sdanielk1977}
406aee18ef8Sdanielk1977
407aee18ef8Sdanielk1977#-----------------------------------------------------------------------
408aee18ef8Sdanielk1977# Test that DELETE trigger tables work with default values, and that when
409aee18ef8Sdanielk1977# a row is updated the default values are correctly transfered to the
410aee18ef8Sdanielk1977# new row.
411aee18ef8Sdanielk1977#
412aee18ef8Sdanielk1977ifcapable trigger {
413aee18ef8Sdanielk1977  do_test alter2-9.1 {
414aee18ef8Sdanielk1977    execsql {
415aee18ef8Sdanielk1977      CREATE TRIGGER trig2 BEFORE DELETE ON t1 BEGIN
416aee18ef8Sdanielk1977      SELECT set_val(
417aee18ef8Sdanielk1977          old.b||' '||typeof(old.b)||' '||old.c||' '||typeof(old.c)
418aee18ef8Sdanielk1977      );
419aee18ef8Sdanielk1977      END;
420aee18ef8Sdanielk1977    }
421aee18ef8Sdanielk1977    list
422aee18ef8Sdanielk1977  } {}
423aee18ef8Sdanielk1977  do_test alter2-9.2 {
424aee18ef8Sdanielk1977    execsql {
425aee18ef8Sdanielk1977      DELETE FROM t1 WHERE a = 2;
426aee18ef8Sdanielk1977    }
427aee18ef8Sdanielk1977    set ::val
4285de872d5Sdrh  } {-123 integer 5 text}
429aee18ef8Sdanielk1977}
430aee18ef8Sdanielk1977
431aee18ef8Sdanielk1977#-----------------------------------------------------------------------
432aee18ef8Sdanielk1977# Test creating an index on a column added with a default value.
433aee18ef8Sdanielk1977#
4344152e677Sdanielk1977ifcapable bloblit {
435aee18ef8Sdanielk1977  do_test alter2-10.1 {
436aee18ef8Sdanielk1977    execsql {
437aee18ef8Sdanielk1977      CREATE TABLE t2(a);
438aee18ef8Sdanielk1977      INSERT INTO t2 VALUES('a');
439aee18ef8Sdanielk1977      INSERT INTO t2 VALUES('b');
440aee18ef8Sdanielk1977      INSERT INTO t2 VALUES('c');
441aee18ef8Sdanielk1977      INSERT INTO t2 VALUES('d');
442aee18ef8Sdanielk1977    }
443aee18ef8Sdanielk1977    alter_table t2 {CREATE TABLE t2(a, b DEFAULT X'ABCD', c DEFAULT NULL);} 3
444aee18ef8Sdanielk1977    catchsql {
445aee18ef8Sdanielk1977      SELECT * FROM sqlite_master;
446aee18ef8Sdanielk1977    }
447aee18ef8Sdanielk1977    execsql {
448aee18ef8Sdanielk1977      SELECT quote(a), quote(b), quote(c) FROM t2 LIMIT 1;
449aee18ef8Sdanielk1977    }
450aee18ef8Sdanielk1977  } {'a' X'ABCD' NULL}
451aee18ef8Sdanielk1977  do_test alter2-10.2 {
452aee18ef8Sdanielk1977    execsql {
453aee18ef8Sdanielk1977      CREATE INDEX i1 ON t2(b);
454aee18ef8Sdanielk1977      SELECT a FROM t2 WHERE b = X'ABCD';
455aee18ef8Sdanielk1977    }
456aee18ef8Sdanielk1977  } {a b c d}
457aee18ef8Sdanielk1977  do_test alter2-10.3 {
458aee18ef8Sdanielk1977    execsql {
459aee18ef8Sdanielk1977      DELETE FROM t2 WHERE a = 'c';
460aee18ef8Sdanielk1977      SELECT a FROM t2 WHERE b = X'ABCD';
461aee18ef8Sdanielk1977    }
462aee18ef8Sdanielk1977  } {a b d}
463aee18ef8Sdanielk1977  do_test alter2-10.4 {
464aee18ef8Sdanielk1977    execsql {
465aee18ef8Sdanielk1977      SELECT count(b) FROM t2 WHERE b = X'ABCD';
466aee18ef8Sdanielk1977    }
467aee18ef8Sdanielk1977  } {3}
4684152e677Sdanielk1977}
469aee18ef8Sdanielk1977
47036963fdcSdanielk1977finish_test
471