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 related functionality. 14# 15# $Id: attach2.test,v 1.33 2005/12/06 17:19:12 danielk1977 Exp $ 16# 17 18set testdir [file dirname $argv0] 19source $testdir/tester.tcl 20 21 22# Ticket #354 23# 24# Databases test.db and test2.db contain identical schemas. Make 25# sure we can attach test2.db from test.db. 26# 27do_test attach2-1.1 { 28 db eval { 29 CREATE TABLE t1(a,b); 30 CREATE INDEX x1 ON t1(a); 31 } 32 file delete -force test2.db 33 file delete -force test2.db-journal 34 sqlite3 db2 test2.db 35 db2 eval { 36 CREATE TABLE t1(a,b); 37 CREATE INDEX x1 ON t1(a); 38 } 39 catchsql { 40 ATTACH 'test2.db' AS t2; 41 } 42} {0 {}} 43 44# Ticket #514 45# 46proc db_list {db} { 47 set list {} 48 foreach {idx name file} [execsql {PRAGMA database_list} $db] { 49 lappend list $idx $name 50 } 51 return $list 52} 53db eval {DETACH t2} 54do_test attach2-2.1 { 55 # lock test2.db then try to attach it. This is no longer an error because 56 # db2 just RESERVES the database. It does not obtain a write-lock until 57 # we COMMIT. 58 db2 eval {BEGIN} 59 db2 eval {UPDATE t1 SET a = 0 WHERE 0} 60 catchsql { 61 ATTACH 'test2.db' AS t2; 62 } 63} {0 {}} 64ifcapable schema_pragmas { 65do_test attach2-2.2 { 66 # make sure test2.db did get attached. 67 db_list db 68} {0 main 2 t2} 69} ;# ifcapable schema_pragmas 70db2 eval {COMMIT} 71 72do_test attach2-2.5 { 73 # Make sure we can read test2.db from db 74 catchsql { 75 SELECT name FROM t2.sqlite_master; 76 } 77} {0 {t1 x1}} 78do_test attach2-2.6 { 79 # lock test2.db and try to read from it. This should still work because 80 # the lock is only a RESERVED lock which does not prevent reading. 81 # 82 db2 eval BEGIN 83 db2 eval {UPDATE t1 SET a = 0 WHERE 0} 84 catchsql { 85 SELECT name FROM t2.sqlite_master; 86 } 87} {0 {t1 x1}} 88do_test attach2-2.7 { 89 # but we can still read from test1.db even though test2.db is locked. 90 catchsql { 91 SELECT name FROM main.sqlite_master; 92 } 93} {0 {t1 x1}} 94do_test attach2-2.8 { 95 # start a transaction on test.db even though test2.db is locked. 96 catchsql { 97 BEGIN; 98 INSERT INTO t1 VALUES(8,9); 99 } 100} {0 {}} 101do_test attach2-2.9 { 102 execsql { 103 SELECT * FROM t1 104 } 105} {8 9} 106do_test attach2-2.10 { 107 # now try to write to test2.db. the write should fail 108 catchsql { 109 INSERT INTO t2.t1 VALUES(1,2); 110 } 111} {1 {database is locked}} 112do_test attach2-2.11 { 113 # when the write failed in the previous test, the transaction should 114 # have rolled back. 115 # 116 # Update for version 3: A transaction is no longer rolled back if a 117 # database is found to be busy. 118 execsql {rollback} 119 db2 eval ROLLBACK 120 execsql { 121 SELECT * FROM t1 122 } 123} {} 124do_test attach2-2.12 { 125 catchsql { 126 COMMIT 127 } 128} {1 {cannot commit - no transaction is active}} 129 130# Ticket #574: Make sure it works using the non-callback API 131# 132do_test attach2-3.1 { 133 db close 134 set DB [sqlite3 db test.db] 135 set rc [catch {sqlite3_prepare $DB "ATTACH 'test2.db' AS t2" -1 TAIL} VM] 136 if {$rc} {lappend rc $VM} 137 sqlite3_step $VM 138 sqlite3_finalize $VM 139 set rc 140} {0} 141do_test attach2-3.2 { 142 set rc [catch {sqlite3_prepare $DB "DETACH t2" -1 TAIL} VM] 143 if {$rc} {lappend rc $VM} 144 sqlite3_step $VM 145 sqlite3_finalize $VM 146 set rc 147} {0} 148 149db close 150for {set i 2} {$i<=15} {incr i} { 151 catch {db$i close} 152} 153 154# A procedure to verify the status of locks on a database. 155# 156proc lock_status {testnum db expected_result} { 157 # If the database was compiled with OMIT_TEMPDB set, then 158 # the lock_status list will not contain an entry for the temp 159 # db. But the test code doesn't know this, so it's easiest 160 # to filter it out here. 161 ifcapable !tempdb { 162 set expected_result [concat \ 163 [lrange $expected_result 0 1] \ 164 [lrange $expected_result 4 end] \ 165 ] 166 } 167 do_test attach2-$testnum [subst { 168 $db cache flush ;# The lock_status pragma should not be cached 169 execsql {PRAGMA lock_status} $db 170 }] $expected_result 171} 172set sqlite_os_trace 0 173 174# Tests attach2-4.* test that read-locks work correctly with attached 175# databases. 176do_test attach2-4.1 { 177 sqlite3 db test.db 178 sqlite3 db2 test.db 179 execsql {ATTACH 'test2.db' as file2} 180 execsql {ATTACH 'test2.db' as file2} db2 181} {} 182 183lock_status 4.1.1 db {main unlocked temp closed file2 unlocked} 184lock_status 4.1.2 db2 {main unlocked temp closed file2 unlocked} 185 186do_test attach2-4.2 { 187 # Handle 'db' read-locks test.db 188 execsql {BEGIN} 189 execsql {SELECT * FROM t1} 190 # Lock status: 191 # db - shared(main) 192 # db2 - 193} {} 194 195lock_status 4.2.1 db {main shared temp closed file2 unlocked} 196lock_status 4.2.2 db2 {main unlocked temp closed file2 unlocked} 197 198do_test attach2-4.3 { 199 # The read lock held by db does not prevent db2 from reading test.db 200 execsql {SELECT * FROM t1} db2 201} {} 202 203lock_status 4.3.1 db {main shared temp closed file2 unlocked} 204lock_status 4.3.2 db2 {main unlocked temp closed file2 unlocked} 205 206do_test attach2-4.4 { 207 # db is holding a read lock on test.db, so we should not be able 208 # to commit a write to test.db from db2 209 catchsql { 210 INSERT INTO t1 VALUES(1, 2) 211 } db2 212} {1 {database is locked}} 213 214lock_status 4.4.1 db {main shared temp closed file2 unlocked} 215lock_status 4.4.2 db2 {main unlocked temp closed file2 unlocked} 216 217do_test attach2-4.5 { 218 # Handle 'db2' reserves file2. 219 execsql {BEGIN} db2 220 execsql {INSERT INTO file2.t1 VALUES(1, 2)} db2 221 # Lock status: 222 # db - shared(main) 223 # db2 - reserved(file2) 224} {} 225 226lock_status 4.5.1 db {main shared temp closed file2 unlocked} 227lock_status 4.5.2 db2 {main unlocked temp closed file2 reserved} 228 229do_test attach2-4.6.1 { 230 # Reads are allowed against a reserved database. 231 catchsql { 232 SELECT * FROM file2.t1; 233 } 234 # Lock status: 235 # db - shared(main), shared(file2) 236 # db2 - reserved(file2) 237} {0 {}} 238 239lock_status 4.6.1.1 db {main shared temp closed file2 shared} 240lock_status 4.6.1.2 db2 {main unlocked temp closed file2 reserved} 241 242do_test attach2-4.6.2 { 243 # Writes against a reserved database are not allowed. 244 catchsql { 245 UPDATE file2.t1 SET a=0; 246 } 247} {1 {database is locked}} 248 249lock_status 4.6.2.1 db {main shared temp closed file2 shared} 250lock_status 4.6.2.2 db2 {main unlocked temp closed file2 reserved} 251 252do_test attach2-4.7 { 253 # Ensure handle 'db' retains the lock on the main file after 254 # failing to obtain a write-lock on file2. 255 catchsql { 256 INSERT INTO t1 VALUES(1, 2) 257 } db2 258} {0 {}} 259 260lock_status 4.7.1 db {main shared temp closed file2 shared} 261lock_status 4.7.2 db2 {main reserved temp closed file2 reserved} 262 263do_test attach2-4.8 { 264 # We should still be able to read test.db from db2 265 execsql {SELECT * FROM t1} db2 266} {1 2} 267 268lock_status 4.8.1 db {main shared temp closed file2 shared} 269lock_status 4.8.2 db2 {main reserved temp closed file2 reserved} 270 271do_test attach2-4.9 { 272 # Try to upgrade the handle 'db' lock. 273 catchsql { 274 INSERT INTO t1 VALUES(1, 2) 275 } 276} {1 {database is locked}} 277 278lock_status 4.9.1 db {main shared temp closed file2 shared} 279lock_status 4.9.2 db2 {main reserved temp closed file2 reserved} 280 281do_test attach2-4.10 { 282 # We cannot commit db2 while db is holding a read-lock 283 catchsql {COMMIT} db2 284} {1 {database is locked}} 285 286lock_status 4.10.1 db {main shared temp closed file2 shared} 287lock_status 4.10.2 db2 {main pending temp closed file2 reserved} 288 289set sqlite_os_trace 0 290do_test attach2-4.11 { 291 # db is able to commit. 292 catchsql {COMMIT} 293} {0 {}} 294 295lock_status 4.11.1 db {main unlocked temp closed file2 unlocked} 296lock_status 4.11.2 db2 {main pending temp closed file2 reserved} 297 298do_test attach2-4.12 { 299 # Now we can commit db2 300 catchsql {COMMIT} db2 301} {0 {}} 302 303lock_status 4.12.1 db {main unlocked temp closed file2 unlocked} 304lock_status 4.12.2 db2 {main unlocked temp closed file2 unlocked} 305 306do_test attach2-4.13 { 307 execsql {SELECT * FROM file2.t1} 308} {1 2} 309do_test attach2-4.14 { 310 execsql {INSERT INTO t1 VALUES(1, 2)} 311} {} 312do_test attach2-4.15 { 313 execsql {SELECT * FROM t1} db2 314} {1 2 1 2} 315 316db close 317db2 close 318file delete -force test2.db 319 320# These tests - attach2-5.* - check that the master journal file is deleted 321# correctly when a multi-file transaction is committed or rolled back. 322# 323# Update: It's not actually created if a rollback occurs, so that test 324# doesn't really prove too much. 325foreach f [glob test.db*] {file delete -force $f} 326do_test attach2-5.1 { 327 sqlite3 db test.db 328 execsql { 329 ATTACH 'test.db2' AS aux; 330 } 331} {} 332do_test attach2-5.2 { 333 execsql { 334 BEGIN; 335 CREATE TABLE tbl(a, b, c); 336 CREATE TABLE aux.tbl(a, b, c); 337 COMMIT; 338 } 339} {} 340do_test attach2-5.3 { 341 lsort [glob test.db*] 342} {test.db test.db2} 343do_test attach2-5.4 { 344 execsql { 345 BEGIN; 346 DROP TABLE aux.tbl; 347 DROP TABLE tbl; 348 ROLLBACK; 349 } 350} {} 351do_test attach2-5.5 { 352 lsort [glob test.db*] 353} {test.db test.db2} 354 355# Check that a database cannot be ATTACHed or DETACHed during a transaction. 356do_test attach2-6.1 { 357 execsql { 358 BEGIN; 359 } 360} {} 361do_test attach2-6.2 { 362 catchsql { 363 ATTACH 'test3.db' as aux2; 364 } 365} {1 {cannot ATTACH database within transaction}} 366 367do_test attach2-6.3 { 368 catchsql { 369 DETACH aux; 370 } 371} {1 {cannot DETACH database within transaction}} 372do_test attach2-6.4 { 373 execsql { 374 COMMIT; 375 DETACH aux; 376 } 377} {} 378 379db close 380 381finish_test 382