1# 2003 July 1 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 schema changes to attached databases. 14# 15# $Id: attach3.test,v 1.11 2004/11/05 05:10:29 drh Exp $ 16# 17 18 19set testdir [file dirname $argv0] 20source $testdir/tester.tcl 21 22# Create tables t1 and t2 in the main database 23execsql { 24 CREATE TABLE t1(a, b); 25 CREATE TABLE t2(c, d); 26} 27 28# Create tables t1 and t2 in database file test2.db 29file delete -force test2.db 30file delete -force test2.db-journal 31sqlite3 db2 test2.db 32execsql { 33 CREATE TABLE t1(a, b); 34 CREATE TABLE t2(c, d); 35} db2 36db2 close 37 38# Create a table in the auxilary database. 39do_test attach3-1.1 { 40 execsql { 41 ATTACH 'test2.db' AS aux; 42 } 43} {} 44do_test attach3-1.2 { 45 execsql { 46 CREATE TABLE aux.t3(e, f); 47 } 48} {} 49do_test attach3-1.3 { 50 execsql { 51 SELECT * FROM sqlite_master WHERE name = 't3'; 52 } 53} {} 54do_test attach3-1.4 { 55 execsql { 56 SELECT * FROM aux.sqlite_master WHERE name = 't3'; 57 } 58} {table t3 t3 4 {CREATE TABLE t3(e, f)}} 59do_test attach3-1.5 { 60 execsql { 61 INSERT INTO t3 VALUES(1, 2); 62 SELECT * FROM t3; 63 } 64} {1 2} 65 66# Create an index on the auxilary database table. 67do_test attach3-2.1 { 68 execsql { 69 CREATE INDEX aux.i1 on t3(e); 70 } 71} {} 72do_test attach3-2.2 { 73 execsql { 74 SELECT * FROM sqlite_master WHERE name = 'i1'; 75 } 76} {} 77do_test attach3-2.3 { 78 execsql { 79 SELECT * FROM aux.sqlite_master WHERE name = 'i1'; 80 } 81} {index i1 t3 5 {CREATE INDEX i1 on t3(e)}} 82 83# Drop the index on the aux database table. 84do_test attach3-3.1 { 85 execsql { 86 DROP INDEX aux.i1; 87 SELECT * FROM aux.sqlite_master WHERE name = 'i1'; 88 } 89} {} 90do_test attach3-3.2 { 91 execsql { 92 CREATE INDEX aux.i1 on t3(e); 93 SELECT * FROM aux.sqlite_master WHERE name = 'i1'; 94 } 95} {index i1 t3 5 {CREATE INDEX i1 on t3(e)}} 96do_test attach3-3.3 { 97 execsql { 98 DROP INDEX i1; 99 SELECT * FROM aux.sqlite_master WHERE name = 'i1'; 100 } 101} {} 102 103# Drop tables t1 and t2 in the auxilary database. 104do_test attach3-4.1 { 105 execsql { 106 DROP TABLE aux.t1; 107 SELECT name FROM aux.sqlite_master; 108 } 109} {t2 t3} 110do_test attach3-4.2 { 111 # This will drop main.t2 112 execsql { 113 DROP TABLE t2; 114 SELECT name FROM aux.sqlite_master; 115 } 116} {t2 t3} 117do_test attach3-4.3 { 118 execsql { 119 DROP TABLE t2; 120 SELECT name FROM aux.sqlite_master; 121 } 122} {t3} 123 124# Create a view in the auxilary database. 125do_test attach3-5.1 { 126 execsql { 127 CREATE VIEW aux.v1 AS SELECT * FROM t3; 128 } 129} {} 130do_test attach3-5.2 { 131 execsql { 132 SELECT * FROM aux.sqlite_master WHERE name = 'v1'; 133 } 134} {view v1 v1 0 {CREATE VIEW v1 AS SELECT * FROM t3}} 135do_test attach3-5.3 { 136 execsql { 137 INSERT INTO aux.t3 VALUES('hello', 'world'); 138 SELECT * FROM v1; 139 } 140} {1 2 hello world} 141 142# Drop the view 143do_test attach3-6.1 { 144 execsql { 145 DROP VIEW aux.v1; 146 } 147} {} 148do_test attach3-6.2 { 149 execsql { 150 SELECT * FROM aux.sqlite_master WHERE name = 'v1'; 151 } 152} {} 153 154ifcapable {trigger} { 155# Create a trigger in the auxilary database. 156do_test attach3-7.1 { 157 execsql { 158 CREATE TRIGGER aux.tr1 AFTER INSERT ON t3 BEGIN 159 INSERT INTO t3 VALUES(new.e*2, new.f*2); 160 END; 161 } 162} {} 163do_test attach3-7.2 { 164 execsql { 165 DELETE FROM t3; 166 INSERT INTO t3 VALUES(10, 20); 167 SELECT * FROM t3; 168 } 169} {10 20 20 40} 170do_test attach3-5.3 { 171 execsql { 172 SELECT * FROM aux.sqlite_master WHERE name = 'tr1'; 173 } 174} {trigger tr1 t3 0 {CREATE TRIGGER tr1 AFTER INSERT ON t3 BEGIN 175 INSERT INTO t3 VALUES(new.e*2, new.f*2); 176 END}} 177 178# Drop the trigger 179do_test attach3-8.1 { 180 execsql { 181 DROP TRIGGER aux.tr1; 182 } 183} {} 184do_test attach3-8.2 { 185 execsql { 186 SELECT * FROM aux.sqlite_master WHERE name = 'tr1'; 187 } 188} {} 189 190# Try to trick SQLite into dropping the wrong temp trigger. 191do_test attach3-9.0 { 192 execsql { 193 CREATE TABLE main.t4(a, b, c); 194 CREATE TABLE aux.t4(a, b, c); 195 CREATE TEMP TRIGGER tst_trigger BEFORE INSERT ON aux.t4 BEGIN 196 SELECT 'hello world'; 197 END; 198 SELECT count(*) FROM sqlite_temp_master; 199 } 200} {1} 201do_test attach3-9.1 { 202 execsql { 203 DROP TABLE main.t4; 204 SELECT count(*) FROM sqlite_temp_master; 205 } 206} {1} 207do_test attach3-9.2 { 208 execsql { 209 DROP TABLE aux.t4; 210 SELECT count(*) FROM sqlite_temp_master; 211 } 212} {0} 213} ;# endif trigger 214 215# Make sure the aux.sqlite_master table is read-only 216do_test attach3-10.0 { 217 catchsql { 218 INSERT INTO aux.sqlite_master VALUES(1, 2, 3, 4, 5); 219 } 220} {1 {table sqlite_master may not be modified}} 221 222# Failure to attach leaves us in a workable state. 223# Ticket #811 224# 225do_test attach3-11.0 { 226 catchsql { 227 ATTACH DATABASE '/nodir/nofile.x' AS notadb; 228 } 229} {1 {unable to open database: /nodir/nofile.x}} 230do_test attach3-11.1 { 231 catchsql { 232 ATTACH DATABASE ':memory:' AS notadb; 233 } 234} {0 {}} 235do_test attach3-11.2 { 236 catchsql { 237 DETACH DATABASE notadb; 238 } 239} {0 {}} 240 241finish_test 242