xref: /sqlite-3.40.0/test/attach2.test (revision b771228e)
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.36 2007/08/10 19:46:14 drh 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  set DB [sqlite3_connection_pointer db]
133  set rc [catch {sqlite3_prepare $DB "ATTACH 'test2.db' AS t2" -1 TAIL} VM]
134  if {$rc} {lappend rc $VM}
135  sqlite3_step $VM
136  sqlite3_finalize $VM
137  set rc
138} {0}
139do_test attach2-3.2 {
140  set rc [catch {sqlite3_prepare $DB "DETACH t2" -1 TAIL} VM]
141  if {$rc} {lappend rc $VM}
142  sqlite3_step $VM
143  sqlite3_finalize $VM
144  set rc
145} {0}
146
147db close
148for {set i 2} {$i<=15} {incr i} {
149  catch {db$i close}
150}
151
152# A procedure to verify the status of locks on a database.
153#
154proc lock_status {testnum db expected_result} {
155  # If the database was compiled with OMIT_TEMPDB set, then
156  # the lock_status list will not contain an entry for the temp
157  # db. But the test code doesn't know this, so it's easiest
158  # to filter it out of the $expected_result list here.
159  ifcapable !tempdb {
160    set expected_result [concat \
161        [lrange $expected_result 0 1] \
162        [lrange $expected_result 4 end] \
163    ]
164  }
165  do_test attach2-$testnum [subst {
166    $db cache flush  ;# The lock_status pragma should not be cached
167    execsql {PRAGMA lock_status} $db
168  }] $expected_result
169}
170set sqlite_os_trace 0
171
172# Tests attach2-4.* test that read-locks work correctly with attached
173# databases.
174do_test attach2-4.1 {
175  sqlite3 db test.db
176  sqlite3 db2 test.db
177  execsql {ATTACH 'test2.db' as file2}
178  execsql {ATTACH 'test2.db' as file2} db2
179} {}
180
181lock_status 4.1.1 db {main unlocked temp closed file2 unlocked}
182lock_status 4.1.2 db2 {main unlocked temp closed file2 unlocked}
183
184do_test attach2-4.2 {
185  # Handle 'db' read-locks test.db
186  execsql {BEGIN}
187  execsql {SELECT * FROM t1}
188  # Lock status:
189  #    db  - shared(main)
190  #    db2 -
191} {}
192
193lock_status 4.2.1 db {main shared temp closed file2 unlocked}
194lock_status 4.2.2 db2 {main unlocked temp closed file2 unlocked}
195
196do_test attach2-4.3 {
197  # The read lock held by db does not prevent db2 from reading test.db
198  execsql {SELECT * FROM t1} db2
199} {}
200
201lock_status 4.3.1 db {main shared temp closed file2 unlocked}
202lock_status 4.3.2 db2 {main unlocked temp closed file2 unlocked}
203
204do_test attach2-4.4 {
205  # db is holding a read lock on test.db, so we should not be able
206  # to commit a write to test.db from db2
207  catchsql {
208    INSERT INTO t1 VALUES(1, 2)
209  } db2
210} {1 {database is locked}}
211
212lock_status 4.4.1 db {main shared temp closed file2 unlocked}
213lock_status 4.4.2 db2 {main unlocked temp closed file2 unlocked}
214
215# We have to make sure that the cache_size and the soft_heap_limit
216# are large enough to hold the entire change in memory.  If either
217# is set too small, then changes will spill to the database, forcing
218# a reserved lock to promote to exclusive.  That will mess up our
219# test results.
220
221set soft_limit [sqlite3_soft_heap_limit 0]
222
223
224do_test attach2-4.5 {
225  # Handle 'db2' reserves file2.
226  execsql {BEGIN} db2
227  execsql {INSERT INTO file2.t1 VALUES(1, 2)} db2
228  # Lock status:
229  #    db  - shared(main)
230  #    db2 - reserved(file2)
231} {}
232
233lock_status 4.5.1 db {main shared temp closed file2 unlocked}
234lock_status 4.5.2 db2 {main unlocked temp closed file2 reserved}
235
236do_test attach2-4.6.1 {
237  # Reads are allowed against a reserved database.
238  catchsql {
239    SELECT * FROM file2.t1;
240  }
241  # Lock status:
242  #    db  - shared(main), shared(file2)
243  #    db2 - reserved(file2)
244} {0 {}}
245
246lock_status 4.6.1.1 db {main shared temp closed file2 shared}
247lock_status 4.6.1.2 db2 {main unlocked temp closed file2 reserved}
248
249do_test attach2-4.6.2 {
250  # Writes against a reserved database are not allowed.
251  catchsql {
252    UPDATE file2.t1 SET a=0;
253  }
254} {1 {database is locked}}
255
256lock_status 4.6.2.1 db {main shared temp closed file2 shared}
257lock_status 4.6.2.2 db2 {main unlocked temp closed file2 reserved}
258
259do_test attach2-4.7 {
260  # Ensure handle 'db' retains the lock on the main file after
261  # failing to obtain a write-lock on file2.
262  catchsql {
263    INSERT INTO t1 VALUES(1, 2)
264  } db2
265} {0 {}}
266
267lock_status 4.7.1 db {main shared temp closed file2 shared}
268lock_status 4.7.2 db2 {main reserved temp closed file2 reserved}
269
270do_test attach2-4.8 {
271  # We should still be able to read test.db from db2
272  execsql {SELECT * FROM t1} db2
273} {1 2}
274
275lock_status 4.8.1 db {main shared temp closed file2 shared}
276lock_status 4.8.2 db2 {main reserved temp closed file2 reserved}
277
278do_test attach2-4.9 {
279  # Try to upgrade the handle 'db' lock.
280  catchsql {
281    INSERT INTO t1 VALUES(1, 2)
282  }
283} {1 {database is locked}}
284
285lock_status 4.9.1 db {main shared temp closed file2 shared}
286lock_status 4.9.2 db2 {main reserved temp closed file2 reserved}
287
288do_test attach2-4.10 {
289  # We cannot commit db2 while db is holding a read-lock
290  catchsql {COMMIT} db2
291} {1 {database is locked}}
292
293lock_status 4.10.1 db {main shared temp closed file2 shared}
294lock_status 4.10.2 db2 {main pending temp closed file2 reserved}
295
296set sqlite_os_trace 0
297do_test attach2-4.11 {
298  # db is able to commit.
299  catchsql {COMMIT}
300} {0 {}}
301
302lock_status 4.11.1 db {main unlocked temp closed file2 unlocked}
303lock_status 4.11.2 db2 {main pending temp closed file2 reserved}
304
305do_test attach2-4.12 {
306  # Now we can commit db2
307  catchsql {COMMIT} db2
308} {0 {}}
309
310lock_status 4.12.1 db {main unlocked temp closed file2 unlocked}
311lock_status 4.12.2 db2 {main unlocked temp closed file2 unlocked}
312
313do_test attach2-4.13 {
314  execsql {SELECT * FROM file2.t1}
315} {1 2}
316do_test attach2-4.14 {
317  execsql {INSERT INTO t1 VALUES(1, 2)}
318} {}
319do_test attach2-4.15 {
320  execsql {SELECT * FROM t1} db2
321} {1 2 1 2}
322
323db close
324db2 close
325file delete -force test2.db
326sqlite3_soft_heap_limit $soft_limit
327
328# These tests - attach2-5.* - check that the master journal file is deleted
329# correctly when a multi-file transaction is committed or rolled back.
330#
331# Update: It's not actually created if a rollback occurs, so that test
332# doesn't really prove too much.
333foreach f [glob test.db*] {file delete -force $f}
334do_test attach2-5.1 {
335  sqlite3 db test.db
336  execsql {
337    ATTACH 'test.db2' AS aux;
338  }
339} {}
340do_test attach2-5.2 {
341  execsql {
342    BEGIN;
343    CREATE TABLE tbl(a, b, c);
344    CREATE TABLE aux.tbl(a, b, c);
345    COMMIT;
346  }
347} {}
348do_test attach2-5.3 {
349  lsort [glob test.db*]
350} {test.db test.db2}
351do_test attach2-5.4 {
352  execsql {
353    BEGIN;
354    DROP TABLE aux.tbl;
355    DROP TABLE tbl;
356    ROLLBACK;
357  }
358} {}
359do_test attach2-5.5 {
360  lsort [glob test.db*]
361} {test.db test.db2}
362
363# Check that a database cannot be ATTACHed or DETACHed during a transaction.
364do_test attach2-6.1 {
365  execsql {
366    BEGIN;
367  }
368} {}
369do_test attach2-6.2 {
370  catchsql {
371    ATTACH 'test3.db' as aux2;
372  }
373} {1 {cannot ATTACH database within transaction}}
374
375do_test attach2-6.3 {
376  catchsql {
377    DETACH aux;
378  }
379} {1 {cannot DETACH database within transaction}}
380do_test attach2-6.4 {
381  execsql {
382    COMMIT;
383    DETACH aux;
384  }
385} {}
386
387db close
388
389finish_test
390