1
0

attach2.test 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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.38 2007/12/13 21:54:11 drh Exp $
  16. #
  17. set testdir [file dirname $argv0]
  18. source $testdir/tester.tcl
  19. ifcapable !attach {
  20. finish_test
  21. return
  22. }
  23. # Ticket #354
  24. #
  25. # Databases test.db and test2.db contain identical schemas. Make
  26. # sure we can attach test2.db from test.db.
  27. #
  28. do_test attach2-1.1 {
  29. db eval {
  30. CREATE TABLE t1(a,b);
  31. CREATE INDEX x1 ON t1(a);
  32. }
  33. forcedelete test2.db
  34. forcedelete test2.db-journal
  35. sqlite3 db2 test2.db
  36. db2 eval {
  37. CREATE TABLE t1(a,b);
  38. CREATE INDEX x1 ON t1(a);
  39. }
  40. catchsql {
  41. ATTACH 'test2.db' AS t2;
  42. }
  43. } {0 {}}
  44. # Ticket #514
  45. #
  46. proc 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. }
  53. db eval {DETACH t2}
  54. do_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 {}}
  64. ifcapable schema_pragmas {
  65. do_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
  70. db2 eval {COMMIT}
  71. do_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}}
  77. do_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}}
  87. do_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}}
  93. do_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 {}}
  100. do_test attach2-2.9 {
  101. execsql {
  102. SELECT * FROM t1
  103. }
  104. } {8 9}
  105. do_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}}
  111. do_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. } {}
  123. do_test attach2-2.12 {
  124. catchsql {
  125. COMMIT
  126. }
  127. } {1 {cannot commit - no transaction is active}}
  128. # Ticket #574: Make sure it works using the non-callback API
  129. #
  130. do_test attach2-3.1 {
  131. set DB [sqlite3_connection_pointer db]
  132. set rc [catch {sqlite3_prepare $DB "ATTACH 'test2.db' AS t2" -1 TAIL} VM]
  133. if {$rc} {lappend rc $VM}
  134. sqlite3_step $VM
  135. sqlite3_finalize $VM
  136. set rc
  137. } {0}
  138. do_test attach2-3.2 {
  139. set rc [catch {sqlite3_prepare $DB "DETACH t2" -1 TAIL} VM]
  140. if {$rc} {lappend rc $VM}
  141. sqlite3_step $VM
  142. sqlite3_finalize $VM
  143. set rc
  144. } {0}
  145. db close
  146. for {set i 2} {$i<=15} {incr i} {
  147. catch {db$i close}
  148. }
  149. # A procedure to verify the status of locks on a database.
  150. #
  151. proc lock_status {testnum db expected_result} {
  152. # If the database was compiled with OMIT_TEMPDB set, then
  153. # the lock_status list will not contain an entry for the temp
  154. # db. But the test code doesn't know this, so its easiest
  155. # to filter it out of the $expected_result list here.
  156. ifcapable !tempdb {
  157. set expected_result [concat \
  158. [lrange $expected_result 0 1] \
  159. [lrange $expected_result 4 end] \
  160. ]
  161. }
  162. do_test attach2-$testnum [subst {
  163. $db cache flush ;# The lock_status pragma should not be cached
  164. execsql {PRAGMA lock_status} $db
  165. }] $expected_result
  166. }
  167. set sqlite_os_trace 0
  168. # Tests attach2-4.* test that read-locks work correctly with attached
  169. # databases.
  170. do_test attach2-4.1 {
  171. sqlite3 db test.db
  172. sqlite3 db2 test.db
  173. execsql {ATTACH 'test2.db' as file2}
  174. execsql {ATTACH 'test2.db' as file2} db2
  175. } {}
  176. lock_status 4.1.1 db {main unlocked temp closed file2 unlocked}
  177. lock_status 4.1.2 db2 {main unlocked temp closed file2 unlocked}
  178. do_test attach2-4.2 {
  179. # Handle 'db' read-locks test.db
  180. execsql {BEGIN}
  181. execsql {SELECT * FROM t1}
  182. # Lock status:
  183. # db - shared(main)
  184. # db2 -
  185. } {}
  186. lock_status 4.2.1 db {main shared temp closed file2 unlocked}
  187. lock_status 4.2.2 db2 {main unlocked temp closed file2 unlocked}
  188. do_test attach2-4.3 {
  189. # The read lock held by db does not prevent db2 from reading test.db
  190. execsql {SELECT * FROM t1} db2
  191. } {}
  192. lock_status 4.3.1 db {main shared temp closed file2 unlocked}
  193. lock_status 4.3.2 db2 {main unlocked temp closed file2 unlocked}
  194. do_test attach2-4.4 {
  195. # db is holding a read lock on test.db, so we should not be able
  196. # to commit a write to test.db from db2
  197. catchsql {
  198. INSERT INTO t1 VALUES(1, 2)
  199. } db2
  200. } {1 {database is locked}}
  201. lock_status 4.4.1 db {main shared temp closed file2 unlocked}
  202. lock_status 4.4.2 db2 {main unlocked temp closed file2 unlocked}
  203. # We have to make sure that the cache_size and the soft_heap_limit
  204. # are large enough to hold the entire change in memory. If either
  205. # is set too small, then changes will spill to the database, forcing
  206. # a reserved lock to promote to exclusive. That will mess up our
  207. # test results.
  208. set soft_limit [sqlite3_soft_heap_limit 0]
  209. do_test attach2-4.5 {
  210. # Handle 'db2' reserves file2.
  211. execsql {BEGIN} db2
  212. execsql {INSERT INTO file2.t1 VALUES(1, 2)} db2
  213. # Lock status:
  214. # db - shared(main)
  215. # db2 - reserved(file2)
  216. } {}
  217. lock_status 4.5.1 db {main shared temp closed file2 unlocked}
  218. lock_status 4.5.2 db2 {main unlocked temp closed file2 reserved}
  219. do_test attach2-4.6.1 {
  220. # Reads are allowed against a reserved database.
  221. catchsql {
  222. SELECT * FROM file2.t1;
  223. }
  224. # Lock status:
  225. # db - shared(main), shared(file2)
  226. # db2 - reserved(file2)
  227. } {0 {}}
  228. lock_status 4.6.1.1 db {main shared temp closed file2 shared}
  229. lock_status 4.6.1.2 db2 {main unlocked temp closed file2 reserved}
  230. do_test attach2-4.6.2 {
  231. # Writes against a reserved database are not allowed.
  232. catchsql {
  233. UPDATE file2.t1 SET a=0;
  234. }
  235. } {1 {database is locked}}
  236. lock_status 4.6.2.1 db {main shared temp closed file2 shared}
  237. lock_status 4.6.2.2 db2 {main unlocked temp closed file2 reserved}
  238. do_test attach2-4.7 {
  239. # Ensure handle 'db' retains the lock on the main file after
  240. # failing to obtain a write-lock on file2.
  241. catchsql {
  242. INSERT INTO t1 VALUES(1, 2)
  243. } db2
  244. } {0 {}}
  245. lock_status 4.7.1 db {main shared temp closed file2 shared}
  246. lock_status 4.7.2 db2 {main reserved temp closed file2 reserved}
  247. do_test attach2-4.8 {
  248. # We should still be able to read test.db from db2
  249. execsql {SELECT * FROM t1} db2
  250. } {1 2}
  251. lock_status 4.8.1 db {main shared temp closed file2 shared}
  252. lock_status 4.8.2 db2 {main reserved temp closed file2 reserved}
  253. do_test attach2-4.9 {
  254. # Try to upgrade the handle 'db' lock.
  255. catchsql {
  256. INSERT INTO t1 VALUES(1, 2)
  257. }
  258. } {1 {database is locked}}
  259. lock_status 4.9.1 db {main shared temp closed file2 shared}
  260. lock_status 4.9.2 db2 {main reserved temp closed file2 reserved}
  261. do_test attach2-4.10 {
  262. # We cannot commit db2 while db is holding a read-lock
  263. catchsql {COMMIT} db2
  264. } {1 {database is locked}}
  265. lock_status 4.10.1 db {main shared temp closed file2 shared}
  266. lock_status 4.10.2 db2 {main pending temp closed file2 reserved}
  267. set sqlite_os_trace 0
  268. do_test attach2-4.11 {
  269. # db is able to commit.
  270. catchsql {COMMIT}
  271. } {0 {}}
  272. lock_status 4.11.1 db {main unlocked temp closed file2 unlocked}
  273. lock_status 4.11.2 db2 {main pending temp closed file2 reserved}
  274. do_test attach2-4.12 {
  275. # Now we can commit db2
  276. catchsql {COMMIT} db2
  277. } {0 {}}
  278. lock_status 4.12.1 db {main unlocked temp closed file2 unlocked}
  279. lock_status 4.12.2 db2 {main unlocked temp closed file2 unlocked}
  280. do_test attach2-4.13 {
  281. execsql {SELECT * FROM file2.t1}
  282. } {1 2}
  283. do_test attach2-4.14 {
  284. execsql {INSERT INTO t1 VALUES(1, 2)}
  285. } {}
  286. do_test attach2-4.15 {
  287. execsql {SELECT * FROM t1} db2
  288. } {1 2 1 2}
  289. db close
  290. db2 close
  291. forcedelete test2.db
  292. sqlite3_soft_heap_limit $soft_limit
  293. # These tests - attach2-5.* - check that the master journal file is deleted
  294. # correctly when a multi-file transaction is committed or rolled back.
  295. #
  296. # Update: It's not actually created if a rollback occurs, so that test
  297. # doesn't really prove too much.
  298. foreach f [glob test.db*] {forcedelete $f}
  299. do_test attach2-5.1 {
  300. sqlite3 db test.db
  301. execsql {
  302. ATTACH 'test.db2' AS aux;
  303. }
  304. } {}
  305. do_test attach2-5.2 {
  306. execsql {
  307. BEGIN;
  308. CREATE TABLE tbl(a, b, c);
  309. CREATE TABLE aux.tbl(a, b, c);
  310. COMMIT;
  311. }
  312. } {}
  313. do_test attach2-5.3 {
  314. lsort [glob test.db*]
  315. } {test.db test.db2}
  316. do_test attach2-5.4 {
  317. execsql {
  318. BEGIN;
  319. DROP TABLE aux.tbl;
  320. DROP TABLE tbl;
  321. ROLLBACK;
  322. }
  323. } {}
  324. do_test attach2-5.5 {
  325. lsort [glob test.db*]
  326. } {test.db test.db2}
  327. # Check that a database cannot be ATTACHed or DETACHed during a transaction.
  328. do_test attach2-6.1 {
  329. execsql {
  330. BEGIN;
  331. }
  332. } {}
  333. do_test attach2-6.2 {
  334. catchsql {
  335. ATTACH 'test3.db' as aux2;
  336. }
  337. } {1 {cannot ATTACH database within transaction}}
  338. # EVIDENCE-OF: R-59740-55581 This statement will fail if SQLite is in
  339. # the middle of a transaction.
  340. #
  341. do_test attach2-6.3 {
  342. catchsql {
  343. DETACH aux;
  344. }
  345. } {1 {cannot DETACH database within transaction}}
  346. do_test attach2-6.4 {
  347. execsql {
  348. COMMIT;
  349. DETACH aux;
  350. }
  351. } {}
  352. db close
  353. finish_test