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} {
27 file delete -force test$i.db
28 file delete -force 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;
69 } {1 {no such table: t2}}
74 } {1 {no such table: two.t2}}
77 ATTACH DATABASE 'test3.db' AS three;
82 SELECT * FROM three.sqlite_master;
87 DETACH DATABASE [three];
92 ATTACH 'test.db' AS db2;
93 ATTACH 'test.db' AS db3;
94 ATTACH 'test.db' AS db4;
95 ATTACH 'test.db' AS db5;
96 ATTACH 'test.db' AS db6;
97 ATTACH 'test.db' AS db7;
98 ATTACH 'test.db' AS db8;
99 ATTACH 'test.db' AS db9;
104 foreach {idx name file} [execsql {PRAGMA database_list} $db] {
105 lappend list $idx $name
109 ifcapable schema_pragmas {
110 do_test attach-1.11b {
112 } {0 main 2 db2 3 db3 4 db4 5 db5 6 db6 7 db7 8 db8 9 db9}
113 } ;# ifcapable schema_pragmas
114 do_test attach-1.12 {
116 ATTACH 'test.db' as db2;
118 } {1 {database db2 is already in use}}
119 do_test attach-1.12.2 {
122 do_test attach-1.13 {
124 ATTACH 'test.db' as db5;
126 } {1 {database db5 is already in use}}
127 do_test attach-1.14 {
129 ATTACH 'test.db' as db9;
131 } {1 {database db9 is already in use}}
132 do_test attach-1.15 {
134 ATTACH 'test.db' as main;
136 } {1 {database main is already in use}}
138 do_test attach-1.16 {
140 ATTACH 'test.db' as temp;
142 } {1 {database temp is already in use}}
144 do_test attach-1.17 {
146 ATTACH 'test.db' as MAIN;
148 } {1 {database MAIN is already in use}}
149 do_test attach-1.18 {
151 ATTACH 'test.db' as db10;
152 ATTACH 'test.db' as db11;
155 if {$SQLITE_MAX_ATTACHED==10} {
156 do_test attach-1.19 {
158 ATTACH 'test.db' as db12;
160 } {1 {too many attached databases - max 10}}
161 do_test attach-1.19.1 {
165 do_test attach-1.20.1 {
170 ifcapable schema_pragmas {
171 do_test attach-1.20.2 {
173 } {0 main 2 db2 3 db3 4 db4 5 db6 6 db7 7 db8 8 db9 9 db10 10 db11}
174 } ;# ifcapable schema_pragmas
175 integrity_check attach-1.20.3
177 execsql {select * from sqlite_temp_master}
179 do_test attach-1.21 {
181 ATTACH 'test.db' as db12;
184 if {$SQLITE_MAX_ATTACHED==10} {
185 do_test attach-1.22 {
187 ATTACH 'test.db' as db13;
189 } {1 {too many attached databases - max 10}}
190 do_test attach-1.22.1 {
194 do_test attach-1.23 {
198 } {1 {no such database: db14}}
199 do_test attach-1.24 {
204 do_test attach-1.25 {
208 } {1 {no such database: db12}}
209 do_test attach-1.26 {
213 } {1 {cannot detach database main}}
216 do_test attach-1.27 {
220 } {1 {cannot detach database Temp}}
222 do_test attach-1.27 {
226 } {1 {no such database: Temp}}
229 do_test attach-1.28 {
242 ifcapable schema_pragmas {
244 do_test attach-1.29 {
248 do_test attach-1.29 {
252 } ;# ifcapable schema_pragmas
254 ifcapable {trigger} { # Only do the following tests if triggers are enabled
257 CREATE TABLE tx(x1,x2,y1,y2);
258 CREATE TRIGGER r1 AFTER UPDATE ON t2 FOR EACH ROW BEGIN
259 INSERT INTO tx(x1,x2,y1,y2) VALUES(OLD.x,NEW.x,OLD.y,NEW.y);
266 UPDATE t2 SET x=x+10;
269 } {1 11 x x 2 12 y y}
272 CREATE TABLE tx(x1,x2,y1,y2);
278 ATTACH 'test2.db' AS db2;
283 UPDATE db2.t2 SET x=x+10;
284 SELECT * FROM db2.tx;
286 } {1 11 x x 2 12 y y 11 21 x x 12 22 y y}
289 SELECT * FROM main.tx;
294 SELECT type, name, tbl_name FROM db2.sqlite_master;
296 } {table t2 t2 table tx tx trigger r1 t2}
298 ifcapable schema_pragmas&&tempdb {
301 } {0 main 1 temp 2 db2}
302 } ;# ifcapable schema_pragmas&&tempdb
303 ifcapable schema_pragmas&&!tempdb {
307 } ;# ifcapable schema_pragmas&&!tempdb
311 CREATE INDEX i2 ON t2(x);
312 SELECT * FROM t2 WHERE x>5;
315 do_test attach-2.10 {
317 SELECT type, name, tbl_name FROM sqlite_master;
319 } {table t2 t2 table tx tx trigger r1 t2 index i2 t2}
320 #do_test attach-2.11 {
322 # SELECT * FROM t2 WHERE x>5;
324 #} {1 {database schema has changed}}
325 ifcapable schema_pragmas {
327 do_test attach-2.12 {
329 } {0 main 1 temp 2 db2}
331 do_test attach-2.12 {
335 } ;# ifcapable schema_pragmas
336 do_test attach-2.13 {
338 SELECT * FROM t2 WHERE x>5;
341 do_test attach-2.14 {
343 SELECT type, name, tbl_name FROM sqlite_master;
345 } {table t1 t1 table tx tx}
346 do_test attach-2.15 {
348 SELECT type, name, tbl_name FROM db2.sqlite_master;
350 } {table t2 t2 table tx tx trigger r1 t2 index i2 t2}
351 do_test attach-2.16 {
355 ATTACH 'test2.db' AS db2;
356 SELECT type, name, tbl_name FROM db2.sqlite_master;
358 } {table t2 t2 table tx tx trigger r1 t2 index i2 t2}
359 } ;# End of ifcapable {trigger}
371 # If we are testing a version of the code that lacks trigger support,
372 # adjust the database contents so that they are the same if triggers
374 ifcapable {!trigger} {
377 INSERT INTO t2 VALUES(21, 'x');
378 INSERT INTO t2 VALUES(22, 'y');
379 CREATE TABLE tx(x1,x2,y1,y2);
380 INSERT INTO tx VALUES(1, 11, 'x', 'x');
381 INSERT INTO tx VALUES(2, 12, 'y', 'y');
382 INSERT INTO tx VALUES(11, 21, 'x', 'x');
383 INSERT INTO tx VALUES(12, 22, 'y', 'y');
384 CREATE INDEX i2 ON t2(x);
392 } {1 {no such table: t2}}
395 ATTACH DATABASE 'test2.db' AS db2;
400 # Even though 'db' has started a transaction, it should not yet have
401 # a lock on test2.db so 'db2' should be readable.
409 # Reading from test2.db from db within a transaction should not
410 # prevent test2.db from being read by db2.
412 execsql {SELECT * FROM t2}
418 # Making a change to test2.db through db causes test2.db to get
419 # a reserved lock. It should still be accessible through db2.
422 UPDATE t2 SET x=x+1 WHERE x=50;
431 execsql {SELECT * FROM t2} db2
434 # Start transactions on both db and db2. Once again, just because
435 # we make a change to test2.db using db2, only a RESERVED lock is
436 # obtained, so test2.db should still be readable using db.
441 execsql {UPDATE t2 SET x=0 WHERE 0} db2
442 catchsql {SELECT * FROM t2}
445 # It is also still accessible from db2.
447 catchsql {SELECT * FROM t2} db2
450 do_test attach-3.10 {
451 execsql {SELECT * FROM t1}
454 do_test attach-3.11 {
455 catchsql {UPDATE t1 SET a=a+1}
457 do_test attach-3.12 {
458 execsql {SELECT * FROM t1}
461 # db2 has a RESERVED lock on test2.db, so db cannot write to any tables
463 do_test attach-3.13 {
464 catchsql {UPDATE t2 SET x=x+1 WHERE x=50}
465 } {1 {database is locked}}
467 # Change for version 3. Transaction is no longer rolled back
468 # for a locked database.
471 # db is able to reread its schema because db2 still only holds a
473 do_test attach-3.14 {
474 catchsql {SELECT * FROM t1}
476 do_test attach-3.15 {
478 execsql {SELECT * FROM t1}
487 CREATE TABLE t3(x,y);
488 CREATE UNIQUE INDEX t3i1 ON t3(x);
489 INSERT INTO t3 VALUES(1,2);
495 CREATE TABLE t3(a,b);
496 CREATE UNIQUE INDEX t3i1b ON t3(a);
497 INSERT INTO t3 VALUES(9,10);
503 ATTACH DATABASE 'test2.db' AS db2;
504 SELECT * FROM db2.t3;
509 SELECT * FROM main.t3;
514 INSERT INTO db2.t3 VALUES(9,10);
515 SELECT * FROM db2.t3;
521 ifcapable {trigger} {
525 CREATE TRIGGER t3r3 AFTER INSERT ON t3 BEGIN
526 INSERT INTO t4 VALUES('db2.' || NEW.x);
528 INSERT INTO t3 VALUES(6,7);
535 CREATE TRIGGER t3r3 AFTER INSERT ON t3 BEGIN
536 INSERT INTO t4 VALUES('main.' || NEW.a);
538 INSERT INTO main.t3 VALUES(11,12);
539 SELECT * FROM main.t4;
543 ifcapable {!trigger} {
544 # When we do not have trigger support, set up the table like they
545 # would have been had triggers been there. The tests that follow need
549 INSERT INTO t3 VALUES(6,7);
550 INSERT INTO t4 VALUES('db2.6');
551 INSERT INTO t4 VALUES('db2.13');
555 INSERT INTO main.t3 VALUES(11,12);
556 INSERT INTO t4 VALUES('main.11');
561 # This one is tricky. On the UNION ALL select, we have to make sure
562 # the schema for both main and db2 is valid before starting to execute
563 # the first query of the UNION ALL. If we wait to test the validity of
564 # the schema for main until after the first query has run, that test will
565 # fail and the query will abort but we will have already output some
566 # results. When the query is retried, the results will be repeated.
571 ATTACH DATABASE 'test2.db' AS db2;
572 INSERT INTO db2.t3 VALUES(13,14);
573 SELECT * FROM db2.t4 UNION ALL SELECT * FROM main.t4;
575 } {db2.6 db2.13 main.11}
578 ifcapable {!trigger} {execsql {INSERT INTO main.t4 VALUES('main.15')}}
580 INSERT INTO main.t3 VALUES(15,16);
581 SELECT * FROM db2.t4 UNION ALL SELECT * FROM main.t4;
583 } {db2.6 db2.13 main.11 main.15}
584 } ;# ifcapable compound
586 ifcapable !compound {
587 ifcapable {!trigger} {execsql {INSERT INTO main.t4 VALUES('main.15')}}
589 ATTACH DATABASE 'test2.db' AS db2;
590 INSERT INTO db2.t3 VALUES(13,14);
591 INSERT INTO main.t3 VALUES(15,16);
593 } ;# ifcapable !compound
596 do_test attach-4.10 {
601 CREATE VIEW v3 AS SELECT x*100+y FROM t3;
605 do_test attach-4.11 {
607 CREATE VIEW v3 AS SELECT a*100+b FROM t3;
611 do_test attach-4.12 {
613 ATTACH DATABASE 'test2.db' AS db2;
614 SELECT * FROM db2.v3;
617 do_test attach-4.13 {
619 SELECT * FROM main.v3;
624 # Tests for the sqliteFix...() routines in attach.c
626 ifcapable {trigger} {
631 file delete -force test2.db
634 ATTACH DATABASE 'test.db' AS orig;
635 CREATE TRIGGER r1 AFTER INSERT ON orig.t1 BEGIN
639 } {1 {trigger r1 cannot reference objects in database orig}}
642 CREATE TABLE t5(x,y);
643 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
651 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
652 SELECT 'no-op' FROM orig.t1;
655 } {1 {trigger r5 cannot reference objects in database orig}}
659 CREATE TEMP TABLE t6(p,q,r);
660 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
661 SELECT 'no-op' FROM temp.t6;
664 } {1 {trigger r5 cannot reference objects in database temp}}
669 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
670 SELECT 'no-op' || (SELECT * FROM temp.t6);
673 } {1 {trigger r5 cannot reference objects in database temp}}
676 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
677 SELECT 'no-op' FROM t1 WHERE x<(SELECT min(x) FROM temp.t6);
680 } {1 {trigger r5 cannot reference objects in database temp}}
683 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
684 SELECT 'no-op' FROM t1 GROUP BY 1 HAVING x<(SELECT min(x) FROM temp.t6);
687 } {1 {trigger r5 cannot reference objects in database temp}}
690 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
691 SELECT max(1,x,(SELECT min(x) FROM temp.t6)) FROM t1;
694 } {1 {trigger r5 cannot reference objects in database temp}}
697 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
698 INSERT INTO t1 VALUES((SELECT min(x) FROM temp.t6),5);
701 } {1 {trigger r5 cannot reference objects in database temp}}
704 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
705 DELETE FROM t1 WHERE x<(SELECT min(x) FROM temp.t6);
708 } {1 {trigger r5 cannot reference objects in database temp}}
712 # Check to make sure we get a sensible error if unable to open
713 # the file that we are trying to attach.
717 ATTACH DATABASE 'no-such-file' AS nosuch;
720 if {$tcl_platform(platform)=="unix"} {
722 sqlite3 dbx cannot-read
723 dbx eval {CREATE TABLE t1(a,b,c)}
725 file attributes cannot-read -permission 0000
726 if {[file writable cannot-read]} {
727 puts "\n**** Tests do not work when run as root ****"
728 file delete -force cannot-read
732 ATTACH DATABASE 'cannot-read' AS noread;
734 } {1 {unable to open database: cannot-read}}
735 do_test attach-6.2.2 {
738 file delete -force cannot-read
741 # Check the error message if we try to access a database that has
745 CREATE TABLE no_such_db.t1(a, b, c);
747 } {1 {unknown database no_such_db}}
748 for {set i 2} {$i<=15} {incr i} {
752 file delete -force test2.db
753 file delete -force no-such-file
757 file delete -force test.db test.db-journal
760 DETACH RAISE ( IGNORE ) IN ( SELECT "AAAAAA" . * ORDER BY
761 REGISTER LIMIT "AAAAAA" . "AAAAAA" OFFSET RAISE ( IGNORE ) NOT NULL )
763 } {1 {no such table: AAAAAA}}
766 # Create a malformed file (a file that is not a valid database)
767 # and try to attach it
770 set fd [open test2.db w]
771 puts $fd "This file is not a valid SQLite database"
774 ATTACH 'test2.db' AS t2;
776 } {1 {file is encrypted or is not a database}}
780 file delete -force test2.db
783 db2 eval {CREATE TABLE t1(x); BEGIN EXCLUSIVE}
785 ATTACH 'test2.db' AS t2;
787 } {1 {database is locked}}
792 file delete -force test2.db
794 # Test that it is possible to attach the same database more than
795 # once when not in shared-cache mode. That this is not possible in
796 # shared-cache mode is tested in shared7.test.
798 file delete -force test4.db
800 ATTACH 'test4.db' AS aux1;
801 CREATE TABLE aux1.t1(a, b);
802 INSERT INTO aux1.t1 VALUES(1, 2);
803 ATTACH 'test4.db' AS aux2;
804 SELECT * FROM aux2.t1;
810 INSERT INTO aux1.t1 VALUES(3, 4);
811 INSERT INTO aux2.t1 VALUES(5, 6);
813 } {1 {database is locked}}
817 SELECT * FROM aux2.t1;
821 # Ticket [abe728bbc311d81334dae9762f0db87c07a98f79].
822 # Multi-database commit on an attached TEMP database.
824 do_test attach-10.1 {
827 ATTACH ':memory:' AS inmem;
829 CREATE TABLE noname.noname(x);
830 CREATE TABLE inmem.inmem(y);
831 CREATE TABLE main.main(z);
833 SELECT name FROM noname.sqlite_master;
834 SELECT name FROM inmem.sqlite_master;
837 do_test attach-10.2 {
839 PRAGMA database_list;
841 } {4 noname {} 5 inmem {}}