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