blob: b7ea4d3fc625c9d8f2ee87ef0080bf35a518f4a3 [file] [log] [blame]
drh0d339e42014-12-19 20:27:02 +00001# 2014-12-19
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.
12#
13# This file implements tests for PRAGMA data_version command.
14#
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19do_execsql_test pragma3-100 {
20 PRAGMA data_version;
21} {1}
22do_execsql_test pragma3-101 {
23 PRAGMA temp.data_version;
24} {1}
25
drhd7107b32014-12-20 14:34:02 +000026# Writing to the pragma is a no-op
drh0d339e42014-12-19 20:27:02 +000027do_execsql_test pragma3-102 {
28 PRAGMA main.data_version=1234;
29 PRAGMA main.data_version;
30} {1 1}
31
drhd7107b32014-12-20 14:34:02 +000032# EVIDENCE-OF: R-27726-60934 The "PRAGMA data_version" command provides
33# an indication that the database file has been modified.
34#
drh4a86d002014-12-22 22:02:20 +000035# EVIDENCE-OF: R-47505-58569 The "PRAGMA data_version" value is
36# unchanged for commits made on the same database connection.
drhd7107b32014-12-20 14:34:02 +000037#
drh0d339e42014-12-19 20:27:02 +000038do_execsql_test pragma3-110 {
drh3da9c042014-12-22 18:41:21 +000039 PRAGMA data_version;
40 BEGIN IMMEDIATE;
41 PRAGMA data_version;
drh0d339e42014-12-19 20:27:02 +000042 CREATE TABLE t1(a);
43 INSERT INTO t1 VALUES(100),(200),(300);
drh3da9c042014-12-22 18:41:21 +000044 PRAGMA data_version;
45 COMMIT;
drh0d339e42014-12-19 20:27:02 +000046 SELECT * FROM t1;
47 PRAGMA data_version;
drh3da9c042014-12-22 18:41:21 +000048} {1 1 1 100 200 300 1}
drh0d339e42014-12-19 20:27:02 +000049
50sqlite3 db2 test.db
51do_test pragma3-120 {
52 db2 eval {
53 SELECT * FROM t1;
54 PRAGMA data_version;
55 }
56} {100 200 300 1}
57
58do_execsql_test pragma3-130 {
drh3da9c042014-12-22 18:41:21 +000059 PRAGMA data_version;
60 BEGIN IMMEDIATE;
61 PRAGMA data_version;
drh0d339e42014-12-19 20:27:02 +000062 INSERT INTO t1 VALUES(400),(500);
drh3da9c042014-12-22 18:41:21 +000063 PRAGMA data_version;
64 COMMIT;
drh0d339e42014-12-19 20:27:02 +000065 SELECT * FROM t1;
66 PRAGMA data_version;
drh542d5582014-12-31 14:18:48 +000067 PRAGMA shrink_memory;
drh3da9c042014-12-22 18:41:21 +000068} {1 1 1 100 200 300 400 500 1}
drh0d339e42014-12-19 20:27:02 +000069
drh3da9c042014-12-22 18:41:21 +000070# EVIDENCE-OF: R-63005-41812 The integer values returned by two
71# invocations of "PRAGMA data_version" from the same connection will be
72# different if changes were committed to the database by any other
73# connection in the interim.
drhd7107b32014-12-20 14:34:02 +000074#
drh3da9c042014-12-22 18:41:21 +000075# Value went from 1 in pragma3-120 to 2 here.
drhd7107b32014-12-20 14:34:02 +000076#
drh0d339e42014-12-19 20:27:02 +000077do_test pragma3-140 {
78 db2 eval {
79 SELECT * FROM t1;
80 PRAGMA data_version;
drh3da9c042014-12-22 18:41:21 +000081 BEGIN IMMEDIATE;
82 PRAGMA data_version;
drh0d339e42014-12-19 20:27:02 +000083 UPDATE t1 SET a=a+1;
drh3da9c042014-12-22 18:41:21 +000084 COMMIT;
drh0d339e42014-12-19 20:27:02 +000085 SELECT * FROM t1;
86 PRAGMA data_version;
87 }
drh3da9c042014-12-22 18:41:21 +000088} {100 200 300 400 500 2 2 101 201 301 401 501 2}
drh0d339e42014-12-19 20:27:02 +000089do_execsql_test pragma3-150 {
90 SELECT * FROM t1;
91 PRAGMA data_version;
drh3da9c042014-12-22 18:41:21 +000092} {101 201 301 401 501 2}
drh0d339e42014-12-19 20:27:02 +000093
drhd7107b32014-12-20 14:34:02 +000094#
drh3da9c042014-12-22 18:41:21 +000095do_test pragma3-160 {
96 db eval {
97 BEGIN;
98 PRAGMA data_version;
99 UPDATE t1 SET a=555 WHERE a=501;
100 PRAGMA data_version;
101 SELECT * FROM t1 ORDER BY a;
102 PRAGMA data_version;
103 }
104} {2 2 101 201 301 401 555 2}
105do_test pragma3-170 {
106 db2 eval {
107 PRAGMA data_version;
108 }
109} {2}
110do_test pragma3-180 {
111 db eval {
112 COMMIT;
113 PRAGMA data_version;
114 }
115} {2}
116do_test pragma3-190 {
117 db2 eval {
118 PRAGMA data_version;
119 }
120} {3}
121
122# EVIDENCE-OF: R-19326-44825 The "PRAGMA data_version" value is a local
123# property of each database connection and so values returned by two
124# concurrent invocations of "PRAGMA data_version" on separate database
125# connections are often different even though the underlying database is
126# identical.
127#
128do_test pragma3-195 {
129 expr {[db eval {PRAGMA data_version}]!=[db2 eval {PRAGMA data_version}]}
130} {1}
131
132# EVIDENCE-OF: R-54562-06892 The behavior of "PRAGMA data_version" is
133# the same for all database connections, including database connections
134# in separate processes and shared cache database connections.
135#
136# The next block checks the behavior for separate processes.
drhd7107b32014-12-20 14:34:02 +0000137#
138do_test pragma3-200 {
drh3da9c042014-12-22 18:41:21 +0000139 db eval {PRAGMA data_version; SELECT * FROM t1;}
140} {2 101 201 301 401 555}
141do_test pragma3-201 {
drhd7107b32014-12-20 14:34:02 +0000142 set fd [open pragma3.txt wb]
143 puts $fd {
144 sqlite3 db test.db;
145 db eval {DELETE FROM t1 WHERE a>300};
146 db close;
147 exit;
148 }
149 close $fd
150 exec [info nameofexec] pragma3.txt
151 forcedelete pragma3.txt
152 db eval {
153 PRAGMA data_version;
154 SELECT * FROM t1;
155 }
drh3da9c042014-12-22 18:41:21 +0000156} {3 101 201}
drh0d339e42014-12-19 20:27:02 +0000157db2 close
drhd7107b32014-12-20 14:34:02 +0000158db close
159
drh3da9c042014-12-22 18:41:21 +0000160# EVIDENCE-OF: R-54562-06892 The behavior of "PRAGMA data_version" is
161# the same for all database connections, including database connections
162# in separate processes and shared cache database connections.
drhd7107b32014-12-20 14:34:02 +0000163#
drh3da9c042014-12-22 18:41:21 +0000164# The next block checks that behavior is the same for shared-cache.
drhd7107b32014-12-20 14:34:02 +0000165#
166ifcapable shared_cache {
167 set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
168 sqlite3 db test.db
169 sqlite3 db2 test.db
170 do_test pragma3-300 {
171 db eval {
172 PRAGMA data_version;
drh3da9c042014-12-22 18:41:21 +0000173 BEGIN;
drhd7107b32014-12-20 14:34:02 +0000174 CREATE TABLE t3(a,b,c);
drh3da9c042014-12-22 18:41:21 +0000175 CREATE TABLE t4(x,y,z);
176 INSERT INTO t4 VALUES(123,456,789);
177 PRAGMA data_version;
178 COMMIT;
drhd7107b32014-12-20 14:34:02 +0000179 PRAGMA data_version;
180 }
drh3da9c042014-12-22 18:41:21 +0000181 } {1 1 1}
drhd7107b32014-12-20 14:34:02 +0000182 do_test pragma3-310 {
183 db2 eval {
184 PRAGMA data_version;
drh3da9c042014-12-22 18:41:21 +0000185 BEGIN;
drhd7107b32014-12-20 14:34:02 +0000186 INSERT INTO t3(a,b,c) VALUES('abc','def','ghi');
187 SELECT * FROM t3;
188 PRAGMA data_version;
189 }
drh3da9c042014-12-22 18:41:21 +0000190 } {2 abc def ghi 2}
191 # The transaction in db2 has not yet committed, so the data_version in
192 # db is unchanged.
drhd7107b32014-12-20 14:34:02 +0000193 do_test pragma3-320 {
194 db eval {
195 PRAGMA data_version;
drh3da9c042014-12-22 18:41:21 +0000196 SELECT * FROM t4;
drhd7107b32014-12-20 14:34:02 +0000197 }
drh3da9c042014-12-22 18:41:21 +0000198 } {1 123 456 789}
199 do_test pragma3-330 {
200 db2 eval {
201 COMMIT;
202 PRAGMA data_version;
203 SELECT * FROM t4;
204 }
205 } {2 123 456 789}
206 do_test pragma3-340 {
207 db eval {
208 PRAGMA data_version;
209 SELECT * FROM t3;
210 SELECT * FROM t4;
211 }
212 } {2 abc def ghi 123 456 789}
drhd7107b32014-12-20 14:34:02 +0000213 db2 close
drh59871fe2014-12-20 14:50:28 +0000214 db close
drhd7107b32014-12-20 14:34:02 +0000215 sqlite3_enable_shared_cache $::enable_shared_cache
216}
217
drh59871fe2014-12-20 14:50:28 +0000218# Make sure this also works in WAL mode
219#
dan55e115f2014-12-30 18:07:34 +0000220# This will not work with the in-memory journal permutation, as opening
221# [db2] switches the journal mode back to "memory"
222#
drh59871fe2014-12-20 14:50:28 +0000223ifcapable wal {
dan55e115f2014-12-30 18:07:34 +0000224if {[permutation]!="inmemory_journal"} {
225
drh59871fe2014-12-20 14:50:28 +0000226 sqlite3 db test.db
227 db eval {PRAGMA journal_mode=WAL}
228 sqlite3 db2 test.db
229 do_test pragma3-400 {
230 db eval {
231 PRAGMA data_version;
232 PRAGMA journal_mode;
233 SELECT * FROM t1;
234 }
drh3da9c042014-12-22 18:41:21 +0000235 } {2 wal 101 201}
drh59871fe2014-12-20 14:50:28 +0000236 do_test pragma3-410 {
237 db2 eval {
238 PRAGMA data_version;
239 PRAGMA journal_mode;
240 SELECT * FROM t1;
241 }
242 } {2 wal 101 201}
243 do_test pragma3-420 {
244 db eval {UPDATE t1 SET a=111*(a/100); PRAGMA data_version; SELECT * FROM t1}
drh3da9c042014-12-22 18:41:21 +0000245 } {2 111 222}
drh59871fe2014-12-20 14:50:28 +0000246 do_test pragma3-430 {
247 db2 eval {PRAGMA data_version; SELECT * FROM t1;}
248 } {3 111 222}
249 db2 close
250}
dan55e115f2014-12-30 18:07:34 +0000251}
drh59871fe2014-12-20 14:50:28 +0000252
drh0d339e42014-12-19 20:27:02 +0000253finish_test