drh | 2d45834 | 2003-04-05 03:42:26 +0000 | [diff] [blame] | 1 | # 2003 April 4 |
| 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 | # This file implements regression tests for SQLite library. The |
| 12 | # focus of this script is testing the ATTACH and DETACH commands |
| 13 | # and related functionality. |
| 14 | # |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame^] | 15 | # $Id: attach.test,v 1.37 2005/01/24 10:26:00 danielk1977 Exp $ |
drh | 2d45834 | 2003-04-05 03:42:26 +0000 | [diff] [blame] | 16 | # |
| 17 | |
| 18 | set testdir [file dirname $argv0] |
| 19 | source $testdir/tester.tcl |
| 20 | |
drh | a73af53 | 2003-04-05 16:56:28 +0000 | [diff] [blame] | 21 | for {set i 2} {$i<=15} {incr i} { |
| 22 | file delete -force test$i.db |
| 23 | file delete -force test$i.db-journal |
| 24 | } |
drh | 2d45834 | 2003-04-05 03:42:26 +0000 | [diff] [blame] | 25 | |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 26 | set btree_trace 0 |
drh | 2d45834 | 2003-04-05 03:42:26 +0000 | [diff] [blame] | 27 | do_test attach-1.1 { |
| 28 | execsql { |
| 29 | CREATE TABLE t1(a,b); |
| 30 | INSERT INTO t1 VALUES(1,2); |
| 31 | INSERT INTO t1 VALUES(3,4); |
| 32 | SELECT * FROM t1; |
| 33 | } |
| 34 | } {1 2 3 4} |
| 35 | do_test attach-1.2 { |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 36 | sqlite3 db2 test2.db |
drh | 2d45834 | 2003-04-05 03:42:26 +0000 | [diff] [blame] | 37 | execsql { |
| 38 | CREATE TABLE t2(x,y); |
| 39 | INSERT INTO t2 VALUES(1,'x'); |
| 40 | INSERT INTO t2 VALUES(2,'y'); |
| 41 | SELECT * FROM t2; |
drh | a73af53 | 2003-04-05 16:56:28 +0000 | [diff] [blame] | 42 | } db2 |
drh | 2d45834 | 2003-04-05 03:42:26 +0000 | [diff] [blame] | 43 | } {1 x 2 y} |
| 44 | do_test attach-1.3 { |
| 45 | execsql { |
| 46 | ATTACH DATABASE 'test2.db' AS two; |
| 47 | SELECT * FROM two.t2; |
| 48 | } |
| 49 | } {1 x 2 y} |
| 50 | do_test attach-1.4 { |
| 51 | execsql { |
| 52 | SELECT * FROM t2; |
| 53 | } |
| 54 | } {1 x 2 y} |
| 55 | do_test attach-1.5 { |
| 56 | execsql { |
| 57 | DETACH DATABASE two; |
| 58 | SELECT * FROM t1; |
| 59 | } |
| 60 | } {1 2 3 4} |
| 61 | do_test attach-1.6 { |
| 62 | catchsql { |
| 63 | SELECT * FROM t2; |
| 64 | } |
| 65 | } {1 {no such table: t2}} |
| 66 | do_test attach-1.7 { |
| 67 | catchsql { |
| 68 | SELECT * FROM two.t2; |
| 69 | } |
| 70 | } {1 {no such table: two.t2}} |
drh | a73af53 | 2003-04-05 16:56:28 +0000 | [diff] [blame] | 71 | do_test attach-1.8 { |
| 72 | catchsql { |
| 73 | ATTACH DATABASE 'test3.db' AS three; |
| 74 | } |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 75 | } {0 {}} |
drh | a73af53 | 2003-04-05 16:56:28 +0000 | [diff] [blame] | 76 | do_test attach-1.9 { |
| 77 | catchsql { |
| 78 | SELECT * FROM three.sqlite_master; |
| 79 | } |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 80 | } {0 {}} |
drh | a73af53 | 2003-04-05 16:56:28 +0000 | [diff] [blame] | 81 | do_test attach-1.10 { |
| 82 | catchsql { |
| 83 | DETACH DATABASE three; |
| 84 | } |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 85 | } {0 {}} |
drh | a73af53 | 2003-04-05 16:56:28 +0000 | [diff] [blame] | 86 | do_test attach-1.11 { |
| 87 | execsql { |
| 88 | ATTACH 'test.db' AS db2; |
| 89 | ATTACH 'test.db' AS db3; |
| 90 | ATTACH 'test.db' AS db4; |
| 91 | ATTACH 'test.db' AS db5; |
| 92 | ATTACH 'test.db' AS db6; |
| 93 | ATTACH 'test.db' AS db7; |
| 94 | ATTACH 'test.db' AS db8; |
| 95 | ATTACH 'test.db' AS db9; |
| 96 | } |
| 97 | } {} |
drh | b8ec209 | 2003-06-04 15:53:02 +0000 | [diff] [blame] | 98 | proc db_list {db} { |
| 99 | set list {} |
| 100 | foreach {idx name file} [execsql {PRAGMA database_list} $db] { |
| 101 | lappend list $idx $name |
drh | a73af53 | 2003-04-05 16:56:28 +0000 | [diff] [blame] | 102 | } |
drh | b8ec209 | 2003-06-04 15:53:02 +0000 | [diff] [blame] | 103 | return $list |
| 104 | } |
danielk1977 | 27188fb | 2004-11-23 10:13:03 +0000 | [diff] [blame] | 105 | ifcapable schema_pragmas { |
drh | b8ec209 | 2003-06-04 15:53:02 +0000 | [diff] [blame] | 106 | do_test attach-1.11b { |
| 107 | db_list db |
drh | 34f4732 | 2004-08-18 15:58:22 +0000 | [diff] [blame] | 108 | } {0 main 2 db2 3 db3 4 db4 5 db5 6 db6 7 db7 8 db8 9 db9} |
danielk1977 | 27188fb | 2004-11-23 10:13:03 +0000 | [diff] [blame] | 109 | } ;# ifcapable schema_pragmas |
drh | a73af53 | 2003-04-05 16:56:28 +0000 | [diff] [blame] | 110 | do_test attach-1.12 { |
| 111 | catchsql { |
| 112 | ATTACH 'test.db' as db2; |
| 113 | } |
| 114 | } {1 {database db2 is already in use}} |
| 115 | do_test attach-1.13 { |
| 116 | catchsql { |
| 117 | ATTACH 'test.db' as db5; |
| 118 | } |
| 119 | } {1 {database db5 is already in use}} |
| 120 | do_test attach-1.14 { |
| 121 | catchsql { |
| 122 | ATTACH 'test.db' as db9; |
| 123 | } |
| 124 | } {1 {database db9 is already in use}} |
| 125 | do_test attach-1.15 { |
| 126 | catchsql { |
| 127 | ATTACH 'test.db' as main; |
| 128 | } |
| 129 | } {1 {database main is already in use}} |
| 130 | do_test attach-1.16 { |
| 131 | catchsql { |
| 132 | ATTACH 'test.db' as temp; |
| 133 | } |
| 134 | } {1 {database temp is already in use}} |
| 135 | do_test attach-1.17 { |
| 136 | catchsql { |
| 137 | ATTACH 'test.db' as MAIN; |
| 138 | } |
| 139 | } {1 {database MAIN is already in use}} |
| 140 | do_test attach-1.18 { |
| 141 | catchsql { |
| 142 | ATTACH 'test.db' as db10; |
| 143 | ATTACH 'test.db' as db11; |
| 144 | } |
| 145 | } {0 {}} |
| 146 | do_test attach-1.19 { |
| 147 | catchsql { |
| 148 | ATTACH 'test.db' as db12; |
| 149 | } |
| 150 | } {1 {too many attached databases - max 10}} |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 151 | do_test attach-1.20.1 { |
drh | a73af53 | 2003-04-05 16:56:28 +0000 | [diff] [blame] | 152 | execsql { |
| 153 | DETACH db5; |
drh | a73af53 | 2003-04-05 16:56:28 +0000 | [diff] [blame] | 154 | } |
danielk1977 | 27188fb | 2004-11-23 10:13:03 +0000 | [diff] [blame] | 155 | } {} |
| 156 | ifcapable schema_pragmas { |
| 157 | do_test attach-1.20.2 { |
drh | b8ec209 | 2003-06-04 15:53:02 +0000 | [diff] [blame] | 158 | db_list db |
drh | 34f4732 | 2004-08-18 15:58:22 +0000 | [diff] [blame] | 159 | } {0 main 2 db2 3 db3 4 db4 5 db6 6 db7 7 db8 8 db9 9 db10 10 db11} |
danielk1977 | 27188fb | 2004-11-23 10:13:03 +0000 | [diff] [blame] | 160 | } ;# ifcapable schema_pragmas |
danielk1977 | a21c6b6 | 2005-01-24 10:25:59 +0000 | [diff] [blame^] | 161 | integrity_check attach-1.20.3 |
drh | 40e016e | 2004-11-04 14:47:11 +0000 | [diff] [blame] | 162 | execsql {select * from sqlite_temp_master} |
drh | a73af53 | 2003-04-05 16:56:28 +0000 | [diff] [blame] | 163 | do_test attach-1.21 { |
| 164 | catchsql { |
| 165 | ATTACH 'test.db' as db12; |
| 166 | } |
| 167 | } {0 {}} |
| 168 | do_test attach-1.22 { |
| 169 | catchsql { |
| 170 | ATTACH 'test.db' as db13; |
| 171 | } |
| 172 | } {1 {too many attached databases - max 10}} |
| 173 | do_test attach-1.23 { |
| 174 | catchsql { |
| 175 | DETACH db14; |
| 176 | } |
| 177 | } {1 {no such database: db14}} |
| 178 | do_test attach-1.24 { |
| 179 | catchsql { |
| 180 | DETACH db12; |
| 181 | } |
| 182 | } {0 {}} |
| 183 | do_test attach-1.25 { |
| 184 | catchsql { |
| 185 | DETACH db12; |
| 186 | } |
| 187 | } {1 {no such database: db12}} |
| 188 | do_test attach-1.26 { |
| 189 | catchsql { |
| 190 | DETACH main; |
| 191 | } |
| 192 | } {1 {cannot detach database main}} |
| 193 | do_test attach-1.27 { |
| 194 | catchsql { |
| 195 | DETACH Temp; |
| 196 | } |
| 197 | } {1 {cannot detach database Temp}} |
| 198 | do_test attach-1.28 { |
| 199 | catchsql { |
| 200 | DETACH db11; |
| 201 | DETACH db10; |
| 202 | DETACH db9; |
| 203 | DETACH db8; |
| 204 | DETACH db7; |
| 205 | DETACH db6; |
| 206 | DETACH db4; |
| 207 | DETACH db3; |
| 208 | DETACH db2; |
| 209 | } |
| 210 | } {0 {}} |
danielk1977 | 27188fb | 2004-11-23 10:13:03 +0000 | [diff] [blame] | 211 | ifcapable schema_pragmas { |
drh | a73af53 | 2003-04-05 16:56:28 +0000 | [diff] [blame] | 212 | do_test attach-1.29 { |
drh | b8ec209 | 2003-06-04 15:53:02 +0000 | [diff] [blame] | 213 | db_list db |
drh | a73af53 | 2003-04-05 16:56:28 +0000 | [diff] [blame] | 214 | } {0 main 1 temp} |
danielk1977 | 27188fb | 2004-11-23 10:13:03 +0000 | [diff] [blame] | 215 | } ;# ifcapable schema_pragmas |
drh | 2d45834 | 2003-04-05 03:42:26 +0000 | [diff] [blame] | 216 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 217 | ifcapable {trigger} { # Only do the following tests if triggers are enabled |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 218 | do_test attach-2.1 { |
| 219 | execsql { |
| 220 | CREATE TABLE tx(x1,x2,y1,y2); |
| 221 | CREATE TRIGGER r1 AFTER UPDATE ON t2 FOR EACH ROW BEGIN |
| 222 | INSERT INTO tx(x1,x2,y1,y2) VALUES(OLD.x,NEW.x,OLD.y,NEW.y); |
| 223 | END; |
| 224 | SELECT * FROM tx; |
| 225 | } db2; |
| 226 | } {} |
| 227 | do_test attach-2.2 { |
| 228 | execsql { |
| 229 | UPDATE t2 SET x=x+10; |
| 230 | SELECT * FROM tx; |
| 231 | } db2; |
| 232 | } {1 11 x x 2 12 y y} |
| 233 | do_test attach-2.3 { |
| 234 | execsql { |
| 235 | CREATE TABLE tx(x1,x2,y1,y2); |
| 236 | SELECT * FROM tx; |
| 237 | } |
| 238 | } {} |
| 239 | do_test attach-2.4 { |
| 240 | execsql { |
| 241 | ATTACH 'test2.db' AS db2; |
| 242 | } |
| 243 | } {} |
| 244 | do_test attach-2.5 { |
| 245 | execsql { |
| 246 | UPDATE db2.t2 SET x=x+10; |
| 247 | SELECT * FROM db2.tx; |
| 248 | } |
| 249 | } {1 11 x x 2 12 y y 11 21 x x 12 22 y y} |
| 250 | do_test attach-2.6 { |
| 251 | execsql { |
| 252 | SELECT * FROM main.tx; |
| 253 | } |
| 254 | } {} |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 255 | do_test attach-2.7 { |
| 256 | execsql { |
| 257 | SELECT type, name, tbl_name FROM db2.sqlite_master; |
| 258 | } |
| 259 | } {table t2 t2 table tx tx trigger r1 t2} |
danielk1977 | 27188fb | 2004-11-23 10:13:03 +0000 | [diff] [blame] | 260 | ifcapable schema_pragmas { |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 261 | do_test attach-2.8 { |
drh | b8ec209 | 2003-06-04 15:53:02 +0000 | [diff] [blame] | 262 | db_list db |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 263 | } {0 main 1 temp 2 db2} |
danielk1977 | 27188fb | 2004-11-23 10:13:03 +0000 | [diff] [blame] | 264 | } ;# ifcapable schema_pragmas |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 265 | do_test attach-2.9 { |
| 266 | execsql { |
| 267 | CREATE INDEX i2 ON t2(x); |
| 268 | SELECT * FROM t2 WHERE x>5; |
| 269 | } db2 |
| 270 | } {21 x 22 y} |
| 271 | do_test attach-2.10 { |
| 272 | execsql { |
| 273 | SELECT type, name, tbl_name FROM sqlite_master; |
| 274 | } db2 |
| 275 | } {table t2 t2 table tx tx trigger r1 t2 index i2 t2} |
drh | b5f70c2 | 2004-02-14 01:39:50 +0000 | [diff] [blame] | 276 | #do_test attach-2.11 { |
| 277 | # catchsql { |
| 278 | # SELECT * FROM t2 WHERE x>5; |
| 279 | # } |
| 280 | #} {1 {database schema has changed}} |
danielk1977 | 27188fb | 2004-11-23 10:13:03 +0000 | [diff] [blame] | 281 | ifcapable schema_pragmas { |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 282 | do_test attach-2.12 { |
drh | b8ec209 | 2003-06-04 15:53:02 +0000 | [diff] [blame] | 283 | db_list db |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 284 | } {0 main 1 temp 2 db2} |
danielk1977 | 27188fb | 2004-11-23 10:13:03 +0000 | [diff] [blame] | 285 | } ;# ifcapable schema_pragmas |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 286 | do_test attach-2.13 { |
| 287 | catchsql { |
| 288 | SELECT * FROM t2 WHERE x>5; |
| 289 | } |
| 290 | } {0 {21 x 22 y}} |
| 291 | do_test attach-2.14 { |
| 292 | execsql { |
| 293 | SELECT type, name, tbl_name FROM sqlite_master; |
| 294 | } |
| 295 | } {table t1 t1 table tx tx} |
| 296 | do_test attach-2.15 { |
| 297 | execsql { |
| 298 | SELECT type, name, tbl_name FROM db2.sqlite_master; |
| 299 | } |
| 300 | } {table t2 t2 table tx tx trigger r1 t2 index i2 t2} |
| 301 | do_test attach-2.16 { |
| 302 | db close |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 303 | sqlite3 db test.db |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 304 | execsql { |
| 305 | ATTACH 'test2.db' AS db2; |
| 306 | SELECT type, name, tbl_name FROM db2.sqlite_master; |
| 307 | } |
| 308 | } {table t2 t2 table tx tx trigger r1 t2 index i2 t2} |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 309 | } ;# End of ifcapable {trigger} |
drh | a73af53 | 2003-04-05 16:56:28 +0000 | [diff] [blame] | 310 | |
drh | 6b86111 | 2003-05-17 19:23:51 +0000 | [diff] [blame] | 311 | do_test attach-3.1 { |
| 312 | db close |
| 313 | db2 close |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 314 | sqlite3 db test.db |
| 315 | sqlite3 db2 test2.db |
drh | 6b86111 | 2003-05-17 19:23:51 +0000 | [diff] [blame] | 316 | execsql { |
| 317 | SELECT * FROM t1 |
| 318 | } |
| 319 | } {1 2 3 4} |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 320 | |
| 321 | # If we are testing a version of the code that lacks trigger support, |
| 322 | # adjust the database contents so that they are the same if triggers |
| 323 | # had been enabled. |
| 324 | ifcapable {!trigger} { |
| 325 | db2 eval { |
| 326 | DELETE FROM t2; |
| 327 | INSERT INTO t2 VALUES(21, 'x'); |
| 328 | INSERT INTO t2 VALUES(22, 'y'); |
| 329 | CREATE TABLE tx(x1,x2,y1,y2); |
| 330 | INSERT INTO tx VALUES(1, 11, 'x', 'x'); |
| 331 | INSERT INTO tx VALUES(2, 12, 'y', 'y'); |
| 332 | INSERT INTO tx VALUES(11, 21, 'x', 'x'); |
| 333 | INSERT INTO tx VALUES(12, 22, 'y', 'y'); |
| 334 | CREATE INDEX i2 ON t2(x); |
| 335 | } |
| 336 | } |
| 337 | |
drh | 6b86111 | 2003-05-17 19:23:51 +0000 | [diff] [blame] | 338 | do_test attach-3.2 { |
| 339 | catchsql { |
| 340 | SELECT * FROM t2 |
| 341 | } |
| 342 | } {1 {no such table: t2}} |
| 343 | do_test attach-3.3 { |
| 344 | catchsql { |
| 345 | ATTACH DATABASE 'test2.db' AS db2; |
| 346 | SELECT * FROM t2 |
| 347 | } |
| 348 | } {0 {21 x 22 y}} |
| 349 | |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 350 | # Even though 'db' has started a transaction, it should not yet have |
| 351 | # a lock on test2.db so 'db2' should be readable. |
drh | 6b86111 | 2003-05-17 19:23:51 +0000 | [diff] [blame] | 352 | do_test attach-3.4 { |
| 353 | execsql BEGIN |
| 354 | catchsql { |
| 355 | SELECT * FROM t2; |
| 356 | } db2; |
| 357 | } {0 {21 x 22 y}} |
| 358 | |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 359 | # Reading from test2.db from db within a transaction should not |
| 360 | # prevent test2.db from being read by db2. |
drh | 6b86111 | 2003-05-17 19:23:51 +0000 | [diff] [blame] | 361 | do_test attach-3.5 { |
| 362 | execsql {SELECT * FROM t2} |
drh | 178286b | 2005-01-23 13:14:55 +0000 | [diff] [blame] | 363 | btree_breakpoint |
drh | 6b86111 | 2003-05-17 19:23:51 +0000 | [diff] [blame] | 364 | catchsql { |
| 365 | SELECT * FROM t2; |
| 366 | } db2; |
| 367 | } {0 {21 x 22 y}} |
| 368 | |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 369 | # Making a change to test2.db through db causes test2.db to get |
| 370 | # a reserved lock. It should still be accessible through db2. |
drh | 6b86111 | 2003-05-17 19:23:51 +0000 | [diff] [blame] | 371 | do_test attach-3.6 { |
| 372 | execsql { |
| 373 | UPDATE t2 SET x=x+1 WHERE x=50; |
| 374 | } |
| 375 | catchsql { |
| 376 | SELECT * FROM t2; |
| 377 | } db2; |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 378 | } {0 {21 x 22 y}} |
drh | 6b86111 | 2003-05-17 19:23:51 +0000 | [diff] [blame] | 379 | |
| 380 | do_test attach-3.7 { |
| 381 | execsql ROLLBACK |
| 382 | execsql {SELECT * FROM t2} db2 |
| 383 | } {21 x 22 y} |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 384 | |
| 385 | # Start transactions on both db and db2. Once again, just because |
| 386 | # we make a change to test2.db using db2, only a RESERVED lock is |
| 387 | # obtained, so test2.db should still be readable using db. |
| 388 | # |
drh | 6b86111 | 2003-05-17 19:23:51 +0000 | [diff] [blame] | 389 | do_test attach-3.8 { |
| 390 | execsql BEGIN |
| 391 | execsql BEGIN db2 |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 392 | execsql {UPDATE t2 SET x=0 WHERE 0} db2 |
drh | 6b86111 | 2003-05-17 19:23:51 +0000 | [diff] [blame] | 393 | catchsql {SELECT * FROM t2} |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 394 | } {0 {21 x 22 y}} |
| 395 | |
| 396 | # It is also still accessible from db2. |
drh | 6b86111 | 2003-05-17 19:23:51 +0000 | [diff] [blame] | 397 | do_test attach-3.9 { |
| 398 | catchsql {SELECT * FROM t2} db2 |
| 399 | } {0 {21 x 22 y}} |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 400 | |
drh | 6b86111 | 2003-05-17 19:23:51 +0000 | [diff] [blame] | 401 | do_test attach-3.10 { |
| 402 | execsql {SELECT * FROM t1} |
| 403 | } {1 2 3 4} |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 404 | |
drh | 6b86111 | 2003-05-17 19:23:51 +0000 | [diff] [blame] | 405 | do_test attach-3.11 { |
| 406 | catchsql {UPDATE t1 SET a=a+1} |
| 407 | } {0 {}} |
| 408 | do_test attach-3.12 { |
| 409 | execsql {SELECT * FROM t1} |
| 410 | } {2 2 4 4} |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 411 | |
| 412 | # db2 has a RESERVED lock on test2.db, so db cannot write to any tables |
| 413 | # in test2.db. |
drh | 6b86111 | 2003-05-17 19:23:51 +0000 | [diff] [blame] | 414 | do_test attach-3.13 { |
| 415 | catchsql {UPDATE t2 SET x=x+1 WHERE x=50} |
| 416 | } {1 {database is locked}} |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 417 | |
| 418 | # Change for version 3. Transaction is no longer rolled back |
| 419 | # for a locked database. |
| 420 | execsql {ROLLBACK} |
| 421 | |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 422 | # db is able to reread its schema because db2 still only holds a |
| 423 | # reserved lock. |
drh | 6b86111 | 2003-05-17 19:23:51 +0000 | [diff] [blame] | 424 | do_test attach-3.14 { |
drh | 8ef83ff | 2004-02-12 15:31:21 +0000 | [diff] [blame] | 425 | catchsql {SELECT * FROM t1} |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 426 | } {0 {1 2 3 4}} |
drh | 8ef83ff | 2004-02-12 15:31:21 +0000 | [diff] [blame] | 427 | do_test attach-3.15 { |
| 428 | execsql COMMIT db2 |
drh | 6b86111 | 2003-05-17 19:23:51 +0000 | [diff] [blame] | 429 | execsql {SELECT * FROM t1} |
| 430 | } {1 2 3 4} |
| 431 | |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 432 | #set btree_trace 1 |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 433 | |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 434 | # Ticket #323 |
| 435 | do_test attach-4.1 { |
| 436 | execsql {DETACH db2} |
| 437 | db2 close |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 438 | sqlite3 db2 test2.db |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 439 | execsql { |
| 440 | CREATE TABLE t3(x,y); |
| 441 | CREATE UNIQUE INDEX t3i1 ON t3(x); |
| 442 | INSERT INTO t3 VALUES(1,2); |
| 443 | SELECT * FROM t3; |
| 444 | } db2; |
| 445 | } {1 2} |
| 446 | do_test attach-4.2 { |
| 447 | execsql { |
| 448 | CREATE TABLE t3(a,b); |
| 449 | CREATE UNIQUE INDEX t3i1b ON t3(a); |
| 450 | INSERT INTO t3 VALUES(9,10); |
| 451 | SELECT * FROM t3; |
| 452 | } |
| 453 | } {9 10} |
| 454 | do_test attach-4.3 { |
| 455 | execsql { |
| 456 | ATTACH DATABASE 'test2.db' AS db2; |
| 457 | SELECT * FROM db2.t3; |
| 458 | } |
| 459 | } {1 2} |
| 460 | do_test attach-4.4 { |
| 461 | execsql { |
| 462 | SELECT * FROM main.t3; |
| 463 | } |
| 464 | } {9 10} |
| 465 | do_test attach-4.5 { |
| 466 | execsql { |
| 467 | INSERT INTO db2.t3 VALUES(9,10); |
| 468 | SELECT * FROM db2.t3; |
| 469 | } |
| 470 | } {1 2 9 10} |
drh | 798da52 | 2004-11-04 04:42:28 +0000 | [diff] [blame] | 471 | execsql { |
| 472 | DETACH db2; |
| 473 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 474 | ifcapable {trigger} { |
| 475 | do_test attach-4.6 { |
| 476 | execsql { |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 477 | CREATE TABLE t4(x); |
| 478 | CREATE TRIGGER t3r3 AFTER INSERT ON t3 BEGIN |
| 479 | INSERT INTO t4 VALUES('db2.' || NEW.x); |
| 480 | END; |
| 481 | INSERT INTO t3 VALUES(6,7); |
| 482 | SELECT * FROM t4; |
| 483 | } db2 |
| 484 | } {db2.6} |
| 485 | do_test attach-4.7 { |
| 486 | execsql { |
| 487 | CREATE TABLE t4(y); |
| 488 | CREATE TRIGGER t3r3 AFTER INSERT ON t3 BEGIN |
| 489 | INSERT INTO t4 VALUES('main.' || NEW.a); |
| 490 | END; |
| 491 | INSERT INTO main.t3 VALUES(11,12); |
| 492 | SELECT * FROM main.t4; |
| 493 | } |
| 494 | } {main.11} |
| 495 | } |
drh | 798da52 | 2004-11-04 04:42:28 +0000 | [diff] [blame] | 496 | ifcapable {!trigger} { |
| 497 | # When we do not have trigger support, set up the table like they |
| 498 | # would have been had triggers been there. The tests that follow need |
| 499 | # this setup. |
| 500 | execsql { |
| 501 | CREATE TABLE t4(x); |
| 502 | INSERT INTO t3 VALUES(6,7); |
| 503 | INSERT INTO t4 VALUES('db2.6'); |
| 504 | INSERT INTO t4 VALUES('db2.13'); |
| 505 | } db2 |
| 506 | execsql { |
| 507 | CREATE TABLE t4(y); |
| 508 | INSERT INTO main.t3 VALUES(11,12); |
| 509 | INSERT INTO t4 VALUES('main.11'); |
| 510 | } |
| 511 | } |
| 512 | |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 513 | |
| 514 | # This one is tricky. On the UNION ALL select, we have to make sure |
| 515 | # the schema for both main and db2 is valid before starting to execute |
| 516 | # the first query of the UNION ALL. If we wait to test the validity of |
| 517 | # the schema for main until after the first query has run, that test will |
| 518 | # fail and the query will abort but we will have already output some |
| 519 | # results. When the query is retried, the results will be repeated. |
| 520 | # |
danielk1977 | 27c7743 | 2004-11-22 13:35:41 +0000 | [diff] [blame] | 521 | ifcapable compound { |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 522 | do_test attach-4.8 { |
| 523 | execsql { |
| 524 | ATTACH DATABASE 'test2.db' AS db2; |
| 525 | INSERT INTO db2.t3 VALUES(13,14); |
| 526 | SELECT * FROM db2.t4 UNION ALL SELECT * FROM main.t4; |
| 527 | } |
| 528 | } {db2.6 db2.13 main.11} |
drh | 8024205 | 2004-06-09 00:48:12 +0000 | [diff] [blame] | 529 | |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 530 | do_test attach-4.9 { |
drh | 798da52 | 2004-11-04 04:42:28 +0000 | [diff] [blame] | 531 | ifcapable {!trigger} {execsql {INSERT INTO main.t4 VALUES('main.15')}} |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 532 | execsql { |
| 533 | INSERT INTO main.t3 VALUES(15,16); |
| 534 | SELECT * FROM db2.t4 UNION ALL SELECT * FROM main.t4; |
| 535 | } |
| 536 | } {db2.6 db2.13 main.11 main.15} |
danielk1977 | 27c7743 | 2004-11-22 13:35:41 +0000 | [diff] [blame] | 537 | } ;# ifcapable compound |
| 538 | |
| 539 | ifcapable !compound { |
| 540 | ifcapable {!trigger} {execsql {INSERT INTO main.t4 VALUES('main.15')}} |
| 541 | execsql { |
| 542 | ATTACH DATABASE 'test2.db' AS db2; |
| 543 | INSERT INTO db2.t3 VALUES(13,14); |
| 544 | INSERT INTO main.t3 VALUES(15,16); |
| 545 | } |
| 546 | } ;# ifcapable !compound |
danielk1977 | 0fa8ddb | 2004-11-22 08:43:32 +0000 | [diff] [blame] | 547 | |
| 548 | ifcapable view { |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 549 | do_test attach-4.10 { |
| 550 | execsql { |
| 551 | DETACH DATABASE db2; |
| 552 | } |
| 553 | execsql { |
| 554 | CREATE VIEW v3 AS SELECT x*100+y FROM t3; |
| 555 | SELECT * FROM v3; |
| 556 | } db2 |
| 557 | } {102 910 607 1314} |
| 558 | do_test attach-4.11 { |
| 559 | execsql { |
| 560 | CREATE VIEW v3 AS SELECT a*100+b FROM t3; |
| 561 | SELECT * FROM v3; |
| 562 | } |
| 563 | } {910 1112 1516} |
| 564 | do_test attach-4.12 { |
| 565 | execsql { |
| 566 | ATTACH DATABASE 'test2.db' AS db2; |
| 567 | SELECT * FROM db2.v3; |
| 568 | } |
| 569 | } {102 910 607 1314} |
| 570 | do_test attach-4.13 { |
| 571 | execsql { |
| 572 | SELECT * FROM main.v3; |
| 573 | } |
| 574 | } {910 1112 1516} |
danielk1977 | 0fa8ddb | 2004-11-22 08:43:32 +0000 | [diff] [blame] | 575 | } ;# ifcapable view |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 576 | |
drh | 4312db5 | 2003-06-03 01:47:11 +0000 | [diff] [blame] | 577 | # Tests for the sqliteFix...() routines in attach.c |
| 578 | # |
drh | 798da52 | 2004-11-04 04:42:28 +0000 | [diff] [blame] | 579 | ifcapable {trigger} { |
drh | 4312db5 | 2003-06-03 01:47:11 +0000 | [diff] [blame] | 580 | do_test attach-5.1 { |
| 581 | db close |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 582 | sqlite3 db test.db |
drh | 9cb733c | 2003-07-18 01:25:34 +0000 | [diff] [blame] | 583 | db2 close |
drh | 4312db5 | 2003-06-03 01:47:11 +0000 | [diff] [blame] | 584 | file delete -force test2.db |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 585 | sqlite3 db2 test2.db |
drh | 4312db5 | 2003-06-03 01:47:11 +0000 | [diff] [blame] | 586 | catchsql { |
| 587 | ATTACH DATABASE 'test.db' AS orig; |
| 588 | CREATE TRIGGER r1 AFTER INSERT ON orig.t1 BEGIN; |
| 589 | SELECT 'no-op'; |
| 590 | END; |
| 591 | } db2 |
danielk1977 | ef2cb63 | 2004-05-29 02:37:19 +0000 | [diff] [blame] | 592 | } {1 {trigger r1 cannot reference objects in database orig}} |
drh | 4312db5 | 2003-06-03 01:47:11 +0000 | [diff] [blame] | 593 | do_test attach-5.2 { |
| 594 | catchsql { |
| 595 | CREATE TABLE t5(x,y); |
| 596 | CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN |
| 597 | SELECT 'no-op'; |
| 598 | END; |
| 599 | } db2 |
| 600 | } {0 {}} |
| 601 | do_test attach-5.3 { |
| 602 | catchsql { |
| 603 | DROP TRIGGER r5; |
| 604 | CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN |
| 605 | SELECT 'no-op' FROM orig.t1; |
| 606 | END; |
| 607 | } db2 |
| 608 | } {1 {trigger r5 cannot reference objects in database orig}} |
| 609 | do_test attach-5.4 { |
| 610 | catchsql { |
| 611 | CREATE TEMP TABLE t6(p,q,r); |
| 612 | CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN |
| 613 | SELECT 'no-op' FROM temp.t6; |
| 614 | END; |
| 615 | } db2 |
| 616 | } {1 {trigger r5 cannot reference objects in database temp}} |
danielk1977 | e61b9f4 | 2005-01-21 04:25:47 +0000 | [diff] [blame] | 617 | ifcapable subquery { |
| 618 | do_test attach-5.5 { |
| 619 | catchsql { |
| 620 | CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN |
| 621 | SELECT 'no-op' || (SELECT * FROM temp.t6); |
| 622 | END; |
| 623 | } db2 |
| 624 | } {1 {trigger r5 cannot reference objects in database temp}} |
| 625 | do_test attach-5.6 { |
| 626 | catchsql { |
| 627 | CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN |
| 628 | SELECT 'no-op' FROM t1 WHERE x<(SELECT min(x) FROM temp.t6); |
| 629 | END; |
| 630 | } db2 |
| 631 | } {1 {trigger r5 cannot reference objects in database temp}} |
| 632 | do_test attach-5.7 { |
| 633 | catchsql { |
| 634 | CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN |
| 635 | SELECT 'no-op' FROM t1 GROUP BY 1 HAVING x<(SELECT min(x) FROM temp.t6); |
| 636 | END; |
| 637 | } db2 |
| 638 | } {1 {trigger r5 cannot reference objects in database temp}} |
| 639 | do_test attach-5.7 { |
| 640 | catchsql { |
| 641 | CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN |
| 642 | SELECT max(1,x,(SELECT min(x) FROM temp.t6)) FROM t1; |
| 643 | END; |
| 644 | } db2 |
| 645 | } {1 {trigger r5 cannot reference objects in database temp}} |
| 646 | do_test attach-5.8 { |
| 647 | catchsql { |
| 648 | CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN |
| 649 | INSERT INTO t1 VALUES((SELECT min(x) FROM temp.t6),5); |
| 650 | END; |
| 651 | } db2 |
| 652 | } {1 {trigger r5 cannot reference objects in database temp}} |
| 653 | do_test attach-5.9 { |
| 654 | catchsql { |
| 655 | CREATE TRIGGER r5 AFTER INSERT ON t5 BEGIN |
| 656 | DELETE FROM t1 WHERE x<(SELECT min(x) FROM temp.t6); |
| 657 | END; |
| 658 | } db2 |
| 659 | } {1 {trigger r5 cannot reference objects in database temp}} |
| 660 | } ;# endif subquery |
drh | 798da52 | 2004-11-04 04:42:28 +0000 | [diff] [blame] | 661 | } ;# endif trigger |
drh | 4312db5 | 2003-06-03 01:47:11 +0000 | [diff] [blame] | 662 | |
| 663 | # Check to make sure we get a sensible error if unable to open |
| 664 | # the file that we are trying to attach. |
| 665 | # |
| 666 | do_test attach-6.1 { |
| 667 | catchsql { |
| 668 | ATTACH DATABASE 'no-such-file' AS nosuch; |
| 669 | } |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 670 | } {0 {}} |
drh | bc2bca0 | 2003-06-04 12:31:53 +0000 | [diff] [blame] | 671 | if {$tcl_platform(platform)=="unix"} { |
| 672 | do_test attach-6.2 { |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 673 | sqlite3 dbx cannot-read |
drh | bc2bca0 | 2003-06-04 12:31:53 +0000 | [diff] [blame] | 674 | dbx eval {CREATE TABLE t1(a,b,c)} |
| 675 | dbx close |
| 676 | file attributes cannot-read -permission 0000 |
| 677 | catchsql { |
| 678 | ATTACH DATABASE 'cannot-read' AS noread; |
| 679 | } |
| 680 | } {1 {unable to open database: cannot-read}} |
| 681 | file delete -force cannot-read |
| 682 | } |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 683 | |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 684 | # Check the error message if we try to access a database that has |
| 685 | # not been attached. |
| 686 | do_test attach-6.3 { |
| 687 | catchsql { |
| 688 | CREATE TABLE no_such_db.t1(a, b, c); |
| 689 | } |
| 690 | } {1 {unknown database no_such_db}} |
drh | 7bf5661 | 2005-01-21 15:52:32 +0000 | [diff] [blame] | 691 | ifcapable !compound { |
| 692 | do_test attach-6.4 { |
| 693 | catchsql { |
| 694 | CREATE TEMP TABLE db2.temp1(a, b, c); |
| 695 | } |
| 696 | } {1 {temporary table name must be unqualified}} |
| 697 | } |
drh | a73af53 | 2003-04-05 16:56:28 +0000 | [diff] [blame] | 698 | for {set i 2} {$i<=15} {incr i} { |
| 699 | catch {db$i close} |
| 700 | } |
drh | 02f9f6b | 2004-05-31 18:21:54 +0000 | [diff] [blame] | 701 | db close |
drh | 4312db5 | 2003-06-03 01:47:11 +0000 | [diff] [blame] | 702 | file delete -force test2.db |
drh | 02f9f6b | 2004-05-31 18:21:54 +0000 | [diff] [blame] | 703 | file delete -force no-such-file |
drh | 2d45834 | 2003-04-05 03:42:26 +0000 | [diff] [blame] | 704 | |
| 705 | |
| 706 | finish_test |