blob: edfd2660c2b6f52d39e0a949a990fd38459b69b8 [file] [log] [blame]
drh1b1f30b2013-12-06 15:37:35 +00001# 2013-12-06
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#
12# Tests for the SQLITE_IOERR_NODB error condition: the database file file
13# is unlinked or renamed out from under SQLite.
14#
15
16if {$tcl_platform(platform)!="unix"} return
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
drh3fee8a62013-12-06 17:23:38 +000021# Create a database file for testing
22#
drh1b1f30b2013-12-06 15:37:35 +000023do_execsql_test pager4-1.1 {
24 CREATE TABLE t1(a,b,c);
25 INSERT INTO t1 VALUES(673,'stone','philips');
26 SELECT * FROM t1;
27} {673 stone philips}
drh3fee8a62013-12-06 17:23:38 +000028
29# After renaming the database file while it is open, one can still
30# read from the database, but writing returns a READONLY error.
31#
drh1b1f30b2013-12-06 15:37:35 +000032file delete -force test-xyz.db
33file rename test.db test-xyz.db
34do_catchsql_test pager4-1.2 {
35 SELECT * FROM t1;
drh3fee8a62013-12-06 17:23:38 +000036} {0 {673 stone philips}}
37do_catchsql_test pager4-1.3 {
38 UPDATE t1 SET a=537;
39} {1 {attempt to write a readonly database}}
40
drhb959a012013-12-07 12:29:22 +000041# Creating a different database file with the same name of the original
42# is detected and still leaves the database read-only.
43#
44sqlite3 db2 test.db
45db2 eval {CREATE TABLE t2(x,y,z)}
46do_catchsql_test pager4-1.4 {
47 UPDATE t1 SET a=948;
48} {1 {attempt to write a readonly database}}
49
drh3fee8a62013-12-06 17:23:38 +000050# Changing the name back clears the READONLY error
51#
drhb959a012013-12-07 12:29:22 +000052db2 close
53file delete -force test.db
drh3fee8a62013-12-06 17:23:38 +000054file rename test-xyz.db test.db
drhb959a012013-12-07 12:29:22 +000055do_catchsql_test pager4-1.5 {
drh3fee8a62013-12-06 17:23:38 +000056 SELECT * FROM t1;
57} {0 {673 stone philips}}
drhb959a012013-12-07 12:29:22 +000058do_catchsql_test pager4-1.6 {
drh3fee8a62013-12-06 17:23:38 +000059 UPDATE t1 SET a=537;
60 SELECT * FROM t1;
61} {0 {537 stone philips}}
62
63# We can write to a renamed database if journal_mode=OFF or
64# journal_mode=MEMORY.
65#
66file rename test.db test-xyz.db
drhb959a012013-12-07 12:29:22 +000067do_catchsql_test pager4-1.7 {
drh3fee8a62013-12-06 17:23:38 +000068 PRAGMA journal_mode=OFF;
69 UPDATE t1 SET a=107;
70 SELECT * FROM t1;
71} {0 {off 107 stone philips}}
drhb959a012013-12-07 12:29:22 +000072do_catchsql_test pager4-1.8 {
drh3fee8a62013-12-06 17:23:38 +000073 PRAGMA journal_mode=MEMORY;
74 UPDATE t1 SET b='magpie';
75 SELECT * FROM t1;
76} {0 {memory 107 magpie philips}}
77
78# Any other journal mode gives a READONLY error
79#
drhb959a012013-12-07 12:29:22 +000080do_catchsql_test pager4-1.9 {
drh3fee8a62013-12-06 17:23:38 +000081 PRAGMA journal_mode=DELETE;
82 UPDATE t1 SET c='jaguar';
83} {1 {attempt to write a readonly database}}
drhb959a012013-12-07 12:29:22 +000084do_catchsql_test pager4-1.10 {
drh3fee8a62013-12-06 17:23:38 +000085 PRAGMA journal_mode=TRUNCATE;
86 UPDATE t1 SET c='jaguar';
87} {1 {attempt to write a readonly database}}
drhb959a012013-12-07 12:29:22 +000088do_catchsql_test pager4-1.11 {
drh3fee8a62013-12-06 17:23:38 +000089 PRAGMA journal_mode=PERSIST;
90 UPDATE t1 SET c='jaguar';
91} {1 {attempt to write a readonly database}}
drh1b1f30b2013-12-06 15:37:35 +000092
93
94finish_test