blob: 2003782e983d66459997a9d0bcad87ca0fb7f2b6 [file] [log] [blame]
drh2d458342003-04-05 03:42:26 +00001# 2003 April 4
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
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.
9#
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.
14#
danielk1977a21c6b62005-01-24 10:25:59 +000015# $Id: attach.test,v 1.37 2005/01/24 10:26:00 danielk1977 Exp $
drh2d458342003-04-05 03:42:26 +000016#
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
drha73af532003-04-05 16:56:28 +000021for {set i 2} {$i<=15} {incr i} {
22 file delete -force test$i.db
23 file delete -force test$i.db-journal
24}
drh2d458342003-04-05 03:42:26 +000025
drha3b321d2004-05-11 09:31:31 +000026set btree_trace 0
drh2d458342003-04-05 03:42:26 +000027do_test attach-1.1 {
28 execsql {
29 CREATE TABLE t1(a,b);
30 INSERT INTO t1 VALUES(1,2);
31 INSERT INTO t1 VALUES(3,4);
32 SELECT * FROM t1;
33 }
34} {1 2 3 4}
35do_test attach-1.2 {
drhef4ac8f2004-06-19 00:16:31 +000036 sqlite3 db2 test2.db
drh2d458342003-04-05 03:42:26 +000037 execsql {
38 CREATE TABLE t2(x,y);
39 INSERT INTO t2 VALUES(1,'x');
40 INSERT INTO t2 VALUES(2,'y');
41 SELECT * FROM t2;
drha73af532003-04-05 16:56:28 +000042 } db2
drh2d458342003-04-05 03:42:26 +000043} {1 x 2 y}
44do_test attach-1.3 {
45 execsql {
46 ATTACH DATABASE 'test2.db' AS two;
47 SELECT * FROM two.t2;
48 }
49} {1 x 2 y}
50do_test attach-1.4 {
51 execsql {
52 SELECT * FROM t2;
53 }
54} {1 x 2 y}
55do_test attach-1.5 {
56 execsql {
57 DETACH DATABASE two;
58 SELECT * FROM t1;
59 }
60} {1 2 3 4}
61do_test attach-1.6 {
62 catchsql {
63 SELECT * FROM t2;
64 }
65} {1 {no such table: t2}}
66do_test attach-1.7 {
67 catchsql {
68 SELECT * FROM two.t2;
69 }
70} {1 {no such table: two.t2}}
drha73af532003-04-05 16:56:28 +000071do_test attach-1.8 {
72 catchsql {
73 ATTACH DATABASE 'test3.db' AS three;
74 }
danielk19773df6b252004-05-29 10:23:19 +000075} {0 {}}
drha73af532003-04-05 16:56:28 +000076do_test attach-1.9 {
77 catchsql {
78 SELECT * FROM three.sqlite_master;
79 }
danielk19773df6b252004-05-29 10:23:19 +000080} {0 {}}
drha73af532003-04-05 16:56:28 +000081do_test attach-1.10 {
82 catchsql {
83 DETACH DATABASE three;
84 }
danielk19773df6b252004-05-29 10:23:19 +000085} {0 {}}
drha73af532003-04-05 16:56:28 +000086do_test attach-1.11 {
87 execsql {
88 ATTACH 'test.db' AS db2;
89 ATTACH 'test.db' AS db3;
90 ATTACH 'test.db' AS db4;
91 ATTACH 'test.db' AS db5;
92 ATTACH 'test.db' AS db6;
93 ATTACH 'test.db' AS db7;
94 ATTACH 'test.db' AS db8;
95 ATTACH 'test.db' AS db9;
96 }
97} {}
drhb8ec2092003-06-04 15:53:02 +000098proc db_list {db} {
99 set list {}
100 foreach {idx name file} [execsql {PRAGMA database_list} $db] {
101 lappend list $idx $name
drha73af532003-04-05 16:56:28 +0000102 }
drhb8ec2092003-06-04 15:53:02 +0000103 return $list
104}
danielk197727188fb2004-11-23 10:13:03 +0000105ifcapable schema_pragmas {
drhb8ec2092003-06-04 15:53:02 +0000106do_test attach-1.11b {
107 db_list db
drh34f47322004-08-18 15:58:22 +0000108} {0 main 2 db2 3 db3 4 db4 5 db5 6 db6 7 db7 8 db8 9 db9}
danielk197727188fb2004-11-23 10:13:03 +0000109} ;# ifcapable schema_pragmas
drha73af532003-04-05 16:56:28 +0000110do_test attach-1.12 {
111 catchsql {
112 ATTACH 'test.db' as db2;
113 }
114} {1 {database db2 is already in use}}
115do_test attach-1.13 {
116 catchsql {
117 ATTACH 'test.db' as db5;
118 }
119} {1 {database db5 is already in use}}
120do_test attach-1.14 {
121 catchsql {
122 ATTACH 'test.db' as db9;
123 }
124} {1 {database db9 is already in use}}
125do_test attach-1.15 {
126 catchsql {
127 ATTACH 'test.db' as main;
128 }
129} {1 {database main is already in use}}
130do_test attach-1.16 {
131 catchsql {
132 ATTACH 'test.db' as temp;
133 }
134} {1 {database temp is already in use}}
135do_test attach-1.17 {
136 catchsql {
137 ATTACH 'test.db' as MAIN;
138 }
139} {1 {database MAIN is already in use}}
140do_test attach-1.18 {
141 catchsql {
142 ATTACH 'test.db' as db10;
143 ATTACH 'test.db' as db11;
144 }
145} {0 {}}
146do_test attach-1.19 {
147 catchsql {
148 ATTACH 'test.db' as db12;
149 }
150} {1 {too many attached databases - max 10}}
drhed717fe2003-06-15 23:42:24 +0000151do_test attach-1.20.1 {
drha73af532003-04-05 16:56:28 +0000152 execsql {
153 DETACH db5;
drha73af532003-04-05 16:56:28 +0000154 }
danielk197727188fb2004-11-23 10:13:03 +0000155} {}
156ifcapable schema_pragmas {
157do_test attach-1.20.2 {
drhb8ec2092003-06-04 15:53:02 +0000158 db_list db
drh34f47322004-08-18 15:58:22 +0000159} {0 main 2 db2 3 db3 4 db4 5 db6 6 db7 7 db8 8 db9 9 db10 10 db11}
danielk197727188fb2004-11-23 10:13:03 +0000160} ;# ifcapable schema_pragmas
danielk1977a21c6b62005-01-24 10:25:59 +0000161integrity_check attach-1.20.3
drh40e016e2004-11-04 14:47:11 +0000162execsql {select * from sqlite_temp_master}
drha73af532003-04-05 16:56:28 +0000163do_test attach-1.21 {
164 catchsql {
165 ATTACH 'test.db' as db12;
166 }
167} {0 {}}
168do_test attach-1.22 {
169 catchsql {
170 ATTACH 'test.db' as db13;
171 }
172} {1 {too many attached databases - max 10}}
173do_test attach-1.23 {
174 catchsql {
175 DETACH db14;
176 }
177} {1 {no such database: db14}}
178do_test attach-1.24 {
179 catchsql {
180 DETACH db12;
181 }
182} {0 {}}
183do_test attach-1.25 {
184 catchsql {
185 DETACH db12;
186 }
187} {1 {no such database: db12}}
188do_test attach-1.26 {
189 catchsql {
190 DETACH main;
191 }
192} {1 {cannot detach database main}}
193do_test attach-1.27 {
194 catchsql {
195 DETACH Temp;
196 }
197} {1 {cannot detach database Temp}}
198do_test attach-1.28 {
199 catchsql {
200 DETACH db11;
201 DETACH db10;
202 DETACH db9;
203 DETACH db8;
204 DETACH db7;
205 DETACH db6;
206 DETACH db4;
207 DETACH db3;
208 DETACH db2;
209 }
210} {0 {}}
danielk197727188fb2004-11-23 10:13:03 +0000211ifcapable schema_pragmas {
drha73af532003-04-05 16:56:28 +0000212do_test attach-1.29 {
drhb8ec2092003-06-04 15:53:02 +0000213 db_list db
drha73af532003-04-05 16:56:28 +0000214} {0 main 1 temp}
danielk197727188fb2004-11-23 10:13:03 +0000215} ;# ifcapable schema_pragmas
drh2d458342003-04-05 03:42:26 +0000216
drhb7f91642004-10-31 02:22:47 +0000217ifcapable {trigger} { # Only do the following tests if triggers are enabled
drha69d9162003-04-17 22:57:53 +0000218do_test attach-2.1 {
219 execsql {
220 CREATE TABLE tx(x1,x2,y1,y2);
221 CREATE TRIGGER r1 AFTER UPDATE ON t2 FOR EACH ROW BEGIN
222 INSERT INTO tx(x1,x2,y1,y2) VALUES(OLD.x,NEW.x,OLD.y,NEW.y);
223 END;
224 SELECT * FROM tx;
225 } db2;
226} {}
227do_test attach-2.2 {
228 execsql {
229 UPDATE t2 SET x=x+10;
230 SELECT * FROM tx;
231 } db2;
232} {1 11 x x 2 12 y y}
233do_test attach-2.3 {
234 execsql {
235 CREATE TABLE tx(x1,x2,y1,y2);
236 SELECT * FROM tx;
237 }
238} {}
239do_test attach-2.4 {
240 execsql {
241 ATTACH 'test2.db' AS db2;
242 }
243} {}
244do_test attach-2.5 {
245 execsql {
246 UPDATE db2.t2 SET x=x+10;
247 SELECT * FROM db2.tx;
248 }
249} {1 11 x x 2 12 y y 11 21 x x 12 22 y y}
250do_test attach-2.6 {
251 execsql {
252 SELECT * FROM main.tx;
253 }
254} {}
drh8bf8dc92003-05-17 17:35:10 +0000255do_test attach-2.7 {
256 execsql {
257 SELECT type, name, tbl_name FROM db2.sqlite_master;
258 }
259} {table t2 t2 table tx tx trigger r1 t2}
danielk197727188fb2004-11-23 10:13:03 +0000260ifcapable schema_pragmas {
drh8bf8dc92003-05-17 17:35:10 +0000261do_test attach-2.8 {
drhb8ec2092003-06-04 15:53:02 +0000262 db_list db
drh8bf8dc92003-05-17 17:35:10 +0000263} {0 main 1 temp 2 db2}
danielk197727188fb2004-11-23 10:13:03 +0000264} ;# ifcapable schema_pragmas
drh8bf8dc92003-05-17 17:35:10 +0000265do_test attach-2.9 {
266 execsql {
267 CREATE INDEX i2 ON t2(x);
268 SELECT * FROM t2 WHERE x>5;
269 } db2
270} {21 x 22 y}
271do_test attach-2.10 {
272 execsql {
273 SELECT type, name, tbl_name FROM sqlite_master;
274 } db2
275} {table t2 t2 table tx tx trigger r1 t2 index i2 t2}
drhb5f70c22004-02-14 01:39:50 +0000276#do_test attach-2.11 {
277# catchsql {
278# SELECT * FROM t2 WHERE x>5;
279# }
280#} {1 {database schema has changed}}
danielk197727188fb2004-11-23 10:13:03 +0000281ifcapable schema_pragmas {
drh8bf8dc92003-05-17 17:35:10 +0000282do_test attach-2.12 {
drhb8ec2092003-06-04 15:53:02 +0000283 db_list db
drh8bf8dc92003-05-17 17:35:10 +0000284} {0 main 1 temp 2 db2}
danielk197727188fb2004-11-23 10:13:03 +0000285} ;# ifcapable schema_pragmas
drh8bf8dc92003-05-17 17:35:10 +0000286do_test attach-2.13 {
287 catchsql {
288 SELECT * FROM t2 WHERE x>5;
289 }
290} {0 {21 x 22 y}}
291do_test attach-2.14 {
292 execsql {
293 SELECT type, name, tbl_name FROM sqlite_master;
294 }
295} {table t1 t1 table tx tx}
296do_test attach-2.15 {
297 execsql {
298 SELECT type, name, tbl_name FROM db2.sqlite_master;
299 }
300} {table t2 t2 table tx tx trigger r1 t2 index i2 t2}
301do_test attach-2.16 {
302 db close
drhef4ac8f2004-06-19 00:16:31 +0000303 sqlite3 db test.db
drh8bf8dc92003-05-17 17:35:10 +0000304 execsql {
305 ATTACH 'test2.db' AS db2;
306 SELECT type, name, tbl_name FROM db2.sqlite_master;
307 }
308} {table t2 t2 table tx tx trigger r1 t2 index i2 t2}
drhb7f91642004-10-31 02:22:47 +0000309} ;# End of ifcapable {trigger}
drha73af532003-04-05 16:56:28 +0000310
drh6b861112003-05-17 19:23:51 +0000311do_test attach-3.1 {
312 db close
313 db2 close
drhef4ac8f2004-06-19 00:16:31 +0000314 sqlite3 db test.db
315 sqlite3 db2 test2.db
drh6b861112003-05-17 19:23:51 +0000316 execsql {
317 SELECT * FROM t1
318 }
319} {1 2 3 4}
drhb7f91642004-10-31 02:22:47 +0000320
321# If we are testing a version of the code that lacks trigger support,
322# adjust the database contents so that they are the same if triggers
323# had been enabled.
324ifcapable {!trigger} {
325 db2 eval {
326 DELETE FROM t2;
327 INSERT INTO t2 VALUES(21, 'x');
328 INSERT INTO t2 VALUES(22, 'y');
329 CREATE TABLE tx(x1,x2,y1,y2);
330 INSERT INTO tx VALUES(1, 11, 'x', 'x');
331 INSERT INTO tx VALUES(2, 12, 'y', 'y');
332 INSERT INTO tx VALUES(11, 21, 'x', 'x');
333 INSERT INTO tx VALUES(12, 22, 'y', 'y');
334 CREATE INDEX i2 ON t2(x);
335 }
336}
337
drh6b861112003-05-17 19:23:51 +0000338do_test attach-3.2 {
339 catchsql {
340 SELECT * FROM t2
341 }
342} {1 {no such table: t2}}
343do_test attach-3.3 {
344 catchsql {
345 ATTACH DATABASE 'test2.db' AS db2;
346 SELECT * FROM t2
347 }
348} {0 {21 x 22 y}}
349
drh80242052004-06-09 00:48:12 +0000350# Even though 'db' has started a transaction, it should not yet have
351# a lock on test2.db so 'db2' should be readable.
drh6b861112003-05-17 19:23:51 +0000352do_test attach-3.4 {
353 execsql BEGIN
354 catchsql {
355 SELECT * FROM t2;
356 } db2;
357} {0 {21 x 22 y}}
358
drh80242052004-06-09 00:48:12 +0000359# Reading from test2.db from db within a transaction should not
360# prevent test2.db from being read by db2.
drh6b861112003-05-17 19:23:51 +0000361do_test attach-3.5 {
362 execsql {SELECT * FROM t2}
drh178286b2005-01-23 13:14:55 +0000363btree_breakpoint
drh6b861112003-05-17 19:23:51 +0000364 catchsql {
365 SELECT * FROM t2;
366 } db2;
367} {0 {21 x 22 y}}
368
drh80242052004-06-09 00:48:12 +0000369# Making a change to test2.db through db causes test2.db to get
370# a reserved lock. It should still be accessible through db2.
drh6b861112003-05-17 19:23:51 +0000371do_test attach-3.6 {
372 execsql {
373 UPDATE t2 SET x=x+1 WHERE x=50;
374 }
375 catchsql {
376 SELECT * FROM t2;
377 } db2;
drh80242052004-06-09 00:48:12 +0000378} {0 {21 x 22 y}}
drh6b861112003-05-17 19:23:51 +0000379
380do_test attach-3.7 {
381 execsql ROLLBACK
382 execsql {SELECT * FROM t2} db2
383} {21 x 22 y}
drh80242052004-06-09 00:48:12 +0000384
385# Start transactions on both db and db2. Once again, just because
386# we make a change to test2.db using db2, only a RESERVED lock is
387# obtained, so test2.db should still be readable using db.
388#
drh6b861112003-05-17 19:23:51 +0000389do_test attach-3.8 {
390 execsql BEGIN
391 execsql BEGIN db2
danielk19771d850a72004-05-31 08:26:49 +0000392 execsql {UPDATE t2 SET x=0 WHERE 0} db2
drh6b861112003-05-17 19:23:51 +0000393 catchsql {SELECT * FROM t2}
drh80242052004-06-09 00:48:12 +0000394} {0 {21 x 22 y}}
395
396# It is also still accessible from db2.
drh6b861112003-05-17 19:23:51 +0000397do_test attach-3.9 {
398 catchsql {SELECT * FROM t2} db2
399} {0 {21 x 22 y}}
drh80242052004-06-09 00:48:12 +0000400
drh6b861112003-05-17 19:23:51 +0000401do_test attach-3.10 {
402 execsql {SELECT * FROM t1}
403} {1 2 3 4}
drh80242052004-06-09 00:48:12 +0000404
drh6b861112003-05-17 19:23:51 +0000405do_test attach-3.11 {
406 catchsql {UPDATE t1 SET a=a+1}
407} {0 {}}
408do_test attach-3.12 {
409 execsql {SELECT * FROM t1}
410} {2 2 4 4}
drh80242052004-06-09 00:48:12 +0000411
412# db2 has a RESERVED lock on test2.db, so db cannot write to any tables
413# in test2.db.
drh6b861112003-05-17 19:23:51 +0000414do_test attach-3.13 {
415 catchsql {UPDATE t2 SET x=x+1 WHERE x=50}
416} {1 {database is locked}}
danielk19771d850a72004-05-31 08:26:49 +0000417
418# Change for version 3. Transaction is no longer rolled back
419# for a locked database.
420execsql {ROLLBACK}
421
drh80242052004-06-09 00:48:12 +0000422# db is able to reread its schema because db2 still only holds a
423# reserved lock.
drh6b861112003-05-17 19:23:51 +0000424do_test attach-3.14 {
drh8ef83ff2004-02-12 15:31:21 +0000425 catchsql {SELECT * FROM t1}
drh80242052004-06-09 00:48:12 +0000426} {0 {1 2 3 4}}
drh8ef83ff2004-02-12 15:31:21 +0000427do_test attach-3.15 {
428 execsql COMMIT db2
drh6b861112003-05-17 19:23:51 +0000429 execsql {SELECT * FROM t1}
430} {1 2 3 4}
431
drhe54ca3f2004-06-07 01:52:14 +0000432#set btree_trace 1
drh80242052004-06-09 00:48:12 +0000433
drhf26e09c2003-05-31 16:21:12 +0000434# Ticket #323
435do_test attach-4.1 {
436 execsql {DETACH db2}
437 db2 close
drhef4ac8f2004-06-19 00:16:31 +0000438 sqlite3 db2 test2.db
drhf26e09c2003-05-31 16:21:12 +0000439 execsql {
440 CREATE TABLE t3(x,y);
441 CREATE UNIQUE INDEX t3i1 ON t3(x);
442 INSERT INTO t3 VALUES(1,2);
443 SELECT * FROM t3;
444 } db2;
445} {1 2}
446do_test attach-4.2 {
447 execsql {
448 CREATE TABLE t3(a,b);
449 CREATE UNIQUE INDEX t3i1b ON t3(a);
450 INSERT INTO t3 VALUES(9,10);
451 SELECT * FROM t3;
452 }
453} {9 10}
454do_test attach-4.3 {
455 execsql {
456 ATTACH DATABASE 'test2.db' AS db2;
457 SELECT * FROM db2.t3;
458 }
459} {1 2}
460do_test attach-4.4 {
461 execsql {
462 SELECT * FROM main.t3;
463 }
464} {9 10}
465do_test attach-4.5 {
466 execsql {
467 INSERT INTO db2.t3 VALUES(9,10);
468 SELECT * FROM db2.t3;
469 }
470} {1 2 9 10}
drh798da522004-11-04 04:42:28 +0000471execsql {
472 DETACH db2;
473}
drhb7f91642004-10-31 02:22:47 +0000474ifcapable {trigger} {
475 do_test attach-4.6 {
476 execsql {
drhb7f91642004-10-31 02:22:47 +0000477 CREATE TABLE t4(x);
478 CREATE TRIGGER t3r3 AFTER INSERT ON t3 BEGIN
479 INSERT INTO t4 VALUES('db2.' || NEW.x);
480 END;
481 INSERT INTO t3 VALUES(6,7);
482 SELECT * FROM t4;
483 } db2
484 } {db2.6}
485 do_test attach-4.7 {
486 execsql {
487 CREATE TABLE t4(y);
488 CREATE TRIGGER t3r3 AFTER INSERT ON t3 BEGIN
489 INSERT INTO t4 VALUES('main.' || NEW.a);
490 END;
491 INSERT INTO main.t3 VALUES(11,12);
492 SELECT * FROM main.t4;
493 }
494 } {main.11}
495}
drh798da522004-11-04 04:42:28 +0000496ifcapable {!trigger} {
497 # When we do not have trigger support, set up the table like they
498 # would have been had triggers been there. The tests that follow need
499 # this setup.
500 execsql {
501 CREATE TABLE t4(x);
502 INSERT INTO t3 VALUES(6,7);
503 INSERT INTO t4 VALUES('db2.6');
504 INSERT INTO t4 VALUES('db2.13');
505 } db2
506 execsql {
507 CREATE TABLE t4(y);
508 INSERT INTO main.t3 VALUES(11,12);
509 INSERT INTO t4 VALUES('main.11');
510 }
511}
512
drh80242052004-06-09 00:48:12 +0000513
514# This one is tricky. On the UNION ALL select, we have to make sure
515# the schema for both main and db2 is valid before starting to execute
516# the first query of the UNION ALL. If we wait to test the validity of
517# the schema for main until after the first query has run, that test will
518# fail and the query will abort but we will have already output some
519# results. When the query is retried, the results will be repeated.
520#
danielk197727c77432004-11-22 13:35:41 +0000521ifcapable compound {
drhf26e09c2003-05-31 16:21:12 +0000522do_test attach-4.8 {
523 execsql {
524 ATTACH DATABASE 'test2.db' AS db2;
525 INSERT INTO db2.t3 VALUES(13,14);
526 SELECT * FROM db2.t4 UNION ALL SELECT * FROM main.t4;
527 }
528} {db2.6 db2.13 main.11}
drh80242052004-06-09 00:48:12 +0000529
drhf26e09c2003-05-31 16:21:12 +0000530do_test attach-4.9 {
drh798da522004-11-04 04:42:28 +0000531 ifcapable {!trigger} {execsql {INSERT INTO main.t4 VALUES('main.15')}}
drhf26e09c2003-05-31 16:21:12 +0000532 execsql {
533 INSERT INTO main.t3 VALUES(15,16);
534 SELECT * FROM db2.t4 UNION ALL SELECT * FROM main.t4;
535 }
536} {db2.6 db2.13 main.11 main.15}
danielk197727c77432004-11-22 13:35:41 +0000537} ;# ifcapable compound
538
539ifcapable !compound {
540 ifcapable {!trigger} {execsql {INSERT INTO main.t4 VALUES('main.15')}}
541 execsql {
542 ATTACH DATABASE 'test2.db' AS db2;
543 INSERT INTO db2.t3 VALUES(13,14);
544 INSERT INTO main.t3 VALUES(15,16);
545 }
546} ;# ifcapable !compound
danielk19770fa8ddb2004-11-22 08:43:32 +0000547
548ifcapable view {
drhf26e09c2003-05-31 16:21:12 +0000549do_test attach-4.10 {
550 execsql {
551 DETACH DATABASE db2;
552 }
553 execsql {
554 CREATE VIEW v3 AS SELECT x*100+y FROM t3;
555 SELECT * FROM v3;
556 } db2
557} {102 910 607 1314}
558do_test attach-4.11 {
559 execsql {
560 CREATE VIEW v3 AS SELECT a*100+b FROM t3;
561 SELECT * FROM v3;
562 }
563} {910 1112 1516}
564do_test attach-4.12 {
565 execsql {
566 ATTACH DATABASE 'test2.db' AS db2;
567 SELECT * FROM db2.v3;
568 }
569} {102 910 607 1314}
570do_test attach-4.13 {
571 execsql {
572 SELECT * FROM main.v3;
573 }
574} {910 1112 1516}
danielk19770fa8ddb2004-11-22 08:43:32 +0000575} ;# ifcapable view
drhf26e09c2003-05-31 16:21:12 +0000576
drh4312db52003-06-03 01:47:11 +0000577# Tests for the sqliteFix...() routines in attach.c
578#
drh798da522004-11-04 04:42:28 +0000579ifcapable {trigger} {
drh4312db52003-06-03 01:47:11 +0000580do_test attach-5.1 {
581 db close
drhef4ac8f2004-06-19 00:16:31 +0000582 sqlite3 db test.db
drh9cb733c2003-07-18 01:25:34 +0000583 db2 close
drh4312db52003-06-03 01:47:11 +0000584 file delete -force test2.db
drhef4ac8f2004-06-19 00:16:31 +0000585 sqlite3 db2 test2.db
drh4312db52003-06-03 01:47:11 +0000586 catchsql {
587 ATTACH DATABASE 'test.db' AS orig;
588 CREATE TRIGGER r1 AFTER INSERT ON orig.t1 BEGIN;
589 SELECT 'no-op';
590 END;
591 } db2
danielk1977ef2cb632004-05-29 02:37:19 +0000592} {1 {trigger r1 cannot reference objects in database orig}}
drh4312db52003-06-03 01:47:11 +0000593do_test attach-5.2 {
594 catchsql {
595 CREATE TABLE t5(x,y);
596 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
597 SELECT 'no-op';
598 END;
599 } db2
600} {0 {}}
601do_test attach-5.3 {
602 catchsql {
603 DROP TRIGGER r5;
604 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
605 SELECT 'no-op' FROM orig.t1;
606 END;
607 } db2
608} {1 {trigger r5 cannot reference objects in database orig}}
609do_test attach-5.4 {
610 catchsql {
611 CREATE TEMP TABLE t6(p,q,r);
612 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
613 SELECT 'no-op' FROM temp.t6;
614 END;
615 } db2
616} {1 {trigger r5 cannot reference objects in database temp}}
danielk1977e61b9f42005-01-21 04:25:47 +0000617ifcapable subquery {
618 do_test attach-5.5 {
619 catchsql {
620 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
621 SELECT 'no-op' || (SELECT * FROM temp.t6);
622 END;
623 } db2
624 } {1 {trigger r5 cannot reference objects in database temp}}
625 do_test attach-5.6 {
626 catchsql {
627 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
628 SELECT 'no-op' FROM t1 WHERE x<(SELECT min(x) FROM temp.t6);
629 END;
630 } db2
631 } {1 {trigger r5 cannot reference objects in database temp}}
632 do_test attach-5.7 {
633 catchsql {
634 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
635 SELECT 'no-op' FROM t1 GROUP BY 1 HAVING x<(SELECT min(x) FROM temp.t6);
636 END;
637 } db2
638 } {1 {trigger r5 cannot reference objects in database temp}}
639 do_test attach-5.7 {
640 catchsql {
641 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
642 SELECT max(1,x,(SELECT min(x) FROM temp.t6)) FROM t1;
643 END;
644 } db2
645 } {1 {trigger r5 cannot reference objects in database temp}}
646 do_test attach-5.8 {
647 catchsql {
648 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
649 INSERT INTO t1 VALUES((SELECT min(x) FROM temp.t6),5);
650 END;
651 } db2
652 } {1 {trigger r5 cannot reference objects in database temp}}
653 do_test attach-5.9 {
654 catchsql {
655 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
656 DELETE FROM t1 WHERE x<(SELECT min(x) FROM temp.t6);
657 END;
658 } db2
659 } {1 {trigger r5 cannot reference objects in database temp}}
660} ;# endif subquery
drh798da522004-11-04 04:42:28 +0000661} ;# endif trigger
drh4312db52003-06-03 01:47:11 +0000662
663# Check to make sure we get a sensible error if unable to open
664# the file that we are trying to attach.
665#
666do_test attach-6.1 {
667 catchsql {
668 ATTACH DATABASE 'no-such-file' AS nosuch;
669 }
danielk19773df6b252004-05-29 10:23:19 +0000670} {0 {}}
drhbc2bca02003-06-04 12:31:53 +0000671if {$tcl_platform(platform)=="unix"} {
672 do_test attach-6.2 {
drhef4ac8f2004-06-19 00:16:31 +0000673 sqlite3 dbx cannot-read
drhbc2bca02003-06-04 12:31:53 +0000674 dbx eval {CREATE TABLE t1(a,b,c)}
675 dbx close
676 file attributes cannot-read -permission 0000
677 catchsql {
678 ATTACH DATABASE 'cannot-read' AS noread;
679 }
680 } {1 {unable to open database: cannot-read}}
681 file delete -force cannot-read
682}
drhf26e09c2003-05-31 16:21:12 +0000683
danielk1977576ec6b2005-01-21 11:55:25 +0000684# Check the error message if we try to access a database that has
685# not been attached.
686do_test attach-6.3 {
687 catchsql {
688 CREATE TABLE no_such_db.t1(a, b, c);
689 }
690} {1 {unknown database no_such_db}}
drh7bf56612005-01-21 15:52:32 +0000691ifcapable !compound {
692 do_test attach-6.4 {
693 catchsql {
694 CREATE TEMP TABLE db2.temp1(a, b, c);
695 }
696 } {1 {temporary table name must be unqualified}}
697}
drha73af532003-04-05 16:56:28 +0000698for {set i 2} {$i<=15} {incr i} {
699 catch {db$i close}
700}
drh02f9f6b2004-05-31 18:21:54 +0000701db close
drh4312db52003-06-03 01:47:11 +0000702file delete -force test2.db
drh02f9f6b2004-05-31 18:21:54 +0000703file delete -force no-such-file
drh2d458342003-04-05 03:42:26 +0000704
705
706finish_test