sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | # 2008 June 28 |
| 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 database locks. |
| 13 | # |
| 14 | |
| 15 | set testdir [file dirname $argv0] |
| 16 | source $testdir/tester.tcl |
| 17 | set testprefix lock5 |
| 18 | |
| 19 | # This file is only run if using the unix backend compiled with the |
| 20 | # SQLITE_ENABLE_LOCKING_STYLE macro. |
| 21 | db close |
| 22 | if {[catch {sqlite3 db test.db -vfs unix-none} msg]} { |
| 23 | finish_test |
| 24 | return |
| 25 | } |
| 26 | db close |
| 27 | forcedelete test.db.lock |
| 28 | |
| 29 | ifcapable lock_proxy_pragmas { |
| 30 | set ::using_proxy 0 |
| 31 | foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] { |
| 32 | set ::using_proxy $value |
| 33 | } |
| 34 | # Disable the proxy locking for these tests |
| 35 | set env(SQLITE_FORCE_PROXY_LOCKING) "0" |
| 36 | } |
| 37 | |
| 38 | |
| 39 | do_test lock5-dotfile.1 { |
| 40 | sqlite3 db test.db -vfs unix-dotfile |
| 41 | execsql { |
| 42 | BEGIN; |
| 43 | CREATE TABLE t1(a, b); |
| 44 | } |
| 45 | } {} |
| 46 | |
| 47 | do_test lock5-dotfile.2 { |
| 48 | file exists test.db.lock |
| 49 | } {1} |
| 50 | |
| 51 | do_test lock5-dotfile.3 { |
| 52 | execsql COMMIT |
| 53 | file exists test.db.lock |
| 54 | } {0} |
| 55 | |
| 56 | do_test lock5-dotfile.4 { |
| 57 | sqlite3 db2 test.db -vfs unix-dotfile |
| 58 | execsql { |
| 59 | INSERT INTO t1 VALUES('a', 'b'); |
| 60 | SELECT * FROM t1; |
| 61 | } db2 |
| 62 | } {a b} |
| 63 | |
| 64 | do_test lock5-dotfile.5 { |
| 65 | execsql { |
| 66 | BEGIN; |
| 67 | SELECT * FROM t1; |
| 68 | } db2 |
| 69 | } {a b} |
| 70 | |
| 71 | do_test lock5-dotfile.6 { |
| 72 | file exists test.db.lock |
| 73 | } {1} |
| 74 | |
| 75 | do_test lock5-dotfile.7 { |
| 76 | catchsql { SELECT * FROM t1; } |
| 77 | } {1 {database is locked}} |
| 78 | |
| 79 | do_test lock5-dotfile.8 { |
| 80 | execsql { |
| 81 | SELECT * FROM t1; |
| 82 | ROLLBACK; |
| 83 | } db2 |
| 84 | } {a b} |
| 85 | |
| 86 | do_test lock5-dotfile.9 { |
| 87 | catchsql { SELECT * FROM t1; } |
| 88 | } {0 {a b}} |
| 89 | |
| 90 | do_test lock5-dotfile.10 { |
| 91 | file exists test.db.lock |
| 92 | } {0} |
| 93 | |
| 94 | do_test lock5-dotfile.X { |
| 95 | db2 close |
| 96 | execsql {BEGIN EXCLUSIVE} |
| 97 | db close |
| 98 | file exists test.db.lock |
| 99 | } {0} |
| 100 | |
| 101 | ##################################################################### |
| 102 | |
| 103 | forcedelete test.db |
| 104 | if {0==[catch {sqlite3 db test.db -vfs unix-flock} msg]} { |
| 105 | |
| 106 | do_test lock5-flock.1 { |
| 107 | sqlite3 db test.db -vfs unix-flock |
| 108 | execsql { |
| 109 | CREATE TABLE t1(a, b); |
| 110 | BEGIN; |
| 111 | INSERT INTO t1 VALUES(1, 2); |
| 112 | } |
| 113 | } {} |
| 114 | |
| 115 | # Make sure we are not accidentally using the dotfile locking scheme. |
| 116 | do_test lock5-flock.2 { |
| 117 | file exists test.db.lock |
| 118 | } {0} |
| 119 | |
| 120 | do_test lock5-flock.3 { |
| 121 | catch { sqlite3 db2 test.db -vfs unix-flock } |
| 122 | catchsql { SELECT * FROM t1 } db2 |
| 123 | } {1 {database is locked}} |
| 124 | |
| 125 | do_test lock5-flock.4 { |
| 126 | execsql COMMIT |
| 127 | catchsql { SELECT * FROM t1 } db2 |
| 128 | } {0 {1 2}} |
| 129 | |
| 130 | do_test lock5-flock.5 { |
| 131 | execsql BEGIN |
| 132 | catchsql { SELECT * FROM t1 } db2 |
| 133 | } {0 {1 2}} |
| 134 | |
| 135 | do_test lock5-flock.6 { |
| 136 | execsql {SELECT * FROM t1} |
| 137 | catchsql { SELECT * FROM t1 } db2 |
| 138 | } {1 {database is locked}} |
| 139 | |
| 140 | do_test lock5-flock.7 { |
| 141 | db close |
| 142 | catchsql { SELECT * FROM t1 } db2 |
| 143 | } {0 {1 2}} |
| 144 | |
| 145 | do_test lock5-flock.8 { |
| 146 | db2 close |
| 147 | } {} |
| 148 | |
| 149 | do_test lock5-flock.9 { |
| 150 | sqlite3 db test.db -vfs unix-flock |
| 151 | execsql { |
| 152 | SELECT * FROM t1 |
| 153 | } |
| 154 | } {1 2} |
| 155 | |
| 156 | do_test lock5-flock.10 { |
| 157 | sqlite3 db2 test.db -vfs unix-flock |
| 158 | execsql { |
| 159 | SELECT * FROM t1 |
| 160 | } db2 |
| 161 | } {1 2} |
| 162 | |
| 163 | do_test lock5-flock.10 { |
| 164 | execsql { |
| 165 | PRAGMA cache_size = 1; |
| 166 | BEGIN; |
| 167 | WITH s(i) AS ( |
| 168 | SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<10000 |
| 169 | ) |
| 170 | INSERT INTO t1 SELECT i, i+1 FROM s; |
| 171 | } |
| 172 | |
| 173 | catchsql { |
| 174 | SELECT * FROM t1 |
| 175 | } db2 |
| 176 | } {1 {database is locked}} |
| 177 | |
| 178 | if {[permutation]!="inmemory_journal"} { |
| 179 | do_test lock5-flock.11 { |
| 180 | forcecopy test.db test.db2 |
| 181 | forcecopy test.db-journal test.db2-journal |
| 182 | db2 close |
| 183 | sqlite3 db2 test.db2 -vfs unix-flock |
| 184 | catchsql { |
| 185 | SELECT * FROM t1 |
| 186 | } db2 |
| 187 | } {0 {1 2}} |
| 188 | |
| 189 | do_test lock5-flock.12 { |
| 190 | file exists test.db2-journal |
| 191 | } 0 |
| 192 | } |
| 193 | |
| 194 | db close |
| 195 | db2 close |
| 196 | |
| 197 | } |
| 198 | |
| 199 | ##################################################################### |
| 200 | |
| 201 | reset_db |
| 202 | |
| 203 | do_test lock5-none.1 { |
| 204 | sqlite3 db test.db -vfs unix-none |
| 205 | sqlite3 db2 test.db -vfs unix-none |
| 206 | execsql { PRAGMA mmap_size = 0 } db2 |
| 207 | execsql { |
| 208 | CREATE TABLE t1(a, b); |
| 209 | INSERT INTO t1 VALUES(1, 2); |
| 210 | BEGIN; |
| 211 | INSERT INTO t1 VALUES(3, 4); |
| 212 | } |
| 213 | } {} |
| 214 | do_test lock5-none.2 { |
| 215 | execsql { SELECT * FROM t1 } |
| 216 | } {1 2 3 4} |
| 217 | do_test lock5-none.3 { |
| 218 | execsql { SELECT * FROM t1; } db2 |
| 219 | } {1 2} |
| 220 | do_test lock5-none.4 { |
| 221 | execsql { |
| 222 | BEGIN; |
| 223 | SELECT * FROM t1; |
| 224 | } db2 |
| 225 | } {1 2} |
| 226 | do_test lock5-none.5 { |
| 227 | execsql COMMIT |
| 228 | execsql {SELECT * FROM t1} db2 |
| 229 | } {1 2} |
| 230 | |
| 231 | ifcapable memorymanage { |
| 232 | if {[permutation]!="memsubsys1" && [permutation]!="memsubsys2"} { |
| 233 | do_test lock5-none.6 { |
| 234 | sqlite3_release_memory 1000000 |
| 235 | execsql {SELECT * FROM t1} db2 |
| 236 | } {1 2 3 4} |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | do_test lock5-none.X { |
| 241 | db close |
| 242 | db2 close |
| 243 | } {} |
| 244 | |
| 245 | ifcapable lock_proxy_pragmas { |
| 246 | set env(SQLITE_FORCE_PROXY_LOCKING) $::using_proxy |
| 247 | } |
| 248 | |
| 249 | ##################################################################### |
| 250 | reset_db |
| 251 | if {[permutation]!="inmemory_journal"} { |
| 252 | |
| 253 | # 1. Create a large database using the unix-dotfile VFS |
| 254 | # 2. Write a large transaction to the db, so that the cache spills, but do |
| 255 | # not commit it. |
| 256 | # 3. Make a copy of the database files on disk. |
| 257 | # 4. Try to read from the copy using unix-dotfile VFS. This fails because |
| 258 | # the dotfile still exists, so SQLite thinks the database is locked. |
| 259 | # 5. Remove the dotfile. |
| 260 | # 6. Try to read the db again. This time, the old transaction is rolled |
| 261 | # back and the read permitted. |
| 262 | # |
| 263 | do_test 2.dotfile.1 { |
| 264 | sqlite3 db test.db -vfs unix-dotfile |
| 265 | execsql { |
| 266 | PRAGMA cache_size = 10; |
| 267 | CREATE TABLE t1(x, y, z); |
| 268 | CREATE INDEX t1x ON t1(x); |
| 269 | WITH s(i) AS ( |
| 270 | SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000 |
| 271 | ) |
| 272 | INSERT INTO t1 SELECT hex(randomblob(20)), hex(randomblob(500)), i FROM s; |
| 273 | } |
| 274 | } {} |
| 275 | |
| 276 | do_execsql_test 2.dotfile.2 { |
| 277 | BEGIN; |
| 278 | UPDATE t1 SET z=z+1, x=hex(randomblob(20)); |
| 279 | } |
| 280 | |
| 281 | do_test 2.dotfile.3 { |
| 282 | list \ |
| 283 | [file exists test.db] \ |
| 284 | [file exists test.db-journal] \ |
| 285 | [file exists test.db.lock] |
| 286 | } {1 1 1} |
| 287 | |
| 288 | do_test 2.dotfile.4 { |
| 289 | forcecopy test.db test.db2 |
| 290 | forcecopy test.db-journal test.db2-journal |
| 291 | file mkdir test.db2.lock |
| 292 | |
| 293 | sqlite3 db2 test.db2 -vfs unix-dotfile |
| 294 | catchsql { |
| 295 | SELECT count(*) FROM t1; |
| 296 | } db2 |
| 297 | } {1 {database is locked}} |
| 298 | |
| 299 | do_test 2.dotfile.5 { |
| 300 | file delete test.db2.lock |
| 301 | execsql { |
| 302 | PRAGMA integrity_check |
| 303 | } db2 |
| 304 | } {ok} |
| 305 | |
| 306 | db2 close |
| 307 | |
| 308 | do_test 2.dotfile.6 { |
| 309 | forcecopy test.db test.db2 |
| 310 | forcecopy test.db-journal test.db2-journal |
| 311 | |
| 312 | sqlite3 db2 file:test.db2?nolock=1 -vfs unix-dotfile -uri 1 |
| 313 | catchsql { |
| 314 | SELECT count(*) FROM t1; |
| 315 | } db2 |
| 316 | } {0 1000} |
| 317 | } |
| 318 | |
| 319 | finish_test |