blob: a5af8f751d2a7979a335922b813da8001ee5fcca [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#
drhfd773cf2009-05-29 14:39:07 +000015# $Id: attach.test,v 1.52 2009/05/29 14:39:08 drh Exp $
drh2d458342003-04-05 03:42:26 +000016#
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
danielk19775a8f9372007-10-09 08:29:32 +000021ifcapable !attach {
22 finish_test
23 return
24}
25
drha73af532003-04-05 16:56:28 +000026for {set i 2} {$i<=15} {incr i} {
27 file delete -force test$i.db
28 file delete -force test$i.db-journal
29}
drh2d458342003-04-05 03:42:26 +000030
31do_test attach-1.1 {
32 execsql {
33 CREATE TABLE t1(a,b);
34 INSERT INTO t1 VALUES(1,2);
35 INSERT INTO t1 VALUES(3,4);
36 SELECT * FROM t1;
37 }
38} {1 2 3 4}
39do_test attach-1.2 {
drhef4ac8f2004-06-19 00:16:31 +000040 sqlite3 db2 test2.db
drh2d458342003-04-05 03:42:26 +000041 execsql {
42 CREATE TABLE t2(x,y);
43 INSERT INTO t2 VALUES(1,'x');
44 INSERT INTO t2 VALUES(2,'y');
45 SELECT * FROM t2;
drha73af532003-04-05 16:56:28 +000046 } db2
drh2d458342003-04-05 03:42:26 +000047} {1 x 2 y}
48do_test attach-1.3 {
49 execsql {
50 ATTACH DATABASE 'test2.db' AS two;
51 SELECT * FROM two.t2;
52 }
53} {1 x 2 y}
54do_test attach-1.4 {
55 execsql {
56 SELECT * FROM t2;
57 }
58} {1 x 2 y}
59do_test attach-1.5 {
60 execsql {
61 DETACH DATABASE two;
62 SELECT * FROM t1;
63 }
64} {1 2 3 4}
65do_test attach-1.6 {
66 catchsql {
67 SELECT * FROM t2;
68 }
69} {1 {no such table: t2}}
70do_test attach-1.7 {
71 catchsql {
72 SELECT * FROM two.t2;
73 }
74} {1 {no such table: two.t2}}
drha73af532003-04-05 16:56:28 +000075do_test attach-1.8 {
76 catchsql {
77 ATTACH DATABASE 'test3.db' AS three;
78 }
danielk19773df6b252004-05-29 10:23:19 +000079} {0 {}}
drha73af532003-04-05 16:56:28 +000080do_test attach-1.9 {
81 catchsql {
82 SELECT * FROM three.sqlite_master;
83 }
danielk19773df6b252004-05-29 10:23:19 +000084} {0 {}}
drha73af532003-04-05 16:56:28 +000085do_test attach-1.10 {
86 catchsql {
drhae8b3612005-03-15 02:04:12 +000087 DETACH DATABASE [three];
drha73af532003-04-05 16:56:28 +000088 }
danielk19773df6b252004-05-29 10:23:19 +000089} {0 {}}
drha73af532003-04-05 16:56:28 +000090do_test attach-1.11 {
91 execsql {
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;
100 }
101} {}
drhb8ec2092003-06-04 15:53:02 +0000102proc db_list {db} {
103 set list {}
104 foreach {idx name file} [execsql {PRAGMA database_list} $db] {
105 lappend list $idx $name
drha73af532003-04-05 16:56:28 +0000106 }
drhb8ec2092003-06-04 15:53:02 +0000107 return $list
108}
danielk197727188fb2004-11-23 10:13:03 +0000109ifcapable schema_pragmas {
drhb8ec2092003-06-04 15:53:02 +0000110do_test attach-1.11b {
111 db_list db
drh34f47322004-08-18 15:58:22 +0000112} {0 main 2 db2 3 db3 4 db4 5 db5 6 db6 7 db7 8 db8 9 db9}
danielk197727188fb2004-11-23 10:13:03 +0000113} ;# ifcapable schema_pragmas
drha73af532003-04-05 16:56:28 +0000114do_test attach-1.12 {
115 catchsql {
116 ATTACH 'test.db' as db2;
117 }
118} {1 {database db2 is already in use}}
drh69544ec2008-02-06 14:11:34 +0000119do_test attach-1.12.2 {
120 db errorcode
121} {1}
drha73af532003-04-05 16:56:28 +0000122do_test attach-1.13 {
123 catchsql {
124 ATTACH 'test.db' as db5;
125 }
126} {1 {database db5 is already in use}}
127do_test attach-1.14 {
128 catchsql {
129 ATTACH 'test.db' as db9;
130 }
131} {1 {database db9 is already in use}}
132do_test attach-1.15 {
133 catchsql {
134 ATTACH 'test.db' as main;
135 }
136} {1 {database main is already in use}}
danielk197753c0f742005-03-29 03:10:59 +0000137ifcapable tempdb {
138 do_test attach-1.16 {
139 catchsql {
140 ATTACH 'test.db' as temp;
141 }
142 } {1 {database temp is already in use}}
143}
drha73af532003-04-05 16:56:28 +0000144do_test attach-1.17 {
145 catchsql {
146 ATTACH 'test.db' as MAIN;
147 }
148} {1 {database MAIN is already in use}}
149do_test attach-1.18 {
150 catchsql {
151 ATTACH 'test.db' as db10;
152 ATTACH 'test.db' as db11;
153 }
154} {0 {}}
155do_test attach-1.19 {
156 catchsql {
157 ATTACH 'test.db' as db12;
158 }
159} {1 {too many attached databases - max 10}}
drh69544ec2008-02-06 14:11:34 +0000160do_test attach-1.19.1 {
161 db errorcode
162} {1}
drhed717fe2003-06-15 23:42:24 +0000163do_test attach-1.20.1 {
drha73af532003-04-05 16:56:28 +0000164 execsql {
165 DETACH db5;
drha73af532003-04-05 16:56:28 +0000166 }
danielk197727188fb2004-11-23 10:13:03 +0000167} {}
168ifcapable schema_pragmas {
169do_test attach-1.20.2 {
drhb8ec2092003-06-04 15:53:02 +0000170 db_list db
drh34f47322004-08-18 15:58:22 +0000171} {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 +0000172} ;# ifcapable schema_pragmas
danielk1977a21c6b62005-01-24 10:25:59 +0000173integrity_check attach-1.20.3
danielk197753c0f742005-03-29 03:10:59 +0000174ifcapable tempdb {
175 execsql {select * from sqlite_temp_master}
176}
drha73af532003-04-05 16:56:28 +0000177do_test attach-1.21 {
178 catchsql {
179 ATTACH 'test.db' as db12;
180 }
181} {0 {}}
182do_test attach-1.22 {
183 catchsql {
184 ATTACH 'test.db' as db13;
185 }
186} {1 {too many attached databases - max 10}}
drh69544ec2008-02-06 14:11:34 +0000187do_test attach-1.22.1 {
188 db errorcode
189} {1}
drha73af532003-04-05 16:56:28 +0000190do_test attach-1.23 {
191 catchsql {
drhae8b3612005-03-15 02:04:12 +0000192 DETACH "db14";
drha73af532003-04-05 16:56:28 +0000193 }
194} {1 {no such database: db14}}
195do_test attach-1.24 {
196 catchsql {
197 DETACH db12;
198 }
199} {0 {}}
200do_test attach-1.25 {
201 catchsql {
202 DETACH db12;
203 }
204} {1 {no such database: db12}}
205do_test attach-1.26 {
206 catchsql {
207 DETACH main;
208 }
209} {1 {cannot detach database main}}
danielk197753c0f742005-03-29 03:10:59 +0000210
211ifcapable tempdb {
212 do_test attach-1.27 {
213 catchsql {
214 DETACH Temp;
215 }
216 } {1 {cannot detach database Temp}}
217} else {
218 do_test attach-1.27 {
219 catchsql {
220 DETACH Temp;
221 }
222 } {1 {no such database: Temp}}
223}
224
drha73af532003-04-05 16:56:28 +0000225do_test attach-1.28 {
226 catchsql {
227 DETACH db11;
228 DETACH db10;
229 DETACH db9;
230 DETACH db8;
231 DETACH db7;
232 DETACH db6;
233 DETACH db4;
234 DETACH db3;
235 DETACH db2;
236 }
237} {0 {}}
danielk197727188fb2004-11-23 10:13:03 +0000238ifcapable schema_pragmas {
danielk197753c0f742005-03-29 03:10:59 +0000239 ifcapable tempdb {
240 do_test attach-1.29 {
241 db_list db
242 } {0 main 1 temp}
243 } else {
244 do_test attach-1.29 {
245 db_list db
246 } {0 main}
247 }
danielk197727188fb2004-11-23 10:13:03 +0000248} ;# ifcapable schema_pragmas
drh2d458342003-04-05 03:42:26 +0000249
drhb7f91642004-10-31 02:22:47 +0000250ifcapable {trigger} { # Only do the following tests if triggers are enabled
drha69d9162003-04-17 22:57:53 +0000251do_test attach-2.1 {
252 execsql {
253 CREATE TABLE tx(x1,x2,y1,y2);
254 CREATE TRIGGER r1 AFTER UPDATE ON t2 FOR EACH ROW BEGIN
255 INSERT INTO tx(x1,x2,y1,y2) VALUES(OLD.x,NEW.x,OLD.y,NEW.y);
256 END;
257 SELECT * FROM tx;
258 } db2;
259} {}
260do_test attach-2.2 {
261 execsql {
262 UPDATE t2 SET x=x+10;
263 SELECT * FROM tx;
264 } db2;
265} {1 11 x x 2 12 y y}
266do_test attach-2.3 {
267 execsql {
268 CREATE TABLE tx(x1,x2,y1,y2);
269 SELECT * FROM tx;
270 }
271} {}
272do_test attach-2.4 {
273 execsql {
274 ATTACH 'test2.db' AS db2;
275 }
276} {}
277do_test attach-2.5 {
278 execsql {
279 UPDATE db2.t2 SET x=x+10;
280 SELECT * FROM db2.tx;
281 }
282} {1 11 x x 2 12 y y 11 21 x x 12 22 y y}
283do_test attach-2.6 {
284 execsql {
285 SELECT * FROM main.tx;
286 }
287} {}
drh8bf8dc92003-05-17 17:35:10 +0000288do_test attach-2.7 {
289 execsql {
290 SELECT type, name, tbl_name FROM db2.sqlite_master;
291 }
292} {table t2 t2 table tx tx trigger r1 t2}
danielk197753c0f742005-03-29 03:10:59 +0000293
294ifcapable schema_pragmas&&tempdb {
295 do_test attach-2.8 {
296 db_list db
297 } {0 main 1 temp 2 db2}
298} ;# ifcapable schema_pragmas&&tempdb
299ifcapable schema_pragmas&&!tempdb {
300 do_test attach-2.8 {
301 db_list db
302 } {0 main 2 db2}
303} ;# ifcapable schema_pragmas&&!tempdb
304
drh8bf8dc92003-05-17 17:35:10 +0000305do_test attach-2.9 {
306 execsql {
307 CREATE INDEX i2 ON t2(x);
308 SELECT * FROM t2 WHERE x>5;
309 } db2
310} {21 x 22 y}
311do_test attach-2.10 {
312 execsql {
313 SELECT type, name, tbl_name FROM sqlite_master;
314 } db2
315} {table t2 t2 table tx tx trigger r1 t2 index i2 t2}
drhb5f70c22004-02-14 01:39:50 +0000316#do_test attach-2.11 {
317# catchsql {
318# SELECT * FROM t2 WHERE x>5;
319# }
320#} {1 {database schema has changed}}
danielk197727188fb2004-11-23 10:13:03 +0000321ifcapable schema_pragmas {
danielk197753c0f742005-03-29 03:10:59 +0000322 ifcapable tempdb {
323 do_test attach-2.12 {
324 db_list db
325 } {0 main 1 temp 2 db2}
326 } else {
327 do_test attach-2.12 {
328 db_list db
329 } {0 main 2 db2}
330 }
danielk197727188fb2004-11-23 10:13:03 +0000331} ;# ifcapable schema_pragmas
drh8bf8dc92003-05-17 17:35:10 +0000332do_test attach-2.13 {
333 catchsql {
334 SELECT * FROM t2 WHERE x>5;
335 }
336} {0 {21 x 22 y}}
337do_test attach-2.14 {
338 execsql {
339 SELECT type, name, tbl_name FROM sqlite_master;
340 }
341} {table t1 t1 table tx tx}
342do_test attach-2.15 {
343 execsql {
344 SELECT type, name, tbl_name FROM db2.sqlite_master;
345 }
346} {table t2 t2 table tx tx trigger r1 t2 index i2 t2}
347do_test attach-2.16 {
348 db close
drhef4ac8f2004-06-19 00:16:31 +0000349 sqlite3 db test.db
drh8bf8dc92003-05-17 17:35:10 +0000350 execsql {
351 ATTACH 'test2.db' AS db2;
352 SELECT type, name, tbl_name FROM db2.sqlite_master;
353 }
354} {table t2 t2 table tx tx trigger r1 t2 index i2 t2}
drhb7f91642004-10-31 02:22:47 +0000355} ;# End of ifcapable {trigger}
drha73af532003-04-05 16:56:28 +0000356
drh6b861112003-05-17 19:23:51 +0000357do_test attach-3.1 {
358 db close
359 db2 close
drhef4ac8f2004-06-19 00:16:31 +0000360 sqlite3 db test.db
361 sqlite3 db2 test2.db
drh6b861112003-05-17 19:23:51 +0000362 execsql {
363 SELECT * FROM t1
364 }
365} {1 2 3 4}
drhb7f91642004-10-31 02:22:47 +0000366
367# If we are testing a version of the code that lacks trigger support,
368# adjust the database contents so that they are the same if triggers
369# had been enabled.
370ifcapable {!trigger} {
371 db2 eval {
372 DELETE FROM t2;
373 INSERT INTO t2 VALUES(21, 'x');
374 INSERT INTO t2 VALUES(22, 'y');
375 CREATE TABLE tx(x1,x2,y1,y2);
376 INSERT INTO tx VALUES(1, 11, 'x', 'x');
377 INSERT INTO tx VALUES(2, 12, 'y', 'y');
378 INSERT INTO tx VALUES(11, 21, 'x', 'x');
379 INSERT INTO tx VALUES(12, 22, 'y', 'y');
380 CREATE INDEX i2 ON t2(x);
381 }
382}
383
drh6b861112003-05-17 19:23:51 +0000384do_test attach-3.2 {
385 catchsql {
386 SELECT * FROM t2
387 }
388} {1 {no such table: t2}}
389do_test attach-3.3 {
390 catchsql {
391 ATTACH DATABASE 'test2.db' AS db2;
392 SELECT * FROM t2
393 }
394} {0 {21 x 22 y}}
395
drh80242052004-06-09 00:48:12 +0000396# Even though 'db' has started a transaction, it should not yet have
397# a lock on test2.db so 'db2' should be readable.
drh6b861112003-05-17 19:23:51 +0000398do_test attach-3.4 {
399 execsql BEGIN
400 catchsql {
401 SELECT * FROM t2;
402 } db2;
403} {0 {21 x 22 y}}
404
drh80242052004-06-09 00:48:12 +0000405# Reading from test2.db from db within a transaction should not
406# prevent test2.db from being read by db2.
drh6b861112003-05-17 19:23:51 +0000407do_test attach-3.5 {
408 execsql {SELECT * FROM t2}
409 catchsql {
410 SELECT * FROM t2;
411 } db2;
412} {0 {21 x 22 y}}
413
drh80242052004-06-09 00:48:12 +0000414# Making a change to test2.db through db causes test2.db to get
415# a reserved lock. It should still be accessible through db2.
drh6b861112003-05-17 19:23:51 +0000416do_test attach-3.6 {
417 execsql {
418 UPDATE t2 SET x=x+1 WHERE x=50;
419 }
420 catchsql {
421 SELECT * FROM t2;
422 } db2;
drh80242052004-06-09 00:48:12 +0000423} {0 {21 x 22 y}}
drh6b861112003-05-17 19:23:51 +0000424
425do_test attach-3.7 {
426 execsql ROLLBACK
427 execsql {SELECT * FROM t2} db2
428} {21 x 22 y}
drh80242052004-06-09 00:48:12 +0000429
430# Start transactions on both db and db2. Once again, just because
431# we make a change to test2.db using db2, only a RESERVED lock is
432# obtained, so test2.db should still be readable using db.
433#
drh6b861112003-05-17 19:23:51 +0000434do_test attach-3.8 {
435 execsql BEGIN
436 execsql BEGIN db2
danielk19771d850a72004-05-31 08:26:49 +0000437 execsql {UPDATE t2 SET x=0 WHERE 0} db2
drh6b861112003-05-17 19:23:51 +0000438 catchsql {SELECT * FROM t2}
drh80242052004-06-09 00:48:12 +0000439} {0 {21 x 22 y}}
440
441# It is also still accessible from db2.
drh6b861112003-05-17 19:23:51 +0000442do_test attach-3.9 {
443 catchsql {SELECT * FROM t2} db2
444} {0 {21 x 22 y}}
drh80242052004-06-09 00:48:12 +0000445
drh6b861112003-05-17 19:23:51 +0000446do_test attach-3.10 {
447 execsql {SELECT * FROM t1}
448} {1 2 3 4}
drh80242052004-06-09 00:48:12 +0000449
drh6b861112003-05-17 19:23:51 +0000450do_test attach-3.11 {
451 catchsql {UPDATE t1 SET a=a+1}
452} {0 {}}
453do_test attach-3.12 {
454 execsql {SELECT * FROM t1}
455} {2 2 4 4}
drh80242052004-06-09 00:48:12 +0000456
457# db2 has a RESERVED lock on test2.db, so db cannot write to any tables
458# in test2.db.
drh6b861112003-05-17 19:23:51 +0000459do_test attach-3.13 {
460 catchsql {UPDATE t2 SET x=x+1 WHERE x=50}
461} {1 {database is locked}}
danielk19771d850a72004-05-31 08:26:49 +0000462
463# Change for version 3. Transaction is no longer rolled back
464# for a locked database.
465execsql {ROLLBACK}
466
drh80242052004-06-09 00:48:12 +0000467# db is able to reread its schema because db2 still only holds a
468# reserved lock.
drh6b861112003-05-17 19:23:51 +0000469do_test attach-3.14 {
drh8ef83ff2004-02-12 15:31:21 +0000470 catchsql {SELECT * FROM t1}
drh80242052004-06-09 00:48:12 +0000471} {0 {1 2 3 4}}
drh8ef83ff2004-02-12 15:31:21 +0000472do_test attach-3.15 {
473 execsql COMMIT db2
drh6b861112003-05-17 19:23:51 +0000474 execsql {SELECT * FROM t1}
475} {1 2 3 4}
476
drhf26e09c2003-05-31 16:21:12 +0000477# Ticket #323
478do_test attach-4.1 {
479 execsql {DETACH db2}
480 db2 close
drhef4ac8f2004-06-19 00:16:31 +0000481 sqlite3 db2 test2.db
drhf26e09c2003-05-31 16:21:12 +0000482 execsql {
483 CREATE TABLE t3(x,y);
484 CREATE UNIQUE INDEX t3i1 ON t3(x);
485 INSERT INTO t3 VALUES(1,2);
486 SELECT * FROM t3;
487 } db2;
488} {1 2}
489do_test attach-4.2 {
490 execsql {
491 CREATE TABLE t3(a,b);
492 CREATE UNIQUE INDEX t3i1b ON t3(a);
493 INSERT INTO t3 VALUES(9,10);
494 SELECT * FROM t3;
495 }
496} {9 10}
497do_test attach-4.3 {
498 execsql {
499 ATTACH DATABASE 'test2.db' AS db2;
500 SELECT * FROM db2.t3;
501 }
502} {1 2}
503do_test attach-4.4 {
504 execsql {
505 SELECT * FROM main.t3;
506 }
507} {9 10}
508do_test attach-4.5 {
509 execsql {
510 INSERT INTO db2.t3 VALUES(9,10);
511 SELECT * FROM db2.t3;
512 }
513} {1 2 9 10}
drh798da522004-11-04 04:42:28 +0000514execsql {
515 DETACH db2;
516}
drhb7f91642004-10-31 02:22:47 +0000517ifcapable {trigger} {
518 do_test attach-4.6 {
519 execsql {
drhb7f91642004-10-31 02:22:47 +0000520 CREATE TABLE t4(x);
521 CREATE TRIGGER t3r3 AFTER INSERT ON t3 BEGIN
522 INSERT INTO t4 VALUES('db2.' || NEW.x);
523 END;
524 INSERT INTO t3 VALUES(6,7);
525 SELECT * FROM t4;
526 } db2
527 } {db2.6}
528 do_test attach-4.7 {
529 execsql {
530 CREATE TABLE t4(y);
531 CREATE TRIGGER t3r3 AFTER INSERT ON t3 BEGIN
532 INSERT INTO t4 VALUES('main.' || NEW.a);
533 END;
534 INSERT INTO main.t3 VALUES(11,12);
535 SELECT * FROM main.t4;
536 }
537 } {main.11}
538}
drh798da522004-11-04 04:42:28 +0000539ifcapable {!trigger} {
540 # When we do not have trigger support, set up the table like they
541 # would have been had triggers been there. The tests that follow need
542 # this setup.
543 execsql {
544 CREATE TABLE t4(x);
545 INSERT INTO t3 VALUES(6,7);
546 INSERT INTO t4 VALUES('db2.6');
547 INSERT INTO t4 VALUES('db2.13');
548 } db2
549 execsql {
550 CREATE TABLE t4(y);
551 INSERT INTO main.t3 VALUES(11,12);
552 INSERT INTO t4 VALUES('main.11');
553 }
554}
555
drh80242052004-06-09 00:48:12 +0000556
557# This one is tricky. On the UNION ALL select, we have to make sure
558# the schema for both main and db2 is valid before starting to execute
559# the first query of the UNION ALL. If we wait to test the validity of
560# the schema for main until after the first query has run, that test will
561# fail and the query will abort but we will have already output some
562# results. When the query is retried, the results will be repeated.
563#
danielk197727c77432004-11-22 13:35:41 +0000564ifcapable compound {
drhf26e09c2003-05-31 16:21:12 +0000565do_test attach-4.8 {
566 execsql {
567 ATTACH DATABASE 'test2.db' AS db2;
568 INSERT INTO db2.t3 VALUES(13,14);
569 SELECT * FROM db2.t4 UNION ALL SELECT * FROM main.t4;
570 }
571} {db2.6 db2.13 main.11}
drh80242052004-06-09 00:48:12 +0000572
drhf26e09c2003-05-31 16:21:12 +0000573do_test attach-4.9 {
drh798da522004-11-04 04:42:28 +0000574 ifcapable {!trigger} {execsql {INSERT INTO main.t4 VALUES('main.15')}}
drhf26e09c2003-05-31 16:21:12 +0000575 execsql {
576 INSERT INTO main.t3 VALUES(15,16);
577 SELECT * FROM db2.t4 UNION ALL SELECT * FROM main.t4;
578 }
579} {db2.6 db2.13 main.11 main.15}
danielk197727c77432004-11-22 13:35:41 +0000580} ;# ifcapable compound
581
582ifcapable !compound {
583 ifcapable {!trigger} {execsql {INSERT INTO main.t4 VALUES('main.15')}}
584 execsql {
585 ATTACH DATABASE 'test2.db' AS db2;
586 INSERT INTO db2.t3 VALUES(13,14);
587 INSERT INTO main.t3 VALUES(15,16);
588 }
589} ;# ifcapable !compound
danielk19770fa8ddb2004-11-22 08:43:32 +0000590
591ifcapable view {
drhf26e09c2003-05-31 16:21:12 +0000592do_test attach-4.10 {
593 execsql {
594 DETACH DATABASE db2;
595 }
596 execsql {
597 CREATE VIEW v3 AS SELECT x*100+y FROM t3;
598 SELECT * FROM v3;
599 } db2
600} {102 910 607 1314}
601do_test attach-4.11 {
602 execsql {
603 CREATE VIEW v3 AS SELECT a*100+b FROM t3;
604 SELECT * FROM v3;
605 }
606} {910 1112 1516}
607do_test attach-4.12 {
608 execsql {
609 ATTACH DATABASE 'test2.db' AS db2;
610 SELECT * FROM db2.v3;
611 }
612} {102 910 607 1314}
613do_test attach-4.13 {
614 execsql {
615 SELECT * FROM main.v3;
616 }
617} {910 1112 1516}
danielk19770fa8ddb2004-11-22 08:43:32 +0000618} ;# ifcapable view
drhf26e09c2003-05-31 16:21:12 +0000619
drh4312db52003-06-03 01:47:11 +0000620# Tests for the sqliteFix...() routines in attach.c
621#
drh798da522004-11-04 04:42:28 +0000622ifcapable {trigger} {
drh4312db52003-06-03 01:47:11 +0000623do_test attach-5.1 {
624 db close
drhef4ac8f2004-06-19 00:16:31 +0000625 sqlite3 db test.db
drh9cb733c2003-07-18 01:25:34 +0000626 db2 close
drh4312db52003-06-03 01:47:11 +0000627 file delete -force test2.db
drhef4ac8f2004-06-19 00:16:31 +0000628 sqlite3 db2 test2.db
drh4312db52003-06-03 01:47:11 +0000629 catchsql {
630 ATTACH DATABASE 'test.db' AS orig;
drh86dac2b2006-05-25 12:17:31 +0000631 CREATE TRIGGER r1 AFTER INSERT ON orig.t1 BEGIN
drh4312db52003-06-03 01:47:11 +0000632 SELECT 'no-op';
633 END;
634 } db2
danielk1977ef2cb632004-05-29 02:37:19 +0000635} {1 {trigger r1 cannot reference objects in database orig}}
drh4312db52003-06-03 01:47:11 +0000636do_test attach-5.2 {
637 catchsql {
638 CREATE TABLE t5(x,y);
639 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
640 SELECT 'no-op';
641 END;
642 } db2
643} {0 {}}
644do_test attach-5.3 {
645 catchsql {
646 DROP TRIGGER r5;
647 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
648 SELECT 'no-op' FROM orig.t1;
649 END;
650 } db2
651} {1 {trigger r5 cannot reference objects in database orig}}
danielk197753c0f742005-03-29 03:10:59 +0000652ifcapable tempdb {
653 do_test attach-5.4 {
654 catchsql {
655 CREATE TEMP TABLE t6(p,q,r);
656 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
657 SELECT 'no-op' FROM temp.t6;
658 END;
659 } db2
660 } {1 {trigger r5 cannot reference objects in database temp}}
661}
danielk1977e61b9f42005-01-21 04:25:47 +0000662ifcapable subquery {
663 do_test attach-5.5 {
664 catchsql {
665 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
666 SELECT 'no-op' || (SELECT * FROM temp.t6);
667 END;
668 } db2
669 } {1 {trigger r5 cannot reference objects in database temp}}
670 do_test attach-5.6 {
671 catchsql {
672 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
673 SELECT 'no-op' FROM t1 WHERE x<(SELECT min(x) FROM temp.t6);
674 END;
675 } db2
676 } {1 {trigger r5 cannot reference objects in database temp}}
677 do_test attach-5.7 {
678 catchsql {
679 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
680 SELECT 'no-op' FROM t1 GROUP BY 1 HAVING x<(SELECT min(x) FROM temp.t6);
681 END;
682 } db2
683 } {1 {trigger r5 cannot reference objects in database temp}}
684 do_test attach-5.7 {
685 catchsql {
686 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
687 SELECT max(1,x,(SELECT min(x) FROM temp.t6)) FROM t1;
688 END;
689 } db2
690 } {1 {trigger r5 cannot reference objects in database temp}}
691 do_test attach-5.8 {
692 catchsql {
693 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
694 INSERT INTO t1 VALUES((SELECT min(x) FROM temp.t6),5);
695 END;
696 } db2
697 } {1 {trigger r5 cannot reference objects in database temp}}
698 do_test attach-5.9 {
699 catchsql {
700 CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN
701 DELETE FROM t1 WHERE x<(SELECT min(x) FROM temp.t6);
702 END;
703 } db2
704 } {1 {trigger r5 cannot reference objects in database temp}}
705} ;# endif subquery
drh798da522004-11-04 04:42:28 +0000706} ;# endif trigger
drh4312db52003-06-03 01:47:11 +0000707
708# Check to make sure we get a sensible error if unable to open
709# the file that we are trying to attach.
710#
711do_test attach-6.1 {
712 catchsql {
713 ATTACH DATABASE 'no-such-file' AS nosuch;
714 }
danielk19773df6b252004-05-29 10:23:19 +0000715} {0 {}}
drhbc2bca02003-06-04 12:31:53 +0000716if {$tcl_platform(platform)=="unix"} {
717 do_test attach-6.2 {
drhef4ac8f2004-06-19 00:16:31 +0000718 sqlite3 dbx cannot-read
drhbc2bca02003-06-04 12:31:53 +0000719 dbx eval {CREATE TABLE t1(a,b,c)}
720 dbx close
721 file attributes cannot-read -permission 0000
drh97ba4c92005-03-02 05:18:57 +0000722 if {[file writable cannot-read]} {
723 puts "\n**** Tests do not work when run as root ****"
724 file delete -force cannot-read
725 exit 1
726 }
drhbc2bca02003-06-04 12:31:53 +0000727 catchsql {
728 ATTACH DATABASE 'cannot-read' AS noread;
729 }
730 } {1 {unable to open database: cannot-read}}
drh69544ec2008-02-06 14:11:34 +0000731 do_test attach-6.2.2 {
732 db errorcode
733 } {14}
drhbc2bca02003-06-04 12:31:53 +0000734 file delete -force cannot-read
735}
drhf26e09c2003-05-31 16:21:12 +0000736
danielk1977576ec6b2005-01-21 11:55:25 +0000737# Check the error message if we try to access a database that has
738# not been attached.
739do_test attach-6.3 {
740 catchsql {
741 CREATE TABLE no_such_db.t1(a, b, c);
742 }
743} {1 {unknown database no_such_db}}
drha73af532003-04-05 16:56:28 +0000744for {set i 2} {$i<=15} {incr i} {
745 catch {db$i close}
746}
drh02f9f6b2004-05-31 18:21:54 +0000747db close
drh4312db52003-06-03 01:47:11 +0000748file delete -force test2.db
drh02f9f6b2004-05-31 18:21:54 +0000749file delete -force no-such-file
drh2d458342003-04-05 03:42:26 +0000750
danielk19774152e672007-09-12 17:01:45 +0000751ifcapable subquery {
752 do_test attach-7.1 {
753 file delete -force test.db test.db-journal
754 sqlite3 db test.db
755 catchsql {
756 DETACH RAISE ( IGNORE ) IN ( SELECT "AAAAAA" . * ORDER BY
757 REGISTER LIMIT "AAAAAA" . "AAAAAA" OFFSET RAISE ( IGNORE ) NOT NULL )
758 }
drhfd773cf2009-05-29 14:39:07 +0000759 } {1 {no such table: AAAAAA}}
danielk19774152e672007-09-12 17:01:45 +0000760}
drh69544ec2008-02-06 14:11:34 +0000761
762# Create a malformed file (a file that is not a valid database)
763# and try to attach it
764#
765do_test attach-8.1 {
766 set fd [open test2.db w]
767 puts $fd "This file is not a valid SQLite database"
768 close $fd
769 catchsql {
770 ATTACH 'test2.db' AS t2;
771 }
772} {1 {file is encrypted or is not a database}}
773do_test attach-8.2 {
774 db errorcode
775} {26}
776file delete -force test2.db
777do_test attach-8.3 {
778 sqlite3 db2 test2.db
779 db2 eval {CREATE TABLE t1(x); BEGIN EXCLUSIVE}
780 catchsql {
781 ATTACH 'test2.db' AS t2;
782 }
783} {1 {database is locked}}
784do_test attach-8.4 {
785 db errorcode
786} {5}
787db2 close
788file delete -force test2.db
789
danac887602009-11-20 10:18:06 +0000790# Test that it is possible to attach the same database more than
791# once when not in shared-cache mode. That this is not possible in
792# shared-cache mode is tested in shared7.test.
793do_test attach-9.1 {
794 file delete -force test4.db
795 execsql {
796 ATTACH 'test4.db' AS aux1;
797 CREATE TABLE aux1.t1(a, b);
798 INSERT INTO aux1.t1 VALUES(1, 2);
799 ATTACH 'test4.db' AS aux2;
800 SELECT * FROM aux2.t1;
801 }
802} {1 2}
803do_test attach-9.2 {
danac887602009-11-20 10:18:06 +0000804 catchsql {
805 BEGIN;
806 INSERT INTO aux1.t1 VALUES(3, 4);
807 INSERT INTO aux2.t1 VALUES(5, 6);
808 }
809} {1 {database is locked}}
810do_test attach-9.3 {
danac887602009-11-20 10:18:06 +0000811 execsql {
812 COMMIT;
813 SELECT * FROM aux2.t1;
814 }
815} {1 2 3 4}
816
drh4413c432009-12-08 13:44:21 +0000817# Ticket [abe728bbc311d81334dae9762f0db87c07a98f79].
818# Multi-database commit on an attached TEMP database.
819#
820do_test attach-10.1 {
821 execsql {
822 ATTACH '' AS noname;
823 ATTACH ':memory:' AS inmem;
824 BEGIN;
825 CREATE TABLE noname.noname(x);
826 CREATE TABLE inmem.inmem(y);
827 CREATE TABLE main.main(z);
828 COMMIT;
829 SELECT name FROM noname.sqlite_master;
830 SELECT name FROM inmem.sqlite_master;
831 }
832} {noname inmem}
833do_test attach-10.2 {
834 lrange [execsql {
835 PRAGMA database_list;
836 }] 9 end
837} {4 noname {} 5 inmem {}}
danbdd9af02010-11-18 16:14:24 +0000838
drh2d458342003-04-05 03:42:26 +0000839finish_test