2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library. The
15 # focus of this script is testing the ATTACH and DETACH commands
16 # and related functionality.
18 # $Id: attach.test,v 1.13 2004/02/14 01:39:50 drh Exp $
21 set testdir [file dirname $argv0]
22 source $testdir/tester.tcl
24 for {set i 2} {$i<=15} {incr i} {
25 file delete -force test$i.db
26 file delete -force test$i.db-journal
32 INSERT INTO t1 VALUES(1,2);
33 INSERT INTO t1 VALUES(3,4);
41 INSERT INTO t2 VALUES(1,'x');
42 INSERT INTO t2 VALUES(2,'y');
48 ATTACH DATABASE 'test2.db' AS two;
67 } {1 {no such table: t2}}
72 } {1 {no such table: two.t2}}
75 ATTACH DATABASE 'test3.db' AS three;
77 } {1 {cannot attach empty database: three}}
80 SELECT * FROM three.sqlite_master;
82 } {1 {no such table: three.sqlite_master}}
85 DETACH DATABASE three;
87 } {1 {no such database: three}}
90 ATTACH 'test.db' AS db2;
91 ATTACH 'test.db' AS db3;
92 ATTACH 'test.db' AS db4;
93 ATTACH 'test.db' AS db5;
94 ATTACH 'test.db' AS db6;
95 ATTACH 'test.db' AS db7;
96 ATTACH 'test.db' AS db8;
97 ATTACH 'test.db' AS db9;
102 foreach {idx name file} [execsql {PRAGMA database_list} $db] {
103 lappend list $idx $name
107 do_test attach-1.11b {
109 } {0 main 1 temp 2 db2 3 db3 4 db4 5 db5 6 db6 7 db7 8 db8 9 db9}
110 do_test attach-1.12 {
112 ATTACH 'test.db' as db2;
114 } {1 {database db2 is already in use}}
115 do_test attach-1.13 {
117 ATTACH 'test.db' as db5;
119 } {1 {database db5 is already in use}}
120 do_test attach-1.14 {
122 ATTACH 'test.db' as db9;
124 } {1 {database db9 is already in use}}
125 do_test attach-1.15 {
127 ATTACH 'test.db' as main;
129 } {1 {database main is already in use}}
130 do_test attach-1.16 {
132 ATTACH 'test.db' as temp;
134 } {1 {database temp is already in use}}
135 do_test attach-1.17 {
137 ATTACH 'test.db' as MAIN;
139 } {1 {database MAIN is already in use}}
140 do_test attach-1.18 {
142 ATTACH 'test.db' as db10;
143 ATTACH 'test.db' as db11;
146 do_test attach-1.19 {
148 ATTACH 'test.db' as db12;
150 } {1 {too many attached databases - max 10}}
151 do_test attach-1.20.1 {
156 } {0 main 1 temp 2 db2 3 db3 4 db4 5 db11 6 db6 7 db7 8 db8 9 db9 10 db10}
157 integrity_check attach-1.20.2
158 do_test attach-1.21 {
160 ATTACH 'test.db' as db12;
163 do_test attach-1.22 {
165 ATTACH 'test.db' as db13;
167 } {1 {too many attached databases - max 10}}
168 do_test attach-1.23 {
172 } {1 {no such database: db14}}
173 do_test attach-1.24 {
178 do_test attach-1.25 {
182 } {1 {no such database: db12}}
183 do_test attach-1.26 {
187 } {1 {cannot detach database main}}
188 do_test attach-1.27 {
192 } {1 {cannot detach database Temp}}
193 do_test attach-1.28 {
206 do_test attach-1.29 {
212 CREATE TABLE tx(x1,x2,y1,y2);
213 CREATE TRIGGER r1 AFTER UPDATE ON t2 FOR EACH ROW BEGIN
214 INSERT INTO tx(x1,x2,y1,y2) VALUES(OLD.x,NEW.x,OLD.y,NEW.y);
221 UPDATE t2 SET x=x+10;
224 } {1 11 x x 2 12 y y}
227 CREATE TABLE tx(x1,x2,y1,y2);
233 ATTACH 'test2.db' AS db2;
238 UPDATE db2.t2 SET x=x+10;
239 SELECT * FROM db2.tx;
241 } {1 11 x x 2 12 y y 11 21 x x 12 22 y y}
244 SELECT * FROM main.tx;
249 SELECT type, name, tbl_name FROM db2.sqlite_master;
251 } {table t2 t2 table tx tx trigger r1 t2}
254 } {0 main 1 temp 2 db2}
257 CREATE INDEX i2 ON t2(x);
258 SELECT * FROM t2 WHERE x>5;
261 do_test attach-2.10 {
263 SELECT type, name, tbl_name FROM sqlite_master;
265 } {table t2 t2 table tx tx trigger r1 t2 index i2 t2}
266 #do_test attach-2.11 {
268 # SELECT * FROM t2 WHERE x>5;
270 #} {1 {database schema has changed}}
271 do_test attach-2.12 {
273 } {0 main 1 temp 2 db2}
274 do_test attach-2.13 {
276 SELECT * FROM t2 WHERE x>5;
279 do_test attach-2.14 {
281 SELECT type, name, tbl_name FROM sqlite_master;
283 } {table t1 t1 table tx tx}
284 do_test attach-2.15 {
286 SELECT type, name, tbl_name FROM db2.sqlite_master;
288 } {table t2 t2 table tx tx trigger r1 t2 index i2 t2}
289 do_test attach-2.16 {
293 ATTACH 'test2.db' AS db2;
294 SELECT type, name, tbl_name FROM db2.sqlite_master;
296 } {table t2 t2 table tx tx trigger r1 t2 index i2 t2}
311 } {1 {no such table: t2}}
314 ATTACH DATABASE 'test2.db' AS db2;
319 # Even though main has a transaction, test2.db should not be locked.
327 # Reading from db2 should not lock test2.db
329 execsql {SELECT * FROM t2}
335 # Making a change to db2 causes test2.ddb to become locked.
338 UPDATE t2 SET x=x+1 WHERE x=50;
343 } {1 {database is locked}}
347 execsql {SELECT * FROM t2} db2
352 catchsql {SELECT * FROM t2}
353 } {1 {database is locked}}
355 catchsql {SELECT * FROM t2} db2
357 do_test attach-3.10 {
358 execsql {SELECT * FROM t1}
360 do_test attach-3.11 {
361 catchsql {UPDATE t1 SET a=a+1}
363 do_test attach-3.12 {
364 execsql {SELECT * FROM t1}
366 do_test attach-3.13 {
367 catchsql {UPDATE t2 SET x=x+1 WHERE x=50}
368 } {1 {database is locked}}
369 do_test attach-3.14 {
370 # Unable to reinitialize the schema tables because the aux database
372 catchsql {SELECT * FROM t1}
373 } {1 {database is locked}}
374 do_test attach-3.15 {
376 execsql {SELECT * FROM t1}
385 CREATE TABLE t3(x,y);
386 CREATE UNIQUE INDEX t3i1 ON t3(x);
387 INSERT INTO t3 VALUES(1,2);
393 CREATE TABLE t3(a,b);
394 CREATE UNIQUE INDEX t3i1b ON t3(a);
395 INSERT INTO t3 VALUES(9,10);
401 ATTACH DATABASE 'test2.db' AS db2;
402 SELECT * FROM db2.t3;
407 SELECT * FROM main.t3;
412 INSERT INTO db2.t3 VALUES(9,10);
413 SELECT * FROM db2.t3;
422 CREATE TRIGGER t3r3 AFTER INSERT ON t3 BEGIN
423 INSERT INTO t4 VALUES('db2.' || NEW.x);
425 INSERT INTO t3 VALUES(6,7);
432 CREATE TRIGGER t3r3 AFTER INSERT ON t3 BEGIN
433 INSERT INTO t4 VALUES('main.' || NEW.a);
435 INSERT INTO main.t3 VALUES(11,12);
436 SELECT * FROM main.t4;
441 ATTACH DATABASE 'test2.db' AS db2;
442 INSERT INTO db2.t3 VALUES(13,14);
443 SELECT * FROM db2.t4 UNION ALL SELECT * FROM main.t4;
445 } {db2.6 db2.13 main.11}
448 INSERT INTO main.t3 VALUES(15,16);
449 SELECT * FROM db2.t4 UNION ALL SELECT * FROM main.t4;
451 } {db2.6 db2.13 main.11 main.15}
452 do_test attach-4.10 {
457 CREATE VIEW v3 AS SELECT x*100+y FROM t3;
461 do_test attach-4.11 {
463 CREATE VIEW v3 AS SELECT a*100+b FROM t3;
467 do_test attach-4.12 {
469 ATTACH DATABASE 'test2.db' AS db2;
470 SELECT * FROM db2.v3;
473 do_test attach-4.13 {
475 SELECT * FROM main.v3;
479 # Tests for the sqliteFix...() routines in attach.c
485 file delete -force test2.db
488 ATTACH DATABASE 'test.db' AS orig;
489 CREATE TRIGGER r1 AFTER INSERT ON orig.t1 BEGIN;
493 } {1 {triggers may not be added to auxiliary database orig}}
496 CREATE TABLE t5(x,y);
497 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
505 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
506 SELECT 'no-op' FROM orig.t1;
509 } {1 {trigger r5 cannot reference objects in database orig}}
512 CREATE TEMP TABLE t6(p,q,r);
513 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
514 SELECT 'no-op' FROM temp.t6;
517 } {1 {trigger r5 cannot reference objects in database temp}}
520 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
521 SELECT 'no-op' || (SELECT * FROM temp.t6);
524 } {1 {trigger r5 cannot reference objects in database temp}}
527 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
528 SELECT 'no-op' FROM t1 WHERE x<(SELECT min(x) FROM temp.t6);
531 } {1 {trigger r5 cannot reference objects in database temp}}
534 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
535 SELECT 'no-op' FROM t1 GROUP BY 1 HAVING x<(SELECT min(x) FROM temp.t6);
538 } {1 {trigger r5 cannot reference objects in database temp}}
541 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
542 SELECT max(1,x,(SELECT min(x) FROM temp.t6)) FROM t1;
545 } {1 {trigger r5 cannot reference objects in database temp}}
548 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
549 INSERT INTO t1 VALUES((SELECT min(x) FROM temp.t6),5);
552 } {1 {trigger r5 cannot reference objects in database temp}}
555 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
556 DELETE FROM t1 WHERE x<(SELECT min(x) FROM temp.t6);
559 } {1 {trigger r5 cannot reference objects in database temp}}
561 # Check to make sure we get a sensible error if unable to open
562 # the file that we are trying to attach.
566 ATTACH DATABASE 'no-such-file' AS nosuch;
568 } {1 {cannot attach empty database: nosuch}}
569 file delete -force no-such-file
570 if {$tcl_platform(platform)=="unix"} {
572 sqlite dbx cannot-read
573 dbx eval {CREATE TABLE t1(a,b,c)}
575 file attributes cannot-read -permission 0000
577 ATTACH DATABASE 'cannot-read' AS noread;
579 } {1 {unable to open database: cannot-read}}
580 file delete -force cannot-read
583 for {set i 2} {$i<=15} {incr i} {
586 file delete -force test2.db