sl@0: # 2005 Jan 24 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. sl@0: # sl@0: # This file tests the various conditions under which an SQLITE_SCHEMA sl@0: # error should be returned. sl@0: # sl@0: # $Id: schema.test,v 1.8 2007/10/09 08:29:33 danielk1977 Exp $ sl@0: sl@0: #--------------------------------------------------------------------- sl@0: # When any of the following types of SQL statements or actions are sl@0: # executed, all pre-compiled statements are invalidated. An attempt sl@0: # to execute an invalidated statement always returns SQLITE_SCHEMA. sl@0: # sl@0: # CREATE/DROP TABLE...................................schema-1.* sl@0: # CREATE/DROP VIEW....................................schema-2.* sl@0: # CREATE/DROP TRIGGER.................................schema-3.* sl@0: # CREATE/DROP INDEX...................................schema-4.* sl@0: # DETACH..............................................schema-5.* sl@0: # Deleting a user-function............................schema-6.* sl@0: # Deleting a collation sequence.......................schema-7.* sl@0: # Setting or changing the authorization function......schema-8.* sl@0: # Rollback of a DDL statement.........................schema-12.* sl@0: # sl@0: # Test cases schema-9.* and schema-10.* test some specific bugs sl@0: # that came up during development. sl@0: # sl@0: # Test cases schema-11.* test that it is impossible to delete or sl@0: # change a collation sequence or user-function while SQL statements sl@0: # are executing. Adding new collations or functions is allowed. sl@0: # sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: do_test schema-1.1 { sl@0: set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL] sl@0: execsql { sl@0: CREATE TABLE abc(a, b, c); sl@0: } sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ERROR} sl@0: do_test schema-1.2 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_SCHEMA} sl@0: do_test schema-1.3 { sl@0: set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL] sl@0: execsql { sl@0: DROP TABLE abc; sl@0: } sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ERROR} sl@0: do_test schema-1.4 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_SCHEMA} sl@0: sl@0: ifcapable view { sl@0: do_test schema-2.1 { sl@0: set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL] sl@0: execsql { sl@0: CREATE VIEW v1 AS SELECT * FROM sqlite_master; sl@0: } sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ERROR} sl@0: do_test schema-2.2 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_SCHEMA} sl@0: do_test schema-2.3 { sl@0: set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL] sl@0: execsql { sl@0: DROP VIEW v1; sl@0: } sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ERROR} sl@0: do_test schema-2.4 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_SCHEMA} sl@0: } sl@0: sl@0: ifcapable trigger { sl@0: do_test schema-3.1 { sl@0: execsql { sl@0: CREATE TABLE abc(a, b, c); sl@0: } sl@0: set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL] sl@0: execsql { sl@0: CREATE TRIGGER abc_trig AFTER INSERT ON abc BEGIN sl@0: SELECT 1, 2, 3; sl@0: END; sl@0: } sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ERROR} sl@0: do_test schema-3.2 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_SCHEMA} sl@0: do_test schema-3.3 { sl@0: set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL] sl@0: execsql { sl@0: DROP TRIGGER abc_trig; sl@0: } sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ERROR} sl@0: do_test schema-3.4 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_SCHEMA} sl@0: } sl@0: sl@0: do_test schema-4.1 { sl@0: catchsql { sl@0: CREATE TABLE abc(a, b, c); sl@0: } sl@0: set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL] sl@0: execsql { sl@0: CREATE INDEX abc_index ON abc(a); sl@0: } sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ERROR} sl@0: do_test schema-4.2 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_SCHEMA} sl@0: do_test schema-4.3 { sl@0: set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL] sl@0: execsql { sl@0: DROP INDEX abc_index; sl@0: } sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ERROR} sl@0: do_test schema-4.4 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_SCHEMA} sl@0: sl@0: #--------------------------------------------------------------------- sl@0: # Tests 5.1 to 5.4 check that prepared statements are invalidated when sl@0: # a database is DETACHed (but not when one is ATTACHed). sl@0: # sl@0: ifcapable attach { sl@0: do_test schema-5.1 { sl@0: set sql {SELECT * FROM abc;} sl@0: set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL] sl@0: execsql { sl@0: ATTACH 'test2.db' AS aux; sl@0: } sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_DONE} sl@0: do_test schema-5.2 { sl@0: sqlite3_reset $::STMT sl@0: } {SQLITE_OK} sl@0: do_test schema-5.3 { sl@0: execsql { sl@0: DETACH aux; sl@0: } sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ERROR} sl@0: do_test schema-5.4 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_SCHEMA} sl@0: } sl@0: sl@0: #--------------------------------------------------------------------- sl@0: # Tests 6.* check that prepared statements are invalidated when sl@0: # a user-function is deleted (but not when one is added). sl@0: do_test schema-6.1 { sl@0: set sql {SELECT * FROM abc;} sl@0: set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL] sl@0: db function hello_function {} sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_DONE} sl@0: do_test schema-6.2 { sl@0: sqlite3_reset $::STMT sl@0: } {SQLITE_OK} sl@0: do_test schema-6.3 { sl@0: sqlite_delete_function $::DB hello_function sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ERROR} sl@0: do_test schema-6.4 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_SCHEMA} sl@0: sl@0: #--------------------------------------------------------------------- sl@0: # Tests 7.* check that prepared statements are invalidated when sl@0: # a collation sequence is deleted (but not when one is added). sl@0: # sl@0: ifcapable utf16 { sl@0: do_test schema-7.1 { sl@0: set sql {SELECT * FROM abc;} sl@0: set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL] sl@0: add_test_collate $::DB 1 1 1 sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_DONE} sl@0: do_test schema-7.2 { sl@0: sqlite3_reset $::STMT sl@0: } {SQLITE_OK} sl@0: do_test schema-7.3 { sl@0: add_test_collate $::DB 0 0 0 sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ERROR} sl@0: do_test schema-7.4 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_SCHEMA} sl@0: } sl@0: sl@0: #--------------------------------------------------------------------- sl@0: # Tests 8.1 and 8.2 check that prepared statements are invalidated when sl@0: # the authorization function is set. sl@0: # sl@0: ifcapable auth { sl@0: do_test schema-8.1 { sl@0: set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL] sl@0: db auth {} sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ERROR} sl@0: do_test schema-8.3 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_SCHEMA} sl@0: } sl@0: sl@0: #--------------------------------------------------------------------- sl@0: # schema-9.1: Test that if a table is dropped by one database connection, sl@0: # other database connections are aware of the schema change. sl@0: # schema-9.2: Test that if a view is dropped by one database connection, sl@0: # other database connections are aware of the schema change. sl@0: # sl@0: do_test schema-9.1 { sl@0: sqlite3 db2 test.db sl@0: execsql { sl@0: DROP TABLE abc; sl@0: } db2 sl@0: db2 close sl@0: catchsql { sl@0: SELECT * FROM abc; sl@0: } sl@0: } {1 {no such table: abc}} sl@0: execsql { sl@0: CREATE TABLE abc(a, b, c); sl@0: } sl@0: ifcapable view { sl@0: do_test schema-9.2 { sl@0: execsql { sl@0: CREATE VIEW abcview AS SELECT * FROM abc; sl@0: } sl@0: sqlite3 db2 test.db sl@0: execsql { sl@0: DROP VIEW abcview; sl@0: } db2 sl@0: db2 close sl@0: catchsql { sl@0: SELECT * FROM abcview; sl@0: } sl@0: } {1 {no such table: abcview}} sl@0: } sl@0: sl@0: #--------------------------------------------------------------------- sl@0: # Test that if a CREATE TABLE statement fails because there are other sl@0: # btree cursors open on the same database file it does not corrupt sl@0: # the sqlite_master table. sl@0: # sl@0: # 2007-05-02: These tests have been overcome by events. Open btree sl@0: # cursors no longer block CREATE TABLE. But there is no reason not sl@0: # to keep the tests in the test suite. sl@0: # sl@0: do_test schema-10.1 { sl@0: execsql { sl@0: INSERT INTO abc VALUES(1, 2, 3); sl@0: } sl@0: set sql {SELECT * FROM abc} sl@0: set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL] sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ROW} sl@0: do_test schema-10.2 { sl@0: catchsql { sl@0: CREATE TABLE t2(a, b, c); sl@0: } sl@0: } {0 {}} sl@0: do_test schema-10.3 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_OK} sl@0: do_test schema-10.4 { sl@0: sqlite3 db2 test.db sl@0: execsql { sl@0: SELECT * FROM abc sl@0: } db2 sl@0: } {1 2 3} sl@0: do_test schema-10.5 { sl@0: db2 close sl@0: } {} sl@0: sl@0: #--------------------------------------------------------------------- sl@0: # Attempting to delete or replace a user-function or collation sequence sl@0: # while there are active statements returns an SQLITE_BUSY error. sl@0: # sl@0: # schema-11.1 - 11.4: User function. sl@0: # schema-11.5 - 11.8: Collation sequence. sl@0: # sl@0: do_test schema-11.1 { sl@0: db function tstfunc {} sl@0: set sql {SELECT * FROM abc} sl@0: set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL] sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ROW} sl@0: do_test schema-11.2 { sl@0: sqlite_delete_function $::DB tstfunc sl@0: } {SQLITE_BUSY} sl@0: do_test schema-11.3 { sl@0: set rc [catch { sl@0: db function tstfunc {} sl@0: } msg] sl@0: list $rc $msg sl@0: } {1 {Unable to delete/modify user-function due to active statements}} sl@0: do_test schema-11.4 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_OK} sl@0: do_test schema-11.5 { sl@0: db collate tstcollate {} sl@0: set sql {SELECT * FROM abc} sl@0: set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL] sl@0: sqlite3_step $::STMT sl@0: } {SQLITE_ROW} sl@0: do_test schema-11.6 { sl@0: sqlite_delete_collation $::DB tstcollate sl@0: } {SQLITE_BUSY} sl@0: do_test schema-11.7 { sl@0: set rc [catch { sl@0: db collate tstcollate {} sl@0: } msg] sl@0: list $rc $msg sl@0: } {1 {Unable to delete/modify collation sequence due to active statements}} sl@0: do_test schema-11.8 { sl@0: sqlite3_finalize $::STMT sl@0: } {SQLITE_OK} sl@0: sl@0: # The following demonstrates why statements need to be expired whenever sl@0: # there is a rollback (explicit or otherwise). sl@0: # sl@0: do_test schema-12.1 { sl@0: # Begin a transaction and create a table. This increments sl@0: # the schema cookie. Then compile an SQL statement, using sl@0: # the current (incremented) value of the cookie. sl@0: execsql { sl@0: BEGIN; sl@0: CREATE TABLE t3(a, b, c); sl@0: } sl@0: set ::STMT [sqlite3_prepare $::DB "CREATE TABLE t4(a,b,c)" -1 TAIL] sl@0: sl@0: # Rollback the transaction, resetting the schema cookie to the value sl@0: # it had at the start of this test case. Then create a table, sl@0: # incrementing the schema cookie. sl@0: execsql { sl@0: ROLLBACK; sl@0: CREATE TABLE t4(a, b, c); sl@0: } sl@0: sl@0: # The schema cookie now has the same value as it did when SQL statement sl@0: # $::STMT was prepared. So unless it has been expired, it would be sl@0: # possible to run the "CREATE TABLE t4" statement and create a sl@0: # duplicate table. sl@0: list [sqlite3_step $::STMT] [sqlite3_finalize $::STMT] sl@0: } {SQLITE_ERROR SQLITE_SCHEMA} sl@0: sl@0: finish_test