sqlite/test/altercol.test
dan 5da06d3d7e Ensure the schema cookie is changed when a column is renamed.
FossilOrigin-Name: 2dec9ea4ab1779d00bb22add0aa9a8536661d8abc9f4c2fb8628e04d4cd84326
2018-08-10 20:19:34 +00:00

170 lines
5.1 KiB
Plaintext

# 2009 February 2
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#*************************************************************************
# This file implements regression tests for SQLite library. The
# focus of this script is testing that SQLite can handle a subtle
# file format change that may be used in the future to implement
# "ALTER TABLE ... RENAME COLUMN ... TO".
#
# $Id: alter4.test,v 1.1 2009/02/02 18:03:22 drh Exp $
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix altercol
# If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
ifcapable !altertable {
finish_test
return
}
foreach {tn before after} {
1 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB)}
{CREATE TABLE t1(a INTEGER, d TEXT, c BLOB)}
2 {CREATE TABLE t1(a INTEGER, x TEXT, "b" BLOB)}
{CREATE TABLE t1(a INTEGER, x TEXT, "d" BLOB)}
3 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, CHECK(b!=''))}
{CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, CHECK(d!=''))}
4 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, CHECK(t1.b!=''))}
{CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, CHECK(t1.d!=''))}
5 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, CHECK( coalesce(b,c) ))}
{CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, CHECK( coalesce(d,c) ))}
6 {CREATE TABLE t1(a INTEGER, "b"TEXT, c BLOB, CHECK( coalesce(b,c) ))}
{CREATE TABLE t1(a INTEGER, "d"TEXT, c BLOB, CHECK( coalesce(d,c) ))}
7 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, PRIMARY KEY(b, c))}
{CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, PRIMARY KEY(d, c))}
8 {CREATE TABLE t1(a INTEGER, b TEXT PRIMARY KEY, c BLOB)}
{CREATE TABLE t1(a INTEGER, d TEXT PRIMARY KEY, c BLOB)}
9 {CREATE TABLE t1(a, b TEXT, c, PRIMARY KEY(a, b), UNIQUE("B"))}
{CREATE TABLE t1(a, d TEXT, c, PRIMARY KEY(a, d), UNIQUE("d"))}
10 {CREATE TABLE t1(a, b, c); CREATE INDEX t1i ON t1(a, c)}
{{CREATE TABLE t1(a, d, c)} {CREATE INDEX t1i ON t1(a, c)}}
11 {CREATE TABLE t1(a, b, c); CREATE INDEX t1i ON t1(b, c)}
{{CREATE TABLE t1(a, d, c)} {CREATE INDEX t1i ON t1(d, c)}}
12 {CREATE TABLE t1(a, b, c); CREATE INDEX t1i ON t1(b+b+b+b, c) WHERE b>0}
{{CREATE TABLE t1(a, d, c)} {CREATE INDEX t1i ON t1(d+d+d+d, c) WHERE d>0}}
13 {CREATE TABLE t1(a, b, c, FOREIGN KEY (b) REFERENCES t2)}
{CREATE TABLE t1(a, d, c, FOREIGN KEY (d) REFERENCES t2)}
} {
reset_db
do_execsql_test 1.$tn.0 $before
do_execsql_test 1.$tn.1 {
INSERT INTO t1 VALUES(1, 2, 3);
}
do_execsql_test 1.$tn.2 {
ALTER TABLE t1 RENAME COLUMN b TO d;
}
do_execsql_test 1.$tn.3 {
SELECT * FROM t1;
} {1 2 3}
if {[string first INDEX $before]>0} {
set res $after
} else {
set res [list $after]
}
do_execsql_test 1.$tn.4 {
SELECT sql FROM sqlite_master WHERE tbl_name='t1' AND sql!=''
} $res
}
#-------------------------------------------------------------------------
#
do_execsql_test 2.0 {
CREATE TABLE t3(a, b, c, d, e, f, g, h, i, j, k, l, m, FOREIGN KEY (b, c, d, e, f, g, h, i, j, k, l, m) REFERENCES t4);
}
sqlite3 db2 test.db
do_execsql_test -db db2 2.1 { SELECT b FROM t3 }
do_execsql_test 2.2 {
ALTER TABLE t3 RENAME b TO biglongname;
SELECT sql FROM sqlite_master WHERE name='t3';
} {{CREATE TABLE t3(a, biglongname, c, d, e, f, g, h, i, j, k, l, m, FOREIGN KEY (biglongname, c, d, e, f, g, h, i, j, k, l, m) REFERENCES t4)}}
do_execsql_test -db db2 2.3 { SELECT biglongname FROM t3 }
#-------------------------------------------------------------------------
#
do_execsql_test 3.0 {
CREATE TABLE t4(x, y, z);
CREATE TRIGGER ttt AFTER INSERT ON t4 WHEN new.y<0 BEGIN
SELECT 1, 2, 3, 4;
END;
INSERT INTO t4 VALUES(3, 2, 1);
}
do_execsql_test 3.1 {
ALTER TABLE t4 RENAME y TO abc;
SELECT sql FROM sqlite_master WHERE name='t4';
} {{CREATE TABLE t4(x, abc, z)}}
db close
sqlite3 db test.db
do_execsql_test 3.2 {
SELECT * FROM t4;
} {3 2 1}
# do_execsql_test 3.3 { INSERT INTO t4 VALUES(6, 5, 4); } {}
#-------------------------------------------------------------------------
#
do_execsql_test 4.0 {
CREATE TABLE c1(a, b, FOREIGN KEY (a, b) REFERENCES p1(c, d));
CREATE TABLE p1(c, d, PRIMARY KEY(c, d));
PRAGMA foreign_keys = 1;
INSERT INTO p1 VALUES(1, 2);
INSERT INTO p1 VALUES(3, 4);
}
do_execsql_test 4.1 {
ALTER TABLE p1 RENAME d TO "silly name";
SELECT sql FROM sqlite_master WHERE name IN ('c1', 'p1');
} {
{CREATE TABLE c1(a, b, FOREIGN KEY (a, b) REFERENCES p1(c, "silly name"))}
{CREATE TABLE p1(c, "silly name", PRIMARY KEY(c, "silly name"))}
}
# do_execsql_test 4.2 { INSERT INTO c1 VALUES(1, 2); }
do_execsql_test 4.3 {
CREATE TABLE c2(a, b, FOREIGN KEY (a, b) REFERENCES p1);
}
do_execsql_test 4.4 {
ALTER TABLE p1 RENAME "silly name" TO reasonable;
SELECT sql FROM sqlite_master WHERE name IN ('c1', 'c2', 'p1');
} {
{CREATE TABLE c1(a, b, FOREIGN KEY (a, b) REFERENCES p1(c, "reasonable"))}
{CREATE TABLE p1(c, "reasonable", PRIMARY KEY(c, "reasonable"))}
{CREATE TABLE c2(a, b, FOREIGN KEY (a, b) REFERENCES p1)}
}
finish_test