3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
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.
15 # $Id: attach2.test,v 1.38 2007/12/13 21:54:11 drh Exp $
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
20 set testprefix attach2
29 # Databases test.db and test2.db contain identical schemas. Make
30 # sure we can attach test2.db from test.db.
35 CREATE INDEX x1 ON t1(a);
38 forcedelete test2.db-journal
42 CREATE INDEX x1 ON t1(a);
45 ATTACH 'test2.db' AS t2;
53 foreach {idx name file} [execsql {PRAGMA database_list} $db] {
54 lappend list $idx $name
60 # lock test2.db then try to attach it. This is no longer an error because
61 # db2 just RESERVES the database. It does not obtain a write-lock until
64 db2 eval {UPDATE t1 SET a = 0 WHERE 0}
66 ATTACH 'test2.db' AS t2;
69 ifcapable schema_pragmas {
71 # make sure test2.db did get attached.
74 } ;# ifcapable schema_pragmas
78 # Make sure we can read test2.db from db
80 SELECT name FROM t2.sqlite_master;
84 # lock test2.db and try to read from it. This should still work because
85 # the lock is only a RESERVED lock which does not prevent reading.
88 db2 eval {UPDATE t1 SET a = 0 WHERE 0}
90 SELECT name FROM t2.sqlite_master;
94 # but we can still read from test1.db even though test2.db is locked.
96 SELECT name FROM main.sqlite_master;
100 # start a transaction on test.db even though test2.db is locked.
103 INSERT INTO t1 VALUES(8,9);
106 do_test attach2-2.9 {
111 do_test attach2-2.10 {
112 # now try to write to test2.db. the write should fail
114 INSERT INTO t2.t1 VALUES(1,2);
116 } {1 {database is locked}}
117 do_test attach2-2.11 {
118 # when the write failed in the previous test, the transaction should
121 # Update for version 3: A transaction is no longer rolled back if a
122 # database is found to be busy.
129 do_test attach2-2.12 {
133 } {1 {cannot commit - no transaction is active}}
135 # Ticket #574: Make sure it works using the non-callback API
137 do_test attach2-3.1 {
138 set DB [sqlite3_connection_pointer db]
139 set rc [catch {sqlite3_prepare $DB "ATTACH 'test2.db' AS t2" -1 TAIL} VM]
140 if {$rc} {lappend rc $VM}
145 do_test attach2-3.2 {
146 set rc [catch {sqlite3_prepare $DB "DETACH t2" -1 TAIL} VM]
147 if {$rc} {lappend rc $VM}
154 for {set i 2} {$i<=15} {incr i} {
158 # A procedure to verify the status of locks on a database.
160 proc lock_status {testnum db expected_result} {
161 # If the database was compiled with OMIT_TEMPDB set, then
162 # the lock_status list will not contain an entry for the temp
163 # db. But the test code doesn't know this, so its easiest
164 # to filter it out of the $expected_result list here.
166 set expected_result [concat \
167 [lrange $expected_result 0 1] \
168 [lrange $expected_result 4 end] \
171 do_test attach2-$testnum [subst {
172 $db cache flush ;# The lock_status pragma should not be cached
173 execsql {PRAGMA lock_status} $db
176 set sqlite_os_trace 0
178 # Tests attach2-4.* test that read-locks work correctly with attached
180 do_test attach2-4.1 {
183 execsql {ATTACH 'test2.db' as file2}
184 execsql {ATTACH 'test2.db' as file2} db2
187 lock_status 4.1.1 db {main unlocked temp closed file2 unlocked}
188 lock_status 4.1.2 db2 {main unlocked temp closed file2 unlocked}
190 do_test attach2-4.2 {
191 # Handle 'db' read-locks test.db
193 execsql {SELECT * FROM t1}
199 lock_status 4.2.1 db {main shared temp closed file2 unlocked}
200 lock_status 4.2.2 db2 {main unlocked temp closed file2 unlocked}
202 do_test attach2-4.3 {
203 # The read lock held by db does not prevent db2 from reading test.db
204 execsql {SELECT * FROM t1} db2
207 lock_status 4.3.1 db {main shared temp closed file2 unlocked}
208 lock_status 4.3.2 db2 {main unlocked temp closed file2 unlocked}
210 do_test attach2-4.4 {
211 # db is holding a read lock on test.db, so we should not be able
212 # to commit a write to test.db from db2
214 INSERT INTO t1 VALUES(1, 2)
216 } {1 {database is locked}}
218 lock_status 4.4.1 db {main shared temp closed file2 unlocked}
219 lock_status 4.4.2 db2 {main unlocked temp closed file2 unlocked}
221 # We have to make sure that the cache_size and the soft_heap_limit
222 # are large enough to hold the entire change in memory. If either
223 # is set too small, then changes will spill to the database, forcing
224 # a reserved lock to promote to exclusive. That will mess up our
227 set soft_limit [sqlite3_soft_heap_limit 0]
230 do_test attach2-4.5 {
231 # Handle 'db2' reserves file2.
233 execsql {INSERT INTO file2.t1 VALUES(1, 2)} db2
236 # db2 - reserved(file2)
239 lock_status 4.5.1 db {main shared temp closed file2 unlocked}
240 lock_status 4.5.2 db2 {main unlocked temp closed file2 reserved}
242 do_test attach2-4.6.1 {
243 # Reads are allowed against a reserved database.
245 SELECT * FROM file2.t1;
248 # db - shared(main), shared(file2)
249 # db2 - reserved(file2)
252 lock_status 4.6.1.1 db {main shared temp closed file2 shared}
253 lock_status 4.6.1.2 db2 {main unlocked temp closed file2 reserved}
255 do_test attach2-4.6.2 {
256 # Writes against a reserved database are not allowed.
258 UPDATE file2.t1 SET a=0;
260 } {1 {database is locked}}
262 lock_status 4.6.2.1 db {main shared temp closed file2 shared}
263 lock_status 4.6.2.2 db2 {main unlocked temp closed file2 reserved}
265 do_test attach2-4.7 {
266 # Ensure handle 'db' retains the lock on the main file after
267 # failing to obtain a write-lock on file2.
269 INSERT INTO t1 VALUES(1, 2)
273 lock_status 4.7.1 db {main shared temp closed file2 shared}
274 lock_status 4.7.2 db2 {main reserved temp closed file2 reserved}
276 do_test attach2-4.8 {
277 # We should still be able to read test.db from db2
278 execsql {SELECT * FROM t1} db2
281 lock_status 4.8.1 db {main shared temp closed file2 shared}
282 lock_status 4.8.2 db2 {main reserved temp closed file2 reserved}
284 do_test attach2-4.9 {
285 # Try to upgrade the handle 'db' lock.
287 INSERT INTO t1 VALUES(1, 2)
289 } {1 {database is locked}}
291 lock_status 4.9.1 db {main shared temp closed file2 shared}
292 lock_status 4.9.2 db2 {main reserved temp closed file2 reserved}
294 do_test attach2-4.10 {
295 # We cannot commit db2 while db is holding a read-lock
296 catchsql {COMMIT} db2
297 } {1 {database is locked}}
299 lock_status 4.10.1 db {main shared temp closed file2 shared}
300 lock_status 4.10.2 db2 {main pending temp closed file2 reserved}
302 set sqlite_os_trace 0
303 do_test attach2-4.11 {
304 # db is able to commit.
308 lock_status 4.11.1 db {main unlocked temp closed file2 unlocked}
309 lock_status 4.11.2 db2 {main pending temp closed file2 reserved}
311 do_test attach2-4.12 {
312 # Now we can commit db2
313 catchsql {COMMIT} db2
316 lock_status 4.12.1 db {main unlocked temp closed file2 unlocked}
317 lock_status 4.12.2 db2 {main unlocked temp closed file2 unlocked}
319 do_test attach2-4.13 {
320 execsql {SELECT * FROM file2.t1}
322 do_test attach2-4.14 {
323 execsql {INSERT INTO t1 VALUES(1, 2)}
325 do_test attach2-4.15 {
326 execsql {SELECT * FROM t1} db2
332 sqlite3_soft_heap_limit $soft_limit
334 # These tests - attach2-5.* - check that the master journal file is deleted
335 # correctly when a multi-file transaction is committed or rolled back.
337 # Update: It's not actually created if a rollback occurs, so that test
338 # doesn't really prove too much.
339 foreach f [glob test.db*] {forcedelete $f}
340 do_test attach2-5.1 {
343 ATTACH 'test.db2' AS aux;
346 do_test attach2-5.2 {
349 CREATE TABLE tbl(a, b, c);
350 CREATE TABLE aux.tbl(a, b, c);
354 do_test attach2-5.3 {
355 lsort [glob test.db*]
357 do_test attach2-5.4 {
365 do_test attach2-5.5 {
366 lsort [glob test.db*]
369 # Check that a database cannot be ATTACHed or DETACHed during a transaction.
370 do_test attach2-6.1 {
375 do_test attach2-6.2 {
377 ATTACH 'test3.db' as aux2;
382 # As of version 3.21.0: it is ok to DETACH from within a transaction
384 do_test attach2-6.3 {
393 forcedelete test.db2 ;# utf-16
394 forcedelete test.db3 ;# utf-16
395 forcedelete test.db4 ;# utf-8
398 do_execsql_test -db db2 1.1 {
399 PRAGMA encoding = 'utf16';
401 INSERT INTO t2 VALUES('text2');
406 do_execsql_test -db db3 1.2 {
407 PRAGMA encoding = 'utf16';
409 INSERT INTO t3 VALUES('text3');
414 do_execsql_test -db db4 1.3 {
415 PRAGMA encoding = 'utf8';
417 INSERT INTO t4 VALUES('text4');
422 do_execsql_test 2.1 {
423 PRAGMA encoding = 'utf16';
424 ATTACH 'test.db2' AS aux;
429 do_execsql_test 2.2 {
430 ATTACH 'test.db4' AS aux;
436 do_execsql_test 2.3 {
437 ATTACH 'test.db3' AS aux;
444 do_catchsql_test 2.4 {
445 ATTACH 'test.db4' AS aux;
446 } {1 {attached databases must use the same text encoding as main database}}