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