blob: 6ea87d704b3f8c933dd1958fdce501c752cb8bc0 [file] [log] [blame]
danielk1977ee8b7992009-03-26 17:13:06 +00001# 2009 March 24
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#
danielk1977ee8b7992009-03-26 17:13:06 +000012
13set testdir [file dirname $argv0]
14source $testdir/tester.tcl
15
16ifcapable {!pager_pragmas} {
17 finish_test
18 return
19}
20
21#-------------------------------------------------------------------------
dan672b41b2010-06-21 05:40:49 +000022# The tests in this file check that the following two bugs (both now fixed)
23# do not reappear.
danielk1977ee8b7992009-03-26 17:13:06 +000024#
dan672b41b2010-06-21 05:40:49 +000025# jrnlmode2-1.*: Demonstrate bug #3745:
26#
27# In persistent journal mode, if:
28#
29# * There is a persistent journal in the file-system, AND
30# * there exists a connection with a shared lock on the db file,
31#
32# then a second connection cannot open a read-transaction on the database.
33# The reason is because while determining that the persistent-journal is
34# not a hot-journal, SQLite currently grabs an exclusive lock on the
35# database file. If this fails because another connection has a shared
36# lock, then SQLITE_BUSY is returned to the user.
37#
38# jrnlmode2-2.*: Demonstrate bug #3751:
39#
40# If a connection is opened in SQLITE_OPEN_READONLY mode, the underlying
41# unix file descriptor on the database file is opened in O_RDONLY mode.
42#
43# When SQLite queries the database file for the schema in order to compile
44# the SELECT statement, it sees the empty journal in the file system, it
45# attempts to obtain an exclusive lock on the database file (this is a
46# bug). The attempt to obtain an exclusive (write) lock on a read-only file
47# fails at the OS level. Under unix, fcntl() reports an EBADF - "Bad file
48# descriptor" - error.
danielk1977ee8b7992009-03-26 17:13:06 +000049#
50
51do_test jrnlmode2-1.1 {
52 execsql {
53 PRAGMA journal_mode = persist;
54 CREATE TABLE t1(a, b);
55 INSERT INTO t1 VALUES(1, 2);
56 }
57} {persist}
58
59do_test jrnlmode2-1.2 {
60 file exists test.db-journal
61} {1}
62
63do_test jrnlmode2-1.3 {
64 sqlite3 db2 test.db
65 execsql { SELECT * FROM t1 } db2
66} {1 2}
67
68do_test jrnlmode2-1.4 {
69 execsql {
70 INSERT INTO t1 VALUES(3, 4);
dan672b41b2010-06-21 05:40:49 +000071 }
72 execsql {
danielk1977ee8b7992009-03-26 17:13:06 +000073 BEGIN;
74 SELECT * FROM t1;
75 }
76 execsql { PRAGMA lock_status }
77} {main shared temp closed}
78
79do_test jrnlmode2-1.5 {
80 file exists test.db-journal
81} {1}
82
83do_test jrnlmode2-1.6 {
84 catchsql { SELECT * FROM t1 } db2
85} {0 {1 2 3 4}}
86
87do_test jrnlmode2-1.7 {
88 execsql { COMMIT }
89 catchsql { SELECT * FROM t1 } db2
90} {0 {1 2 3 4}}
91
92
93
94do_test jrnlmode2-2.1 {
95 db2 close
96 execsql { PRAGMA journal_mode = truncate }
97 execsql { INSERT INTO t1 VALUES(5, 6) }
98} {}
99
100do_test jrnlmode2-2.2 {
101 file exists test.db-journal
102} {1}
103
104do_test jrnlmode2-2.3 {
105 file size test.db-journal
106} {0}
107
108do_test jrnlmode2-2.4 {
109 sqlite3 db2 test.db -readonly 1
110 catchsql { SELECT * FROM t1 } db2
111} {0 {1 2 3 4 5 6}}
112
113do_test jrnlmode2-2.5 {
dan672b41b2010-06-21 05:40:49 +0000114 db close
mistachkinfda06be2011-08-02 00:57:34 +0000115 delete_file test.db-journal
danielk1977ee8b7992009-03-26 17:13:06 +0000116} {}
danielk1977ee8b7992009-03-26 17:13:06 +0000117do_test jrnlmode2-2.6 {
118 sqlite3 db2 test.db -readonly 1
119 catchsql { SELECT * FROM t1 } db2
120} {0 {1 2 3 4 5 6}}
121
122catch { db2 close }
123finish_test