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 schema changes to attached databases.
15 # $Id: attach3.test,v 1.18 2007/10/09 08:29:32 danielk1977 Exp $
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
26 # The tests in this file were written before SQLite supported recursive
27 # trigger invocation, and some tests depend on that to pass. So disable
28 # recursive triggers for this file.
29 catchsql { pragma recursive_triggers = off }
31 # Create tables t1 and t2 in the main database
33 CREATE TABLE t1(a, b);
34 CREATE TABLE t2(c, d);
37 # Create tables t1 and t2 in database file test2.db
39 forcedelete test2.db-journal
42 CREATE TABLE t1(a, b);
43 CREATE TABLE t2(c, d);
47 # Create a table in the auxilary database.
50 ATTACH 'test2.db' AS aux;
55 CREATE TABLE aux.t3(e, f);
60 SELECT * FROM sqlite_master WHERE name = 't3';
65 SELECT * FROM aux.sqlite_master WHERE name = 't3';
67 } "table t3 t3 [expr $AUTOVACUUM?5:4] {CREATE TABLE t3(e, f)}"
70 INSERT INTO t3 VALUES(1, 2);
75 # Create an index on the auxilary database table.
78 CREATE INDEX aux.i1 on t3(e);
83 SELECT * FROM sqlite_master WHERE name = 'i1';
88 SELECT * FROM aux.sqlite_master WHERE name = 'i1';
90 } "index i1 t3 [expr $AUTOVACUUM?6:5] {CREATE INDEX i1 on t3(e)}"
92 # Drop the index on the aux database table.
96 SELECT * FROM aux.sqlite_master WHERE name = 'i1';
101 CREATE INDEX aux.i1 on t3(e);
102 SELECT * FROM aux.sqlite_master WHERE name = 'i1';
104 } "index i1 t3 [expr $AUTOVACUUM?6:5] {CREATE INDEX i1 on t3(e)}"
105 do_test attach3-3.3 {
108 SELECT * FROM aux.sqlite_master WHERE name = 'i1';
112 # Drop tables t1 and t2 in the auxilary database.
113 do_test attach3-4.1 {
116 SELECT name FROM aux.sqlite_master;
119 do_test attach3-4.2 {
120 # This will drop main.t2
123 SELECT name FROM aux.sqlite_master;
126 do_test attach3-4.3 {
129 SELECT name FROM aux.sqlite_master;
133 # Create a view in the auxilary database.
135 do_test attach3-5.1 {
137 CREATE VIEW aux.v1 AS SELECT * FROM t3;
140 do_test attach3-5.2 {
142 SELECT * FROM aux.sqlite_master WHERE name = 'v1';
144 } {view v1 v1 0 {CREATE VIEW v1 AS SELECT * FROM t3}}
145 do_test attach3-5.3 {
147 INSERT INTO aux.t3 VALUES('hello', 'world');
153 do_test attach3-6.1 {
158 do_test attach3-6.2 {
160 SELECT * FROM aux.sqlite_master WHERE name = 'v1';
165 ifcapable {trigger} {
166 # Create a trigger in the auxilary database.
167 do_test attach3-7.1 {
169 CREATE TRIGGER aux.tr1 AFTER INSERT ON t3 BEGIN
170 INSERT INTO t3 VALUES(new.e*2, new.f*2);
174 do_test attach3-7.2 {
177 INSERT INTO t3 VALUES(10, 20);
181 do_test attach3-5.3 {
183 SELECT * FROM aux.sqlite_master WHERE name = 'tr1';
185 } {trigger tr1 t3 0 {CREATE TRIGGER tr1 AFTER INSERT ON t3 BEGIN
186 INSERT INTO t3 VALUES(new.e*2, new.f*2);
190 do_test attach3-8.1 {
192 DROP TRIGGER aux.tr1;
195 do_test attach3-8.2 {
197 SELECT * FROM aux.sqlite_master WHERE name = 'tr1';
202 # Try to trick SQLite into dropping the wrong temp trigger.
203 do_test attach3-9.0 {
205 CREATE TABLE main.t4(a, b, c);
206 CREATE TABLE aux.t4(a, b, c);
207 CREATE TEMP TRIGGER tst_trigger BEFORE INSERT ON aux.t4 BEGIN
208 SELECT 'hello world';
210 SELECT count(*) FROM sqlite_temp_master;
213 do_test attach3-9.1 {
216 SELECT count(*) FROM sqlite_temp_master;
219 do_test attach3-9.2 {
222 SELECT count(*) FROM sqlite_temp_master;
228 # Make sure the aux.sqlite_master table is read-only
229 do_test attach3-10.0 {
231 INSERT INTO aux.sqlite_master VALUES(1, 2, 3, 4, 5);
233 } {1 {table sqlite_master may not be modified}}
235 # Failure to attach leaves us in a workable state.
238 do_test attach3-11.0 {
240 ATTACH DATABASE '/nodir/nofile.x' AS notadb;
242 } {1 {unable to open database: /nodir/nofile.x}}
243 do_test attach3-11.1 {
245 ATTACH DATABASE ':memory:' AS notadb;
248 do_test attach3-11.2 {
250 DETACH DATABASE notadb;
254 # Return a list of attached databases
258 PRAGMA database_list;
261 foreach {n id file} $x {lappend y $id}
265 ifcapable schema_pragmas&&tempdb {
268 execsql {create temp table dummy(dummy)}
273 do_test attach3-12.1 {
276 do_test attach3-12.2 {
278 ATTACH DATABASE ? AS ?
282 do_test attach3-12.3 {
288 do_test attach3-12.4 {
294 do_test attach3-12.5 {
296 ATTACH DATABASE '' AS ''
300 do_test attach3-12.6 {
306 do_test attach3-12.7 {
308 ATTACH DATABASE '' AS ?
312 do_test attach3-12.8 {
318 do_test attach3-12.9 {
320 ATTACH DATABASE '' AS NULL
324 do_test attach3-12.10 {
330 do_test attach3-12.11 {
334 } {1 {no such database: }}
335 do_test attach3-12.12 {
340 } {1 {database is already in use}}
341 do_test attach3-12.13 {
344 do_test attach3-12.14 {
351 } ;# ifcapable pragma