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. The
12 # focus of this script is testing the ATTACH and DETACH commands
13 # and related functionality.
15 # $Id: attach.test,v 1.52 2009/05/29 14:39:08 drh Exp $
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
26 for {set i 2} {$i<=15} {incr i} {
28 forcedelete test$i.db-journal
34 INSERT INTO t1 VALUES(1,2);
35 INSERT INTO t1 VALUES(3,4);
43 INSERT INTO t2 VALUES(1,'x');
44 INSERT INTO t2 VALUES(2,'y');
50 ATTACH DATABASE 'test2.db' AS two;
55 # Tests for the sqlite3_db_filename interface
57 do_test attach-1.3.1 {
58 file tail [sqlite3_db_filename db main]
60 do_test attach-1.3.2 {
61 file tail [sqlite3_db_filename db MAIN]
63 do_test attach-1.3.3 {
64 file tail [sqlite3_db_filename db temp]
66 do_test attach-1.3.4 {
67 file tail [sqlite3_db_filename db two]
69 do_test attach-1.3.5 {
70 file tail [sqlite3_db_filename db three]
88 } {1 {no such table: t2}}
93 } {1 {no such table: two.t2}}
96 ATTACH DATABASE 'test3.db' AS three;
101 SELECT * FROM three.sqlite_master;
104 do_test attach-1.10 {
106 DETACH DATABASE [three];
109 do_test attach-1.11 {
111 ATTACH 'test.db' AS db2;
112 ATTACH 'test.db' AS db3;
113 ATTACH 'test.db' AS db4;
114 ATTACH 'test.db' AS db5;
115 ATTACH 'test.db' AS db6;
116 ATTACH 'test.db' AS db7;
117 ATTACH 'test.db' AS db8;
118 ATTACH 'test.db' AS db9;
123 foreach {idx name file} [execsql {PRAGMA database_list} $db] {
124 lappend list $idx $name
128 ifcapable schema_pragmas {
129 do_test attach-1.11b {
131 } {0 main 2 db2 3 db3 4 db4 5 db5 6 db6 7 db7 8 db8 9 db9}
132 } ;# ifcapable schema_pragmas
133 do_test attach-1.12 {
135 ATTACH 'test.db' as db2;
137 } {1 {database db2 is already in use}}
138 do_test attach-1.12.2 {
141 do_test attach-1.13 {
143 ATTACH 'test.db' as db5;
145 } {1 {database db5 is already in use}}
146 do_test attach-1.14 {
148 ATTACH 'test.db' as db9;
150 } {1 {database db9 is already in use}}
151 do_test attach-1.15 {
153 ATTACH 'test.db' as main;
155 } {1 {database main is already in use}}
157 do_test attach-1.16 {
159 ATTACH 'test.db' as temp;
161 } {1 {database temp is already in use}}
163 do_test attach-1.17 {
165 ATTACH 'test.db' as MAIN;
167 } {1 {database MAIN is already in use}}
168 do_test attach-1.18 {
170 ATTACH 'test.db' as db10;
171 ATTACH 'test.db' as db11;
174 if {$SQLITE_MAX_ATTACHED==10} {
175 do_test attach-1.19 {
177 ATTACH 'test.db' as db12;
179 } {1 {too many attached databases - max 10}}
180 do_test attach-1.19.1 {
184 do_test attach-1.20.1 {
189 ifcapable schema_pragmas {
190 do_test attach-1.20.2 {
192 } {0 main 2 db2 3 db3 4 db4 5 db6 6 db7 7 db8 8 db9 9 db10 10 db11}
193 } ;# ifcapable schema_pragmas
194 integrity_check attach-1.20.3
196 execsql {select * from sqlite_temp_master}
198 do_test attach-1.21 {
200 ATTACH 'test.db' as db12;
203 if {$SQLITE_MAX_ATTACHED==10} {
204 do_test attach-1.22 {
206 ATTACH 'test.db' as db13;
208 } {1 {too many attached databases - max 10}}
209 do_test attach-1.22.1 {
213 do_test attach-1.23 {
217 } {1 {no such database: db14}}
218 do_test attach-1.24 {
223 do_test attach-1.25 {
227 } {1 {no such database: db12}}
228 do_test attach-1.26 {
232 } {1 {cannot detach database main}}
235 do_test attach-1.27 {
239 } {1 {cannot detach database Temp}}
241 do_test attach-1.27 {
245 } {1 {no such database: Temp}}
248 do_test attach-1.28 {
261 ifcapable schema_pragmas {
263 do_test attach-1.29 {
267 do_test attach-1.29 {
271 } ;# ifcapable schema_pragmas
273 ifcapable {trigger} { # Only do the following tests if triggers are enabled
276 CREATE TABLE tx(x1,x2,y1,y2);
277 CREATE TRIGGER r1 AFTER UPDATE ON t2 FOR EACH ROW BEGIN
278 INSERT INTO tx(x1,x2,y1,y2) VALUES(OLD.x,NEW.x,OLD.y,NEW.y);
285 UPDATE t2 SET x=x+10;
288 } {1 11 x x 2 12 y y}
291 CREATE TABLE tx(x1,x2,y1,y2);
297 ATTACH 'test2.db' AS db2;
302 UPDATE db2.t2 SET x=x+10;
303 SELECT * FROM db2.tx;
305 } {1 11 x x 2 12 y y 11 21 x x 12 22 y y}
308 SELECT * FROM main.tx;
313 SELECT type, name, tbl_name FROM db2.sqlite_master;
315 } {table t2 t2 table tx tx trigger r1 t2}
317 ifcapable schema_pragmas&&tempdb {
320 } {0 main 1 temp 2 db2}
321 } ;# ifcapable schema_pragmas&&tempdb
322 ifcapable schema_pragmas&&!tempdb {
326 } ;# ifcapable schema_pragmas&&!tempdb
330 CREATE INDEX i2 ON t2(x);
331 SELECT * FROM t2 WHERE x>5;
334 do_test attach-2.10 {
336 SELECT type, name, tbl_name FROM sqlite_master;
338 } {table t2 t2 table tx tx trigger r1 t2 index i2 t2}
339 #do_test attach-2.11 {
341 # SELECT * FROM t2 WHERE x>5;
343 #} {1 {database schema has changed}}
344 ifcapable schema_pragmas {
346 do_test attach-2.12 {
348 } {0 main 1 temp 2 db2}
350 do_test attach-2.12 {
354 } ;# ifcapable schema_pragmas
355 do_test attach-2.13 {
357 SELECT * FROM t2 WHERE x>5;
360 do_test attach-2.14 {
362 SELECT type, name, tbl_name FROM sqlite_master;
364 } {table t1 t1 table tx tx}
365 do_test attach-2.15 {
367 SELECT type, name, tbl_name FROM db2.sqlite_master;
369 } {table t2 t2 table tx tx trigger r1 t2 index i2 t2}
370 do_test attach-2.16 {
374 ATTACH 'test2.db' AS db2;
375 SELECT type, name, tbl_name FROM db2.sqlite_master;
377 } {table t2 t2 table tx tx trigger r1 t2 index i2 t2}
378 } ;# End of ifcapable {trigger}
390 # If we are testing a version of the code that lacks trigger support,
391 # adjust the database contents so that they are the same if triggers
393 ifcapable {!trigger} {
396 INSERT INTO t2 VALUES(21, 'x');
397 INSERT INTO t2 VALUES(22, 'y');
398 CREATE TABLE tx(x1,x2,y1,y2);
399 INSERT INTO tx VALUES(1, 11, 'x', 'x');
400 INSERT INTO tx VALUES(2, 12, 'y', 'y');
401 INSERT INTO tx VALUES(11, 21, 'x', 'x');
402 INSERT INTO tx VALUES(12, 22, 'y', 'y');
403 CREATE INDEX i2 ON t2(x);
411 } {1 {no such table: t2}}
414 ATTACH DATABASE 'test2.db' AS db2;
419 # Even though 'db' has started a transaction, it should not yet have
420 # a lock on test2.db so 'db2' should be readable.
428 # Reading from test2.db from db within a transaction should not
429 # prevent test2.db from being read by db2.
431 execsql {SELECT * FROM t2}
437 # Making a change to test2.db through db causes test2.db to get
438 # a reserved lock. It should still be accessible through db2.
441 UPDATE t2 SET x=x+1 WHERE x=50;
450 execsql {SELECT * FROM t2} db2
453 # Start transactions on both db and db2. Once again, just because
454 # we make a change to test2.db using db2, only a RESERVED lock is
455 # obtained, so test2.db should still be readable using db.
460 execsql {UPDATE t2 SET x=0 WHERE 0} db2
461 catchsql {SELECT * FROM t2}
464 # It is also still accessible from db2.
466 catchsql {SELECT * FROM t2} db2
469 do_test attach-3.10 {
470 execsql {SELECT * FROM t1}
473 do_test attach-3.11 {
474 catchsql {UPDATE t1 SET a=a+1}
476 do_test attach-3.12 {
477 execsql {SELECT * FROM t1}
480 # db2 has a RESERVED lock on test2.db, so db cannot write to any tables
482 do_test attach-3.13 {
483 catchsql {UPDATE t2 SET x=x+1 WHERE x=50}
484 } {1 {database is locked}}
486 # Change for version 3. Transaction is no longer rolled back
487 # for a locked database.
490 # db is able to reread its schema because db2 still only holds a
492 do_test attach-3.14 {
493 catchsql {SELECT * FROM t1}
495 do_test attach-3.15 {
497 execsql {SELECT * FROM t1}
506 CREATE TABLE t3(x,y);
507 CREATE UNIQUE INDEX t3i1 ON t3(x);
508 INSERT INTO t3 VALUES(1,2);
514 CREATE TABLE t3(a,b);
515 CREATE UNIQUE INDEX t3i1b ON t3(a);
516 INSERT INTO t3 VALUES(9,10);
522 ATTACH DATABASE 'test2.db' AS db2;
523 SELECT * FROM db2.t3;
528 SELECT * FROM main.t3;
533 INSERT INTO db2.t3 VALUES(9,10);
534 SELECT * FROM db2.t3;
540 ifcapable {trigger} {
544 CREATE TRIGGER t3r3 AFTER INSERT ON t3 BEGIN
545 INSERT INTO t4 VALUES('db2.' || NEW.x);
547 INSERT INTO t3 VALUES(6,7);
554 CREATE TRIGGER t3r3 AFTER INSERT ON t3 BEGIN
555 INSERT INTO t4 VALUES('main.' || NEW.a);
557 INSERT INTO main.t3 VALUES(11,12);
558 SELECT * FROM main.t4;
562 ifcapable {!trigger} {
563 # When we do not have trigger support, set up the table like they
564 # would have been had triggers been there. The tests that follow need
568 INSERT INTO t3 VALUES(6,7);
569 INSERT INTO t4 VALUES('db2.6');
570 INSERT INTO t4 VALUES('db2.13');
574 INSERT INTO main.t3 VALUES(11,12);
575 INSERT INTO t4 VALUES('main.11');
580 # This one is tricky. On the UNION ALL select, we have to make sure
581 # the schema for both main and db2 is valid before starting to execute
582 # the first query of the UNION ALL. If we wait to test the validity of
583 # the schema for main until after the first query has run, that test will
584 # fail and the query will abort but we will have already output some
585 # results. When the query is retried, the results will be repeated.
590 ATTACH DATABASE 'test2.db' AS db2;
591 INSERT INTO db2.t3 VALUES(13,14);
592 SELECT * FROM db2.t4 UNION ALL SELECT * FROM main.t4;
594 } {db2.6 db2.13 main.11}
597 ifcapable {!trigger} {execsql {INSERT INTO main.t4 VALUES('main.15')}}
599 INSERT INTO main.t3 VALUES(15,16);
600 SELECT * FROM db2.t4 UNION ALL SELECT * FROM main.t4;
602 } {db2.6 db2.13 main.11 main.15}
603 } ;# ifcapable compound
605 ifcapable !compound {
606 ifcapable {!trigger} {execsql {INSERT INTO main.t4 VALUES('main.15')}}
608 ATTACH DATABASE 'test2.db' AS db2;
609 INSERT INTO db2.t3 VALUES(13,14);
610 INSERT INTO main.t3 VALUES(15,16);
612 } ;# ifcapable !compound
615 do_test attach-4.10 {
620 CREATE VIEW v3 AS SELECT x*100+y FROM t3;
624 do_test attach-4.11 {
626 CREATE VIEW v3 AS SELECT a*100+b FROM t3;
630 do_test attach-4.12 {
632 ATTACH DATABASE 'test2.db' AS db2;
633 SELECT * FROM db2.v3;
636 do_test attach-4.13 {
638 SELECT * FROM main.v3;
643 # Tests for the sqliteFix...() routines in attach.c
645 ifcapable {trigger} {
653 ATTACH DATABASE 'test.db' AS orig;
654 CREATE TRIGGER r1 AFTER INSERT ON orig.t1 BEGIN
658 } {1 {trigger r1 cannot reference objects in database orig}}
661 CREATE TABLE t5(x,y);
662 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
670 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
671 SELECT 'no-op' FROM orig.t1;
674 } {1 {trigger r5 cannot reference objects in database orig}}
678 CREATE TEMP TABLE t6(p,q,r);
679 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
680 SELECT 'no-op' FROM temp.t6;
683 } {1 {trigger r5 cannot reference objects in database temp}}
688 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
689 SELECT 'no-op' || (SELECT * FROM temp.t6);
692 } {1 {trigger r5 cannot reference objects in database temp}}
695 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
696 SELECT 'no-op' FROM t1 WHERE x<(SELECT min(x) FROM temp.t6);
699 } {1 {trigger r5 cannot reference objects in database temp}}
702 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
703 SELECT 'no-op' FROM t1 GROUP BY 1 HAVING x<(SELECT min(x) FROM temp.t6);
706 } {1 {trigger r5 cannot reference objects in database temp}}
709 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
710 SELECT max(1,x,(SELECT min(x) FROM temp.t6)) FROM t1;
713 } {1 {trigger r5 cannot reference objects in database temp}}
716 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
717 INSERT INTO t1 VALUES((SELECT min(x) FROM temp.t6),5);
720 } {1 {trigger r5 cannot reference objects in database temp}}
723 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
724 DELETE FROM t1 WHERE x<(SELECT min(x) FROM temp.t6);
727 } {1 {trigger r5 cannot reference objects in database temp}}
731 # Check to make sure we get a sensible error if unable to open
732 # the file that we are trying to attach.
736 ATTACH DATABASE 'no-such-file' AS nosuch;
739 if {$tcl_platform(platform)=="unix"} {
741 sqlite3 dbx cannot-read
742 dbx eval {CREATE TABLE t1(a,b,c)}
744 file attributes cannot-read -permission 0000
745 if {[file writable cannot-read]} {
746 puts "\n**** Tests do not work when run as root ****"
747 forcedelete cannot-read
751 ATTACH DATABASE 'cannot-read' AS noread;
753 } {1 {unable to open database: cannot-read}}
754 do_test attach-6.2.2 {
757 forcedelete cannot-read
760 # Check the error message if we try to access a database that has
764 CREATE TABLE no_such_db.t1(a, b, c);
766 } {1 {unknown database no_such_db}}
767 for {set i 2} {$i<=15} {incr i} {
772 forcedelete no-such-file
776 forcedelete test.db test.db-journal
779 DETACH RAISE ( IGNORE ) IN ( SELECT "AAAAAA" . * ORDER BY
780 REGISTER LIMIT "AAAAAA" . "AAAAAA" OFFSET RAISE ( IGNORE ) NOT NULL )
782 } {1 {no such table: AAAAAA}}
785 # Create a malformed file (a file that is not a valid database)
786 # and try to attach it
789 set fd [open test2.db w]
790 puts $fd "This file is not a valid SQLite database"
793 ATTACH 'test2.db' AS t2;
795 } {1 {file is encrypted or is not a database}}
802 db2 eval {CREATE TABLE t1(x); BEGIN EXCLUSIVE}
804 ATTACH 'test2.db' AS t2;
806 } {1 {database is locked}}
813 # Test that it is possible to attach the same database more than
814 # once when not in shared-cache mode. That this is not possible in
815 # shared-cache mode is tested in shared7.test.
819 ATTACH 'test4.db' AS aux1;
820 CREATE TABLE aux1.t1(a, b);
821 INSERT INTO aux1.t1 VALUES(1, 2);
822 ATTACH 'test4.db' AS aux2;
823 SELECT * FROM aux2.t1;
829 INSERT INTO aux1.t1 VALUES(3, 4);
830 INSERT INTO aux2.t1 VALUES(5, 6);
832 } {1 {database is locked}}
836 SELECT * FROM aux2.t1;
840 # Ticket [abe728bbc311d81334dae9762f0db87c07a98f79].
841 # Multi-database commit on an attached TEMP database.
843 do_test attach-10.1 {
846 ATTACH ':memory:' AS inmem;
848 CREATE TABLE noname.noname(x);
849 CREATE TABLE inmem.inmem(y);
850 CREATE TABLE main.main(z);
852 SELECT name FROM noname.sqlite_master;
853 SELECT name FROM inmem.sqlite_master;
856 do_test attach-10.2 {
858 PRAGMA database_list;
860 } {4 noname {} 5 inmem {}}