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 | # |
| 12 | # Tests for the SQLITE_IOERR_NODB error condition: the database file file |
| 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 | |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 21 | # Create a database file for testing |
| 22 | # |
drh | 1b1f30b | 2013-12-06 15:37:35 +0000 | [diff] [blame] | 23 | do_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} |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 28 | |
| 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 | # |
drh | 1b1f30b | 2013-12-06 15:37:35 +0000 | [diff] [blame] | 32 | file delete -force test-xyz.db |
| 33 | file rename test.db test-xyz.db |
| 34 | do_catchsql_test pager4-1.2 { |
| 35 | SELECT * FROM t1; |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 36 | } {0 {673 stone philips}} |
| 37 | do_catchsql_test pager4-1.3 { |
| 38 | UPDATE t1 SET a=537; |
| 39 | } {1 {attempt to write a readonly database}} |
| 40 | |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 41 | # Creating a different database file with the same name of the original |
| 42 | # is detected and still leaves the database read-only. |
| 43 | # |
| 44 | sqlite3 db2 test.db |
| 45 | db2 eval {CREATE TABLE t2(x,y,z)} |
| 46 | do_catchsql_test pager4-1.4 { |
| 47 | UPDATE t1 SET a=948; |
| 48 | } {1 {attempt to write a readonly database}} |
| 49 | |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 50 | # Changing the name back clears the READONLY error |
| 51 | # |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 52 | db2 close |
| 53 | file delete -force test.db |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 54 | file rename test-xyz.db test.db |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 55 | do_catchsql_test pager4-1.5 { |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 56 | SELECT * FROM t1; |
| 57 | } {0 {673 stone philips}} |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 58 | do_catchsql_test pager4-1.6 { |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 59 | 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 | # |
| 66 | file rename test.db test-xyz.db |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 67 | do_catchsql_test pager4-1.7 { |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 68 | PRAGMA journal_mode=OFF; |
| 69 | UPDATE t1 SET a=107; |
| 70 | SELECT * FROM t1; |
| 71 | } {0 {off 107 stone philips}} |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 72 | do_catchsql_test pager4-1.8 { |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 73 | 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 | # |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 80 | do_catchsql_test pager4-1.9 { |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 81 | PRAGMA journal_mode=DELETE; |
| 82 | UPDATE t1 SET c='jaguar'; |
| 83 | } {1 {attempt to write a readonly database}} |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 84 | do_catchsql_test pager4-1.10 { |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 85 | PRAGMA journal_mode=TRUNCATE; |
| 86 | UPDATE t1 SET c='jaguar'; |
| 87 | } {1 {attempt to write a readonly database}} |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 88 | do_catchsql_test pager4-1.11 { |
drh | 3fee8a6 | 2013-12-06 17:23:38 +0000 | [diff] [blame] | 89 | PRAGMA journal_mode=PERSIST; |
| 90 | UPDATE t1 SET c='jaguar'; |
| 91 | } {1 {attempt to write a readonly database}} |
drh | 1b1f30b | 2013-12-06 15:37:35 +0000 | [diff] [blame] | 92 | |
| 93 | |
| 94 | finish_test |