xref: /sqlite-3.40.0/test/walmode.test (revision 057f1ecd)
1# 2010 April 19
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#
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19do_test walmode-1.1 {
20  set sqlite_sync_count 0
21  execsql { PRAGMA page_size = 1024 }
22  execsql { PRAGMA journal_mode = wal }
23} {wal}
24do_test walmode-1.2 {
25  file size test.db
26} {1024}
27do_test walmode-1.3 {
28  set sqlite_sync_count
29} {4}
30do_test walmode-1.4 {
31  file exists test.db-wal
32} {0}
33do_test walmode-1.5 {
34  execsql { CREATE TABLE t1(a, b) }
35  file size test.db
36} {1024}
37do_test walmode-1.6 {
38  file exists test.db-wal
39} {1}
40do_test walmode-1.7 {
41  db close
42  file exists test.db-wal
43} {0}
44
45# There is now a database file with the read and write versions set to 2
46# in the file system. This file should default to WAL mode.
47#
48do_test walmode-2.1 {
49  sqlite3 db test.db
50  file exists test.db-wal
51} {0}
52do_test walmode-2.2 {
53  execsql { SELECT * FROM sqlite_master }
54  file exists test.db-wal
55} {1}
56do_test walmode-2.3 {
57  db close
58  file exists test.db-wal
59} {0}
60
61# If the first statement executed is "PRAGMA journal_mode = wal", and
62# the file is already configured for WAL (read and write versions set
63# to 2), then there should be no need to write the database. The
64# statement should cause the client to connect to the log file.
65#
66set sqlite_sync_count 0
67do_test walmode-3.1 {
68  sqlite3 db test.db
69  execsql { PRAGMA journal_mode = wal }
70} {wal}
71do_test walmode-3.2 {
72  list $sqlite_sync_count [file exists test.db-wal] [file size test.db-wal]
73} {0 1 0}
74
75# Test that changing back to journal_mode=persist works.
76#
77do_test walmode-4.1 {
78  execsql { INSERT INTO t1 VALUES(1, 2) }
79  execsql { PRAGMA journal_mode = persist }
80} {persist}
81do_test walmode-4.2 {
82  list [file exists test.db-journal] [file exists test.db-wal]
83} {1 0}
84do_test walmode-4.3 {
85  execsql { SELECT * FROM t1 }
86} {1 2}
87do_test walmode-4.4 {
88  db close
89  sqlite3 db test.db
90  execsql { SELECT * FROM t1 }
91} {1 2}
92do_test walmode-4.5 {
93  list [file exists test.db-journal] [file exists test.db-wal]
94} {1 0}
95
96# Test that nothing goes wrong if a connection is prevented from changing
97# from WAL to rollback mode because a second connection has the database
98# open. Or from rollback to WAL.
99#
100do_test walmode-4.1 {
101  sqlite3 db2 test.db
102  execsql { PRAGMA main.journal_mode } db2
103} {delete}
104do_test walmode-4.2 {
105  execsql { PRAGMA main.journal_mode = wal } db
106} {wal}
107do_test walmode-4.3 {
108  execsql { SELECT * FROM t1 } db2
109} {1 2}
110do_test walmode-4.4 {
111  catchsql { PRAGMA journal_mode = delete } db
112} {1 {database is locked}}
113do_test walmode-4.5 {
114  execsql { PRAGMA main.journal_mode } db
115} {wal}
116do_test walmode-4.6 {
117  db2 close
118  execsql { PRAGMA journal_mode = delete } db
119} {delete}
120do_test walmode-4.7 {
121  execsql { PRAGMA main.journal_mode } db
122} {delete}
123do_test walmode-4.8 {
124  list [file exists test.db-journal] [file exists test.db-wal]
125} {0 0}
126do_test walmode-4.9 {
127  sqlite3 db2 test.db
128  execsql {
129    BEGIN;
130      SELECT * FROM t1;
131  } db2
132} {1 2}
133do_test walmode-4.11 {
134  execsql { PRAGMA main.journal_mode } db
135} {delete}
136do_test walmode-4.10 {
137  catchsql { PRAGMA main.journal_mode = wal } db
138} {1 {database is locked}}
139do_test walmode-4.11 {
140  execsql { PRAGMA main.journal_mode } db
141} {delete}
142catch { db close }
143catch { db2 close }
144
145# Test that it is not possible to change a temporary or in-memory database
146# to WAL mode. WAL mode is for persistent file-backed databases only.
147#
148#   walmode-5.1.*: Try to set journal_mode=WAL on [sqlite3 db :memory:] database.
149#   walmode-5.2.*: Try to set journal_mode=WAL on [sqlite3 db ""] database.
150#   walmode-5.3.*: Try to set temp.journal_mode=WAL.
151#
152do_test walmode-5.1.1 {
153  sqlite3 db :memory:
154  execsql { PRAGMA main.journal_mode }
155} {memory}
156breakpoint
157do_test walmode-5.1.2 {
158  execsql { PRAGMA main.journal_mode = wal }
159} {memory}
160do_test walmode-5.1.3 {
161  execsql {
162    BEGIN;
163      CREATE TABLE t1(a, b);
164      INSERT INTO t1 VALUES(1, 2);
165    COMMIT;
166    SELECT * FROM t1;
167    PRAGMA main.journal_mode;
168  }
169} {1 2 memory}
170do_test walmode-5.1.4 {
171  execsql { PRAGMA main.journal_mode = wal }
172} {memory}
173do_test walmode-5.1.5 {
174  execsql {
175    INSERT INTO t1 VALUES(3, 4);
176    SELECT * FROM t1;
177    PRAGMA main.journal_mode;
178  }
179} {1 2 3 4 memory}
180
181do_test walmode-5.2.1 {
182  sqlite3 db ""
183  execsql { PRAGMA main.journal_mode }
184} {delete}
185do_test walmode-5.2.2 {
186  execsql { PRAGMA main.journal_mode = wal }
187} {delete}
188do_test walmode-5.2.3 {
189  execsql {
190    BEGIN;
191      CREATE TABLE t1(a, b);
192      INSERT INTO t1 VALUES(1, 2);
193    COMMIT;
194    SELECT * FROM t1;
195    PRAGMA main.journal_mode;
196  }
197} {1 2 delete}
198do_test walmode-5.2.4 {
199  execsql { PRAGMA main.journal_mode = wal }
200} {delete}
201do_test walmode-5.2.5 {
202  execsql {
203    INSERT INTO t1 VALUES(3, 4);
204    SELECT * FROM t1;
205    PRAGMA main.journal_mode;
206  }
207} {1 2 3 4 delete}
208
209if {$TEMP_STORE>=2} {
210  set tempJrnlMode memory
211} else {
212  set tempJrnlMode delete
213}
214do_test walmode-5.3.1 {
215  sqlite3 db test.db
216  execsql { PRAGMA temp.journal_mode }
217} $tempJrnlMode
218do_test walmode-5.3.2 {
219  execsql { PRAGMA temp.journal_mode = wal }
220} $tempJrnlMode
221do_test walmode-5.3.3 {
222  execsql {
223    BEGIN;
224      CREATE TEMP TABLE t1(a, b);
225      INSERT INTO t1 VALUES(1, 2);
226    COMMIT;
227    SELECT * FROM t1;
228    PRAGMA temp.journal_mode;
229  }
230} [list 1 2 $tempJrnlMode]
231do_test walmode-5.3.4 {
232  execsql { PRAGMA temp.journal_mode = wal }
233} $tempJrnlMode
234do_test walmode-5.3.5 {
235  execsql {
236    INSERT INTO t1 VALUES(3, 4);
237    SELECT * FROM t1;
238    PRAGMA temp.journal_mode;
239  }
240} [list 1 2 3 4 $tempJrnlMode]
241
242finish_test
243