blob: 2cf73d1b1752293f4cdf4d2038e041f5186a7ec3 [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#
dan705a4872014-01-27 16:35:15 +000012# Tests for the SQLITE_READONLY_DBMOVED error condition: the database file
drh1b1f30b2013-12-06 15:37:35 +000013# 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
dan705a4872014-01-27 16:35:15 +000021if {[permutation]=="inmemory_journal"} {
22 finish_test
23 return
24}
25
drh3fee8a62013-12-06 17:23:38 +000026# Create a database file for testing
27#
drh1b1f30b2013-12-06 15:37:35 +000028do_execsql_test pager4-1.1 {
29 CREATE TABLE t1(a,b,c);
30 INSERT INTO t1 VALUES(673,'stone','philips');
31 SELECT * FROM t1;
32} {673 stone philips}
drh3fee8a62013-12-06 17:23:38 +000033
34# After renaming the database file while it is open, one can still
35# read from the database, but writing returns a READONLY error.
36#
drh1b1f30b2013-12-06 15:37:35 +000037file delete -force test-xyz.db
38file rename test.db test-xyz.db
39do_catchsql_test pager4-1.2 {
40 SELECT * FROM t1;
drh3fee8a62013-12-06 17:23:38 +000041} {0 {673 stone philips}}
42do_catchsql_test pager4-1.3 {
43 UPDATE t1 SET a=537;
44} {1 {attempt to write a readonly database}}
45
drhb959a012013-12-07 12:29:22 +000046# Creating a different database file with the same name of the original
47# is detected and still leaves the database read-only.
48#
49sqlite3 db2 test.db
50db2 eval {CREATE TABLE t2(x,y,z)}
51do_catchsql_test pager4-1.4 {
52 UPDATE t1 SET a=948;
53} {1 {attempt to write a readonly database}}
54
drh3fee8a62013-12-06 17:23:38 +000055# Changing the name back clears the READONLY error
56#
drhb959a012013-12-07 12:29:22 +000057db2 close
58file delete -force test.db
drh3fee8a62013-12-06 17:23:38 +000059file rename test-xyz.db test.db
drhb959a012013-12-07 12:29:22 +000060do_catchsql_test pager4-1.5 {
drh3fee8a62013-12-06 17:23:38 +000061 SELECT * FROM t1;
62} {0 {673 stone philips}}
drhb959a012013-12-07 12:29:22 +000063do_catchsql_test pager4-1.6 {
drh3fee8a62013-12-06 17:23:38 +000064 UPDATE t1 SET a=537;
65 SELECT * FROM t1;
66} {0 {537 stone philips}}
67
68# We can write to a renamed database if journal_mode=OFF or
69# journal_mode=MEMORY.
70#
71file rename test.db test-xyz.db
drhb959a012013-12-07 12:29:22 +000072do_catchsql_test pager4-1.7 {
drh3fee8a62013-12-06 17:23:38 +000073 PRAGMA journal_mode=OFF;
74 UPDATE t1 SET a=107;
75 SELECT * FROM t1;
76} {0 {off 107 stone philips}}
drhb959a012013-12-07 12:29:22 +000077do_catchsql_test pager4-1.8 {
drh3fee8a62013-12-06 17:23:38 +000078 PRAGMA journal_mode=MEMORY;
79 UPDATE t1 SET b='magpie';
80 SELECT * FROM t1;
81} {0 {memory 107 magpie philips}}
82
83# Any other journal mode gives a READONLY error
84#
drhb959a012013-12-07 12:29:22 +000085do_catchsql_test pager4-1.9 {
drh3fee8a62013-12-06 17:23:38 +000086 PRAGMA journal_mode=DELETE;
87 UPDATE t1 SET c='jaguar';
88} {1 {attempt to write a readonly database}}
drhb959a012013-12-07 12:29:22 +000089do_catchsql_test pager4-1.10 {
drh3fee8a62013-12-06 17:23:38 +000090 PRAGMA journal_mode=TRUNCATE;
91 UPDATE t1 SET c='jaguar';
92} {1 {attempt to write a readonly database}}
drhb959a012013-12-07 12:29:22 +000093do_catchsql_test pager4-1.11 {
drh3fee8a62013-12-06 17:23:38 +000094 PRAGMA journal_mode=PERSIST;
95 UPDATE t1 SET c='jaguar';
96} {1 {attempt to write a readonly database}}
drh1b1f30b2013-12-06 15:37:35 +000097
98
99finish_test