xref: /illumos-gate/usr/src/lib/libsqlite/test/attach2.test (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
1*1da57d55SToomas Soome#
2c5c4113dSnw141292# 2003 July 1
3c5c4113dSnw141292#
4c5c4113dSnw141292# The author disclaims copyright to this source code.  In place of
5c5c4113dSnw141292# a legal notice, here is a blessing:
6c5c4113dSnw141292#
7c5c4113dSnw141292#    May you do good and not evil.
8c5c4113dSnw141292#    May you find forgiveness for yourself and forgive others.
9c5c4113dSnw141292#    May you share freely, never taking more than you give.
10c5c4113dSnw141292#
11c5c4113dSnw141292#***********************************************************************
12c5c4113dSnw141292# This file implements regression tests for SQLite library.  The
13c5c4113dSnw141292# focus of this script is testing the ATTACH and DETACH commands
14c5c4113dSnw141292# and related functionality.
15c5c4113dSnw141292#
16c5c4113dSnw141292# $Id: attach2.test,v 1.5 2004/02/12 15:31:22 drh Exp $
17c5c4113dSnw141292#
18c5c4113dSnw141292
19c5c4113dSnw141292
20c5c4113dSnw141292set testdir [file dirname $argv0]
21c5c4113dSnw141292source $testdir/tester.tcl
22c5c4113dSnw141292
23c5c4113dSnw141292# Ticket #354
24c5c4113dSnw141292#
25c5c4113dSnw141292do_test attach2-1.1 {
26c5c4113dSnw141292  db eval {
27c5c4113dSnw141292    CREATE TABLE t1(a,b);
28c5c4113dSnw141292    CREATE INDEX x1 ON t1(a);
29c5c4113dSnw141292  }
30c5c4113dSnw141292  file delete -force test2.db
31c5c4113dSnw141292  file delete -force test2.db-journal
32c5c4113dSnw141292  sqlite db2 test2.db
33c5c4113dSnw141292  db2 eval {
34c5c4113dSnw141292    CREATE TABLE t1(a,b);
35c5c4113dSnw141292    CREATE INDEX x1 ON t1(a);
36c5c4113dSnw141292  }
37c5c4113dSnw141292  catchsql {
38c5c4113dSnw141292    ATTACH 'test2.db' AS t2;
39c5c4113dSnw141292  }
40c5c4113dSnw141292} {0 {}}
41c5c4113dSnw141292
42c5c4113dSnw141292# Ticket #514
43c5c4113dSnw141292#
44c5c4113dSnw141292proc db_list {db} {
45c5c4113dSnw141292  set list {}
46c5c4113dSnw141292  foreach {idx name file} [execsql {PRAGMA database_list} $db] {
47c5c4113dSnw141292    lappend list $idx $name
48c5c4113dSnw141292  }
49c5c4113dSnw141292  return $list
50c5c4113dSnw141292}
51c5c4113dSnw141292db eval {DETACH t2}
52c5c4113dSnw141292do_test attach2-2.1 {
53c5c4113dSnw141292  # lock test2.db then try to attach it.  Should get an error.
54c5c4113dSnw141292  db2 eval {BEGIN}
55c5c4113dSnw141292  catchsql {
56c5c4113dSnw141292    ATTACH 'test2.db' AS t2;
57c5c4113dSnw141292  }
58c5c4113dSnw141292} {1 {database is locked}}
59c5c4113dSnw141292do_test attach2-2.2 {
60c5c4113dSnw141292  # make sure test2.db did not get attached.
61c5c4113dSnw141292  db_list db
62c5c4113dSnw141292} {0 main 1 temp}
63c5c4113dSnw141292do_test attach2-2.3 {
64c5c4113dSnw141292  # unlock test2.db and try to attach again.  should work this time.
65c5c4113dSnw141292  db2 eval {COMMIT}
66c5c4113dSnw141292  catchsql {
67c5c4113dSnw141292    ATTACH 'test2.db' AS t2;
68c5c4113dSnw141292  }
69c5c4113dSnw141292} {0 {}}
70c5c4113dSnw141292do_test attach2-2.4 {
71c5c4113dSnw141292  db_list db
72c5c4113dSnw141292} {0 main 1 temp 2 t2}
73c5c4113dSnw141292do_test attach2-2.5 {
74c5c4113dSnw141292  catchsql {
75c5c4113dSnw141292    SELECT name FROM t2.sqlite_master;
76c5c4113dSnw141292  }
77c5c4113dSnw141292} {0 {t1 x1}}
78c5c4113dSnw141292do_test attach2-2.6 {
79c5c4113dSnw141292  # lock test2.db and try to read from it.  should get an error.
80c5c4113dSnw141292  db2 eval BEGIN
81c5c4113dSnw141292  catchsql {
82c5c4113dSnw141292    SELECT name FROM t2.sqlite_master;
83c5c4113dSnw141292  }
84c5c4113dSnw141292} {1 {database is locked}}
85c5c4113dSnw141292do_test attach2-2.7 {
86c5c4113dSnw141292  # but we can still read from test1.db even though test2.db is locked.
87c5c4113dSnw141292  catchsql {
88c5c4113dSnw141292    SELECT name FROM main.sqlite_master;
89c5c4113dSnw141292  }
90c5c4113dSnw141292} {0 {t1 x1}}
91c5c4113dSnw141292do_test attach2-2.8 {
92c5c4113dSnw141292  # start a transaction on test.db even though test2.db is locked.
93c5c4113dSnw141292  catchsql {
94c5c4113dSnw141292    BEGIN;
95c5c4113dSnw141292    INSERT INTO t1 VALUES(8,9);
96c5c4113dSnw141292  }
97c5c4113dSnw141292} {0 {}}
98c5c4113dSnw141292do_test attach2-2.9 {
99c5c4113dSnw141292  execsql {
100c5c4113dSnw141292    SELECT * FROM t1
101c5c4113dSnw141292  }
102c5c4113dSnw141292} {8 9}
103c5c4113dSnw141292do_test attach2-2.10 {
104c5c4113dSnw141292  # now try to write to test2.db.  the write should fail
105c5c4113dSnw141292  catchsql {
106c5c4113dSnw141292    INSERT INTO t2.t1 VALUES(1,2);
107c5c4113dSnw141292  }
108c5c4113dSnw141292} {1 {database is locked}}
109c5c4113dSnw141292do_test attach2-2.11 {
110c5c4113dSnw141292  # when the write failed in the previous test, the transaction should
111c5c4113dSnw141292  # have rolled back.
112c5c4113dSnw141292  db2 eval ROLLBACK
113c5c4113dSnw141292  execsql {
114c5c4113dSnw141292    SELECT * FROM t1
115c5c4113dSnw141292  }
116c5c4113dSnw141292} {}
117c5c4113dSnw141292do_test attach2-2.12 {
118c5c4113dSnw141292  catchsql {
119c5c4113dSnw141292    COMMIT
120c5c4113dSnw141292  }
121c5c4113dSnw141292} {1 {cannot commit - no transaction is active}}
122c5c4113dSnw141292
123c5c4113dSnw141292# Ticket #574:  Make sure it works usingi the non-callback API
124c5c4113dSnw141292#
125c5c4113dSnw141292do_test attach2-3.1 {
126c5c4113dSnw141292  db close
127c5c4113dSnw141292  set DB [sqlite db test.db]
128c5c4113dSnw141292  set rc [catch {sqlite_compile $DB "ATTACH 'test2.db' AS t2" TAIL} VM]
129c5c4113dSnw141292  if {$rc} {lappend rc $VM}
130c5c4113dSnw141292  sqlite_finalize $VM
131c5c4113dSnw141292  set rc
132c5c4113dSnw141292} {0}
133c5c4113dSnw141292do_test attach2-3.2 {
134c5c4113dSnw141292  set rc [catch {sqlite_compile $DB "DETACH t2" TAIL} VM]
135c5c4113dSnw141292  if {$rc} {lappend rc $VM}
136c5c4113dSnw141292  sqlite_finalize $VM
137c5c4113dSnw141292  set rc
138c5c4113dSnw141292} {0}
139c5c4113dSnw141292
140c5c4113dSnw141292db close
141c5c4113dSnw141292for {set i 2} {$i<=15} {incr i} {
142c5c4113dSnw141292  catch {db$i close}
143c5c4113dSnw141292}
144c5c4113dSnw141292file delete -force test2.db
145c5c4113dSnw141292
146c5c4113dSnw141292
147c5c4113dSnw141292finish_test
148