Enhance the command-line completion extension to return the names of
[sqlite.git] / test / pragma3.test
blob8a4f4f9d84acd57c6ed54d6a9091bffe433f203c
1 # 2014-12-19
3 # The author disclaims copyright to this source code.  In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library.
13 # This file implements tests for PRAGMA data_version command.
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
18 do_not_use_codec
20 do_execsql_test pragma3-100 {
21   PRAGMA data_version;
22 } {1}
23 do_execsql_test pragma3-101 {
24   PRAGMA temp.data_version;
25 } {1}
27 # Writing to the pragma is a no-op 
28 do_execsql_test pragma3-102 {
29   PRAGMA main.data_version=1234;
30   PRAGMA main.data_version;
31 } {1 1}
33 # EVIDENCE-OF: R-27726-60934 The "PRAGMA data_version" command provides
34 # an indication that the database file has been modified.
36 # EVIDENCE-OF: R-47505-58569 The "PRAGMA data_version" value is
37 # unchanged for commits made on the same database connection.
39 do_execsql_test pragma3-110 {
40   PRAGMA data_version;
41   BEGIN IMMEDIATE;
42   PRAGMA data_version;
43   CREATE TABLE t1(a);
44   INSERT INTO t1 VALUES(100),(200),(300);
45   PRAGMA data_version;
46   COMMIT;
47   SELECT * FROM t1;
48   PRAGMA data_version;
49 } {1 1 1 100 200 300 1}
51 sqlite3 db2 test.db
52 do_test pragma3-120 {
53   db2 eval {
54     SELECT * FROM t1;
55     PRAGMA data_version;
56   }
57 } {100 200 300 1}
59 do_execsql_test pragma3-130 {
60   PRAGMA data_version;
61   BEGIN IMMEDIATE;
62   PRAGMA data_version;
63   INSERT INTO t1 VALUES(400),(500);
64   PRAGMA data_version;
65   COMMIT;
66   SELECT * FROM t1;
67   PRAGMA data_version;
68   PRAGMA shrink_memory;
69 } {1 1 1 100 200 300 400 500 1}
71 # EVIDENCE-OF: R-63005-41812 The integer values returned by two
72 # invocations of "PRAGMA data_version" from the same connection will be
73 # different if changes were committed to the database by any other
74 # connection in the interim.
76 # Value went from 1 in pragma3-120 to 2 here.
78 do_test pragma3-140 {
79   db2 eval {
80     SELECT * FROM t1;
81     PRAGMA data_version;
82     BEGIN IMMEDIATE;
83     PRAGMA data_version;
84     UPDATE t1 SET a=a+1;
85     COMMIT;
86     SELECT * FROM t1;
87     PRAGMA data_version;
88   }
89 } {100 200 300 400 500 2 2 101 201 301 401 501 2}
90 do_execsql_test pragma3-150 {
91   SELECT * FROM t1;
92   PRAGMA data_version;
93 } {101 201 301 401 501 2}
96 do_test pragma3-160 {
97   db eval {
98     BEGIN;
99     PRAGMA data_version;
100     UPDATE t1 SET a=555 WHERE a=501;
101     PRAGMA data_version;
102     SELECT * FROM t1 ORDER BY a;
103     PRAGMA data_version;
104   }
105 } {2 2 101 201 301 401 555 2}
106 do_test pragma3-170 {
107   db2 eval {
108     PRAGMA data_version;
109   }
110 } {2}
111 do_test pragma3-180 {
112   db eval {
113     COMMIT;
114     PRAGMA data_version;
115   }
116 } {2}
117 do_test pragma3-190 {
118   db2 eval {
119     PRAGMA data_version;
120   }
121 } {3}
123 # EVIDENCE-OF: R-19326-44825 The "PRAGMA data_version" value is a local
124 # property of each database connection and so values returned by two
125 # concurrent invocations of "PRAGMA data_version" on separate database
126 # connections are often different even though the underlying database is
127 # identical.
129 do_test pragma3-195 {
130   expr {[db eval {PRAGMA data_version}]!=[db2 eval {PRAGMA data_version}]}
131 } {1}
133 # EVIDENCE-OF: R-54562-06892 The behavior of "PRAGMA data_version" is
134 # the same for all database connections, including database connections
135 # in separate processes and shared cache database connections.
137 # The next block checks the behavior for separate processes.
139 do_test pragma3-200 {
140   db eval {PRAGMA data_version; SELECT * FROM t1;}
141 } {2 101 201 301 401 555}
142 do_test pragma3-201 {
143   set fd [open pragma3.txt wb]
144   puts $fd {
145      sqlite3 db test.db;
146      db eval {DELETE FROM t1 WHERE a>300};
147      db close;
148      exit;
149   }
150   close $fd
151   exec [info nameofexec] pragma3.txt
152   forcedelete pragma3.txt
153   db eval {
154     PRAGMA data_version;
155     SELECT * FROM t1;
156   }
157 } {3 101 201}
158 db2 close
159 db close
161 # EVIDENCE-OF: R-54562-06892 The behavior of "PRAGMA data_version" is
162 # the same for all database connections, including database connections
163 # in separate processes and shared cache database connections.
165 # The next block checks that behavior is the same for shared-cache.
167 ifcapable shared_cache {
168   set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
169   sqlite3 db test.db
170   sqlite3 db2 test.db
171   do_test pragma3-300 {
172     db eval {
173       PRAGMA data_version;
174       BEGIN;
175       CREATE TABLE t3(a,b,c);
176       CREATE TABLE t4(x,y,z);
177       INSERT INTO t4 VALUES(123,456,789);
178       PRAGMA data_version;
179       COMMIT;
180       PRAGMA data_version;
181     }
182   } {1 1 1}
183   do_test pragma3-310 {
184     db2 eval {
185       PRAGMA data_version;
186       BEGIN;
187       INSERT INTO t3(a,b,c) VALUES('abc','def','ghi');
188       SELECT * FROM t3;
189       PRAGMA data_version;
190     }
191   } {2 abc def ghi 2}
192   # The transaction in db2 has not yet committed, so the data_version in
193   # db is unchanged.
194   do_test pragma3-320 {
195     db eval {
196       PRAGMA data_version;
197       SELECT * FROM t4;
198     }
199   } {1 123 456 789}
200   do_test pragma3-330 {
201     db2 eval {
202       COMMIT;
203       PRAGMA data_version;
204       SELECT * FROM t4;
205     }
206   } {2 123 456 789}
207   do_test pragma3-340 {
208     db eval {
209       PRAGMA data_version;
210       SELECT * FROM t3;
211       SELECT * FROM t4;
212     }
213   } {2 abc def ghi 123 456 789}
214   db2 close
215   db close
216   sqlite3_enable_shared_cache $::enable_shared_cache
219 # Make sure this also works in WAL mode
221 # This will not work with the in-memory journal permutation, as opening
222 # [db2] switches the journal mode back to "memory"
224 if {[wal_is_capable]} {
225 if {[permutation]!="inmemory_journal"} {
227   sqlite3 db test.db
228   db eval {PRAGMA journal_mode=WAL}
229   sqlite3 db2 test.db
230   do_test pragma3-400 {
231     db eval {
232       PRAGMA data_version;
233       PRAGMA journal_mode;
234       SELECT * FROM t1;
235     }
236   } {2 wal 101 201}
237   do_test pragma3-410 {
238     db2 eval {
239       PRAGMA data_version;
240       PRAGMA journal_mode;
241       SELECT * FROM t1;
242     }
243   } {2 wal 101 201}
244   do_test pragma3-420 {
245     db eval {UPDATE t1 SET a=111*(a/100); PRAGMA data_version; SELECT * FROM t1}
246   } {2 111 222}
247   do_test pragma3-430 {
248     db2 eval {PRAGMA data_version; SELECT * FROM t1;}
249   } {3 111 222}
250   db2 close
254 finish_test