summaryrefslogtreecommitdiff
path: root/test/tempdb.test
blob: 790d92bed418a93d86f898ee300a2a8fade19e87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# 2008 April 14
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.
# The focus of this file is in making sure that rolling back
# a statement journal works correctly.
#
# $Id: tempdb.test,v 1.4 2009/06/05 17:09:12 drh Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl

if {[atomic_batch_write test.db]} {
  finish_test
  return
}

# Use a temporary database.
#
db close
sqlite3 db {}

# Force a statement journal rollback on a database file that
# has never been opened.
#
do_test tempdb-1.1 {
  execsql {
    BEGIN;
    CREATE TABLE t1(x UNIQUE);
    CREATE TABLE t2(y);
    INSERT INTO t2 VALUES('hello');
    INSERT INTO t2 VALUES(NULL);
  }
  # Because of the transaction, the temporary database file
  # has not even been opened yet.  The following statement
  # will cause a statement journal rollback on this non-existant
  # file.
  catchsql {
    INSERT INTO t1
    SELECT CASE WHEN y IS NULL THEN test_error('oops', 11) ELSE y END
      FROM t2;
  }
} {1 oops}

# Verify that no writes occurred in t1.
#
do_test tempdb-1.2 {
  execsql {
    SELECT * FROM t1
  }
} {}

do_test tempdb-2.1 {
  # Set $::jrnl_in_memory if the journal file is expected to be in-memory.
  # Similarly, set $::subj_in_memory if the sub-journal file is expected
  # to be in memory. These variables are used to calculate the expected
  # number of open files in the test cases below.
  #
  set jrnl_in_memory [expr {[permutation] eq "inmemory_journal"}]
  set subj_in_memory [expr {$jrnl_in_memory || $TEMP_STORE>=2}]

  db close
  sqlite3 db test.db
} {}
do_test tempdb-2.2 {
  execsql {
    CREATE TABLE t1 (a PRIMARY KEY, b, c);
    CREATE TABLE t2 (a, b, c);
    BEGIN;
      INSERT INTO t1 VALUES(1, 2, 3);
      INSERT INTO t1 VALUES(4, 5, 6);
      INSERT INTO t2 VALUES(7, 8, 9);
      INSERT INTO t2 SELECT * FROM t1;
  }
  catchsql { INSERT INTO t1 SELECT * FROM t2 }
  set sqlite_open_file_count
} [expr 1 + (0==$jrnl_in_memory)]
do_test tempdb-2.3 {
  execsql {
    PRAGMA temp_store = 'memory';
    ROLLBACK;
    BEGIN;
      INSERT INTO t1 VALUES(1, 2, 3);
      INSERT INTO t1 VALUES(4, 5, 6);
      INSERT INTO t2 SELECT * FROM t1;
  }
  catchsql { INSERT INTO t1 SELECT * FROM t2 }
  set sqlite_open_file_count
} [expr 1 + (0==$jrnl_in_memory)]

#-------------------------------------------------------------------------
# Rolling back a transaction on a file-backed TEMP database, then
# committing a write transaction that does not modify the database, must
# not mark dirty pages left over from earlier transactions as clean
# without writing them to the temp file.
#
foreach {tn temp_store} {1 FILE 2 MEMORY} {
  reset_db
  do_execsql_test tempdb-3.$tn.0 "
    PRAGMA temp_store = $temp_store;
    PRAGMA temp.page_size = 4096;
    PRAGMA temp.cache_size = 10;
    CREATE TEMP TABLE t1(a INTEGER PRIMARY KEY, b, c);
    CREATE INDEX i1 ON t1(b);
    CREATE TEMP TABLE t2(x, y);
    WITH RECURSIVE c(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM c WHERE i<5)
    INSERT INTO t1(b,c) SELECT (i*494)%1000, hex(zeroblob(1500)) || i FROM c;
  "

  do_catchsql_test tempdb-3.$tn.1 {
    INSERT OR ABORT INTO t1(a,b,c) SELECT a+1, b, c FROM t1 ORDER BY a DESC;
  } {1 {UNIQUE constraint failed: t1.a}}

  do_execsql_test tempdb-3.$tn.2 {
    PRAGMA temp.integrity_check;
  } {ok}

  do_execsql_test tempdb-3.$tn.3 {
    UPDATE t1 SET c=hex(zeroblob(5))||a, b=b+1 WHERE a%7=0;
    CREATE INDEX IF NOT EXISTS i2 ON t2(x);
    PRAGMA temp.integrity_check;
  } {ok}

  do_execsql_test tempdb-3.$tn.4 {
    SELECT a, b, substr(c, 3001) FROM t1 ORDER BY a;
  } {1 494 1 2 988 2 3 482 3 4 976 4 5 470 5}
}

# Same again, but with an empty "BEGIN IMMEDIATE; COMMIT" as the
# transaction that does not modify the database.
#
reset_db
do_execsql_test tempdb-4.0 {
  PRAGMA temp_store = FILE;
  PRAGMA temp.page_size = 4096;
  PRAGMA temp.cache_size = 10;
  CREATE TEMP TABLE t1(a INTEGER PRIMARY KEY, b, c);
  CREATE INDEX i1 ON t1(b);
  CREATE TEMP TABLE t2(x, y);
  WITH RECURSIVE c(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM c WHERE i<5)
  INSERT INTO t1(b,c) SELECT (i*494)%1000, hex(zeroblob(1500)) || i FROM c;
}
do_catchsql_test tempdb-4.1 {
  INSERT OR ABORT INTO t1(a,b,c) SELECT a+1, b, c FROM t1 ORDER BY a DESC;
} {1 {UNIQUE constraint failed: t1.a}}
do_execsql_test tempdb-4.2 {
  BEGIN IMMEDIATE;
  COMMIT;
  CREATE INDEX i2 ON t2(x);
  PRAGMA temp.integrity_check;
} {ok}
do_execsql_test tempdb-4.3 {
  SELECT a, b, substr(c, 3001) FROM t1 ORDER BY a;
} {1 494 1 2 988 2 3 482 3 4 976 4 5 470 5}

finish_test