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