987db767d2
FossilOrigin-Name: db829dc1a2d7afa49798a2fd32d1f070185b23e513416e65d8144fda24f23b50
329 lines
9.5 KiB
Plaintext
329 lines
9.5 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)}
|
|
|
|
15 {CREATE TABLE t1(a INTEGER, b INTEGER, c BLOB, PRIMARY KEY(b))}
|
|
{CREATE TABLE t1(a INTEGER, d INTEGER, c BLOB, PRIMARY KEY(d))}
|
|
|
|
16 {CREATE TABLE t1(a INTEGER, b INTEGER PRIMARY KEY, c BLOB)}
|
|
{CREATE TABLE t1(a INTEGER, d INTEGER PRIMARY KEY, c BLOB)}
|
|
|
|
14 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, PRIMARY KEY(b))}
|
|
{CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, PRIMARY KEY(d))}
|
|
|
|
15 {CREATE TABLE t1(a INTEGER, b INTEGER, c BLOB, PRIMARY KEY(b))}
|
|
{CREATE TABLE t1(a INTEGER, d INTEGER, c BLOB, PRIMARY KEY(d))}
|
|
|
|
16 {CREATE TABLE t1(a INTEGER, b INTEGER PRIMARY KEY, c BLOB)}
|
|
{CREATE TABLE t1(a INTEGER, d INTEGER PRIMARY KEY, c BLOB)}
|
|
|
|
} {
|
|
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)}
|
|
}
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
do_execsql_test 5.0 {
|
|
CREATE TABLE t5(a, b, c);
|
|
CREATE INDEX t5a ON t5(a);
|
|
INSERT INTO t5 VALUES(1, 2, 3), (4, 5, 6);
|
|
ANALYZE;
|
|
}
|
|
|
|
do_execsql_test 5.1 {
|
|
ALTER TABLE t5 RENAME b TO big;
|
|
SELECT big FROM t5;
|
|
} {2 5}
|
|
|
|
do_catchsql_test 6.1 {
|
|
ALTER TABLE sqlite_stat1 RENAME tbl TO thetable;
|
|
} {1 {table sqlite_stat1 may not be altered}}
|
|
|
|
#-------------------------------------------------------------------------
|
|
#
|
|
do_execsql_test 6.0 {
|
|
CREATE TABLE blob(
|
|
rid INTEGER PRIMARY KEY,
|
|
rcvid INTEGER,
|
|
size INTEGER,
|
|
uuid TEXT UNIQUE NOT NULL,
|
|
content BLOB,
|
|
CHECK( length(uuid)>=40 AND rid>0 )
|
|
);
|
|
}
|
|
|
|
do_execsql_test 6.1 {
|
|
ALTER TABLE "blob" RENAME COLUMN "rid" TO "a1";
|
|
}
|
|
|
|
do_catchsql_test 6.2 {
|
|
ALTER TABLE "blob" RENAME COLUMN "a1" TO [where];
|
|
} {0 {}}
|
|
|
|
do_execsql_test 6.3 {
|
|
SELECT "where" FROM blob;
|
|
} {}
|
|
|
|
#-------------------------------------------------------------------------
|
|
# Triggers.
|
|
#
|
|
reset_db
|
|
do_execsql_test 7.0 {
|
|
CREATE TABLE c(x);
|
|
INSERT INTO c VALUES(0);
|
|
CREATE TABLE t6("col a", "col b", "col c");
|
|
CREATE TRIGGER zzz AFTER UPDATE OF "col a", "col c" ON t6 BEGIN
|
|
UPDATE c SET x=x+1;
|
|
END;
|
|
}
|
|
|
|
do_execsql_test 7.1 {
|
|
INSERT INTO t6 VALUES(0, 0, 0);
|
|
UPDATE t6 SET "col c" = 1;
|
|
SELECT * FROM c;
|
|
} {1}
|
|
|
|
do_execsql_test 7.2 {
|
|
ALTER TABLE t6 RENAME "col c" TO "col 3";
|
|
}
|
|
|
|
do_execsql_test 7.3 {
|
|
UPDATE t6 SET "col 3" = 0;
|
|
SELECT * FROM c;
|
|
} {2}
|
|
|
|
#-------------------------------------------------------------------------
|
|
# Views.
|
|
#
|
|
reset_db
|
|
do_execsql_test 8.0 {
|
|
CREATE TABLE a1(x INTEGER, y TEXT, z BLOB, PRIMARY KEY(x));
|
|
CREATE TABLE a2(a, b, c);
|
|
CREATE VIEW v1 AS SELECT x, y, z FROM a1;
|
|
}
|
|
|
|
do_execsql_test 8.1 {
|
|
ALTER TABLE a1 RENAME y TO yyy;
|
|
SELECT sql FROM sqlite_master WHERE type='view';
|
|
} {{CREATE VIEW v1 AS SELECT x, yyy, z FROM a1}}
|
|
|
|
do_execsql_test 8.2.1 {
|
|
DROP VIEW v1;
|
|
CREATE VIEW v2 AS SELECT x, x+x, a, a+a FROM a1, a2;
|
|
} {}
|
|
do_execsql_test 8.2.2 {
|
|
ALTER TABLE a1 RENAME x TO xxx;
|
|
}
|
|
do_execsql_test 8.2.3 {
|
|
SELECT sql FROM sqlite_master WHERE type='view';
|
|
} {{CREATE VIEW v2 AS SELECT xxx, xxx+xxx, a, a+a FROM a1, a2}}
|
|
|
|
do_execsql_test 8.3.1 {
|
|
DROP TABLE a2;
|
|
DROP VIEW v2;
|
|
CREATE TABLE a2(a INTEGER PRIMARY KEY, b, c);
|
|
CREATE VIEW v2 AS SELECT xxx, xxx+xxx, a, a+a FROM a1, a2;
|
|
} {}
|
|
do_execsql_test 8.3.2 {
|
|
ALTER TABLE a1 RENAME xxx TO x;
|
|
}
|
|
do_execsql_test 8.3.3 {
|
|
SELECT sql FROM sqlite_master WHERE type='view';
|
|
} {{CREATE VIEW v2 AS SELECT x, x+x, a, a+a FROM a1, a2}}
|
|
|
|
do_execsql_test 8.4.0 {
|
|
CREATE TABLE b1(a, b, c);
|
|
CREATE TABLE b2(x, y, z);
|
|
}
|
|
|
|
do_execsql_test 8.4.1 {
|
|
CREATE VIEW vvv AS SELECT c+c || coalesce(c, c) FROM b1, b2 WHERE x=c GROUP BY c HAVING c>0;
|
|
ALTER TABLE b1 RENAME c TO "a;b";
|
|
SELECT sql FROM sqlite_master WHERE name='vvv';
|
|
} {{CREATE VIEW vvv AS SELECT "a;b"+"a;b" || coalesce("a;b", "a;b") FROM b1, b2 WHERE x="a;b" GROUP BY "a;b" HAVING "a;b">0}}
|
|
|
|
do_execsql_test 8.4.2 {
|
|
CREATE VIEW www AS SELECT b FROM b1 UNION ALL SELECT y FROM b2;
|
|
ALTER TABLE b1 RENAME b TO bbb;
|
|
SELECT sql FROM sqlite_master WHERE name='www';
|
|
} {{CREATE VIEW www AS SELECT bbb FROM b1 UNION ALL SELECT y FROM b2}}
|
|
|
|
db collate nocase {string compare}
|
|
|
|
do_execsql_test 8.4.3 {
|
|
CREATE VIEW xxx AS SELECT a FROM b1 UNION SELECT x FROM b2 ORDER BY 1 COLLATE nocase;
|
|
}
|
|
|
|
do_execsql_test 8.4.4 {
|
|
ALTER TABLE b2 RENAME x TO hello;
|
|
SELECT sql FROM sqlite_master WHERE name='xxx';
|
|
} {{CREATE VIEW xxx AS SELECT a FROM b1 UNION SELECT hello FROM b2 ORDER BY 1 COLLATE nocase}}
|
|
|
|
do_execsql_test 8.4.5 {
|
|
CREATE VIEW zzz AS SELECT george, ringo FROM b1;
|
|
ALTER TABLE b1 RENAME a TO aaa;
|
|
SELECT sql FROM sqlite_master WHERE name = 'zzz'
|
|
} {{CREATE VIEW zzz AS SELECT george, ringo FROM b1}}
|
|
|
|
|
|
finish_test
|