drh | 1b1f30b | 2013-12-06 15:37:35 +0000 | [diff] [blame] | 1 | # 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 | # |
dan | 705a487 | 2014-01-27 16:35:15 +0000 | [diff] [blame] | 12 | # Tests for the SQLITE_READONLY_DBMOVED error condition: the database file |
drh | 1b1f30b | 2013-12-06 15:37:35 +0000 | [diff] [blame] | 13 | # is unlinked or renamed out from under SQLite. |
| 14 | # |
| 15 | |
| 16 | if {$tcl_platform(platform)!="unix"} return |
| 17 | |
| 18 | set testdir [file dirname $argv0] |
| 19 | source $testdir/tester.tcl |
| 20 | |
dan | 705a487 | 2014-01-27 16:35:15 +0000 | [diff] [blame] | 21 | if {[permutation]=="inmemory_journal"} { |
| 22 | finish_test |
| 23 | return |
| 24 | } |
| 25 | |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 26 | # Create a database file for testing |
| 27 | # |
drh | 1b1f30b | 2013-12-06 15:37:35 +0000 | [diff] [blame] | 28 | do_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} |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 33 | |
| 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 | # |
drh | 1b1f30b | 2013-12-06 15:37:35 +0000 | [diff] [blame] | 37 | file delete -force test-xyz.db |
| 38 | file rename test.db test-xyz.db |
| 39 | do_catchsql_test pager4-1.2 { |
| 40 | SELECT * FROM t1; |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 41 | } {0 {673 stone philips}} |
| 42 | do_catchsql_test pager4-1.3 { |
| 43 | UPDATE t1 SET a=537; |
| 44 | } {1 {attempt to write a readonly database}} |
| 45 | |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 46 | # Creating a different database file with the same name of the original |
| 47 | # is detected and still leaves the database read-only. |
| 48 | # |
| 49 | sqlite3 db2 test.db |
| 50 | db2 eval {CREATE TABLE t2(x,y,z)} |
| 51 | do_catchsql_test pager4-1.4 { |
| 52 | UPDATE t1 SET a=948; |
| 53 | } {1 {attempt to write a readonly database}} |
| 54 | |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 55 | # Changing the name back clears the READONLY error |
| 56 | # |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 57 | db2 close |
| 58 | file delete -force test.db |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 59 | file rename test-xyz.db test.db |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 60 | do_catchsql_test pager4-1.5 { |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 61 | SELECT * FROM t1; |
| 62 | } {0 {673 stone philips}} |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 63 | do_catchsql_test pager4-1.6 { |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 64 | 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 | # |
| 71 | file rename test.db test-xyz.db |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 72 | do_catchsql_test pager4-1.7 { |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 73 | PRAGMA journal_mode=OFF; |
| 74 | UPDATE t1 SET a=107; |
| 75 | SELECT * FROM t1; |
| 76 | } {0 {off 107 stone philips}} |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 77 | do_catchsql_test pager4-1.8 { |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 78 | 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 | # |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 85 | do_catchsql_test pager4-1.9 { |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 86 | PRAGMA journal_mode=DELETE; |
| 87 | UPDATE t1 SET c='jaguar'; |
| 88 | } {1 {attempt to write a readonly database}} |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 89 | do_catchsql_test pager4-1.10 { |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 90 | PRAGMA journal_mode=TRUNCATE; |
| 91 | UPDATE t1 SET c='jaguar'; |
| 92 | } {1 {attempt to write a readonly database}} |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 93 | do_catchsql_test pager4-1.11 { |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 94 | PRAGMA journal_mode=PERSIST; |
| 95 | UPDATE t1 SET c='jaguar'; |
| 96 | } {1 {attempt to write a readonly database}} |
drh | 1b1f30b | 2013-12-06 15:37:35 +0000 | [diff] [blame] | 97 | |
| 98 | |
| 99 | finish_test |