sl@0: # 2003 July 1 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. The sl@0: # focus of this script is testing the ATTACH and DETACH commands sl@0: # and related functionality. sl@0: # sl@0: # $Id: attach2.test,v 1.38 2007/12/13 21:54:11 drh Exp $ sl@0: # sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: ifcapable !attach { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: # Ticket #354 sl@0: # sl@0: # Databases test.db and test2.db contain identical schemas. Make sl@0: # sure we can attach test2.db from test.db. sl@0: # sl@0: do_test attach2-1.1 { sl@0: db eval { sl@0: CREATE TABLE t1(a,b); sl@0: CREATE INDEX x1 ON t1(a); sl@0: } sl@0: file delete -force test2.db sl@0: file delete -force test2.db-journal sl@0: sqlite3 db2 test2.db sl@0: db2 eval { sl@0: CREATE TABLE t1(a,b); sl@0: CREATE INDEX x1 ON t1(a); sl@0: } sl@0: catchsql { sl@0: ATTACH 'test2.db' AS t2; sl@0: } sl@0: } {0 {}} sl@0: sl@0: # Ticket #514 sl@0: # sl@0: proc db_list {db} { sl@0: set list {} sl@0: foreach {idx name file} [execsql {PRAGMA database_list} $db] { sl@0: lappend list $idx $name sl@0: } sl@0: return $list sl@0: } sl@0: db eval {DETACH t2} sl@0: do_test attach2-2.1 { sl@0: # lock test2.db then try to attach it. This is no longer an error because sl@0: # db2 just RESERVES the database. It does not obtain a write-lock until sl@0: # we COMMIT. sl@0: db2 eval {BEGIN} sl@0: db2 eval {UPDATE t1 SET a = 0 WHERE 0} sl@0: catchsql { sl@0: ATTACH 'test2.db' AS t2; sl@0: } sl@0: } {0 {}} sl@0: ifcapable schema_pragmas { sl@0: do_test attach2-2.2 { sl@0: # make sure test2.db did get attached. sl@0: db_list db sl@0: } {0 main 2 t2} sl@0: } ;# ifcapable schema_pragmas sl@0: db2 eval {COMMIT} sl@0: sl@0: do_test attach2-2.5 { sl@0: # Make sure we can read test2.db from db sl@0: catchsql { sl@0: SELECT name FROM t2.sqlite_master; sl@0: } sl@0: } {0 {t1 x1}} sl@0: do_test attach2-2.6 { sl@0: # lock test2.db and try to read from it. This should still work because sl@0: # the lock is only a RESERVED lock which does not prevent reading. sl@0: # sl@0: db2 eval BEGIN sl@0: db2 eval {UPDATE t1 SET a = 0 WHERE 0} sl@0: catchsql { sl@0: SELECT name FROM t2.sqlite_master; sl@0: } sl@0: } {0 {t1 x1}} sl@0: do_test attach2-2.7 { sl@0: # but we can still read from test1.db even though test2.db is locked. sl@0: catchsql { sl@0: SELECT name FROM main.sqlite_master; sl@0: } sl@0: } {0 {t1 x1}} sl@0: do_test attach2-2.8 { sl@0: # start a transaction on test.db even though test2.db is locked. sl@0: catchsql { sl@0: BEGIN; sl@0: INSERT INTO t1 VALUES(8,9); sl@0: } sl@0: } {0 {}} sl@0: do_test attach2-2.9 { sl@0: execsql { sl@0: SELECT * FROM t1 sl@0: } sl@0: } {8 9} sl@0: do_test attach2-2.10 { sl@0: # now try to write to test2.db. the write should fail sl@0: catchsql { sl@0: INSERT INTO t2.t1 VALUES(1,2); sl@0: } sl@0: } {1 {database is locked}} sl@0: do_test attach2-2.11 { sl@0: # when the write failed in the previous test, the transaction should sl@0: # have rolled back. sl@0: # sl@0: # Update for version 3: A transaction is no longer rolled back if a sl@0: # database is found to be busy. sl@0: execsql {rollback} sl@0: db2 eval ROLLBACK sl@0: execsql { sl@0: SELECT * FROM t1 sl@0: } sl@0: } {} sl@0: do_test attach2-2.12 { sl@0: catchsql { sl@0: COMMIT sl@0: } sl@0: } {1 {cannot commit - no transaction is active}} sl@0: sl@0: # Ticket #574: Make sure it works using the non-callback API sl@0: # sl@0: do_test attach2-3.1 { sl@0: set DB [sqlite3_connection_pointer db] sl@0: set rc [catch {sqlite3_prepare $DB "ATTACH 'test2.db' AS t2" -1 TAIL} VM] sl@0: if {$rc} {lappend rc $VM} sl@0: sqlite3_step $VM sl@0: sqlite3_finalize $VM sl@0: set rc sl@0: } {0} sl@0: do_test attach2-3.2 { sl@0: set rc [catch {sqlite3_prepare $DB "DETACH t2" -1 TAIL} VM] sl@0: if {$rc} {lappend rc $VM} sl@0: sqlite3_step $VM sl@0: sqlite3_finalize $VM sl@0: set rc sl@0: } {0} sl@0: sl@0: db close sl@0: for {set i 2} {$i<=15} {incr i} { sl@0: catch {db$i close} sl@0: } sl@0: sl@0: # A procedure to verify the status of locks on a database. sl@0: # sl@0: proc lock_status {testnum db expected_result} { sl@0: # If the database was compiled with OMIT_TEMPDB set, then sl@0: # the lock_status list will not contain an entry for the temp sl@0: # db. But the test code doesn't know this, so its easiest sl@0: # to filter it out of the $expected_result list here. sl@0: ifcapable !tempdb { sl@0: set expected_result [concat \ sl@0: [lrange $expected_result 0 1] \ sl@0: [lrange $expected_result 4 end] \ sl@0: ] sl@0: } sl@0: do_test attach2-$testnum [subst { sl@0: $db cache flush ;# The lock_status pragma should not be cached sl@0: execsql {PRAGMA lock_status} $db sl@0: }] $expected_result sl@0: } sl@0: set sqlite_os_trace 0 sl@0: sl@0: # Tests attach2-4.* test that read-locks work correctly with attached sl@0: # databases. sl@0: do_test attach2-4.1 { sl@0: sqlite3 db test.db sl@0: sqlite3 db2 test.db sl@0: execsql {ATTACH 'test2.db' as file2} sl@0: execsql {ATTACH 'test2.db' as file2} db2 sl@0: } {} sl@0: sl@0: lock_status 4.1.1 db {main unlocked temp closed file2 unlocked} sl@0: lock_status 4.1.2 db2 {main unlocked temp closed file2 unlocked} sl@0: sl@0: do_test attach2-4.2 { sl@0: # Handle 'db' read-locks test.db sl@0: execsql {BEGIN} sl@0: execsql {SELECT * FROM t1} sl@0: # Lock status: sl@0: # db - shared(main) sl@0: # db2 - sl@0: } {} sl@0: sl@0: lock_status 4.2.1 db {main shared temp closed file2 unlocked} sl@0: lock_status 4.2.2 db2 {main unlocked temp closed file2 unlocked} sl@0: sl@0: do_test attach2-4.3 { sl@0: # The read lock held by db does not prevent db2 from reading test.db sl@0: execsql {SELECT * FROM t1} db2 sl@0: } {} sl@0: sl@0: lock_status 4.3.1 db {main shared temp closed file2 unlocked} sl@0: lock_status 4.3.2 db2 {main unlocked temp closed file2 unlocked} sl@0: sl@0: do_test attach2-4.4 { sl@0: # db is holding a read lock on test.db, so we should not be able sl@0: # to commit a write to test.db from db2 sl@0: catchsql { sl@0: INSERT INTO t1 VALUES(1, 2) sl@0: } db2 sl@0: } {1 {database is locked}} sl@0: sl@0: lock_status 4.4.1 db {main shared temp closed file2 unlocked} sl@0: lock_status 4.4.2 db2 {main unlocked temp closed file2 unlocked} sl@0: sl@0: # We have to make sure that the cache_size and the soft_heap_limit sl@0: # are large enough to hold the entire change in memory. If either sl@0: # is set too small, then changes will spill to the database, forcing sl@0: # a reserved lock to promote to exclusive. That will mess up our sl@0: # test results. sl@0: sl@0: set soft_limit [sqlite3_soft_heap_limit 0] sl@0: sl@0: sl@0: do_test attach2-4.5 { sl@0: # Handle 'db2' reserves file2. sl@0: execsql {BEGIN} db2 sl@0: execsql {INSERT INTO file2.t1 VALUES(1, 2)} db2 sl@0: # Lock status: sl@0: # db - shared(main) sl@0: # db2 - reserved(file2) sl@0: } {} sl@0: sl@0: lock_status 4.5.1 db {main shared temp closed file2 unlocked} sl@0: lock_status 4.5.2 db2 {main unlocked temp closed file2 reserved} sl@0: sl@0: do_test attach2-4.6.1 { sl@0: # Reads are allowed against a reserved database. sl@0: catchsql { sl@0: SELECT * FROM file2.t1; sl@0: } sl@0: # Lock status: sl@0: # db - shared(main), shared(file2) sl@0: # db2 - reserved(file2) sl@0: } {0 {}} sl@0: sl@0: lock_status 4.6.1.1 db {main shared temp closed file2 shared} sl@0: lock_status 4.6.1.2 db2 {main unlocked temp closed file2 reserved} sl@0: sl@0: do_test attach2-4.6.2 { sl@0: # Writes against a reserved database are not allowed. sl@0: catchsql { sl@0: UPDATE file2.t1 SET a=0; sl@0: } sl@0: } {1 {database is locked}} sl@0: sl@0: lock_status 4.6.2.1 db {main shared temp closed file2 shared} sl@0: lock_status 4.6.2.2 db2 {main unlocked temp closed file2 reserved} sl@0: sl@0: do_test attach2-4.7 { sl@0: # Ensure handle 'db' retains the lock on the main file after sl@0: # failing to obtain a write-lock on file2. sl@0: catchsql { sl@0: INSERT INTO t1 VALUES(1, 2) sl@0: } db2 sl@0: } {0 {}} sl@0: sl@0: lock_status 4.7.1 db {main shared temp closed file2 shared} sl@0: lock_status 4.7.2 db2 {main reserved temp closed file2 reserved} sl@0: sl@0: do_test attach2-4.8 { sl@0: # We should still be able to read test.db from db2 sl@0: execsql {SELECT * FROM t1} db2 sl@0: } {1 2} sl@0: sl@0: lock_status 4.8.1 db {main shared temp closed file2 shared} sl@0: lock_status 4.8.2 db2 {main reserved temp closed file2 reserved} sl@0: sl@0: do_test attach2-4.9 { sl@0: # Try to upgrade the handle 'db' lock. sl@0: catchsql { sl@0: INSERT INTO t1 VALUES(1, 2) sl@0: } sl@0: } {1 {database is locked}} sl@0: sl@0: lock_status 4.9.1 db {main shared temp closed file2 shared} sl@0: lock_status 4.9.2 db2 {main reserved temp closed file2 reserved} sl@0: sl@0: do_test attach2-4.10 { sl@0: # We cannot commit db2 while db is holding a read-lock sl@0: catchsql {COMMIT} db2 sl@0: } {1 {database is locked}} sl@0: sl@0: lock_status 4.10.1 db {main shared temp closed file2 shared} sl@0: lock_status 4.10.2 db2 {main pending temp closed file2 reserved} sl@0: sl@0: set sqlite_os_trace 0 sl@0: do_test attach2-4.11 { sl@0: # db is able to commit. sl@0: catchsql {COMMIT} sl@0: } {0 {}} sl@0: sl@0: lock_status 4.11.1 db {main unlocked temp closed file2 unlocked} sl@0: lock_status 4.11.2 db2 {main pending temp closed file2 reserved} sl@0: sl@0: do_test attach2-4.12 { sl@0: # Now we can commit db2 sl@0: catchsql {COMMIT} db2 sl@0: } {0 {}} sl@0: sl@0: lock_status 4.12.1 db {main unlocked temp closed file2 unlocked} sl@0: lock_status 4.12.2 db2 {main unlocked temp closed file2 unlocked} sl@0: sl@0: do_test attach2-4.13 { sl@0: execsql {SELECT * FROM file2.t1} sl@0: } {1 2} sl@0: do_test attach2-4.14 { sl@0: execsql {INSERT INTO t1 VALUES(1, 2)} sl@0: } {} sl@0: do_test attach2-4.15 { sl@0: execsql {SELECT * FROM t1} db2 sl@0: } {1 2 1 2} sl@0: sl@0: db close sl@0: db2 close sl@0: file delete -force test2.db sl@0: sqlite3_soft_heap_limit $soft_limit sl@0: sl@0: # These tests - attach2-5.* - check that the master journal file is deleted sl@0: # correctly when a multi-file transaction is committed or rolled back. sl@0: # sl@0: # Update: It's not actually created if a rollback occurs, so that test sl@0: # doesn't really prove too much. sl@0: foreach f [glob test.db*] {file delete -force $f} sl@0: do_test attach2-5.1 { sl@0: sqlite3 db test.db sl@0: execsql { sl@0: ATTACH 'test.db2' AS aux; sl@0: } sl@0: } {} sl@0: do_test attach2-5.2 { sl@0: execsql { sl@0: BEGIN; sl@0: CREATE TABLE tbl(a, b, c); sl@0: CREATE TABLE aux.tbl(a, b, c); sl@0: COMMIT; sl@0: } sl@0: } {} sl@0: do_test attach2-5.3 { sl@0: lsort [glob test.db*] sl@0: } {test.db test.db2} sl@0: do_test attach2-5.4 { sl@0: execsql { sl@0: BEGIN; sl@0: DROP TABLE aux.tbl; sl@0: DROP TABLE tbl; sl@0: ROLLBACK; sl@0: } sl@0: } {} sl@0: do_test attach2-5.5 { sl@0: lsort [glob test.db*] sl@0: } {test.db test.db2} sl@0: sl@0: # Check that a database cannot be ATTACHed or DETACHed during a transaction. sl@0: do_test attach2-6.1 { sl@0: execsql { sl@0: BEGIN; sl@0: } sl@0: } {} sl@0: do_test attach2-6.2 { sl@0: catchsql { sl@0: ATTACH 'test3.db' as aux2; sl@0: } sl@0: } {1 {cannot ATTACH database within transaction}} sl@0: sl@0: do_test attach2-6.3 { sl@0: catchsql { sl@0: DETACH aux; sl@0: } sl@0: } {1 {cannot DETACH database within transaction}} sl@0: do_test attach2-6.4 { sl@0: execsql { sl@0: COMMIT; sl@0: DETACH aux; sl@0: } sl@0: } {} sl@0: sl@0: db close sl@0: sl@0: finish_test