danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1 | # 2005 December 30 |
| 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 | #*********************************************************************** |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 11 | # |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame^] | 12 | # $Id: shared.test,v 1.17 2006/01/17 09:35:02 danielk1977 Exp $ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 13 | |
| 14 | set testdir [file dirname $argv0] |
| 15 | source $testdir/tester.tcl |
| 16 | db close |
| 17 | |
| 18 | ifcapable !shared_cache { |
| 19 | finish_test |
| 20 | return |
| 21 | } |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 22 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 23 | set ::enable_shared_cache [sqlite3_enable_shared_cache 1] |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 24 | |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 25 | foreach av [list 0 1] { |
| 26 | |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 27 | # Open the database connection and execute the auto-vacuum pragma |
| 28 | file delete -force test.db |
| 29 | sqlite3 db test.db |
| 30 | |
| 31 | ifcapable autovacuum { |
| 32 | do_test shared-[expr $av+1].1.0 { |
| 33 | execsql "pragma auto_vacuum=$::av" |
| 34 | execsql {pragma auto_vacuum} |
| 35 | } "$av" |
| 36 | } else { |
| 37 | if {$av} { |
| 38 | db close |
| 39 | break |
| 40 | } |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 41 | } |
| 42 | |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 43 | incr av |
| 44 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 45 | # Test organization: |
| 46 | # |
| 47 | # shared-1.*: Simple test to verify basic sanity of table level locking when |
| 48 | # two connections share a pager cache. |
| 49 | # shared-2.*: Test that a read transaction can co-exist with a |
| 50 | # write-transaction, including a simple test to ensure the |
| 51 | # external locking protocol is still working. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 52 | # shared-3.*: Simple test of read-uncommitted mode. |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 53 | # shared-4.*: Check that the schema is locked and unlocked correctly. |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 54 | # shared-5.*: Test that creating/dropping schema items works when databases |
| 55 | # are attached in different orders to different handles. |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 56 | # shared-6.*: Locking, UNION ALL queries and sub-queries. |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 57 | # shared-7.*: Autovacuum and shared-cache. |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 58 | # |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 59 | |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 60 | do_test shared-$av.1.1 { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 61 | # Open a second database on the file test.db. It should use the same pager |
| 62 | # cache and schema as the original connection. Verify that only 1 file is |
| 63 | # opened. |
| 64 | sqlite3 db2 test.db |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 65 | set ::sqlite_open_file_count |
| 66 | } {1} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 67 | do_test shared-$av.1.2 { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 68 | # Add a table and a single row of data via the first connection. |
| 69 | # Ensure that the second connection can see them. |
| 70 | execsql { |
| 71 | CREATE TABLE abc(a, b, c); |
| 72 | INSERT INTO abc VALUES(1, 2, 3); |
| 73 | } db |
| 74 | execsql { |
| 75 | SELECT * FROM abc; |
| 76 | } db2 |
| 77 | } {1 2 3} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 78 | do_test shared-$av.1.3 { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 79 | # Have the first connection begin a transaction and obtain a read-lock |
| 80 | # on table abc. This should not prevent the second connection from |
| 81 | # querying abc. |
| 82 | execsql { |
| 83 | BEGIN; |
| 84 | SELECT * FROM abc; |
| 85 | } |
| 86 | execsql { |
| 87 | SELECT * FROM abc; |
| 88 | } db2 |
| 89 | } {1 2 3} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 90 | do_test shared-$av.1.4 { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 91 | # Try to insert a row into abc via connection 2. This should fail because |
| 92 | # of the read-lock connection 1 is holding on table abc (obtained in the |
| 93 | # previous test case). |
| 94 | catchsql { |
| 95 | INSERT INTO abc VALUES(4, 5, 6); |
| 96 | } db2 |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 97 | } {1 {database table is locked: abc}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 98 | do_test shared-$av.1.5 { |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 99 | # Using connection 2 (the one without the open transaction), try to create |
| 100 | # a new table. This should fail because of the open read transaction |
| 101 | # held by connection 1. |
| 102 | catchsql { |
| 103 | CREATE TABLE def(d, e, f); |
| 104 | } db2 |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 105 | } {1 {database table is locked: sqlite_master}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 106 | do_test shared-$av.1.6 { |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 107 | # Upgrade connection 1's transaction to a write transaction. Create |
| 108 | # a new table - def - and insert a row into it. Because the connection 1 |
| 109 | # transaction modifies the schema, it should not be possible for |
| 110 | # connection 2 to access the database at all until the connection 1 |
| 111 | # has finished the transaction. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 112 | execsql { |
| 113 | CREATE TABLE def(d, e, f); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 114 | INSERT INTO def VALUES('IV', 'V', 'VI'); |
| 115 | } |
| 116 | } {} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 117 | do_test shared-$av.1.7 { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 118 | # Read from the sqlite_master table with connection 1 (inside the |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 119 | # transaction). Then test that we can not do this with connection 2. This |
| 120 | # is because of the schema-modified lock established by connection 1 |
| 121 | # in the previous test case. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 122 | execsql { |
| 123 | SELECT * FROM sqlite_master; |
| 124 | } |
| 125 | catchsql { |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 126 | SELECT * FROM sqlite_master; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 127 | } db2 |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 128 | } {1 {database schema is locked: main}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 129 | do_test shared-$av.1.8 { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 130 | # Commit the connection 1 transaction. |
| 131 | execsql { |
| 132 | COMMIT; |
| 133 | } |
| 134 | } {} |
| 135 | |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 136 | do_test shared-$av.2.1 { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 137 | # Open connection db3 to the database. Use a different path to the same |
| 138 | # file so that db3 does *not* share the same pager cache as db and db2 |
| 139 | # (there should be two open file handles). |
| 140 | sqlite3 db3 ./test.db |
| 141 | set ::sqlite_open_file_count |
| 142 | } {2} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 143 | do_test shared-$av.2.2 { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 144 | # Start read transactions on db and db2 (the shared pager cache). Ensure |
| 145 | # db3 cannot write to the database. |
| 146 | execsql { |
| 147 | BEGIN; |
| 148 | SELECT * FROM abc; |
| 149 | } |
| 150 | execsql { |
| 151 | BEGIN; |
| 152 | SELECT * FROM abc; |
| 153 | } db2 |
| 154 | catchsql { |
| 155 | INSERT INTO abc VALUES(1, 2, 3); |
| 156 | } db2 |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 157 | } {1 {database table is locked: abc}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 158 | do_test shared-$av.2.3 { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 159 | # Turn db's transaction into a write-transaction. db3 should still be |
| 160 | # able to read from table def (but will not see the new row). Connection |
| 161 | # db2 should not be able to read def (because of the write-lock). |
| 162 | |
| 163 | # Todo: The failed "INSERT INTO abc ..." statement in the above test |
| 164 | # has started a write-transaction on db2 (should this be so?). This |
| 165 | # would prevent connection db from starting a write-transaction. So roll the |
| 166 | # db2 transaction back and replace it with a new read transaction. |
| 167 | execsql { |
| 168 | ROLLBACK; |
| 169 | BEGIN; |
| 170 | SELECT * FROM abc; |
| 171 | } db2 |
| 172 | |
| 173 | execsql { |
| 174 | INSERT INTO def VALUES('VII', 'VIII', 'IX'); |
| 175 | } |
| 176 | concat [ |
| 177 | catchsql { SELECT * FROM def; } db3 |
| 178 | ] [ |
| 179 | catchsql { SELECT * FROM def; } db2 |
| 180 | ] |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 181 | } {0 {IV V VI} 1 {database table is locked: def}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 182 | do_test shared-$av.2.4 { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 183 | # Commit the open transaction on db. db2 still holds a read-transaction. |
| 184 | # This should prevent db3 from writing to the database, but not from |
| 185 | # reading. |
| 186 | execsql { |
| 187 | COMMIT; |
| 188 | } |
| 189 | concat [ |
| 190 | catchsql { SELECT * FROM def; } db3 |
| 191 | ] [ |
| 192 | catchsql { INSERT INTO def VALUES('X', 'XI', 'XII'); } db3 |
| 193 | ] |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 194 | } {0 {IV V VI VII VIII IX} 1 {database is locked}} |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 195 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 196 | catchsql COMMIT db2 |
| 197 | |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 198 | do_test shared-$av.3.1.1 { |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 199 | # This test case starts a linear scan of table 'seq' using a |
| 200 | # read-uncommitted connection. In the middle of the scan, rows are added |
| 201 | # to the end of the seq table (ahead of the current cursor position). |
| 202 | # The uncommitted rows should be included in the results of the scan. |
| 203 | execsql " |
| 204 | CREATE TABLE seq(i, x); |
| 205 | INSERT INTO seq VALUES(1, '[string repeat X 500]'); |
| 206 | INSERT INTO seq VALUES(2, '[string repeat X 500]'); |
| 207 | " |
| 208 | execsql {SELECT * FROM sqlite_master} db2 |
| 209 | execsql {PRAGMA read_uncommitted = 1} db2 |
| 210 | |
| 211 | set ret [list] |
| 212 | db2 eval {SELECT i FROM seq} { |
| 213 | if {$i < 4} { |
danielk1977 | 1576cd9 | 2006-01-14 08:02:28 +0000 | [diff] [blame] | 214 | set max [execsql {SELECT max(i) FROM seq}] |
| 215 | db eval { |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame^] | 216 | INSERT INTO seq SELECT i + :max, x FROM seq; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | lappend ret $i |
| 220 | } |
| 221 | set ret |
| 222 | } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 223 | do_test shared-$av.3.1.2 { |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 224 | # Another linear scan through table seq using a read-uncommitted connection. |
| 225 | # This time, delete each row as it is read. Should not affect the results of |
| 226 | # the scan, but the table should be empty after the scan is concluded |
| 227 | # (test 3.1.3 verifies this). |
| 228 | set ret [list] |
| 229 | db2 eval {SELECT i FROM seq} { |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame^] | 230 | db eval {DELETE FROM seq WHERE i = :i} |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 231 | lappend ret $i |
| 232 | } |
| 233 | set ret |
| 234 | } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 235 | do_test shared-$av.3.1.3 { |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 236 | execsql { |
| 237 | SELECT * FROM seq; |
| 238 | } |
| 239 | } {} |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 240 | |
| 241 | catch {db close} |
| 242 | catch {db2 close} |
| 243 | catch {db3 close} |
| 244 | |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 245 | #-------------------------------------------------------------------------- |
| 246 | # Tests shared-4.* test that the schema locking rules are applied |
| 247 | # correctly. i.e.: |
| 248 | # |
| 249 | # 1. All transactions require a read-lock on the schemas of databases they |
| 250 | # access. |
| 251 | # 2. Transactions that modify a database schema require a write-lock on that |
| 252 | # schema. |
| 253 | # 3. It is not possible to compile a statement while another handle has a |
| 254 | # write-lock on the schema. |
| 255 | # |
| 256 | |
| 257 | # Open two database handles db and db2. Each has a single attach database |
| 258 | # (as well as main): |
| 259 | # |
| 260 | # db.main -> ./test.db |
| 261 | # db.test2 -> ./test2.db |
| 262 | # db2.main -> ./test2.db |
| 263 | # db2.test -> ./test.db |
| 264 | # |
| 265 | file delete -force test.db |
| 266 | file delete -force test2.db |
| 267 | file delete -force test2.db-journal |
| 268 | sqlite3 db test.db |
| 269 | sqlite3 db2 test2.db |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 270 | do_test shared-$av.4.1.1 { |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 271 | set sqlite_open_file_count |
| 272 | } {2} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 273 | do_test shared-$av.4.1.2 { |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 274 | execsql {ATTACH 'test2.db' AS test2} |
| 275 | set sqlite_open_file_count |
| 276 | } {2} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 277 | do_test shared-$av.4.1.3 { |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 278 | execsql {ATTACH 'test.db' AS test} db2 |
| 279 | set sqlite_open_file_count |
| 280 | } {2} |
| 281 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 282 | # Sanity check: Create a table in ./test.db via handle db, and test that handle |
| 283 | # db2 can "see" the new table immediately. A handle using a seperate pager |
| 284 | # cache would have to reload the database schema before this were possible. |
| 285 | # |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 286 | do_test shared-$av.4.2.1 { |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 287 | execsql { |
| 288 | CREATE TABLE abc(a, b, c); |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 289 | CREATE TABLE def(d, e, f); |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 290 | INSERT INTO abc VALUES('i', 'ii', 'iii'); |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 291 | INSERT INTO def VALUES('I', 'II', 'III'); |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 292 | } |
| 293 | } {} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 294 | do_test shared-$av.4.2.2 { |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 295 | execsql { |
| 296 | SELECT * FROM test.abc; |
| 297 | } db2 |
| 298 | } {i ii iii} |
| 299 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 300 | # Open a read-transaction and read from table abc via handle 2. Check that |
| 301 | # handle 1 can read table abc. Check that handle 1 cannot modify table abc |
| 302 | # or the database schema. Then check that handle 1 can modify table def. |
| 303 | # |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 304 | do_test shared-$av.4.3.1 { |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 305 | execsql { |
| 306 | BEGIN; |
| 307 | SELECT * FROM test.abc; |
| 308 | } db2 |
| 309 | } {i ii iii} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 310 | do_test shared-$av.4.3.2 { |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 311 | catchsql { |
| 312 | INSERT INTO abc VALUES('iv', 'v', 'vi'); |
| 313 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 314 | } {1 {database table is locked: abc}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 315 | do_test shared-$av.4.3.3 { |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 316 | catchsql { |
| 317 | CREATE TABLE ghi(g, h, i); |
| 318 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 319 | } {1 {database table is locked: sqlite_master}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 320 | do_test shared-$av.4.3.3 { |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 321 | catchsql { |
| 322 | INSERT INTO def VALUES('IV', 'V', 'VI'); |
| 323 | } |
| 324 | } {0 {}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 325 | do_test shared-$av.4.3.4 { |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 326 | # Cleanup: commit the transaction opened by db2. |
| 327 | execsql { |
| 328 | COMMIT |
| 329 | } db2 |
| 330 | } {} |
| 331 | |
| 332 | # Open a write-transaction using handle 1 and modify the database schema. |
| 333 | # Then try to execute a compiled statement to read from the same |
| 334 | # database via handle 2 (fails to get the lock on sqlite_master). Also |
| 335 | # try to compile a read of the same database using handle 2 (also fails). |
| 336 | # Finally, compile a read of the other database using handle 2. This |
| 337 | # should also fail. |
| 338 | # |
danielk1977 | ff89079 | 2006-01-16 16:24:25 +0000 | [diff] [blame] | 339 | ifcapable compound { |
| 340 | do_test shared-$av.4.4.1.2 { |
| 341 | # Sanity check 1: Check that the schema is what we think it is when viewed |
| 342 | # via handle 1. |
| 343 | execsql { |
| 344 | CREATE TABLE test2.ghi(g, h, i); |
| 345 | SELECT 'test.db:'||name FROM sqlite_master |
| 346 | UNION ALL |
| 347 | SELECT 'test2.db:'||name FROM test2.sqlite_master; |
| 348 | } |
| 349 | } {test.db:abc test.db:def test2.db:ghi} |
| 350 | do_test shared-$av.4.4.1.2 { |
| 351 | # Sanity check 2: Check that the schema is what we think it is when viewed |
| 352 | # via handle 2. |
| 353 | execsql { |
| 354 | SELECT 'test2.db:'||name FROM sqlite_master |
| 355 | UNION ALL |
| 356 | SELECT 'test.db:'||name FROM test.sqlite_master; |
| 357 | } db2 |
| 358 | } {test2.db:ghi test.db:abc test.db:def} |
| 359 | } |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 360 | |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 361 | do_test shared-$av.4.4.2 { |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 362 | set ::DB2 [sqlite3_connection_pointer db2] |
| 363 | set sql {SELECT * FROM abc} |
| 364 | set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY] |
| 365 | execsql { |
| 366 | BEGIN; |
| 367 | CREATE TABLE jkl(j, k, l); |
| 368 | } |
| 369 | sqlite3_step $::STMT1 |
| 370 | } {SQLITE_ERROR} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 371 | do_test shared-$av.4.4.3 { |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 372 | sqlite3_finalize $::STMT1 |
| 373 | } {SQLITE_LOCKED} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 374 | do_test shared-$av.4.4.4 { |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 375 | set rc [catch { |
| 376 | set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY] |
| 377 | } msg] |
| 378 | list $rc $msg |
| 379 | } {1 {(6) database schema is locked: test}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 380 | do_test shared-$av.4.4.5 { |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 381 | set rc [catch { |
| 382 | set ::STMT1 [sqlite3_prepare $::DB2 "SELECT * FROM ghi" -1 DUMMY] |
| 383 | } msg] |
| 384 | list $rc $msg |
| 385 | } {1 {(6) database schema is locked: test}} |
| 386 | |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 387 | |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 388 | catch {db2 close} |
| 389 | catch {db close} |
| 390 | |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 391 | #-------------------------------------------------------------------------- |
| 392 | # Tests shared-5.* |
| 393 | # |
| 394 | foreach db [list test.db test1.db test2.db test3.db] { |
| 395 | file delete -force $db ${db}-journal |
| 396 | } |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 397 | do_test shared-$av.5.1.1 { |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 398 | sqlite3 db1 test.db |
| 399 | sqlite3 db2 test.db |
| 400 | execsql { |
| 401 | ATTACH 'test1.db' AS test1; |
| 402 | ATTACH 'test2.db' AS test2; |
| 403 | ATTACH 'test3.db' AS test3; |
| 404 | } db1 |
| 405 | execsql { |
| 406 | ATTACH 'test3.db' AS test3; |
| 407 | ATTACH 'test2.db' AS test2; |
| 408 | ATTACH 'test1.db' AS test1; |
| 409 | } db2 |
| 410 | } {} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 411 | do_test shared-$av.5.1.2 { |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 412 | execsql { |
| 413 | CREATE TABLE test1.t1(a, b); |
| 414 | CREATE INDEX test1.i1 ON t1(a, b); |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 415 | } db1 |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame^] | 416 | } {} |
| 417 | ifcapable view { |
| 418 | do_test shared-$av.5.1.3 { |
| 419 | execsql { |
| 420 | CREATE VIEW test1.v1 AS SELECT * FROM t1; |
| 421 | } db1 |
| 422 | } {} |
| 423 | } |
| 424 | ifcapable trigger { |
| 425 | do_test shared-$av.5.1.4 { |
| 426 | execsql { |
| 427 | CREATE TRIGGER test1.trig1 AFTER INSERT ON t1 BEGIN |
| 428 | INSERT INTO t1 VALUES(new.a, new.b); |
| 429 | END; |
| 430 | } db1 |
| 431 | } {} |
| 432 | } |
| 433 | do_test shared-$av.5.1.5 { |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 434 | execsql { |
| 435 | DROP INDEX i1; |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame^] | 436 | } db2 |
| 437 | } {} |
| 438 | ifcapable view { |
| 439 | do_test shared-$av.5.1.6 { |
| 440 | execsql { |
| 441 | DROP VIEW v1; |
| 442 | } db2 |
| 443 | } {} |
| 444 | } |
| 445 | ifcapable trigger { |
| 446 | do_test shared-$av.5.1.7 { |
| 447 | execsql { |
| 448 | DROP TRIGGER trig1; |
| 449 | } db2 |
| 450 | } {} |
| 451 | } |
| 452 | do_test shared-$av.5.1.8 { |
| 453 | execsql { |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 454 | DROP TABLE t1; |
| 455 | } db2 |
| 456 | } {} |
danielk1977 | ff89079 | 2006-01-16 16:24:25 +0000 | [diff] [blame] | 457 | ifcapable compound { |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame^] | 458 | do_test shared-$av.5.1.9 { |
danielk1977 | ff89079 | 2006-01-16 16:24:25 +0000 | [diff] [blame] | 459 | execsql { |
| 460 | SELECT * FROM sqlite_master UNION ALL SELECT * FROM test1.sqlite_master |
| 461 | } db1 |
| 462 | } {} |
| 463 | } |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 464 | |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 465 | #-------------------------------------------------------------------------- |
| 466 | # Tests shared-6.* test that a query obtains all the read-locks it needs |
| 467 | # before starting execution of the query. This means that there is no chance |
| 468 | # some rows of data will be returned before a lock fails and SQLITE_LOCK |
| 469 | # is returned. |
| 470 | # |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 471 | do_test shared-$av.6.1.1 { |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 472 | execsql { |
| 473 | CREATE TABLE t1(a, b); |
| 474 | CREATE TABLE t2(a, b); |
| 475 | INSERT INTO t1 VALUES(1, 2); |
| 476 | INSERT INTO t2 VALUES(3, 4); |
| 477 | } db1 |
danielk1977 | ff89079 | 2006-01-16 16:24:25 +0000 | [diff] [blame] | 478 | } {} |
| 479 | ifcapable compound { |
| 480 | do_test shared-$av.6.1.2 { |
| 481 | execsql { |
| 482 | SELECT * FROM t1 UNION ALL SELECT * FROM t2; |
| 483 | } db2 |
| 484 | } {1 2 3 4} |
| 485 | } |
| 486 | do_test shared-$av.6.1.3 { |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 487 | # Establish a write lock on table t2 via connection db2. Then make a |
| 488 | # UNION all query using connection db1 that first accesses t1, followed |
| 489 | # by t2. If the locks are grabbed at the start of the statement (as |
| 490 | # they should be), no rows are returned. If (as was previously the case) |
| 491 | # they are grabbed as the tables are accessed, the t1 rows will be |
| 492 | # returned before the query fails. |
| 493 | # |
| 494 | execsql { |
| 495 | BEGIN; |
| 496 | INSERT INTO t2 VALUES(5, 6); |
| 497 | } db2 |
| 498 | set ret [list] |
| 499 | catch { |
| 500 | db1 eval {SELECT * FROM t1 UNION ALL SELECT * FROM t2} { |
| 501 | lappend ret $a $b |
| 502 | } |
| 503 | } |
| 504 | set ret |
| 505 | } {} |
danielk1977 | ff89079 | 2006-01-16 16:24:25 +0000 | [diff] [blame] | 506 | do_test shared-$av.6.1.4 { |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 507 | execsql { |
| 508 | COMMIT; |
| 509 | BEGIN; |
| 510 | INSERT INTO t1 VALUES(7, 8); |
| 511 | } db2 |
| 512 | set ret [list] |
| 513 | catch { |
| 514 | db1 eval { |
| 515 | SELECT (CASE WHEN a>4 THEN (SELECT a FROM t1) ELSE 0 END) AS d FROM t2; |
| 516 | } { |
| 517 | lappend ret $d |
| 518 | } |
| 519 | } |
| 520 | set ret |
| 521 | } {} |
| 522 | |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 523 | catch {db1 close} |
| 524 | catch {db2 close} |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 525 | foreach f [list test.db test2.db] { |
| 526 | file delete -force $f ${f}-journal |
| 527 | } |
| 528 | |
| 529 | #-------------------------------------------------------------------------- |
| 530 | # Tests shared-7.* test auto-vacuum does not invalidate cursors from |
| 531 | # other shared-cache users when it reorganizes the database on |
| 532 | # COMMIT. |
| 533 | # |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 534 | do_test shared-$av.7.1 { |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 535 | # This test case sets up a test database in auto-vacuum mode consisting |
| 536 | # of two tables, t1 and t2. Both have a single index. Table t1 is |
| 537 | # populated first (so consists of pages toward the start of the db file), |
| 538 | # t2 second (pages toward the end of the file). |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 539 | sqlite3 db test.db |
| 540 | sqlite3 db2 test.db |
| 541 | execsql { |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 542 | BEGIN; |
| 543 | CREATE TABLE t1(a PRIMARY KEY, b); |
| 544 | CREATE TABLE t2(a PRIMARY KEY, b); |
| 545 | } |
drh | 7a91dd8 | 2006-01-11 01:08:34 +0000 | [diff] [blame] | 546 | set ::contents {} |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 547 | for {set i 0} {$i < 100} {incr i} { |
| 548 | set a [string repeat "$i " 20] |
| 549 | set b [string repeat "$i " 20] |
| 550 | db eval { |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame^] | 551 | INSERT INTO t1 VALUES(:a, :b); |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 552 | } |
| 553 | lappend ::contents [list [expr $i+1] $a $b] |
| 554 | } |
| 555 | execsql { |
| 556 | INSERT INTO t2 SELECT * FROM t1; |
| 557 | COMMIT; |
| 558 | } |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 559 | } {} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 560 | do_test shared-$av.7.2 { |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 561 | # This test case deletes the contents of table t1 (the one at the start of |
| 562 | # the file) while many cursors are open on table t2 and it's index. All of |
| 563 | # the non-root pages will be moved from the end to the start of the file |
| 564 | # when the DELETE is committed - this test verifies that moving the pages |
| 565 | # does not disturb the open cursors. |
| 566 | # |
| 567 | |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 568 | proc lockrow {db tbl oids body} { |
| 569 | set ret [list] |
| 570 | db eval "SELECT oid AS i, a, b FROM $tbl ORDER BY a" { |
| 571 | if {$i==[lindex $oids 0]} { |
| 572 | set noids [lrange $oids 1 end] |
| 573 | if {[llength $noids]==0} { |
| 574 | set subret [eval $body] |
| 575 | } else { |
| 576 | set subret [lockrow $db $tbl $noids $body] |
| 577 | } |
| 578 | } |
| 579 | lappend ret [list $i $a $b] |
| 580 | } |
| 581 | return [linsert $subret 0 $ret] |
| 582 | } |
| 583 | proc locktblrows {db tbl body} { |
| 584 | set oids [db eval "SELECT oid FROM $tbl"] |
| 585 | lockrow $db $tbl $oids $body |
| 586 | } |
| 587 | |
| 588 | set scans [locktblrows db t2 { |
| 589 | execsql { |
| 590 | DELETE FROM t1; |
| 591 | } db2 |
| 592 | }] |
| 593 | set error 0 |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 594 | |
| 595 | # Test that each SELECT query returned the expected contents of t2. |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 596 | foreach s $scans { |
| 597 | if {[lsort -integer -index 0 $s]!=$::contents} { |
| 598 | set error 1 |
| 599 | } |
| 600 | } |
| 601 | set error |
| 602 | } {0} |
| 603 | |
| 604 | catch {db close} |
| 605 | catch {db2 close} |
drh | 7a91dd8 | 2006-01-11 01:08:34 +0000 | [diff] [blame] | 606 | unset -nocomplain contents |
danielk1977 | aaf2268 | 2006-01-06 15:03:48 +0000 | [diff] [blame] | 607 | |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 608 | #-------------------------------------------------------------------------- |
| 609 | # The following tests try to trick the shared-cache code into assuming |
| 610 | # the wrong encoding for a database. |
| 611 | # |
| 612 | file delete -force test.db test.db-journal |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame^] | 613 | ifcapable utf16 { |
| 614 | do_test shared-$av.8.1.1 { |
| 615 | sqlite3 db test.db |
| 616 | execsql { |
| 617 | PRAGMA encoding = 'UTF-16'; |
| 618 | SELECT * FROM sqlite_master; |
| 619 | } |
| 620 | } {} |
| 621 | do_test shared-$av.8.1.2 { |
| 622 | string range [execsql {PRAGMA encoding;}] 0 end-2 |
| 623 | } {UTF-16} |
| 624 | do_test shared-$av.8.1.3 { |
| 625 | sqlite3 db2 test.db |
| 626 | execsql { |
| 627 | PRAGMA encoding = 'UTF-8'; |
| 628 | CREATE TABLE abc(a, b, c); |
| 629 | } db2 |
| 630 | } {} |
| 631 | do_test shared-$av.8.1.4 { |
| 632 | execsql { |
| 633 | SELECT * FROM sqlite_master; |
| 634 | } |
| 635 | } "table abc abc [expr $AUTOVACUUM?3:2] {CREATE TABLE abc(a, b, c)}" |
| 636 | do_test shared-$av.8.1.5 { |
| 637 | db2 close |
| 638 | execsql { |
| 639 | PRAGMA encoding; |
| 640 | } |
| 641 | } {UTF-8} |
| 642 | file delete -force test2.db test2.db-journal |
| 643 | do_test shared-$av.8.2.1 { |
| 644 | execsql { |
| 645 | ATTACH 'test2.db' AS aux; |
| 646 | SELECT * FROM aux.sqlite_master; |
| 647 | } |
| 648 | } {} |
| 649 | do_test shared-$av.8.2.2 { |
| 650 | sqlite3 db2 test2.db |
| 651 | execsql { |
| 652 | PRAGMA encoding = 'UTF-16'; |
| 653 | CREATE TABLE def(d, e, f); |
| 654 | } db2 |
| 655 | string range [execsql {PRAGMA encoding;} db2] 0 end-2 |
| 656 | } {UTF-16} |
| 657 | do_test shared-$av.8.2.3 { |
| 658 | catchsql { |
| 659 | SELECT * FROM aux.sqlite_master; |
| 660 | } |
| 661 | } {1 {attached databases must use the same text encoding as main database}} |
| 662 | } |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 663 | |
| 664 | catch {db close} |
| 665 | catch {db2 close} |
danielk1977 | eecfb3e | 2006-01-10 12:31:39 +0000 | [diff] [blame] | 666 | file delete -force test.db test2.db |
| 667 | |
danielk1977 | eecfb3e | 2006-01-10 12:31:39 +0000 | [diff] [blame] | 668 | #--------------------------------------------------------------------------- |
| 669 | # The following tests - shared-9.* - test interactions between TEMP triggers |
| 670 | # and shared-schemas. |
| 671 | # |
| 672 | ifcapable trigger&&tempdb { |
| 673 | |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 674 | do_test shared-$av.9.1 { |
danielk1977 | eecfb3e | 2006-01-10 12:31:39 +0000 | [diff] [blame] | 675 | sqlite3 db test.db |
| 676 | sqlite3 db2 test.db |
| 677 | execsql { |
| 678 | CREATE TABLE abc(a, b, c); |
| 679 | CREATE TABLE abc_mirror(a, b, c); |
| 680 | CREATE TEMP TRIGGER BEFORE INSERT ON abc BEGIN |
| 681 | INSERT INTO abc_mirror(a, b, c) VALUES(new.a, new.b, new.c); |
| 682 | END; |
| 683 | INSERT INTO abc VALUES(1, 2, 3); |
| 684 | SELECT * FROM abc_mirror; |
| 685 | } |
| 686 | } {1 2 3} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 687 | do_test shared-$av.9.2 { |
danielk1977 | eecfb3e | 2006-01-10 12:31:39 +0000 | [diff] [blame] | 688 | execsql { |
| 689 | INSERT INTO abc VALUES(4, 5, 6); |
| 690 | SELECT * FROM abc_mirror; |
| 691 | } db2 |
| 692 | } {1 2 3} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 693 | do_test shared-$av.9.3 { |
danielk1977 | eecfb3e | 2006-01-10 12:31:39 +0000 | [diff] [blame] | 694 | db close |
| 695 | db2 close |
| 696 | } {} |
| 697 | |
| 698 | } ; # End shared-9.* |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 699 | |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 700 | #--------------------------------------------------------------------------- |
| 701 | # The following tests - shared-10.* - test that the library behaves |
| 702 | # correctly when a connection to a shared-cache is closed. |
| 703 | # |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 704 | do_test shared-$av.10.1 { |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 705 | # Create a small sample database with two connections to it (db and db2). |
| 706 | file delete -force test.db |
| 707 | sqlite3 db test.db |
| 708 | sqlite3 db2 test.db |
| 709 | execsql { |
| 710 | CREATE TABLE ab(a PRIMARY KEY, b); |
| 711 | CREATE TABLE de(d PRIMARY KEY, e); |
| 712 | INSERT INTO ab VALUES('Chiang Mai', 100000); |
| 713 | INSERT INTO ab VALUES('Bangkok', 8000000); |
| 714 | INSERT INTO de VALUES('Ubon', 120000); |
| 715 | INSERT INTO de VALUES('Khon Kaen', 200000); |
| 716 | } |
| 717 | } {} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 718 | do_test shared-$av.10.2 { |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 719 | # Open a read-transaction with the first connection, a write-transaction |
| 720 | # with the second. |
| 721 | execsql { |
| 722 | BEGIN; |
| 723 | SELECT * FROM ab; |
| 724 | } |
| 725 | execsql { |
| 726 | BEGIN; |
| 727 | INSERT INTO de VALUES('Pataya', 30000); |
| 728 | } db2 |
| 729 | } {} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 730 | do_test shared-$av.10.3 { |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 731 | # An external connection should be able to read the database, but not |
| 732 | # prepare a write operation. |
| 733 | sqlite3 db3 ./test.db |
| 734 | execsql { |
| 735 | SELECT * FROM ab; |
| 736 | } db3 |
| 737 | catchsql { |
| 738 | BEGIN; |
| 739 | INSERT INTO de VALUES('Pataya', 30000); |
| 740 | } db3 |
| 741 | } {1 {database is locked}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 742 | do_test shared-$av.10.4 { |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 743 | # Close the connection with the write-transaction open |
| 744 | db2 close |
| 745 | } {} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 746 | do_test shared-$av.10.5 { |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 747 | # Test that the db2 transaction has been automatically rolled back. |
| 748 | # If it has not the ('Pataya', 30000) entry will still be in the table. |
| 749 | execsql { |
| 750 | SELECT * FROM de; |
| 751 | } |
| 752 | } {Ubon 120000 {Khon Kaen} 200000} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 753 | do_test shared-$av.10.5 { |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 754 | # Closing db2 should have dropped the shared-cache back to a read-lock. |
| 755 | # So db3 should be able to prepare a write... |
| 756 | catchsql {INSERT INTO de VALUES('Pataya', 30000);} db3 |
| 757 | } {0 {}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 758 | do_test shared-$av.10.6 { |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 759 | # ... but not commit it. |
| 760 | catchsql {COMMIT} db3 |
| 761 | } {1 {database is locked}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 762 | do_test shared-$av.10.7 { |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 763 | # Commit the (read-only) db transaction. Check via db3 to make sure the |
| 764 | # contents of table "de" are still as they should be. |
| 765 | execsql { |
| 766 | COMMIT; |
| 767 | } |
| 768 | execsql { |
| 769 | SELECT * FROM de; |
| 770 | } db3 |
| 771 | } {Ubon 120000 {Khon Kaen} 200000 Pataya 30000} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 772 | do_test shared-$av.10.9 { |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 773 | # Commit the external transaction. |
| 774 | catchsql {COMMIT} db3 |
| 775 | } {0 {}} |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 776 | integrity_check shared-$av.10.10 |
| 777 | do_test shared-$av.10.11 { |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 778 | db close |
| 779 | db3 close |
| 780 | } {} |
| 781 | |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 782 | } |
| 783 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 784 | sqlite3_enable_shared_cache $::enable_shared_cache |
danielk1977 | a96a710 | 2006-01-16 12:46:41 +0000 | [diff] [blame] | 785 | finish_test |