sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | # 2008 April 14 |
| 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. |
| 12 | # The focus of this file is in making sure that rolling back |
| 13 | # a statement journal works correctly. |
| 14 | # |
| 15 | # $Id: tempdb.test,v 1.4 2009/06/05 17:09:12 drh Exp $ |
| 16 | |
| 17 | set testdir [file dirname $argv0] |
| 18 | source $testdir/tester.tcl |
| 19 | |
| 20 | if {[atomic_batch_write test.db]} { |
| 21 | finish_test |
| 22 | return |
| 23 | } |
| 24 | |
| 25 | # Use a temporary database. |
| 26 | # |
| 27 | db close |
| 28 | sqlite3 db {} |
| 29 | |
| 30 | # Force a statement journal rollback on a database file that |
| 31 | # has never been opened. |
| 32 | # |
| 33 | do_test tempdb-1.1 { |
| 34 | execsql { |
| 35 | BEGIN; |
| 36 | CREATE TABLE t1(x UNIQUE); |
| 37 | CREATE TABLE t2(y); |
| 38 | INSERT INTO t2 VALUES('hello'); |
| 39 | INSERT INTO t2 VALUES(NULL); |
| 40 | } |
| 41 | # Because of the transaction, the temporary database file |
| 42 | # has not even been opened yet. The following statement |
| 43 | # will cause a statement journal rollback on this non-existant |
| 44 | # file. |
| 45 | catchsql { |
| 46 | INSERT INTO t1 |
| 47 | SELECT CASE WHEN y IS NULL THEN test_error('oops', 11) ELSE y END |
| 48 | FROM t2; |
| 49 | } |
| 50 | } {1 oops} |
| 51 | |
| 52 | # Verify that no writes occurred in t1. |
| 53 | # |
| 54 | do_test tempdb-1.2 { |
| 55 | execsql { |
| 56 | SELECT * FROM t1 |
| 57 | } |
| 58 | } {} |
| 59 | |
| 60 | do_test tempdb-2.1 { |
| 61 | # Set $::jrnl_in_memory if the journal file is expected to be in-memory. |
| 62 | # Similarly, set $::subj_in_memory if the sub-journal file is expected |
| 63 | # to be in memory. These variables are used to calculate the expected |
| 64 | # number of open files in the test cases below. |
| 65 | # |
| 66 | set jrnl_in_memory [expr {[permutation] eq "inmemory_journal"}] |
| 67 | set subj_in_memory [expr {$jrnl_in_memory || $TEMP_STORE>=2}] |
| 68 | |
| 69 | db close |
| 70 | sqlite3 db test.db |
| 71 | } {} |
| 72 | do_test tempdb-2.2 { |
| 73 | execsql { |
| 74 | CREATE TABLE t1 (a PRIMARY KEY, b, c); |
| 75 | CREATE TABLE t2 (a, b, c); |
| 76 | BEGIN; |
| 77 | INSERT INTO t1 VALUES(1, 2, 3); |
| 78 | INSERT INTO t1 VALUES(4, 5, 6); |
| 79 | INSERT INTO t2 VALUES(7, 8, 9); |
| 80 | INSERT INTO t2 SELECT * FROM t1; |
| 81 | } |
| 82 | catchsql { INSERT INTO t1 SELECT * FROM t2 } |
| 83 | set sqlite_open_file_count |
| 84 | } [expr 1 + (0==$jrnl_in_memory)] |
| 85 | do_test tempdb-2.3 { |
| 86 | execsql { |
| 87 | PRAGMA temp_store = 'memory'; |
| 88 | ROLLBACK; |
| 89 | BEGIN; |
| 90 | INSERT INTO t1 VALUES(1, 2, 3); |
| 91 | INSERT INTO t1 VALUES(4, 5, 6); |
| 92 | INSERT INTO t2 SELECT * FROM t1; |
| 93 | } |
| 94 | catchsql { INSERT INTO t1 SELECT * FROM t2 } |
| 95 | set sqlite_open_file_count |
| 96 | } [expr 1 + (0==$jrnl_in_memory)] |
| 97 | |
| 98 | #------------------------------------------------------------------------- |
| 99 | # Rolling back a transaction on a file-backed TEMP database, then |
| 100 | # committing a write transaction that does not modify the database, must |
| 101 | # not mark dirty pages left over from earlier transactions as clean |
| 102 | # without writing them to the temp file. |
| 103 | # |
| 104 | foreach {tn temp_store} {1 FILE 2 MEMORY} { |
| 105 | reset_db |
| 106 | do_execsql_test tempdb-3.$tn.0 " |
| 107 | PRAGMA temp_store = $temp_store; |
| 108 | PRAGMA temp.page_size = 4096; |
| 109 | PRAGMA temp.cache_size = 10; |
| 110 | CREATE TEMP TABLE t1(a INTEGER PRIMARY KEY, b, c); |
| 111 | CREATE INDEX i1 ON t1(b); |
| 112 | CREATE TEMP TABLE t2(x, y); |
| 113 | WITH RECURSIVE c(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM c WHERE i<5) |
| 114 | INSERT INTO t1(b,c) SELECT (i*494)%1000, hex(zeroblob(1500)) || i FROM c; |
| 115 | " |
| 116 | |
| 117 | do_catchsql_test tempdb-3.$tn.1 { |
| 118 | INSERT OR ABORT INTO t1(a,b,c) SELECT a+1, b, c FROM t1 ORDER BY a DESC; |
| 119 | } {1 {UNIQUE constraint failed: t1.a}} |
| 120 | |
| 121 | do_execsql_test tempdb-3.$tn.2 { |
| 122 | PRAGMA temp.integrity_check; |
| 123 | } {ok} |
| 124 | |
| 125 | do_execsql_test tempdb-3.$tn.3 { |
| 126 | UPDATE t1 SET c=hex(zeroblob(5))||a, b=b+1 WHERE a%7=0; |
| 127 | CREATE INDEX IF NOT EXISTS i2 ON t2(x); |
| 128 | PRAGMA temp.integrity_check; |
| 129 | } {ok} |
| 130 | |
| 131 | do_execsql_test tempdb-3.$tn.4 { |
| 132 | SELECT a, b, substr(c, 3001) FROM t1 ORDER BY a; |
| 133 | } {1 494 1 2 988 2 3 482 3 4 976 4 5 470 5} |
| 134 | } |
| 135 | |
| 136 | # Same again, but with an empty "BEGIN IMMEDIATE; COMMIT" as the |
| 137 | # transaction that does not modify the database. |
| 138 | # |
| 139 | reset_db |
| 140 | do_execsql_test tempdb-4.0 { |
| 141 | PRAGMA temp_store = FILE; |
| 142 | PRAGMA temp.page_size = 4096; |
| 143 | PRAGMA temp.cache_size = 10; |
| 144 | CREATE TEMP TABLE t1(a INTEGER PRIMARY KEY, b, c); |
| 145 | CREATE INDEX i1 ON t1(b); |
| 146 | CREATE TEMP TABLE t2(x, y); |
| 147 | WITH RECURSIVE c(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM c WHERE i<5) |
| 148 | INSERT INTO t1(b,c) SELECT (i*494)%1000, hex(zeroblob(1500)) || i FROM c; |
| 149 | } |
| 150 | do_catchsql_test tempdb-4.1 { |
| 151 | INSERT OR ABORT INTO t1(a,b,c) SELECT a+1, b, c FROM t1 ORDER BY a DESC; |
| 152 | } {1 {UNIQUE constraint failed: t1.a}} |
| 153 | do_execsql_test tempdb-4.2 { |
| 154 | BEGIN IMMEDIATE; |
| 155 | COMMIT; |
| 156 | CREATE INDEX i2 ON t2(x); |
| 157 | PRAGMA temp.integrity_check; |
| 158 | } {ok} |
| 159 | do_execsql_test tempdb-4.3 { |
| 160 | SELECT a, b, substr(c, 3001) FROM t1 ORDER BY a; |
| 161 | } {1 494 1 2 988 2 3 482 3 4 976 4 5 470 5} |
| 162 | |
| 163 | finish_test |