1*c5c4113dSnw141292 2*c5c4113dSnw141292#pragma ident "%Z%%M% %I% %E% SMI" 3*c5c4113dSnw141292 4*c5c4113dSnw141292# 2003 July 1 5*c5c4113dSnw141292# 6*c5c4113dSnw141292# The author disclaims copyright to this source code. In place of 7*c5c4113dSnw141292# a legal notice, here is a blessing: 8*c5c4113dSnw141292# 9*c5c4113dSnw141292# May you do good and not evil. 10*c5c4113dSnw141292# May you find forgiveness for yourself and forgive others. 11*c5c4113dSnw141292# May you share freely, never taking more than you give. 12*c5c4113dSnw141292# 13*c5c4113dSnw141292#*********************************************************************** 14*c5c4113dSnw141292# This file implements regression tests for SQLite library. The 15*c5c4113dSnw141292# focus of this script is testing the ATTACH and DETACH commands 16*c5c4113dSnw141292# and related functionality. 17*c5c4113dSnw141292# 18*c5c4113dSnw141292# $Id: attach2.test,v 1.5 2004/02/12 15:31:22 drh Exp $ 19*c5c4113dSnw141292# 20*c5c4113dSnw141292 21*c5c4113dSnw141292 22*c5c4113dSnw141292set testdir [file dirname $argv0] 23*c5c4113dSnw141292source $testdir/tester.tcl 24*c5c4113dSnw141292 25*c5c4113dSnw141292# Ticket #354 26*c5c4113dSnw141292# 27*c5c4113dSnw141292do_test attach2-1.1 { 28*c5c4113dSnw141292 db eval { 29*c5c4113dSnw141292 CREATE TABLE t1(a,b); 30*c5c4113dSnw141292 CREATE INDEX x1 ON t1(a); 31*c5c4113dSnw141292 } 32*c5c4113dSnw141292 file delete -force test2.db 33*c5c4113dSnw141292 file delete -force test2.db-journal 34*c5c4113dSnw141292 sqlite db2 test2.db 35*c5c4113dSnw141292 db2 eval { 36*c5c4113dSnw141292 CREATE TABLE t1(a,b); 37*c5c4113dSnw141292 CREATE INDEX x1 ON t1(a); 38*c5c4113dSnw141292 } 39*c5c4113dSnw141292 catchsql { 40*c5c4113dSnw141292 ATTACH 'test2.db' AS t2; 41*c5c4113dSnw141292 } 42*c5c4113dSnw141292} {0 {}} 43*c5c4113dSnw141292 44*c5c4113dSnw141292# Ticket #514 45*c5c4113dSnw141292# 46*c5c4113dSnw141292proc db_list {db} { 47*c5c4113dSnw141292 set list {} 48*c5c4113dSnw141292 foreach {idx name file} [execsql {PRAGMA database_list} $db] { 49*c5c4113dSnw141292 lappend list $idx $name 50*c5c4113dSnw141292 } 51*c5c4113dSnw141292 return $list 52*c5c4113dSnw141292} 53*c5c4113dSnw141292db eval {DETACH t2} 54*c5c4113dSnw141292do_test attach2-2.1 { 55*c5c4113dSnw141292 # lock test2.db then try to attach it. Should get an error. 56*c5c4113dSnw141292 db2 eval {BEGIN} 57*c5c4113dSnw141292 catchsql { 58*c5c4113dSnw141292 ATTACH 'test2.db' AS t2; 59*c5c4113dSnw141292 } 60*c5c4113dSnw141292} {1 {database is locked}} 61*c5c4113dSnw141292do_test attach2-2.2 { 62*c5c4113dSnw141292 # make sure test2.db did not get attached. 63*c5c4113dSnw141292 db_list db 64*c5c4113dSnw141292} {0 main 1 temp} 65*c5c4113dSnw141292do_test attach2-2.3 { 66*c5c4113dSnw141292 # unlock test2.db and try to attach again. should work this time. 67*c5c4113dSnw141292 db2 eval {COMMIT} 68*c5c4113dSnw141292 catchsql { 69*c5c4113dSnw141292 ATTACH 'test2.db' AS t2; 70*c5c4113dSnw141292 } 71*c5c4113dSnw141292} {0 {}} 72*c5c4113dSnw141292do_test attach2-2.4 { 73*c5c4113dSnw141292 db_list db 74*c5c4113dSnw141292} {0 main 1 temp 2 t2} 75*c5c4113dSnw141292do_test attach2-2.5 { 76*c5c4113dSnw141292 catchsql { 77*c5c4113dSnw141292 SELECT name FROM t2.sqlite_master; 78*c5c4113dSnw141292 } 79*c5c4113dSnw141292} {0 {t1 x1}} 80*c5c4113dSnw141292do_test attach2-2.6 { 81*c5c4113dSnw141292 # lock test2.db and try to read from it. should get an error. 82*c5c4113dSnw141292 db2 eval BEGIN 83*c5c4113dSnw141292 catchsql { 84*c5c4113dSnw141292 SELECT name FROM t2.sqlite_master; 85*c5c4113dSnw141292 } 86*c5c4113dSnw141292} {1 {database is locked}} 87*c5c4113dSnw141292do_test attach2-2.7 { 88*c5c4113dSnw141292 # but we can still read from test1.db even though test2.db is locked. 89*c5c4113dSnw141292 catchsql { 90*c5c4113dSnw141292 SELECT name FROM main.sqlite_master; 91*c5c4113dSnw141292 } 92*c5c4113dSnw141292} {0 {t1 x1}} 93*c5c4113dSnw141292do_test attach2-2.8 { 94*c5c4113dSnw141292 # start a transaction on test.db even though test2.db is locked. 95*c5c4113dSnw141292 catchsql { 96*c5c4113dSnw141292 BEGIN; 97*c5c4113dSnw141292 INSERT INTO t1 VALUES(8,9); 98*c5c4113dSnw141292 } 99*c5c4113dSnw141292} {0 {}} 100*c5c4113dSnw141292do_test attach2-2.9 { 101*c5c4113dSnw141292 execsql { 102*c5c4113dSnw141292 SELECT * FROM t1 103*c5c4113dSnw141292 } 104*c5c4113dSnw141292} {8 9} 105*c5c4113dSnw141292do_test attach2-2.10 { 106*c5c4113dSnw141292 # now try to write to test2.db. the write should fail 107*c5c4113dSnw141292 catchsql { 108*c5c4113dSnw141292 INSERT INTO t2.t1 VALUES(1,2); 109*c5c4113dSnw141292 } 110*c5c4113dSnw141292} {1 {database is locked}} 111*c5c4113dSnw141292do_test attach2-2.11 { 112*c5c4113dSnw141292 # when the write failed in the previous test, the transaction should 113*c5c4113dSnw141292 # have rolled back. 114*c5c4113dSnw141292 db2 eval ROLLBACK 115*c5c4113dSnw141292 execsql { 116*c5c4113dSnw141292 SELECT * FROM t1 117*c5c4113dSnw141292 } 118*c5c4113dSnw141292} {} 119*c5c4113dSnw141292do_test attach2-2.12 { 120*c5c4113dSnw141292 catchsql { 121*c5c4113dSnw141292 COMMIT 122*c5c4113dSnw141292 } 123*c5c4113dSnw141292} {1 {cannot commit - no transaction is active}} 124*c5c4113dSnw141292 125*c5c4113dSnw141292# Ticket #574: Make sure it works usingi the non-callback API 126*c5c4113dSnw141292# 127*c5c4113dSnw141292do_test attach2-3.1 { 128*c5c4113dSnw141292 db close 129*c5c4113dSnw141292 set DB [sqlite db test.db] 130*c5c4113dSnw141292 set rc [catch {sqlite_compile $DB "ATTACH 'test2.db' AS t2" TAIL} VM] 131*c5c4113dSnw141292 if {$rc} {lappend rc $VM} 132*c5c4113dSnw141292 sqlite_finalize $VM 133*c5c4113dSnw141292 set rc 134*c5c4113dSnw141292} {0} 135*c5c4113dSnw141292do_test attach2-3.2 { 136*c5c4113dSnw141292 set rc [catch {sqlite_compile $DB "DETACH t2" TAIL} VM] 137*c5c4113dSnw141292 if {$rc} {lappend rc $VM} 138*c5c4113dSnw141292 sqlite_finalize $VM 139*c5c4113dSnw141292 set rc 140*c5c4113dSnw141292} {0} 141*c5c4113dSnw141292 142*c5c4113dSnw141292db close 143*c5c4113dSnw141292for {set i 2} {$i<=15} {incr i} { 144*c5c4113dSnw141292 catch {db$i close} 145*c5c4113dSnw141292} 146*c5c4113dSnw141292file delete -force test2.db 147*c5c4113dSnw141292 148*c5c4113dSnw141292 149*c5c4113dSnw141292finish_test 150