3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library.
13 # This file tests the various conditions under which an SQLITE_SCHEMA
14 # error should be returned.
16 # $Id: schema.test,v 1.9 2009/02/04 17:40:58 drh Exp $
18 #---------------------------------------------------------------------
19 # When any of the following types of SQL statements or actions are
20 # executed, all pre-compiled statements are invalidated. An attempt
21 # to execute an invalidated statement always returns SQLITE_SCHEMA.
23 # CREATE/DROP TABLE...................................schema-1.*
24 # CREATE/DROP VIEW....................................schema-2.*
25 # CREATE/DROP TRIGGER.................................schema-3.*
26 # CREATE/DROP INDEX...................................schema-4.*
27 # DETACH..............................................schema-5.*
28 # Deleting a user-function............................schema-6.*
29 # Deleting a collation sequence.......................schema-7.*
30 # Setting or changing the authorization function......schema-8.*
31 # Rollback of a DDL statement.........................schema-12.*
33 # Test cases schema-9.* and schema-10.* test some specific bugs
34 # that came up during development.
36 # Test cases schema-11.* test that it is impossible to delete or
37 # change a collation sequence or user-function while SQL statements
38 # are executing. Adding new collations or functions is allowed.
41 set testdir [file dirname $argv0]
42 source $testdir/tester.tcl
45 set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
47 CREATE TABLE abc(a, b, c);
52 sqlite3_finalize $::STMT
55 set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
62 sqlite3_finalize $::STMT
67 set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
69 CREATE VIEW v1 AS SELECT * FROM sqlite_master;
74 sqlite3_finalize $::STMT
77 set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
84 sqlite3_finalize $::STMT
91 CREATE TABLE abc(a, b, c);
93 set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
95 CREATE TRIGGER abc_trig AFTER INSERT ON abc BEGIN
102 sqlite3_finalize $::STMT
105 set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
107 DROP TRIGGER abc_trig;
112 sqlite3_finalize $::STMT
118 CREATE TABLE abc(a, b, c);
120 set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
122 CREATE INDEX abc_index ON abc(a);
127 sqlite3_finalize $::STMT
130 set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
132 DROP INDEX abc_index;
137 sqlite3_finalize $::STMT
140 #---------------------------------------------------------------------
141 # Tests 5.1 to 5.4 check that prepared statements are invalidated when
142 # a database is DETACHed (but not when one is ATTACHed).
146 set sql {SELECT * FROM abc;}
147 set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL]
149 ATTACH 'test2.db' AS aux;
154 sqlite3_reset $::STMT
163 sqlite3_finalize $::STMT
167 #---------------------------------------------------------------------
168 # Tests 6.* check that prepared statements are invalidated when
169 # a user-function is deleted (but not when one is added).
171 set sql {SELECT * FROM abc;}
172 set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL]
173 db function hello_function {}
177 sqlite3_reset $::STMT
180 sqlite_delete_function $::DB hello_function
184 sqlite3_finalize $::STMT
187 #---------------------------------------------------------------------
188 # Tests 7.* check that prepared statements are invalidated when
189 # a collation sequence is deleted (but not when one is added).
193 set sql {SELECT * FROM abc;}
194 set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL]
195 add_test_collate $::DB 1 1 1
199 sqlite3_reset $::STMT
202 add_test_collate $::DB 0 0 0
206 sqlite3_finalize $::STMT
210 #---------------------------------------------------------------------
211 # Tests 8.1 and 8.2 check that prepared statements are invalidated when
212 # the authorization function is set to a non-null function. Tests 8.11
213 # and 8.12 verify that no invalidations occur when the authorizer is
217 proc noop_auth {args} {return SQLITE_OK}
219 set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
224 sqlite3_finalize $::STMT
226 do_test schema-8.11 {
227 set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
231 do_test schema-8.12 {
232 sqlite3_finalize $::STMT
237 #---------------------------------------------------------------------
238 # schema-9.1: Test that if a table is dropped by one database connection,
239 # other database connections are aware of the schema change.
240 # schema-9.2: Test that if a view is dropped by one database connection,
241 # other database connections are aware of the schema change.
252 } {1 {no such table: abc}}
254 CREATE TABLE abc(a, b, c);
259 CREATE VIEW abcview AS SELECT * FROM abc;
267 SELECT * FROM abcview;
269 } {1 {no such table: abcview}}
272 #---------------------------------------------------------------------
273 # Test that if a CREATE TABLE statement fails because there are other
274 # btree cursors open on the same database file it does not corrupt
275 # the sqlite_master table.
277 # 2007-05-02: These tests have been overcome by events. Open btree
278 # cursors no longer block CREATE TABLE. But there is no reason not
279 # to keep the tests in the test suite.
281 do_test schema-10.1 {
283 INSERT INTO abc VALUES(1, 2, 3);
285 set sql {SELECT * FROM abc}
286 set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL]
289 do_test schema-10.2 {
291 CREATE TABLE t2(a, b, c);
294 do_test schema-10.3 {
295 sqlite3_finalize $::STMT
297 do_test schema-10.4 {
303 do_test schema-10.5 {
307 #---------------------------------------------------------------------
308 # Attempting to delete or replace a user-function or collation sequence
309 # while there are active statements returns an SQLITE_BUSY error.
311 # schema-11.1 - 11.4: User function.
312 # schema-11.5 - 11.8: Collation sequence.
314 do_test schema-11.1 {
315 db function tstfunc {}
316 set sql {SELECT * FROM abc}
317 set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL]
320 do_test schema-11.2 {
321 sqlite_delete_function $::DB tstfunc
323 do_test schema-11.3 {
325 db function tstfunc {}
328 } {1 {unable to delete/modify user-function due to active statements}}
329 do_test schema-11.4 {
330 sqlite3_finalize $::STMT
332 do_test schema-11.5 {
333 db collate tstcollate {}
334 set sql {SELECT * FROM abc}
335 set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL]
338 do_test schema-11.6 {
339 sqlite_delete_collation $::DB tstcollate
341 do_test schema-11.7 {
343 db collate tstcollate {}
346 } {1 {unable to delete/modify collation sequence due to active statements}}
347 do_test schema-11.8 {
348 sqlite3_finalize $::STMT
351 # The following demonstrates why statements need to be expired whenever
352 # there is a rollback (explicit or otherwise).
354 do_test schema-12.1 {
355 # Begin a transaction and create a table. This increments
356 # the schema cookie. Then compile an SQL statement, using
357 # the current (incremented) value of the cookie.
360 CREATE TABLE t3(a, b, c);
362 set ::STMT [sqlite3_prepare $::DB "CREATE TABLE t4(a,b,c)" -1 TAIL]
364 # Rollback the transaction, resetting the schema cookie to the value
365 # it had at the start of this test case. Then create a table,
366 # incrementing the schema cookie.
369 CREATE TABLE t4(a, b, c);
372 # The schema cookie now has the same value as it did when SQL statement
373 # $::STMT was prepared. So unless it has been expired, it would be
374 # possible to run the "CREATE TABLE t4" statement and create a
376 list [sqlite3_step $::STMT] [sqlite3_finalize $::STMT]
377 } {SQLITE_ERROR SQLITE_SCHEMA}
381 do_test schema-13.1 {
382 set S [sqlite3_prepare_v2 db "SELECT * FROM sqlite_master" -1 dummy]
383 db function hello hello
387 if {[lindex $args 0] == "SQLITE_READ"} {return SQLITE_DENY}
393 do_test schema-13.2 {
397 do_test schema-13.3 {