dan | d3f8f94 | 2010-04-13 11:35:01 +0000 | [diff] [blame] | 1 | # 2010 April 13 |
| 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 file is testing the operation of the library in |
| 13 | # "PRAGMA journal_mode=WAL" mode. |
| 14 | # |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 15 | |
| 16 | set testdir [file dirname $argv0] |
| 17 | source $testdir/tester.tcl |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 18 | source $testdir/lock_common.tcl |
dan | a4a9095 | 2010-06-15 19:07:42 +0000 | [diff] [blame] | 19 | source $testdir/malloc_common.tcl |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 20 | source $testdir/wal_common.tcl |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 21 | |
dan | 5cf5353 | 2010-05-01 16:40:20 +0000 | [diff] [blame] | 22 | ifcapable !wal {finish_test ; return } |
| 23 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 24 | proc reopen_db {} { |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 25 | catch { db close } |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 26 | file delete -force test.db test.db-wal test.db-wal-summary |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 27 | sqlite3_wal db test.db |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 28 | } |
| 29 | |
dan | 6703239 | 2010-04-17 15:42:43 +0000 | [diff] [blame] | 30 | set ::blobcnt 0 |
| 31 | proc blob {nByte} { |
| 32 | incr ::blobcnt |
| 33 | return [string range [string repeat "${::blobcnt}x" $nByte] 1 $nByte] |
| 34 | } |
| 35 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 36 | proc sqlite3_wal {args} { |
| 37 | eval sqlite3 $args |
dan | 65bddc1 | 2010-05-07 12:49:22 +0000 | [diff] [blame] | 38 | [lindex $args 0] eval { PRAGMA auto_vacuum = 0 } |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 39 | [lindex $args 0] eval { PRAGMA page_size = 1024 } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 40 | [lindex $args 0] eval { PRAGMA journal_mode = wal } |
dan | 6703239 | 2010-04-17 15:42:43 +0000 | [diff] [blame] | 41 | [lindex $args 0] eval { PRAGMA synchronous = normal } |
| 42 | [lindex $args 0] function blob blob |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 43 | } |
| 44 | |
dan | 3de777f | 2010-04-17 12:31:37 +0000 | [diff] [blame] | 45 | proc log_deleted {logfile} { |
| 46 | return [expr [file exists $logfile]==0] |
| 47 | } |
| 48 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 49 | # |
| 50 | # These are 'warm-body' tests used while developing the WAL code. They |
| 51 | # serve to prove that a few really simple cases work: |
| 52 | # |
| 53 | # wal-1.*: Read and write the database. |
| 54 | # wal-2.*: Test MVCC with one reader, one writer. |
| 55 | # wal-3.*: Test transaction rollback. |
| 56 | # wal-4.*: Test savepoint/statement rollback. |
| 57 | # wal-5.*: Test the temp database. |
| 58 | # wal-6.*: Test creating databases with different page sizes. |
| 59 | # |
dan | 998ad21 | 2010-05-07 06:59:08 +0000 | [diff] [blame] | 60 | # |
dan | 4bcc498 | 2010-08-16 19:23:02 +0000 | [diff] [blame] | 61 | # |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 62 | do_test wal-0.1 { |
dan | 65bddc1 | 2010-05-07 12:49:22 +0000 | [diff] [blame] | 63 | execsql { PRAGMA auto_vacuum = 0 } |
dan | 6703239 | 2010-04-17 15:42:43 +0000 | [diff] [blame] | 64 | execsql { PRAGMA synchronous = normal } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 65 | execsql { PRAGMA journal_mode = wal } |
| 66 | } {wal} |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 67 | do_test wal-0.2 { |
| 68 | file size test.db |
| 69 | } {1024} |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 70 | |
| 71 | do_test wal-1.0 { |
| 72 | execsql { |
| 73 | BEGIN; |
| 74 | CREATE TABLE t1(a, b); |
| 75 | } |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 76 | list [file exists test.db-journal] \ |
| 77 | [file exists test.db-wal] \ |
| 78 | [file size test.db] |
| 79 | } {0 1 1024} |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 80 | do_test wal-1.1 { |
| 81 | execsql COMMIT |
| 82 | list [file exists test.db-journal] [file exists test.db-wal] |
| 83 | } {0 1} |
| 84 | do_test wal-1.2 { |
| 85 | # There are now two pages in the log. |
| 86 | file size test.db-wal |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 87 | } [wal_file_size 2 1024] |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 88 | |
| 89 | do_test wal-1.3 { |
| 90 | execsql { SELECT * FROM sqlite_master } |
| 91 | } {table t1 t1 2 {CREATE TABLE t1(a, b)}} |
| 92 | |
| 93 | do_test wal-1.4 { |
| 94 | execsql { INSERT INTO t1 VALUES(1, 2) } |
| 95 | execsql { INSERT INTO t1 VALUES(3, 4) } |
| 96 | execsql { INSERT INTO t1 VALUES(5, 6) } |
| 97 | execsql { INSERT INTO t1 VALUES(7, 8) } |
| 98 | execsql { INSERT INTO t1 VALUES(9, 10) } |
| 99 | } {} |
| 100 | |
| 101 | do_test wal-1.5 { |
| 102 | execsql { SELECT * FROM t1 } |
| 103 | } {1 2 3 4 5 6 7 8 9 10} |
| 104 | |
| 105 | do_test wal-2.1 { |
| 106 | sqlite3_wal db2 ./test.db |
| 107 | execsql { BEGIN; SELECT * FROM t1 } db2 |
| 108 | } {1 2 3 4 5 6 7 8 9 10} |
| 109 | |
| 110 | do_test wal-2.2 { |
| 111 | execsql { INSERT INTO t1 VALUES(11, 12) } |
| 112 | execsql { SELECT * FROM t1 } |
| 113 | } {1 2 3 4 5 6 7 8 9 10 11 12} |
| 114 | |
| 115 | do_test wal-2.3 { |
| 116 | execsql { SELECT * FROM t1 } db2 |
| 117 | } {1 2 3 4 5 6 7 8 9 10} |
| 118 | |
| 119 | do_test wal-2.4 { |
| 120 | execsql { INSERT INTO t1 VALUES(13, 14) } |
| 121 | execsql { SELECT * FROM t1 } |
| 122 | } {1 2 3 4 5 6 7 8 9 10 11 12 13 14} |
| 123 | |
| 124 | do_test wal-2.5 { |
| 125 | execsql { SELECT * FROM t1 } db2 |
| 126 | } {1 2 3 4 5 6 7 8 9 10} |
| 127 | |
| 128 | do_test wal-2.6 { |
| 129 | execsql { COMMIT; SELECT * FROM t1 } db2 |
| 130 | } {1 2 3 4 5 6 7 8 9 10 11 12 13 14} |
| 131 | |
| 132 | do_test wal-3.1 { |
| 133 | execsql { BEGIN; DELETE FROM t1 } |
| 134 | execsql { SELECT * FROM t1 } |
| 135 | } {} |
| 136 | do_test wal-3.2 { |
| 137 | execsql { SELECT * FROM t1 } db2 |
| 138 | } {1 2 3 4 5 6 7 8 9 10 11 12 13 14} |
| 139 | do_test wal-3.3 { |
| 140 | execsql { ROLLBACK } |
| 141 | execsql { SELECT * FROM t1 } |
| 142 | } {1 2 3 4 5 6 7 8 9 10 11 12 13 14} |
| 143 | db2 close |
| 144 | |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 145 | #------------------------------------------------------------------------- |
| 146 | # The following tests, wal-4.*, test that savepoints work with WAL |
| 147 | # databases. |
| 148 | # |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 149 | do_test wal-4.1 { |
| 150 | execsql { |
| 151 | DELETE FROM t1; |
| 152 | BEGIN; |
| 153 | INSERT INTO t1 VALUES('a', 'b'); |
| 154 | SAVEPOINT sp; |
| 155 | INSERT INTO t1 VALUES('c', 'd'); |
| 156 | SELECT * FROM t1; |
| 157 | } |
| 158 | } {a b c d} |
| 159 | do_test wal-4.2 { |
| 160 | execsql { |
| 161 | ROLLBACK TO sp; |
| 162 | SELECT * FROM t1; |
| 163 | } |
| 164 | } {a b} |
| 165 | do_test wal-4.3 { |
| 166 | execsql { |
| 167 | COMMIT; |
| 168 | SELECT * FROM t1; |
| 169 | } |
| 170 | } {a b} |
| 171 | |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 172 | do_test wal-4.4.1 { |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 173 | db close |
| 174 | sqlite3 db test.db |
| 175 | db func blob blob |
| 176 | list [execsql { SELECT * FROM t1 }] [file size test.db-wal] |
| 177 | } {{a b} 0} |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 178 | do_test wal-4.4.2 { |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 179 | execsql { PRAGMA cache_size = 10 } |
| 180 | execsql { |
| 181 | CREATE TABLE t2(a, b); |
| 182 | INSERT INTO t2 VALUES(blob(400), blob(400)); |
| 183 | SAVEPOINT tr; |
| 184 | INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 2 */ |
| 185 | INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 4 */ |
| 186 | INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 8 */ |
| 187 | INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 16 */ |
| 188 | INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 32 */ |
| 189 | INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 2 */ |
| 190 | INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 4 */ |
| 191 | INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 8 */ |
| 192 | INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 16 */ |
| 193 | INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 32 */ |
| 194 | SELECT count(*) FROM t2; |
| 195 | } |
| 196 | } {32} |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 197 | do_test wal-4.4.3 { |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 198 | execsql { ROLLBACK TO tr } |
| 199 | } {} |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 200 | do_test wal-4.4.4 { |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 201 | set logsize [file size test.db-wal] |
| 202 | execsql { |
| 203 | INSERT INTO t1 VALUES('x', 'y'); |
| 204 | RELEASE tr; |
| 205 | } |
| 206 | expr { $logsize == [file size test.db-wal] } |
| 207 | } {1} |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 208 | do_test wal-4.4.5 { |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 209 | execsql { SELECT count(*) FROM t2 } |
| 210 | } {1} |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 211 | do_test wal-4.4.6 { |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 212 | file copy -force test.db test2.db |
| 213 | file copy -force test.db-wal test2.db-wal |
| 214 | sqlite3 db2 test2.db |
| 215 | execsql { SELECT count(*) FROM t2 ; SELECT count(*) FROM t1 } db2 |
| 216 | } {1 2} |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 217 | do_test wal-4.4.7 { |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 218 | execsql { PRAGMA integrity_check } db2 |
| 219 | } {ok} |
| 220 | db2 close |
| 221 | |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 222 | do_test wal-4.5.1 { |
| 223 | reopen_db |
| 224 | db func blob blob |
| 225 | execsql { |
| 226 | PRAGMA journal_mode = WAL; |
| 227 | CREATE TABLE t1(a, b); |
| 228 | INSERT INTO t1 VALUES('a', 'b'); |
| 229 | } |
| 230 | sqlite3 db test.db |
| 231 | db func blob blob |
| 232 | list [execsql { SELECT * FROM t1 }] [file size test.db-wal] |
| 233 | } {{a b} 0} |
| 234 | do_test wal-4.5.2 { |
| 235 | execsql { PRAGMA cache_size = 10 } |
| 236 | execsql { |
| 237 | CREATE TABLE t2(a, b); |
| 238 | BEGIN; |
| 239 | INSERT INTO t2 VALUES(blob(400), blob(400)); |
| 240 | SAVEPOINT tr; |
| 241 | INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 2 */ |
| 242 | INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 4 */ |
| 243 | INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 8 */ |
| 244 | INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 16 */ |
| 245 | INSERT INTO t2 SELECT blob(400), blob(400) FROM t2; /* 32 */ |
| 246 | INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 2 */ |
| 247 | INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 4 */ |
| 248 | INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 8 */ |
| 249 | INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 16 */ |
| 250 | INSERT INTO t1 SELECT blob(400), blob(400) FROM t1; /* 32 */ |
| 251 | SELECT count(*) FROM t2; |
| 252 | } |
| 253 | } {32} |
| 254 | do_test wal-4.5.3 { |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 255 | execsql { ROLLBACK TO tr } |
| 256 | } {} |
| 257 | do_test wal-4.5.4 { |
| 258 | set logsize [file size test.db-wal] |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 259 | execsql { |
| 260 | INSERT INTO t1 VALUES('x', 'y'); |
| 261 | RELEASE tr; |
| 262 | COMMIT; |
| 263 | } |
| 264 | expr { $logsize == [file size test.db-wal] } |
| 265 | } {1} |
| 266 | do_test wal-4.5.5 { |
| 267 | execsql { SELECT count(*) FROM t2 ; SELECT count(*) FROM t1 } |
| 268 | } {1 2} |
| 269 | do_test wal-4.5.6 { |
| 270 | file copy -force test.db test2.db |
| 271 | file copy -force test.db-wal test2.db-wal |
| 272 | sqlite3 db2 test2.db |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 273 | execsql { SELECT count(*) FROM t2 ; SELECT count(*) FROM t1 } db2 |
| 274 | } {1 2} |
| 275 | do_test wal-4.5.7 { |
| 276 | execsql { PRAGMA integrity_check } db2 |
| 277 | } {ok} |
| 278 | db2 close |
| 279 | |
dan | b7d53f5 | 2010-05-06 17:28:08 +0000 | [diff] [blame] | 280 | do_test wal-4.6.1 { |
| 281 | execsql { |
| 282 | DELETE FROM t2; |
| 283 | PRAGMA wal_checkpoint; |
| 284 | BEGIN; |
| 285 | INSERT INTO t2 VALUES('w', 'x'); |
| 286 | SAVEPOINT save; |
| 287 | INSERT INTO t2 VALUES('y', 'z'); |
| 288 | ROLLBACK TO save; |
| 289 | COMMIT; |
| 290 | SELECT * FROM t2; |
| 291 | } |
| 292 | } {w x} |
| 293 | |
dan | 4cd78b4 | 2010-04-26 16:57:10 +0000 | [diff] [blame] | 294 | |
dan | 74d6cd8 | 2010-04-24 18:44:05 +0000 | [diff] [blame] | 295 | reopen_db |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 296 | do_test wal-5.1 { |
| 297 | execsql { |
| 298 | CREATE TEMP TABLE t2(a, b); |
| 299 | INSERT INTO t2 VALUES(1, 2); |
| 300 | } |
| 301 | } {} |
| 302 | do_test wal-5.2 { |
| 303 | execsql { |
| 304 | BEGIN; |
| 305 | INSERT INTO t2 VALUES(3, 4); |
| 306 | SELECT * FROM t2; |
| 307 | } |
| 308 | } {1 2 3 4} |
| 309 | do_test wal-5.3 { |
| 310 | execsql { |
| 311 | ROLLBACK; |
| 312 | SELECT * FROM t2; |
| 313 | } |
| 314 | } {1 2} |
| 315 | do_test wal-5.4 { |
| 316 | execsql { |
| 317 | CREATE TEMP TABLE t3(x UNIQUE); |
| 318 | BEGIN; |
| 319 | INSERT INTO t2 VALUES(3, 4); |
| 320 | INSERT INTO t3 VALUES('abc'); |
| 321 | } |
| 322 | catchsql { INSERT INTO t3 VALUES('abc') } |
| 323 | } {1 {column x is not unique}} |
| 324 | do_test wal-5.5 { |
| 325 | execsql { |
| 326 | COMMIT; |
| 327 | SELECT * FROM t2; |
| 328 | } |
| 329 | } {1 2 3 4} |
| 330 | db close |
| 331 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 332 | foreach sector {512 4096} { |
| 333 | sqlite3_simulate_device -sectorsize $sector |
| 334 | foreach pgsz {512 1024 2048 4096} { |
| 335 | file delete -force test.db test.db-wal |
| 336 | do_test wal-6.$sector.$pgsz.1 { |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 337 | sqlite3 db test.db -vfs devsym |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 338 | execsql " |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 339 | PRAGMA page_size = $pgsz; |
dan | 65bddc1 | 2010-05-07 12:49:22 +0000 | [diff] [blame] | 340 | PRAGMA auto_vacuum = 0; |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 341 | PRAGMA journal_mode = wal; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 342 | " |
| 343 | execsql " |
| 344 | CREATE TABLE t1(a, b); |
| 345 | INSERT INTO t1 VALUES(1, 2); |
| 346 | " |
| 347 | db close |
| 348 | file size test.db |
| 349 | } [expr $pgsz*2] |
| 350 | |
| 351 | do_test wal-6.$sector.$pgsz.2 { |
dan | 3de777f | 2010-04-17 12:31:37 +0000 | [diff] [blame] | 352 | log_deleted test.db-wal |
| 353 | } {1} |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
| 357 | do_test wal-7.1 { |
| 358 | file delete -force test.db test.db-wal |
| 359 | sqlite3_wal db test.db |
| 360 | execsql { |
| 361 | PRAGMA page_size = 1024; |
| 362 | CREATE TABLE t1(a, b); |
| 363 | INSERT INTO t1 VALUES(1, 2); |
| 364 | } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 365 | list [file size test.db] [file size test.db-wal] |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 366 | } [list 1024 [wal_file_size 3 1024]] |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 367 | do_test wal-7.2 { |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 368 | execsql { PRAGMA wal_checkpoint } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 369 | list [file size test.db] [file size test.db-wal] |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 370 | } [list 2048 [wal_file_size 3 1024]] |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 371 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 372 | # Execute some transactions in auto-vacuum mode to test database file |
| 373 | # truncation. |
| 374 | # |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 375 | do_test wal-8.1 { |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 376 | reopen_db |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 377 | catch { db close } |
| 378 | file delete -force test.db test.db-wal |
| 379 | |
| 380 | sqlite3 db test.db |
| 381 | db function blob blob |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 382 | execsql { |
| 383 | PRAGMA auto_vacuum = 1; |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 384 | PRAGMA journal_mode = wal; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 385 | PRAGMA auto_vacuum; |
| 386 | } |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 387 | } {wal 1} |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 388 | do_test wal-8.2 { |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 389 | execsql { |
| 390 | PRAGMA page_size = 1024; |
| 391 | CREATE TABLE t1(x); |
dan | 6703239 | 2010-04-17 15:42:43 +0000 | [diff] [blame] | 392 | INSERT INTO t1 VALUES(blob(900)); |
| 393 | INSERT INTO t1 VALUES(blob(900)); |
| 394 | INSERT INTO t1 SELECT blob(900) FROM t1; /* 4 */ |
| 395 | INSERT INTO t1 SELECT blob(900) FROM t1; /* 8 */ |
| 396 | INSERT INTO t1 SELECT blob(900) FROM t1; /* 16 */ |
| 397 | INSERT INTO t1 SELECT blob(900) FROM t1; /* 32 */ |
| 398 | INSERT INTO t1 SELECT blob(900) FROM t1; /* 64 */ |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 399 | PRAGMA wal_checkpoint; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 400 | } |
| 401 | file size test.db |
dan | 80a1526 | 2010-04-13 11:45:31 +0000 | [diff] [blame] | 402 | } [expr 68*1024] |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 403 | do_test wal-8.3 { |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 404 | execsql { |
| 405 | DELETE FROM t1 WHERE rowid<54; |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 406 | PRAGMA wal_checkpoint; |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 407 | } |
| 408 | file size test.db |
| 409 | } [expr 14*1024] |
| 410 | |
| 411 | # Run some "warm-body" tests to ensure that log-summary files with more |
| 412 | # than 256 entries (log summaries that contain index blocks) work Ok. |
| 413 | # |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 414 | do_test wal-9.1 { |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 415 | reopen_db |
| 416 | execsql { |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 417 | CREATE TABLE t1(x PRIMARY KEY); |
dan | 6703239 | 2010-04-17 15:42:43 +0000 | [diff] [blame] | 418 | INSERT INTO t1 VALUES(blob(900)); |
| 419 | INSERT INTO t1 VALUES(blob(900)); |
| 420 | INSERT INTO t1 SELECT blob(900) FROM t1; /* 4 */ |
| 421 | INSERT INTO t1 SELECT blob(900) FROM t1; /* 8 */ |
| 422 | INSERT INTO t1 SELECT blob(900) FROM t1; /* 16 */ |
| 423 | INSERT INTO t1 SELECT blob(900) FROM t1; /* 32 */ |
| 424 | INSERT INTO t1 SELECT blob(900) FROM t1; /* 64 */ |
| 425 | INSERT INTO t1 SELECT blob(900) FROM t1; /* 128 */ |
| 426 | INSERT INTO t1 SELECT blob(900) FROM t1; /* 256 */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 427 | } |
| 428 | file size test.db |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 429 | } 1024 |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 430 | do_test wal-9.2 { |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 431 | sqlite3_wal db2 test.db |
| 432 | execsql {PRAGMA integrity_check } db2 |
| 433 | } {ok} |
| 434 | |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 435 | do_test wal-9.3 { |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 436 | file delete -force test2.db test2.db-wal |
| 437 | file copy test.db test2.db |
| 438 | file copy test.db-wal test2.db-wal |
| 439 | sqlite3_wal db3 test2.db |
| 440 | execsql {PRAGMA integrity_check } db3 |
| 441 | } {ok} |
| 442 | db3 close |
| 443 | |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 444 | do_test wal-9.4 { |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 445 | execsql { PRAGMA wal_checkpoint } |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 446 | db2 close |
| 447 | sqlite3_wal db2 test.db |
| 448 | execsql {PRAGMA integrity_check } db2 |
| 449 | } {ok} |
| 450 | |
dan | 80a1526 | 2010-04-13 11:45:31 +0000 | [diff] [blame] | 451 | foreach handle {db db2 db3} { catch { $handle close } } |
| 452 | unset handle |
| 453 | |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 454 | #------------------------------------------------------------------------- |
| 455 | # The following block of tests - wal-10.* - test that the WAL locking |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 456 | # scheme works in simple cases. This block of tests is run twice. Once |
| 457 | # using multiple connections in the address space of the current process, |
| 458 | # and once with all connections except one running in external processes. |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 459 | # |
dan | a4a9095 | 2010-06-15 19:07:42 +0000 | [diff] [blame] | 460 | do_multiclient_test tn { |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 461 | |
| 462 | # Initialize the database schema and contents. |
| 463 | # |
| 464 | do_test wal-10.$tn.1 { |
| 465 | execsql { |
dan | a4a9095 | 2010-06-15 19:07:42 +0000 | [diff] [blame] | 466 | PRAGMA journal_mode = wal; |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 467 | CREATE TABLE t1(a, b); |
| 468 | INSERT INTO t1 VALUES(1, 2); |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 469 | SELECT * FROM t1; |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 470 | } |
dan | a4a9095 | 2010-06-15 19:07:42 +0000 | [diff] [blame] | 471 | } {wal 1 2} |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 472 | |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 473 | # Open a transaction and write to the database using [db]. Check that [db2] |
| 474 | # is still able to read the snapshot before the transaction was opened. |
| 475 | # |
| 476 | do_test wal-10.$tn.2 { |
| 477 | execsql { BEGIN; INSERT INTO t1 VALUES(3, 4); } |
| 478 | sql2 {SELECT * FROM t1} |
| 479 | } {1 2} |
| 480 | |
| 481 | # Have [db] commit the transaction. Check that [db2] is now seeing the |
| 482 | # new, updated snapshot. |
| 483 | # |
| 484 | do_test wal-10.$tn.3 { |
| 485 | execsql { COMMIT } |
| 486 | sql2 {SELECT * FROM t1} |
| 487 | } {1 2 3 4} |
| 488 | |
| 489 | # Have [db2] open a read transaction. Then write to the db via [db]. Check |
| 490 | # that [db2] is still seeing the original snapshot. Then read with [db3]. |
| 491 | # [db3] should see the newly committed data. |
| 492 | # |
| 493 | do_test wal-10.$tn.4 { |
| 494 | sql2 { BEGIN ; SELECT * FROM t1} |
| 495 | } {1 2 3 4} |
| 496 | do_test wal-10.$tn.5 { |
| 497 | execsql { INSERT INTO t1 VALUES(5, 6); } |
| 498 | sql2 {SELECT * FROM t1} |
| 499 | } {1 2 3 4} |
| 500 | do_test wal-10.$tn.6 { |
| 501 | sql3 {SELECT * FROM t1} |
| 502 | } {1 2 3 4 5 6} |
| 503 | do_test wal-10.$tn.7 { |
| 504 | sql2 COMMIT |
| 505 | } {} |
| 506 | |
| 507 | # Have [db2] open a write transaction. Then attempt to write to the |
| 508 | # database via [db]. This should fail (writer lock cannot be obtained). |
| 509 | # |
| 510 | # Then open a read-transaction with [db]. Commit the [db2] transaction |
| 511 | # to disk. Verify that [db] still cannot write to the database (because |
| 512 | # it is reading an old snapshot). |
| 513 | # |
| 514 | # Close the current [db] transaction. Open a new one. [db] can now write |
| 515 | # to the database (as it is not locked and [db] is reading the latest |
| 516 | # snapshot). |
| 517 | # |
| 518 | do_test wal-10.$tn.7 { |
| 519 | sql2 { BEGIN; INSERT INTO t1 VALUES(7, 8) ; } |
| 520 | catchsql { INSERT INTO t1 VALUES(9, 10) } |
| 521 | } {1 {database is locked}} |
| 522 | do_test wal-10.$tn.8 { |
| 523 | execsql { BEGIN ; SELECT * FROM t1 } |
| 524 | } {1 2 3 4 5 6} |
| 525 | do_test wal-10.$tn.9 { |
| 526 | sql2 COMMIT |
| 527 | catchsql { INSERT INTO t1 VALUES(9, 10) } |
| 528 | } {1 {database is locked}} |
| 529 | do_test wal-10.$tn.10 { |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 530 | execsql { COMMIT } |
| 531 | execsql { BEGIN } |
| 532 | execsql { INSERT INTO t1 VALUES(9, 10) } |
| 533 | execsql { COMMIT } |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 534 | execsql { SELECT * FROM t1 } |
| 535 | } {1 2 3 4 5 6 7 8 9 10} |
| 536 | |
| 537 | # Open a read transaction with [db2]. Check that this prevents [db] from |
| 538 | # checkpointing the database. But not from writing to it. |
| 539 | # |
| 540 | do_test wal-10.$tn.11 { |
| 541 | sql2 { BEGIN; SELECT * FROM t1 } |
| 542 | } {1 2 3 4 5 6 7 8 9 10} |
| 543 | do_test wal-10.$tn.12 { |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 544 | catchsql { PRAGMA wal_checkpoint } |
drh | 34116ea | 2010-05-31 12:30:52 +0000 | [diff] [blame] | 545 | } {0 {}} ;# Reader no longer block checkpoints |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 546 | do_test wal-10.$tn.13 { |
| 547 | execsql { INSERT INTO t1 VALUES(11, 12) } |
| 548 | sql2 {SELECT * FROM t1} |
| 549 | } {1 2 3 4 5 6 7 8 9 10} |
| 550 | |
drh | 34116ea | 2010-05-31 12:30:52 +0000 | [diff] [blame] | 551 | # Writers do not block checkpoints any more either. |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 552 | # |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 553 | do_test wal-10.$tn.14 { |
drh | 34116ea | 2010-05-31 12:30:52 +0000 | [diff] [blame] | 554 | catchsql { PRAGMA wal_checkpoint } |
| 555 | } {0 {}} |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 556 | |
drh | a2ac9df | 2010-05-31 13:11:49 +0000 | [diff] [blame] | 557 | # The following series of test cases used to verify another blocking |
| 558 | # case in WAL - a case which no longer blocks. |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 559 | # |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 560 | do_test wal-10.$tn.15 { |
drh | 34116ea | 2010-05-31 12:30:52 +0000 | [diff] [blame] | 561 | sql2 { COMMIT; BEGIN; SELECT * FROM t1; } |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 562 | } {1 2 3 4 5 6 7 8 9 10 11 12} |
| 563 | do_test wal-10.$tn.16 { |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 564 | catchsql { PRAGMA wal_checkpoint } |
drh | a2ac9df | 2010-05-31 13:11:49 +0000 | [diff] [blame] | 565 | } {0 {}} |
dan | 49320f8 | 2010-04-14 18:50:08 +0000 | [diff] [blame] | 566 | do_test wal-10.$tn.17 { |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 567 | execsql { PRAGMA wal_checkpoint } |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 568 | } {} |
dan | 49320f8 | 2010-04-14 18:50:08 +0000 | [diff] [blame] | 569 | do_test wal-10.$tn.18 { |
drh | a2ac9df | 2010-05-31 13:11:49 +0000 | [diff] [blame] | 570 | sql3 { BEGIN; SELECT * FROM t1 } |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 571 | } {1 2 3 4 5 6 7 8 9 10 11 12} |
dan | 49320f8 | 2010-04-14 18:50:08 +0000 | [diff] [blame] | 572 | do_test wal-10.$tn.19 { |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 573 | catchsql { INSERT INTO t1 VALUES(13, 14) } |
drh | a2ac9df | 2010-05-31 13:11:49 +0000 | [diff] [blame] | 574 | } {0 {}} |
dan | 49320f8 | 2010-04-14 18:50:08 +0000 | [diff] [blame] | 575 | do_test wal-10.$tn.20 { |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 576 | execsql { SELECT * FROM t1 } |
drh | a2ac9df | 2010-05-31 13:11:49 +0000 | [diff] [blame] | 577 | } {1 2 3 4 5 6 7 8 9 10 11 12 13 14} |
dan | 49320f8 | 2010-04-14 18:50:08 +0000 | [diff] [blame] | 578 | do_test wal-10.$tn.21 { |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 579 | sql3 COMMIT |
drh | a2ac9df | 2010-05-31 13:11:49 +0000 | [diff] [blame] | 580 | sql2 COMMIT |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 581 | } {} |
dan | 49320f8 | 2010-04-14 18:50:08 +0000 | [diff] [blame] | 582 | do_test wal-10.$tn.22 { |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 583 | execsql { SELECT * FROM t1 } |
| 584 | } {1 2 3 4 5 6 7 8 9 10 11 12 13 14} |
dan | b9bf16b | 2010-04-14 11:23:30 +0000 | [diff] [blame] | 585 | |
drh | a2ac9df | 2010-05-31 13:11:49 +0000 | [diff] [blame] | 586 | # Another series of tests that used to demonstrate blocking behavior |
| 587 | # but which now work. |
dan | 49320f8 | 2010-04-14 18:50:08 +0000 | [diff] [blame] | 588 | # |
| 589 | do_test wal-10.$tn.23 { |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 590 | execsql { PRAGMA wal_checkpoint } |
dan | 49320f8 | 2010-04-14 18:50:08 +0000 | [diff] [blame] | 591 | } {} |
| 592 | do_test wal-10.$tn.24 { |
| 593 | sql2 { BEGIN; SELECT * FROM t1; } |
| 594 | } {1 2 3 4 5 6 7 8 9 10 11 12 13 14} |
| 595 | do_test wal-10.$tn.25 { |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 596 | execsql { PRAGMA wal_checkpoint } |
dan | 49320f8 | 2010-04-14 18:50:08 +0000 | [diff] [blame] | 597 | } {} |
| 598 | do_test wal-10.$tn.26 { |
| 599 | catchsql { INSERT INTO t1 VALUES(15, 16) } |
drh | a2ac9df | 2010-05-31 13:11:49 +0000 | [diff] [blame] | 600 | } {0 {}} |
dan | 49320f8 | 2010-04-14 18:50:08 +0000 | [diff] [blame] | 601 | do_test wal-10.$tn.27 { |
drh | a2ac9df | 2010-05-31 13:11:49 +0000 | [diff] [blame] | 602 | sql3 { INSERT INTO t1 VALUES(17, 18) } |
dan | 49320f8 | 2010-04-14 18:50:08 +0000 | [diff] [blame] | 603 | } {} |
| 604 | do_test wal-10.$tn.28 { |
| 605 | code3 { |
| 606 | set ::STMT [sqlite3_prepare db3 "SELECT * FROM t1" -1 TAIL] |
| 607 | sqlite3_step $::STMT |
| 608 | } |
dan | 49320f8 | 2010-04-14 18:50:08 +0000 | [diff] [blame] | 609 | execsql { SELECT * FROM t1 } |
drh | a2ac9df | 2010-05-31 13:11:49 +0000 | [diff] [blame] | 610 | } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18} |
dan | 49320f8 | 2010-04-14 18:50:08 +0000 | [diff] [blame] | 611 | do_test wal-10.$tn.29 { |
drh | a2ac9df | 2010-05-31 13:11:49 +0000 | [diff] [blame] | 612 | execsql { INSERT INTO t1 VALUES(19, 20) } |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 613 | catchsql { PRAGMA wal_checkpoint } |
drh | a2ac9df | 2010-05-31 13:11:49 +0000 | [diff] [blame] | 614 | } {0 {}} |
dan | 49320f8 | 2010-04-14 18:50:08 +0000 | [diff] [blame] | 615 | do_test wal-10.$tn.30 { |
| 616 | code3 { sqlite3_finalize $::STMT } |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 617 | execsql { PRAGMA wal_checkpoint } |
dan | 49320f8 | 2010-04-14 18:50:08 +0000 | [diff] [blame] | 618 | } {} |
| 619 | |
| 620 | # At one point, if a reader failed to upgrade to a writer because it |
| 621 | # was reading an old snapshot, the write-locks were not being released. |
| 622 | # Test that this bug has been fixed. |
| 623 | # |
| 624 | do_test wal-10.$tn.31 { |
drh | a2ac9df | 2010-05-31 13:11:49 +0000 | [diff] [blame] | 625 | sql2 COMMIT |
dan | 49320f8 | 2010-04-14 18:50:08 +0000 | [diff] [blame] | 626 | execsql { BEGIN ; SELECT * FROM t1 } |
drh | a2ac9df | 2010-05-31 13:11:49 +0000 | [diff] [blame] | 627 | sql2 { INSERT INTO t1 VALUES(21, 22) } |
| 628 | catchsql { INSERT INTO t1 VALUES(23, 24) } |
dan | 49320f8 | 2010-04-14 18:50:08 +0000 | [diff] [blame] | 629 | } {1 {database is locked}} |
| 630 | do_test wal-10.$tn.32 { |
| 631 | # This statement would fail when the bug was present. |
drh | a2ac9df | 2010-05-31 13:11:49 +0000 | [diff] [blame] | 632 | sql2 { INSERT INTO t1 VALUES(23, 24) } |
dan | 49320f8 | 2010-04-14 18:50:08 +0000 | [diff] [blame] | 633 | } {} |
| 634 | do_test wal-10.$tn.33 { |
| 635 | execsql { SELECT * FROM t1 ; COMMIT } |
drh | a2ac9df | 2010-05-31 13:11:49 +0000 | [diff] [blame] | 636 | } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20} |
dan | 49320f8 | 2010-04-14 18:50:08 +0000 | [diff] [blame] | 637 | do_test wal-10.$tn.34 { |
| 638 | execsql { SELECT * FROM t1 } |
drh | a2ac9df | 2010-05-31 13:11:49 +0000 | [diff] [blame] | 639 | } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24} |
dan | 49320f8 | 2010-04-14 18:50:08 +0000 | [diff] [blame] | 640 | |
dan | 8b348af | 2010-04-27 18:43:16 +0000 | [diff] [blame] | 641 | # Test that if a checkpointer cannot obtain the required locks, it |
| 642 | # releases all locks before returning a busy error. |
| 643 | # |
| 644 | do_test wal-10.$tn.35 { |
| 645 | execsql { |
| 646 | DELETE FROM t1; |
| 647 | INSERT INTO t1 VALUES('a', 'b'); |
| 648 | INSERT INTO t1 VALUES('c', 'd'); |
| 649 | } |
| 650 | sql2 { |
| 651 | BEGIN; |
| 652 | SELECT * FROM t1; |
| 653 | } |
| 654 | } {a b c d} |
dan | 8b348af | 2010-04-27 18:43:16 +0000 | [diff] [blame] | 655 | do_test wal-10.$tn.36 { |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 656 | catchsql { PRAGMA wal_checkpoint } |
drh | a2ac9df | 2010-05-31 13:11:49 +0000 | [diff] [blame] | 657 | } {0 {}} |
dan | 8b348af | 2010-04-27 18:43:16 +0000 | [diff] [blame] | 658 | do_test wal-10.$tn.36 { |
| 659 | sql3 { INSERT INTO t1 VALUES('e', 'f') } |
| 660 | sql2 { SELECT * FROM t1 } |
| 661 | } {a b c d} |
| 662 | do_test wal-10.$tn.37 { |
| 663 | sql2 COMMIT |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 664 | execsql { PRAGMA wal_checkpoint } |
dan | 8b348af | 2010-04-27 18:43:16 +0000 | [diff] [blame] | 665 | } {} |
dan | e264d98 | 2010-04-14 18:06:50 +0000 | [diff] [blame] | 666 | } |
| 667 | |
dan | 4cc6fb6 | 2010-04-15 16:45:34 +0000 | [diff] [blame] | 668 | #------------------------------------------------------------------------- |
| 669 | # This block of tests, wal-11.*, test that nothing goes terribly wrong |
| 670 | # if frames must be written to the log file before a transaction is |
| 671 | # committed (in order to free up memory). |
| 672 | # |
| 673 | do_test wal-11.1 { |
| 674 | reopen_db |
| 675 | execsql { |
| 676 | PRAGMA cache_size = 10; |
| 677 | PRAGMA page_size = 1024; |
| 678 | CREATE TABLE t1(x PRIMARY KEY); |
| 679 | } |
| 680 | list [expr [file size test.db]/1024] [expr [file size test.db-wal]/1044] |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 681 | } {1 3} |
dan | 4cc6fb6 | 2010-04-15 16:45:34 +0000 | [diff] [blame] | 682 | do_test wal-11.2 { |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 683 | execsql { PRAGMA wal_checkpoint } |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 684 | list [expr [file size test.db]/1024] [file size test.db-wal] |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 685 | } [list 3 [wal_file_size 3 1024]] |
dan | 4cc6fb6 | 2010-04-15 16:45:34 +0000 | [diff] [blame] | 686 | do_test wal-11.3 { |
dan | 6703239 | 2010-04-17 15:42:43 +0000 | [diff] [blame] | 687 | execsql { INSERT INTO t1 VALUES( blob(900) ) } |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 688 | list [expr [file size test.db]/1024] [file size test.db-wal] |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 689 | } [list 3 [wal_file_size 4 1024]] |
dan | 4cc6fb6 | 2010-04-15 16:45:34 +0000 | [diff] [blame] | 690 | |
| 691 | do_test wal-11.4 { |
| 692 | execsql { |
| 693 | BEGIN; |
dan | 6703239 | 2010-04-17 15:42:43 +0000 | [diff] [blame] | 694 | INSERT INTO t1 SELECT blob(900) FROM t1; -- 2 |
| 695 | INSERT INTO t1 SELECT blob(900) FROM t1; -- 4 |
| 696 | INSERT INTO t1 SELECT blob(900) FROM t1; -- 8 |
| 697 | INSERT INTO t1 SELECT blob(900) FROM t1; -- 16 |
dan | 4cc6fb6 | 2010-04-15 16:45:34 +0000 | [diff] [blame] | 698 | } |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 699 | list [expr [file size test.db]/1024] [file size test.db-wal] |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 700 | } [list 3 [wal_file_size 32 1024]] |
dan | 4cc6fb6 | 2010-04-15 16:45:34 +0000 | [diff] [blame] | 701 | do_test wal-11.5 { |
| 702 | execsql { |
| 703 | SELECT count(*) FROM t1; |
| 704 | PRAGMA integrity_check; |
| 705 | } |
| 706 | } {16 ok} |
| 707 | do_test wal-11.6 { |
| 708 | execsql COMMIT |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 709 | list [expr [file size test.db]/1024] [file size test.db-wal] |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 710 | } [list 3 [wal_file_size 41 1024]] |
dan | 4cc6fb6 | 2010-04-15 16:45:34 +0000 | [diff] [blame] | 711 | do_test wal-11.7 { |
| 712 | execsql { |
| 713 | SELECT count(*) FROM t1; |
| 714 | PRAGMA integrity_check; |
| 715 | } |
| 716 | } {16 ok} |
| 717 | do_test wal-11.8 { |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 718 | execsql { PRAGMA wal_checkpoint } |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 719 | list [expr [file size test.db]/1024] [file size test.db-wal] |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 720 | } [list 37 [wal_file_size 41 1024]] |
dan | 4cc6fb6 | 2010-04-15 16:45:34 +0000 | [diff] [blame] | 721 | do_test wal-11.9 { |
| 722 | db close |
dan | 3de777f | 2010-04-17 12:31:37 +0000 | [diff] [blame] | 723 | list [expr [file size test.db]/1024] [log_deleted test.db-wal] |
| 724 | } {37 1} |
dan | 6703239 | 2010-04-17 15:42:43 +0000 | [diff] [blame] | 725 | sqlite3_wal db test.db |
dan | 4cc6fb6 | 2010-04-15 16:45:34 +0000 | [diff] [blame] | 726 | do_test wal-11.10 { |
| 727 | execsql { |
| 728 | PRAGMA cache_size = 10; |
| 729 | BEGIN; |
dan | 6703239 | 2010-04-17 15:42:43 +0000 | [diff] [blame] | 730 | INSERT INTO t1 SELECT blob(900) FROM t1; -- 32 |
dan | 4cc6fb6 | 2010-04-15 16:45:34 +0000 | [diff] [blame] | 731 | SELECT count(*) FROM t1; |
| 732 | } |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 733 | list [expr [file size test.db]/1024] [file size test.db-wal] |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 734 | } [list 37 [wal_file_size 37 1024]] |
dan | 4cc6fb6 | 2010-04-15 16:45:34 +0000 | [diff] [blame] | 735 | do_test wal-11.11 { |
| 736 | execsql { |
| 737 | SELECT count(*) FROM t1; |
| 738 | ROLLBACK; |
| 739 | SELECT count(*) FROM t1; |
| 740 | } |
| 741 | } {32 16} |
| 742 | do_test wal-11.12 { |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 743 | list [expr [file size test.db]/1024] [file size test.db-wal] |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 744 | } [list 37 [wal_file_size 37 1024]] |
dan | 4cc6fb6 | 2010-04-15 16:45:34 +0000 | [diff] [blame] | 745 | do_test wal-11.13 { |
| 746 | execsql { |
dan | 6703239 | 2010-04-17 15:42:43 +0000 | [diff] [blame] | 747 | INSERT INTO t1 VALUES( blob(900) ); |
dan | 4cc6fb6 | 2010-04-15 16:45:34 +0000 | [diff] [blame] | 748 | SELECT count(*) FROM t1; |
| 749 | PRAGMA integrity_check; |
| 750 | } |
| 751 | } {17 ok} |
| 752 | do_test wal-11.14 { |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 753 | list [expr [file size test.db]/1024] [file size test.db-wal] |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 754 | } [list 37 [wal_file_size 37 1024]] |
dan | 4cc6fb6 | 2010-04-15 16:45:34 +0000 | [diff] [blame] | 755 | |
| 756 | |
dan | 4a4b01d | 2010-04-16 11:30:18 +0000 | [diff] [blame] | 757 | #------------------------------------------------------------------------- |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 758 | # This block of tests, wal-12.*, tests the fix for a problem that |
| 759 | # could occur if a log that is a prefix of an older log is written |
| 760 | # into a reused log file. |
dan | 4a4b01d | 2010-04-16 11:30:18 +0000 | [diff] [blame] | 761 | # |
| 762 | reopen_db |
| 763 | do_test wal-12.1 { |
| 764 | execsql { |
| 765 | PRAGMA page_size = 1024; |
| 766 | CREATE TABLE t1(x, y); |
| 767 | CREATE TABLE t2(x, y); |
| 768 | INSERT INTO t1 VALUES('A', 1); |
| 769 | } |
dan | 97a3135 | 2010-04-16 13:59:31 +0000 | [diff] [blame] | 770 | list [expr [file size test.db]/1024] [file size test.db-wal] |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 771 | } [list 1 [wal_file_size 5 1024]] |
dan | 4a4b01d | 2010-04-16 11:30:18 +0000 | [diff] [blame] | 772 | do_test wal-12.2 { |
| 773 | db close |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 774 | sqlite3 db test.db |
dan | 4a4b01d | 2010-04-16 11:30:18 +0000 | [diff] [blame] | 775 | execsql { |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 776 | PRAGMA synchronous = normal; |
dan | 4a4b01d | 2010-04-16 11:30:18 +0000 | [diff] [blame] | 777 | UPDATE t1 SET y = 0 WHERE x = 'A'; |
| 778 | } |
| 779 | list [expr [file size test.db]/1024] [expr [file size test.db-wal]/1044] |
| 780 | } {3 1} |
| 781 | do_test wal-12.3 { |
| 782 | execsql { INSERT INTO t2 VALUES('B', 1) } |
| 783 | list [expr [file size test.db]/1024] [expr [file size test.db-wal]/1044] |
| 784 | } {3 2} |
dan | 4a4b01d | 2010-04-16 11:30:18 +0000 | [diff] [blame] | 785 | do_test wal-12.4 { |
| 786 | file copy -force test.db test2.db |
| 787 | file copy -force test.db-wal test2.db-wal |
| 788 | sqlite3_wal db2 test2.db |
dan | 4a4b01d | 2010-04-16 11:30:18 +0000 | [diff] [blame] | 789 | execsql { SELECT * FROM t2 } db2 |
| 790 | } {B 1} |
| 791 | db2 close |
dan | 4a4b01d | 2010-04-16 11:30:18 +0000 | [diff] [blame] | 792 | do_test wal-12.5 { |
| 793 | execsql { |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 794 | PRAGMA wal_checkpoint; |
dan | 4a4b01d | 2010-04-16 11:30:18 +0000 | [diff] [blame] | 795 | UPDATE t2 SET y = 2 WHERE x = 'B'; |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 796 | PRAGMA wal_checkpoint; |
dan | 4a4b01d | 2010-04-16 11:30:18 +0000 | [diff] [blame] | 797 | UPDATE t1 SET y = 1 WHERE x = 'A'; |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 798 | PRAGMA wal_checkpoint; |
dan | 4a4b01d | 2010-04-16 11:30:18 +0000 | [diff] [blame] | 799 | UPDATE t1 SET y = 0 WHERE x = 'A'; |
| 800 | SELECT * FROM t2; |
| 801 | } |
| 802 | } {B 2} |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 803 | do_test wal-12.6 { |
dan | 4a4b01d | 2010-04-16 11:30:18 +0000 | [diff] [blame] | 804 | file copy -force test.db test2.db |
| 805 | file copy -force test.db-wal test2.db-wal |
| 806 | sqlite3_wal db2 test2.db |
| 807 | execsql { SELECT * FROM t2 } db2 |
| 808 | } {B 2} |
| 809 | db2 close |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 810 | db close |
| 811 | |
| 812 | #------------------------------------------------------------------------- |
| 813 | # Test large log summaries. |
| 814 | # |
dan | 8d6ad1c | 2010-05-04 10:36:20 +0000 | [diff] [blame] | 815 | # In this case "large" usually means a log file that requires a wal-index |
| 816 | # mapping larger than 64KB (the default initial allocation). A 64KB wal-index |
| 817 | # is large enough for a log file that contains approximately 13100 frames. |
| 818 | # So the following tests create logs containing at least this many frames. |
| 819 | # |
| 820 | # wal-13.1.*: This test case creates a very large log file within the |
| 821 | # file-system (around 200MB). The log file does not contain |
| 822 | # any valid frames. Test that the database file can still be |
| 823 | # opened and queried, and that the invalid log file causes no |
| 824 | # problems. |
| 825 | # |
| 826 | # wal-13.2.*: Test that a process may create a large log file and query |
| 827 | # the database (including the log file that it itself created). |
| 828 | # |
| 829 | # wal-13.3.*: Test that if a very large log file is created, and then a |
| 830 | # second connection is opened on the database file, it is possible |
| 831 | # to query the database (and the very large log) using the |
| 832 | # second connection. |
| 833 | # |
| 834 | # wal-13.4.*: Same test as wal-13.3.*. Except in this case the second |
| 835 | # connection is opened by an external process. |
| 836 | # |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 837 | do_test wal-13.1.1 { |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 838 | list [file exists test.db] [file exists test.db-wal] |
| 839 | } {1 0} |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 840 | do_test wal-13.1.2 { |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 841 | set fd [open test.db-wal w] |
| 842 | seek $fd [expr 200*1024*1024] |
| 843 | puts $fd "" |
| 844 | close $fd |
| 845 | sqlite3 db test.db |
| 846 | execsql { SELECT * FROM t2 } |
| 847 | } {B 2} |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 848 | do_test wal-13.1.3 { |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 849 | db close |
| 850 | file exists test.db-wal |
| 851 | } {0} |
dan | 8d6ad1c | 2010-05-04 10:36:20 +0000 | [diff] [blame] | 852 | |
| 853 | do_test wal-13.2.1 { |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 854 | sqlite3 db test.db |
| 855 | execsql { SELECT count(*) FROM t2 } |
| 856 | } {1} |
dan | 8d6ad1c | 2010-05-04 10:36:20 +0000 | [diff] [blame] | 857 | do_test wal-13.2.2 { |
dan | 65bddc1 | 2010-05-07 12:49:22 +0000 | [diff] [blame] | 858 | db function blob blob |
dan | 8d6ad1c | 2010-05-04 10:36:20 +0000 | [diff] [blame] | 859 | for {set i 0} {$i < 16} {incr i} { |
dan | 65bddc1 | 2010-05-07 12:49:22 +0000 | [diff] [blame] | 860 | execsql { INSERT INTO t2 SELECT blob(400), blob(400) FROM t2 } |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 861 | } |
| 862 | execsql { SELECT count(*) FROM t2 } |
dan | 8d6ad1c | 2010-05-04 10:36:20 +0000 | [diff] [blame] | 863 | } [expr int(pow(2, 16))] |
dan | 65bddc1 | 2010-05-07 12:49:22 +0000 | [diff] [blame] | 864 | do_test wal-13.2.3 { |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 865 | expr [file size test.db-wal] > [wal_file_size 33000 1024] |
dan | c6315a4 | 2010-05-07 13:52:42 +0000 | [diff] [blame] | 866 | } 1 |
dan | ce4f05f | 2010-04-22 19:14:13 +0000 | [diff] [blame] | 867 | |
dan | a4a9095 | 2010-06-15 19:07:42 +0000 | [diff] [blame] | 868 | do_multiclient_test tn { |
| 869 | incr tn 2 |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 870 | |
| 871 | do_test wal-13.$tn.0 { |
dan | a4a9095 | 2010-06-15 19:07:42 +0000 | [diff] [blame] | 872 | sql1 { |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 873 | PRAGMA journal_mode = WAL; |
| 874 | CREATE TABLE t1(x); |
dan | 8d6ad1c | 2010-05-04 10:36:20 +0000 | [diff] [blame] | 875 | INSERT INTO t1 SELECT randomblob(800); |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 876 | } |
dan | a4a9095 | 2010-06-15 19:07:42 +0000 | [diff] [blame] | 877 | sql1 { SELECT count(*) FROM t1 } |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 878 | } {1} |
| 879 | |
| 880 | for {set ii 1} {$ii<16} {incr ii} { |
| 881 | do_test wal-13.$tn.$ii.a { |
dan | a4a9095 | 2010-06-15 19:07:42 +0000 | [diff] [blame] | 882 | sql2 { INSERT INTO t1 SELECT randomblob(800) FROM t1 } |
| 883 | sql2 { SELECT count(*) FROM t1 } |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 884 | } [expr (1<<$ii)] |
| 885 | do_test wal-13.$tn.$ii.b { |
dan | a4a9095 | 2010-06-15 19:07:42 +0000 | [diff] [blame] | 886 | sql1 { SELECT count(*) FROM t1 } |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 887 | } [expr (1<<$ii)] |
| 888 | do_test wal-13.$tn.$ii.c { |
dan | a4a9095 | 2010-06-15 19:07:42 +0000 | [diff] [blame] | 889 | sql1 { SELECT count(*) FROM t1 } |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 890 | } [expr (1<<$ii)] |
drh | 1b48aa4 | 2010-04-30 17:28:35 +0000 | [diff] [blame] | 891 | do_test wal-13.$tn.$ii.d { |
dan | a4a9095 | 2010-06-15 19:07:42 +0000 | [diff] [blame] | 892 | sql1 { PRAGMA integrity_check } |
drh | 1b48aa4 | 2010-04-30 17:28:35 +0000 | [diff] [blame] | 893 | } {ok} |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 894 | } |
dan | 31f98fc | 2010-04-27 05:42:32 +0000 | [diff] [blame] | 895 | } |
dan | 4a4b01d | 2010-04-16 11:30:18 +0000 | [diff] [blame] | 896 | |
dan | 31c0390 | 2010-04-29 14:51:33 +0000 | [diff] [blame] | 897 | #------------------------------------------------------------------------- |
| 898 | # Check a fun corruption case has been fixed. |
| 899 | # |
| 900 | # The problem was that after performing a checkpoint using a connection |
| 901 | # that had an out-of-date pager-cache, the next time the connection was |
| 902 | # used it did not realize the cache was out-of-date and proceeded to |
| 903 | # operate with an inconsistent cache. Leading to corruption. |
| 904 | # |
dan | 31c0390 | 2010-04-29 14:51:33 +0000 | [diff] [blame] | 905 | catch { db close } |
| 906 | catch { db2 close } |
| 907 | catch { db3 close } |
| 908 | file delete -force test.db test.db-wal |
| 909 | sqlite3 db test.db |
| 910 | sqlite3 db2 test.db |
dan | 31c0390 | 2010-04-29 14:51:33 +0000 | [diff] [blame] | 911 | do_test wal-14 { |
| 912 | execsql { |
| 913 | PRAGMA journal_mode = WAL; |
| 914 | CREATE TABLE t1(a PRIMARY KEY, b); |
| 915 | INSERT INTO t1 VALUES(randomblob(10), randomblob(100)); |
| 916 | INSERT INTO t1 SELECT randomblob(10), randomblob(100) FROM t1; |
| 917 | INSERT INTO t1 SELECT randomblob(10), randomblob(100) FROM t1; |
| 918 | INSERT INTO t1 SELECT randomblob(10), randomblob(100) FROM t1; |
| 919 | } |
| 920 | |
| 921 | db2 eval { |
| 922 | INSERT INTO t1 SELECT randomblob(10), randomblob(100); |
| 923 | INSERT INTO t1 SELECT randomblob(10), randomblob(100); |
| 924 | INSERT INTO t1 SELECT randomblob(10), randomblob(100); |
| 925 | INSERT INTO t1 SELECT randomblob(10), randomblob(100); |
| 926 | } |
| 927 | |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 928 | # After executing the "PRAGMA wal_checkpoint", connection [db] was being |
dan | 31c0390 | 2010-04-29 14:51:33 +0000 | [diff] [blame] | 929 | # left with an inconsistent cache. Running the CREATE INDEX statement |
| 930 | # in this state led to database corruption. |
| 931 | catchsql { |
dan | 5a299f9 | 2010-05-03 11:05:08 +0000 | [diff] [blame] | 932 | PRAGMA wal_checkpoint; |
dan | 31c0390 | 2010-04-29 14:51:33 +0000 | [diff] [blame] | 933 | CREATE INDEX i1 on t1(b); |
| 934 | } |
| 935 | |
| 936 | db2 eval { PRAGMA integrity_check } |
| 937 | } {ok} |
| 938 | |
dan | 185cca6 | 2010-04-29 14:58:53 +0000 | [diff] [blame] | 939 | catch { db close } |
| 940 | catch { db2 close } |
dan | 87c1fe1 | 2010-05-03 12:14:15 +0000 | [diff] [blame] | 941 | |
| 942 | #------------------------------------------------------------------------- |
| 943 | # The following block of tests - wal-15.* - focus on testing the |
| 944 | # implementation of the sqlite3_wal_checkpoint() interface. |
| 945 | # |
| 946 | file delete -force test.db test.db-wal |
| 947 | sqlite3 db test.db |
| 948 | do_test wal-15.1 { |
| 949 | execsql { |
dan | 65bddc1 | 2010-05-07 12:49:22 +0000 | [diff] [blame] | 950 | PRAGMA auto_vacuum = 0; |
dan | 87c1fe1 | 2010-05-03 12:14:15 +0000 | [diff] [blame] | 951 | PRAGMA page_size = 1024; |
| 952 | PRAGMA journal_mode = WAL; |
| 953 | } |
| 954 | execsql { |
| 955 | CREATE TABLE t1(a, b); |
| 956 | INSERT INTO t1 VALUES(1, 2); |
| 957 | } |
| 958 | } {} |
| 959 | |
| 960 | # Test that an error is returned if the database name is not recognized |
| 961 | # |
| 962 | do_test wal-15.2.1 { |
| 963 | sqlite3_wal_checkpoint db aux |
| 964 | } {SQLITE_ERROR} |
| 965 | do_test wal-15.2.2 { |
| 966 | sqlite3_errcode db |
| 967 | } {SQLITE_ERROR} |
| 968 | do_test wal-15.2.3 { |
| 969 | sqlite3_errmsg db |
| 970 | } {unknown database: aux} |
| 971 | |
| 972 | # Test that an error is returned if an attempt is made to checkpoint |
| 973 | # if a transaction is open on the database. |
| 974 | # |
| 975 | do_test wal-15.3.1 { |
| 976 | execsql { |
| 977 | BEGIN; |
| 978 | INSERT INTO t1 VALUES(3, 4); |
| 979 | } |
| 980 | sqlite3_wal_checkpoint db main |
| 981 | } {SQLITE_LOCKED} |
| 982 | do_test wal-15.3.2 { |
| 983 | sqlite3_errcode db |
| 984 | } {SQLITE_LOCKED} |
| 985 | do_test wal-15.3.3 { |
| 986 | sqlite3_errmsg db |
| 987 | } {database table is locked} |
| 988 | |
dan | dcb1169 | 2010-05-31 14:18:45 +0000 | [diff] [blame] | 989 | # Earlier versions returned an error is returned if the db cannot be |
| 990 | # checkpointed because of locks held by another connection. Check that |
| 991 | # this is no longer the case. |
dan | 87c1fe1 | 2010-05-03 12:14:15 +0000 | [diff] [blame] | 992 | # |
| 993 | sqlite3 db2 test.db |
| 994 | do_test wal-15.4.1 { |
| 995 | execsql { |
| 996 | BEGIN; |
| 997 | SELECT * FROM t1; |
| 998 | } db2 |
| 999 | } {1 2} |
| 1000 | do_test wal-15.4.2 { |
| 1001 | execsql { COMMIT } |
| 1002 | sqlite3_wal_checkpoint db |
dan | dcb1169 | 2010-05-31 14:18:45 +0000 | [diff] [blame] | 1003 | } {SQLITE_OK} |
dan | 87c1fe1 | 2010-05-03 12:14:15 +0000 | [diff] [blame] | 1004 | do_test wal-15.4.3 { |
| 1005 | sqlite3_errmsg db |
dan | dcb1169 | 2010-05-31 14:18:45 +0000 | [diff] [blame] | 1006 | } {not an error} |
dan | 87c1fe1 | 2010-05-03 12:14:15 +0000 | [diff] [blame] | 1007 | |
| 1008 | # After [db2] drops its lock, [db] may checkpoint the db. |
| 1009 | # |
| 1010 | do_test wal-15.4.4 { |
| 1011 | execsql { COMMIT } db2 |
| 1012 | sqlite3_wal_checkpoint db |
| 1013 | } {SQLITE_OK} |
| 1014 | do_test wal-15.4.5 { |
| 1015 | sqlite3_errmsg db |
| 1016 | } {not an error} |
| 1017 | do_test wal-15.4.6 { |
| 1018 | file size test.db |
| 1019 | } [expr 1024*2] |
| 1020 | |
| 1021 | catch { db2 close } |
| 1022 | catch { db close } |
dan | af0cfd3 | 2010-05-03 15:58:50 +0000 | [diff] [blame] | 1023 | |
| 1024 | #------------------------------------------------------------------------- |
| 1025 | # The following block of tests - wal-16.* - test that if a NULL pointer or |
| 1026 | # an empty string is passed as the second argument of the wal_checkpoint() |
| 1027 | # API, an attempt is made to checkpoint all attached databases. |
| 1028 | # |
| 1029 | foreach {tn ckpt_cmd ckpt_res ckpt_main ckpt_aux} { |
| 1030 | 1 {sqlite3_wal_checkpoint db} SQLITE_OK 1 1 |
| 1031 | 2 {sqlite3_wal_checkpoint db ""} SQLITE_OK 1 1 |
| 1032 | 3 {db eval "PRAGMA wal_checkpoint"} {} 1 1 |
| 1033 | |
| 1034 | 4 {sqlite3_wal_checkpoint db main} SQLITE_OK 1 0 |
| 1035 | 5 {sqlite3_wal_checkpoint db aux} SQLITE_OK 0 1 |
| 1036 | 6 {sqlite3_wal_checkpoint db temp} SQLITE_OK 0 0 |
| 1037 | 7 {db eval "PRAGMA main.wal_checkpoint"} {} 1 0 |
| 1038 | 8 {db eval "PRAGMA aux.wal_checkpoint"} {} 0 1 |
| 1039 | 9 {db eval "PRAGMA temp.wal_checkpoint"} {} 0 0 |
| 1040 | } { |
| 1041 | do_test wal-16.$tn.1 { |
| 1042 | file delete -force test2.db test2.db-wal test2.db-journal |
| 1043 | file delete -force test.db test.db-wal test.db-journal |
| 1044 | |
| 1045 | sqlite3 db test.db |
| 1046 | execsql { |
| 1047 | ATTACH 'test2.db' AS aux; |
dan | 65bddc1 | 2010-05-07 12:49:22 +0000 | [diff] [blame] | 1048 | PRAGMA main.auto_vacuum = 0; |
| 1049 | PRAGMA aux.auto_vacuum = 0; |
dan | af0cfd3 | 2010-05-03 15:58:50 +0000 | [diff] [blame] | 1050 | PRAGMA main.journal_mode = WAL; |
| 1051 | PRAGMA aux.journal_mode = WAL; |
| 1052 | PRAGMA synchronous = NORMAL; |
| 1053 | } |
| 1054 | } {wal wal} |
| 1055 | |
| 1056 | do_test wal-16.$tn.2 { |
| 1057 | execsql { |
| 1058 | CREATE TABLE main.t1(a, b, PRIMARY KEY(a, b)); |
| 1059 | CREATE TABLE aux.t2(a, b, PRIMARY KEY(a, b)); |
| 1060 | |
| 1061 | INSERT INTO t2 VALUES(1, randomblob(1000)); |
| 1062 | INSERT INTO t2 VALUES(2, randomblob(1000)); |
| 1063 | INSERT INTO t1 SELECT * FROM t2; |
| 1064 | } |
| 1065 | |
| 1066 | list [file size test.db] [file size test.db-wal] |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 1067 | } [list [expr 1*1024] [wal_file_size 10 1024]] |
dan | af0cfd3 | 2010-05-03 15:58:50 +0000 | [diff] [blame] | 1068 | do_test wal-16.$tn.3 { |
| 1069 | list [file size test2.db] [file size test2.db-wal] |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 1070 | } [list [expr 1*1024] [wal_file_size 16 1024]] |
dan | af0cfd3 | 2010-05-03 15:58:50 +0000 | [diff] [blame] | 1071 | |
| 1072 | do_test wal-16.$tn.4 [list eval $ckpt_cmd] $ckpt_res |
| 1073 | |
| 1074 | do_test wal-16.$tn.5 { |
| 1075 | list [file size test.db] [file size test.db-wal] |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 1076 | } [list [expr ($ckpt_main ? 7 : 1)*1024] [wal_file_size 10 1024]] |
dan | af0cfd3 | 2010-05-03 15:58:50 +0000 | [diff] [blame] | 1077 | |
| 1078 | do_test wal-16.$tn.6 { |
| 1079 | list [file size test2.db] [file size test2.db-wal] |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 1080 | } [list [expr ($ckpt_aux ? 7 : 1)*1024] [wal_file_size 16 1024]] |
dan | af0cfd3 | 2010-05-03 15:58:50 +0000 | [diff] [blame] | 1081 | |
| 1082 | catch { db close } |
| 1083 | } |
| 1084 | |
dan | 8d6ad1c | 2010-05-04 10:36:20 +0000 | [diff] [blame] | 1085 | #------------------------------------------------------------------------- |
| 1086 | # The following tests - wal-17.* - attempt to verify that the correct |
| 1087 | # number of "padding" frames are appended to the log file when a transaction |
| 1088 | # is committed in synchronous=FULL mode. |
| 1089 | # |
| 1090 | # Do this by creating a database that uses 512 byte pages. Then writing |
| 1091 | # a transaction that modifies 171 pages. In synchronous=NORMAL mode, this |
| 1092 | # produces a log file of: |
| 1093 | # |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 1094 | # 32 + (24+512)*171 = 90312 bytes. |
dan | 8d6ad1c | 2010-05-04 10:36:20 +0000 | [diff] [blame] | 1095 | # |
| 1096 | # Slightly larger than 11*8192 = 90112 bytes. |
| 1097 | # |
| 1098 | # Run the test using various different sector-sizes. In each case, the |
| 1099 | # WAL code should write the 90300 bytes of log file containing the |
| 1100 | # transaction, then append as may frames as are required to extend the |
| 1101 | # log file so that no part of the next transaction will be written into |
| 1102 | # a disk-sector used by transaction just committed. |
| 1103 | # |
| 1104 | set old_pending_byte [sqlite3_test_control_pending_byte 0x10000000] |
| 1105 | catch { db close } |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 1106 | foreach {tn sectorsize logsize} " |
| 1107 | 1 128 [wal_file_size 172 512] |
| 1108 | 2 256 [wal_file_size 172 512] |
| 1109 | 3 512 [wal_file_size 172 512] |
| 1110 | 4 1024 [wal_file_size 172 512] |
| 1111 | 5 2048 [wal_file_size 172 512] |
| 1112 | 6 4096 [wal_file_size 176 512] |
| 1113 | 7 8192 [wal_file_size 184 512] |
| 1114 | " { |
dan | 8d6ad1c | 2010-05-04 10:36:20 +0000 | [diff] [blame] | 1115 | file delete -force test.db test.db-wal test.db-journal |
| 1116 | sqlite3_simulate_device -sectorsize $sectorsize |
| 1117 | sqlite3 db test.db -vfs devsym |
| 1118 | |
| 1119 | do_test wal-17.$tn.1 { |
| 1120 | execsql { |
| 1121 | PRAGMA auto_vacuum = 0; |
| 1122 | PRAGMA page_size = 512; |
| 1123 | PRAGMA journal_mode = WAL; |
| 1124 | PRAGMA synchronous = FULL; |
| 1125 | } |
| 1126 | execsql { |
| 1127 | BEGIN; |
| 1128 | CREATE TABLE t(x); |
| 1129 | } |
| 1130 | for {set i 0} {$i<166} {incr i} { |
| 1131 | execsql { INSERT INTO t VALUES(randomblob(400)) } |
| 1132 | } |
| 1133 | execsql COMMIT |
| 1134 | |
| 1135 | file size test.db-wal |
| 1136 | } $logsize |
| 1137 | |
| 1138 | do_test wal-17.$tn.2 { |
| 1139 | file size test.db |
| 1140 | } 512 |
| 1141 | |
| 1142 | do_test wal-17.$tn.3 { |
| 1143 | db close |
| 1144 | file size test.db |
| 1145 | } [expr 512*171] |
| 1146 | } |
| 1147 | sqlite3_test_control_pending_byte $old_pending_byte |
| 1148 | |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 1149 | #------------------------------------------------------------------------- |
| 1150 | # This test - wal-18.* - verifies a couple of specific conditions that |
| 1151 | # may be encountered while recovering a log file are handled correctly: |
| 1152 | # |
| 1153 | # wal-18.1.* When the first 32-bits of a frame checksum is correct but |
| 1154 | # the second 32-bits are false, and |
| 1155 | # |
| 1156 | # wal-18.2.* When the page-size field that occurs at the start of a log |
| 1157 | # file is a power of 2 greater than 16384 or smaller than 512. |
| 1158 | # |
| 1159 | file delete -force test.db test.db-wal test.db-journal |
| 1160 | do_test wal-18.0 { |
| 1161 | sqlite3 db test.db |
| 1162 | execsql { |
| 1163 | PRAGMA page_size = 1024; |
| 1164 | PRAGMA auto_vacuum = 0; |
| 1165 | PRAGMA journal_mode = WAL; |
| 1166 | PRAGMA synchronous = OFF; |
| 1167 | |
| 1168 | CREATE TABLE t1(a, b, UNIQUE(a, b)); |
| 1169 | INSERT INTO t1 VALUES(0, 0); |
| 1170 | PRAGMA wal_checkpoint; |
| 1171 | |
| 1172 | INSERT INTO t1 VALUES(1, 2); -- frames 1 and 2 |
| 1173 | INSERT INTO t1 VALUES(3, 4); -- frames 3 and 4 |
| 1174 | INSERT INTO t1 VALUES(5, 6); -- frames 5 and 6 |
| 1175 | } |
| 1176 | |
| 1177 | file copy -force test.db testX.db |
| 1178 | file copy -force test.db-wal testX.db-wal |
| 1179 | db close |
| 1180 | list [file size testX.db] [file size testX.db-wal] |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 1181 | } [list [expr 3*1024] [wal_file_size 6 1024]] |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 1182 | |
dan | c9e4665 | 2010-05-06 13:36:47 +0000 | [diff] [blame] | 1183 | unset -nocomplain nFrame result |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 1184 | foreach {nFrame result} { |
| 1185 | 0 {0 0} |
| 1186 | 1 {0 0} |
| 1187 | 2 {0 0 1 2} |
| 1188 | 3 {0 0 1 2} |
| 1189 | 4 {0 0 1 2 3 4} |
| 1190 | 5 {0 0 1 2 3 4} |
| 1191 | 6 {0 0 1 2 3 4 5 6} |
| 1192 | } { |
| 1193 | do_test wal-18.1.$nFrame { |
| 1194 | file copy -force testX.db test.db |
| 1195 | file copy -force testX.db-wal test.db-wal |
| 1196 | |
drh | 23ea97b | 2010-05-20 16:45:58 +0000 | [diff] [blame] | 1197 | hexio_write test.db-wal [expr 24 + $nFrame*(24+1024) + 20] 00000000 |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 1198 | |
| 1199 | sqlite3 db test.db |
| 1200 | execsql { |
| 1201 | SELECT * FROM t1; |
| 1202 | PRAGMA integrity_check; |
| 1203 | } |
| 1204 | } [concat $result ok] |
| 1205 | db close |
| 1206 | } |
| 1207 | |
| 1208 | proc randomblob {pgsz} { |
| 1209 | sqlite3 rbdb :memory: |
| 1210 | set blob [rbdb one {SELECT randomblob($pgsz)}] |
| 1211 | rbdb close |
| 1212 | set blob |
| 1213 | } |
| 1214 | |
| 1215 | proc logcksum {ckv1 ckv2 blob} { |
| 1216 | upvar $ckv1 c1 |
| 1217 | upvar $ckv2 c2 |
| 1218 | |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 1219 | set scanpattern I* |
| 1220 | if {$::tcl_platform(byteOrder) eq "littleEndian"} { |
| 1221 | set scanpattern i* |
| 1222 | } |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 1223 | |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 1224 | binary scan $blob $scanpattern values |
drh | 4c1cb6a | 2010-05-19 19:09:37 +0000 | [diff] [blame] | 1225 | foreach {v1 v2} $values { |
| 1226 | set c1 [expr {($c1 + $v1 + $c2)&0xFFFFFFFF}] |
| 1227 | set c2 [expr {($c2 + $v2 + $c1)&0xFFFFFFFF}] |
| 1228 | } |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | file copy -force test.db testX.db |
| 1232 | foreach {tn pgsz works} { |
| 1233 | 1 128 0 |
| 1234 | 2 256 0 |
| 1235 | 3 512 1 |
| 1236 | 4 1024 1 |
| 1237 | 5 2048 1 |
| 1238 | 6 4096 1 |
| 1239 | 7 8192 1 |
| 1240 | 8 16384 1 |
| 1241 | 9 32768 1 |
drh | b2eced5 | 2010-08-12 02:41:12 +0000 | [diff] [blame] | 1242 | 10 65536 1 |
| 1243 | 11 131072 0 |
drh | 4c1cb6a | 2010-05-19 19:09:37 +0000 | [diff] [blame] | 1244 | 11 1016 0 |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 1245 | } { |
| 1246 | |
dan | 65bddc1 | 2010-05-07 12:49:22 +0000 | [diff] [blame] | 1247 | if {$::SQLITE_MAX_PAGE_SIZE < $pgsz} { |
| 1248 | set works 0 |
| 1249 | } |
| 1250 | |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 1251 | for {set pg 1} {$pg <= 3} {incr pg} { |
| 1252 | file copy -force testX.db test.db |
| 1253 | file delete -force test.db-wal |
| 1254 | |
| 1255 | # Check that the database now exists and consists of three pages. And |
| 1256 | # that there is no associated wal file. |
| 1257 | # |
| 1258 | do_test wal-18.2.$tn.$pg.1 { file exists test.db-wal } 0 |
| 1259 | do_test wal-18.2.$tn.$pg.2 { file exists test.db } 1 |
| 1260 | do_test wal-18.2.$tn.$pg.3 { file size test.db } [expr 1024*3] |
| 1261 | |
| 1262 | do_test wal-18.2.$tn.$pg.4 { |
| 1263 | |
| 1264 | # Create a wal file that contains a single frame (database page |
| 1265 | # number $pg) with the commit flag set. The frame checksum is |
| 1266 | # correct, but the contents of the database page are corrupt. |
| 1267 | # |
| 1268 | # The page-size in the log file header is set to $pgsz. If the |
| 1269 | # WAL code considers $pgsz to be a valid SQLite database file page-size, |
| 1270 | # the database will be corrupt (because the garbage frame contents |
| 1271 | # will be treated as valid content). If $pgsz is invalid (too small |
| 1272 | # or too large), the db will not be corrupt as the log file will |
| 1273 | # be ignored. |
| 1274 | # |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 1275 | set walhdr [binary format IIIIII 931071618 3007000 $pgsz 1234 22 23] |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 1276 | set framebody [randomblob $pgsz] |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 1277 | set framehdr [binary format IIII $pg 5 22 23] |
| 1278 | set c1 0 |
| 1279 | set c2 0 |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 1280 | logcksum c1 c2 $walhdr |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 1281 | |
| 1282 | append walhdr [binary format II $c1 $c2] |
dan | 71d8991 | 2010-05-24 13:57:42 +0000 | [diff] [blame] | 1283 | logcksum c1 c2 [string range $framehdr 0 7] |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 1284 | logcksum c1 c2 $framebody |
drh | 7e26372 | 2010-05-20 21:21:09 +0000 | [diff] [blame] | 1285 | set framehdr [binary format IIIIII $pg 5 22 23 $c1 $c2] |
dan | b8fd6c2 | 2010-05-24 10:39:36 +0000 | [diff] [blame] | 1286 | |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 1287 | set fd [open test.db-wal w] |
| 1288 | fconfigure $fd -encoding binary -translation binary |
| 1289 | puts -nonewline $fd $walhdr |
| 1290 | puts -nonewline $fd $framehdr |
| 1291 | puts -nonewline $fd $framebody |
| 1292 | close $fd |
| 1293 | |
| 1294 | file size test.db-wal |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 1295 | } [wal_file_size 1 $pgsz] |
dan | b6e099a | 2010-05-04 14:47:39 +0000 | [diff] [blame] | 1296 | |
| 1297 | do_test wal-18.2.$tn.$pg.5 { |
| 1298 | sqlite3 db test.db |
| 1299 | set rc [catch { db one {PRAGMA integrity_check} } msg] |
| 1300 | expr { $rc!=0 || $msg!="ok" } |
| 1301 | } $works |
| 1302 | |
| 1303 | db close |
| 1304 | } |
| 1305 | } |
| 1306 | |
dan | b7d53f5 | 2010-05-06 17:28:08 +0000 | [diff] [blame] | 1307 | #------------------------------------------------------------------------- |
| 1308 | # The following test - wal-19.* - fixes a bug that was present during |
| 1309 | # development. |
| 1310 | # |
| 1311 | # When a database connection in WAL mode is closed, it attempts an |
| 1312 | # EXCLUSIVE lock on the database file. If the lock is obtained, the |
| 1313 | # connection knows that it is the last connection to disconnect from |
| 1314 | # the database, so it runs a checkpoint operation. The bug was that |
| 1315 | # the connection was not updating its private copy of the wal-index |
| 1316 | # header before doing so, meaning that it could checkpoint an old |
| 1317 | # snapshot. |
| 1318 | # |
| 1319 | do_test wal-19.1 { |
| 1320 | file delete -force test.db test.db-wal test.db-journal |
| 1321 | sqlite3 db test.db |
| 1322 | sqlite3 db2 test.db |
| 1323 | execsql { |
| 1324 | PRAGMA journal_mode = WAL; |
| 1325 | CREATE TABLE t1(a, b); |
| 1326 | INSERT INTO t1 VALUES(1, 2); |
| 1327 | INSERT INTO t1 VALUES(3, 4); |
| 1328 | } |
| 1329 | execsql { SELECT * FROM t1 } db2 |
| 1330 | } {1 2 3 4} |
| 1331 | do_test wal-19.2 { |
| 1332 | execsql { |
| 1333 | INSERT INTO t1 VALUES(5, 6); |
| 1334 | SELECT * FROM t1; |
| 1335 | } |
| 1336 | } {1 2 3 4 5 6} |
| 1337 | do_test wal-19.3 { |
| 1338 | db close |
| 1339 | db2 close |
| 1340 | file exists test.db-wal |
| 1341 | } {0} |
| 1342 | do_test wal-19.4 { |
| 1343 | # When the bug was present, the following was returning {1 2 3 4} only, |
| 1344 | # as [db2] had an out-of-date copy of the wal-index header when it was |
| 1345 | # closed. |
| 1346 | # |
| 1347 | sqlite3 db test.db |
| 1348 | execsql { SELECT * FROM t1 } |
| 1349 | } {1 2 3 4 5 6} |
| 1350 | |
dan | 998ad21 | 2010-05-07 06:59:08 +0000 | [diff] [blame] | 1351 | #------------------------------------------------------------------------- |
| 1352 | # This test - wal-20.* - uses two connections. One in this process and |
| 1353 | # the other in an external process. The procedure is: |
| 1354 | # |
| 1355 | # 1. Using connection 1, create the database schema. |
| 1356 | # |
| 1357 | # 2. Using connection 2 (in an external process), add so much |
| 1358 | # data to the database without checkpointing that a wal-index |
| 1359 | # larger than 64KB is required. |
| 1360 | # |
| 1361 | # 3. Using connection 1, checkpoint the database. Make sure all |
| 1362 | # the data is present and the database is not corrupt. |
| 1363 | # |
| 1364 | # At one point, SQLite was failing to grow the mapping of the wal-index |
| 1365 | # file in step 3 and the checkpoint was corrupting the database file. |
| 1366 | # |
| 1367 | do_test wal-20.1 { |
shaneh | a10069d | 2010-05-11 02:46:16 +0000 | [diff] [blame] | 1368 | catch {db close} |
dan | 998ad21 | 2010-05-07 06:59:08 +0000 | [diff] [blame] | 1369 | file delete -force test.db test.db-wal test.db-journal |
| 1370 | sqlite3 db test.db |
| 1371 | execsql { |
| 1372 | PRAGMA journal_mode = WAL; |
| 1373 | CREATE TABLE t1(x); |
| 1374 | INSERT INTO t1 VALUES(randomblob(900)); |
| 1375 | SELECT count(*) FROM t1; |
| 1376 | } |
| 1377 | } {wal 1} |
| 1378 | do_test wal-20.2 { |
| 1379 | set ::buddy [launch_testfixture] |
| 1380 | testfixture $::buddy { |
| 1381 | sqlite3 db test.db |
| 1382 | db transaction { db eval { |
| 1383 | PRAGMA wal_autocheckpoint = 0; |
| 1384 | INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2 */ |
| 1385 | INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4 */ |
| 1386 | INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8 */ |
| 1387 | INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16 */ |
| 1388 | INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 32 */ |
| 1389 | INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 64 */ |
| 1390 | INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 128 */ |
| 1391 | INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 256 */ |
| 1392 | INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 512 */ |
| 1393 | INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 1024 */ |
| 1394 | INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2048 */ |
| 1395 | INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4096 */ |
| 1396 | INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8192 */ |
| 1397 | INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16384 */ |
| 1398 | } } |
| 1399 | } |
| 1400 | } {0} |
| 1401 | do_test wal-20.3 { |
| 1402 | close $::buddy |
| 1403 | execsql { |
| 1404 | PRAGMA wal_checkpoint; |
| 1405 | SELECT count(*) FROM t1; |
| 1406 | } |
| 1407 | } {16384} |
| 1408 | do_test wal-20.4 { |
| 1409 | db close |
| 1410 | sqlite3 db test.db |
| 1411 | execsql { SELECT count(*) FROM t1 } |
| 1412 | } {16384} |
| 1413 | integrity_check wal-20.5 |
| 1414 | |
dan | af0cfd3 | 2010-05-03 15:58:50 +0000 | [diff] [blame] | 1415 | catch { db2 close } |
| 1416 | catch { db close } |
dan | 6e6bd56 | 2010-06-02 18:59:03 +0000 | [diff] [blame] | 1417 | |
| 1418 | do_test wal-21.1 { |
dan | 10f5a50 | 2010-06-23 15:55:43 +0000 | [diff] [blame] | 1419 | faultsim_delete_and_reopen |
dan | 6e6bd56 | 2010-06-02 18:59:03 +0000 | [diff] [blame] | 1420 | execsql { |
| 1421 | PRAGMA journal_mode = WAL; |
| 1422 | CREATE TABLE t1(a, b); |
| 1423 | INSERT INTO t1 VALUES(1, 2); |
| 1424 | INSERT INTO t1 VALUES(3, 4); |
| 1425 | INSERT INTO t1 VALUES(5, 6); |
| 1426 | INSERT INTO t1 VALUES(7, 8); |
| 1427 | INSERT INTO t1 VALUES(9, 10); |
| 1428 | INSERT INTO t1 VALUES(11, 12); |
| 1429 | } |
| 1430 | } {wal} |
| 1431 | do_test wal-21.2 { |
| 1432 | execsql { |
| 1433 | PRAGMA cache_size = 10; |
| 1434 | PRAGMA wal_checkpoint; |
| 1435 | BEGIN; |
| 1436 | SAVEPOINT s; |
| 1437 | INSERT INTO t1 SELECT randomblob(900), randomblob(900) FROM t1; |
| 1438 | ROLLBACK TO s; |
| 1439 | COMMIT; |
| 1440 | SELECT * FROM t1; |
| 1441 | } |
| 1442 | } {1 2 3 4 5 6 7 8 9 10 11 12} |
| 1443 | do_test wal-21.3 { |
| 1444 | execsql { PRAGMA integrity_check } |
| 1445 | } {ok} |
| 1446 | |
dan | 4bcc498 | 2010-08-16 19:23:02 +0000 | [diff] [blame] | 1447 | #------------------------------------------------------------------------- |
| 1448 | # Test reading and writing of databases with different page-sizes. |
| 1449 | # |
| 1450 | foreach pgsz {512 1024 2048 4096 8192 16384 32768 65536} { |
| 1451 | do_multiclient_test tn [string map [list %PGSZ% $pgsz] { |
dan | eb8763d | 2010-08-17 14:52:22 +0000 | [diff] [blame] | 1452 | do_test wal-22.%PGSZ%.$tn.1 { |
dan | 4bcc498 | 2010-08-16 19:23:02 +0000 | [diff] [blame] | 1453 | sql1 { |
| 1454 | PRAGMA main.page_size = %PGSZ%; |
| 1455 | PRAGMA auto_vacuum = 0; |
| 1456 | PRAGMA journal_mode = WAL; |
| 1457 | CREATE TABLE t1(x UNIQUE); |
| 1458 | INSERT INTO t1 SELECT randomblob(800); |
| 1459 | INSERT INTO t1 SELECT randomblob(800); |
| 1460 | INSERT INTO t1 SELECT randomblob(800); |
| 1461 | } |
| 1462 | } {wal} |
dan | eb8763d | 2010-08-17 14:52:22 +0000 | [diff] [blame] | 1463 | do_test wal-22.%PGSZ%.$tn.2 { sql2 { PRAGMA integrity_check } } {ok} |
| 1464 | do_test wal-22.%PGSZ%.$tn.3 { |
dan | 4bcc498 | 2010-08-16 19:23:02 +0000 | [diff] [blame] | 1465 | sql1 {PRAGMA wal_checkpoint} |
| 1466 | expr {[file size test.db] % %PGSZ%} |
| 1467 | } {0} |
| 1468 | }] |
| 1469 | } |
| 1470 | |
dan | eb8763d | 2010-08-17 14:52:22 +0000 | [diff] [blame] | 1471 | #------------------------------------------------------------------------- |
| 1472 | # Test that when 1 or more pages are recovered from a WAL file, |
| 1473 | # sqlite3_log() is invoked to report this to the user. |
| 1474 | # |
shaneh | 35cf908 | 2010-08-18 14:54:03 +0000 | [diff] [blame] | 1475 | set walfile [file nativename [file join [pwd] test.db-wal]] |
dan | eb8763d | 2010-08-17 14:52:22 +0000 | [diff] [blame] | 1476 | catch {db close} |
| 1477 | file delete -force test.db |
| 1478 | do_test wal-23.1 { |
| 1479 | faultsim_delete_and_reopen |
| 1480 | execsql { |
| 1481 | CREATE TABLE t1(a, b); |
| 1482 | PRAGMA journal_mode = WAL; |
| 1483 | INSERT INTO t1 VALUES(1, 2); |
| 1484 | INSERT INTO t1 VALUES(3, 4); |
| 1485 | } |
| 1486 | faultsim_save_and_close |
| 1487 | |
| 1488 | sqlite3_shutdown |
| 1489 | test_sqlite3_log [list lappend ::log] |
| 1490 | set ::log [list] |
| 1491 | sqlite3 db test.db |
| 1492 | execsql { SELECT * FROM t1 } |
| 1493 | } {1 2 3 4} |
| 1494 | do_test wal-23.2 { set ::log } {} |
| 1495 | |
| 1496 | do_test wal-23.3 { |
| 1497 | db close |
| 1498 | set ::log [list] |
| 1499 | faultsim_restore_and_reopen |
| 1500 | execsql { SELECT * FROM t1 } |
| 1501 | } {1 2 3 4} |
dan | 5a9e07e | 2010-08-18 15:25:17 +0000 | [diff] [blame] | 1502 | set nPage [expr 2+$AUTOVACUUM] |
dan | eb8763d | 2010-08-17 14:52:22 +0000 | [diff] [blame] | 1503 | do_test wal-23.4 { |
| 1504 | set ::log |
dan | 5a9e07e | 2010-08-18 15:25:17 +0000 | [diff] [blame] | 1505 | } [list SQLITE_OK "Recovered $nPage frames from WAL file $walfile"] |
dan | eb8763d | 2010-08-17 14:52:22 +0000 | [diff] [blame] | 1506 | |
| 1507 | db close |
| 1508 | sqlite3_shutdown |
| 1509 | test_sqlite3_log |
| 1510 | sqlite3_initialize |
| 1511 | |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1512 | finish_test |