blob: 8a4f4f9d84acd57c6ed54d6a9091bffe433f203c [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
drh7da56b42016-03-14 18:34:42 +000018do_not_use_codec
drh0d339e42014-12-19 20:27:02 +000019
20do_execsql_test pragma3-100 {
21 PRAGMA data_version;
22} {1}
23do_execsql_test pragma3-101 {
24 PRAGMA temp.data_version;
25} {1}
26
drhd7107b32014-12-20 14:34:02 +000027# Writing to the pragma is a no-op
drh0d339e42014-12-19 20:27:02 +000028do_execsql_test pragma3-102 {
29 PRAGMA main.data_version=1234;
30 PRAGMA main.data_version;
31} {1 1}
32
drhd7107b32014-12-20 14:34:02 +000033# EVIDENCE-OF: R-27726-60934 The "PRAGMA data_version" command provides
34# an indication that the database file has been modified.
35#
drh4a86d002014-12-22 22:02:20 +000036# EVIDENCE-OF: R-47505-58569 The "PRAGMA data_version" value is
37# unchanged for commits made on the same database connection.
drhd7107b32014-12-20 14:34:02 +000038#
drh0d339e42014-12-19 20:27:02 +000039do_execsql_test pragma3-110 {
drh3da9c042014-12-22 18:41:21 +000040 PRAGMA data_version;
41 BEGIN IMMEDIATE;
42 PRAGMA data_version;
drh0d339e42014-12-19 20:27:02 +000043 CREATE TABLE t1(a);
44 INSERT INTO t1 VALUES(100),(200),(300);
drh3da9c042014-12-22 18:41:21 +000045 PRAGMA data_version;
46 COMMIT;
drh0d339e42014-12-19 20:27:02 +000047 SELECT * FROM t1;
48 PRAGMA data_version;
drh3da9c042014-12-22 18:41:21 +000049} {1 1 1 100 200 300 1}
drh0d339e42014-12-19 20:27:02 +000050
51sqlite3 db2 test.db
52do_test pragma3-120 {
53 db2 eval {
54 SELECT * FROM t1;
55 PRAGMA data_version;
56 }
57} {100 200 300 1}
58
59do_execsql_test pragma3-130 {
drh3da9c042014-12-22 18:41:21 +000060 PRAGMA data_version;
61 BEGIN IMMEDIATE;
62 PRAGMA data_version;
drh0d339e42014-12-19 20:27:02 +000063 INSERT INTO t1 VALUES(400),(500);
drh3da9c042014-12-22 18:41:21 +000064 PRAGMA data_version;
65 COMMIT;
drh0d339e42014-12-19 20:27:02 +000066 SELECT * FROM t1;
67 PRAGMA data_version;
drh542d5582014-12-31 14:18:48 +000068 PRAGMA shrink_memory;
drh3da9c042014-12-22 18:41:21 +000069} {1 1 1 100 200 300 400 500 1}
drh0d339e42014-12-19 20:27:02 +000070
drh3da9c042014-12-22 18:41:21 +000071# 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.
drhd7107b32014-12-20 14:34:02 +000075#
drh3da9c042014-12-22 18:41:21 +000076# Value went from 1 in pragma3-120 to 2 here.
drhd7107b32014-12-20 14:34:02 +000077#
drh0d339e42014-12-19 20:27:02 +000078do_test pragma3-140 {
79 db2 eval {
80 SELECT * FROM t1;
81 PRAGMA data_version;
drh3da9c042014-12-22 18:41:21 +000082 BEGIN IMMEDIATE;
83 PRAGMA data_version;
drh0d339e42014-12-19 20:27:02 +000084 UPDATE t1 SET a=a+1;
drh3da9c042014-12-22 18:41:21 +000085 COMMIT;
drh0d339e42014-12-19 20:27:02 +000086 SELECT * FROM t1;
87 PRAGMA data_version;
88 }
drh3da9c042014-12-22 18:41:21 +000089} {100 200 300 400 500 2 2 101 201 301 401 501 2}
drh0d339e42014-12-19 20:27:02 +000090do_execsql_test pragma3-150 {
91 SELECT * FROM t1;
92 PRAGMA data_version;
drh3da9c042014-12-22 18:41:21 +000093} {101 201 301 401 501 2}
drh0d339e42014-12-19 20:27:02 +000094
drhd7107b32014-12-20 14:34:02 +000095#
drh3da9c042014-12-22 18:41:21 +000096do_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}
106do_test pragma3-170 {
107 db2 eval {
108 PRAGMA data_version;
109 }
110} {2}
111do_test pragma3-180 {
112 db eval {
113 COMMIT;
114 PRAGMA data_version;
115 }
116} {2}
117do_test pragma3-190 {
118 db2 eval {
119 PRAGMA data_version;
120 }
121} {3}
122
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.
128#
129do_test pragma3-195 {
130 expr {[db eval {PRAGMA data_version}]!=[db2 eval {PRAGMA data_version}]}
131} {1}
132
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.
136#
137# The next block checks the behavior for separate processes.
drhd7107b32014-12-20 14:34:02 +0000138#
139do_test pragma3-200 {
drh3da9c042014-12-22 18:41:21 +0000140 db eval {PRAGMA data_version; SELECT * FROM t1;}
141} {2 101 201 301 401 555}
142do_test pragma3-201 {
drhd7107b32014-12-20 14:34:02 +0000143 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 }
drh3da9c042014-12-22 18:41:21 +0000157} {3 101 201}
drh0d339e42014-12-19 20:27:02 +0000158db2 close
drhd7107b32014-12-20 14:34:02 +0000159db close
160
drh3da9c042014-12-22 18:41:21 +0000161# 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.
drhd7107b32014-12-20 14:34:02 +0000164#
drh3da9c042014-12-22 18:41:21 +0000165# The next block checks that behavior is the same for shared-cache.
drhd7107b32014-12-20 14:34:02 +0000166#
167ifcapable 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;
drh3da9c042014-12-22 18:41:21 +0000174 BEGIN;
drhd7107b32014-12-20 14:34:02 +0000175 CREATE TABLE t3(a,b,c);
drh3da9c042014-12-22 18:41:21 +0000176 CREATE TABLE t4(x,y,z);
177 INSERT INTO t4 VALUES(123,456,789);
178 PRAGMA data_version;
179 COMMIT;
drhd7107b32014-12-20 14:34:02 +0000180 PRAGMA data_version;
181 }
drh3da9c042014-12-22 18:41:21 +0000182 } {1 1 1}
drhd7107b32014-12-20 14:34:02 +0000183 do_test pragma3-310 {
184 db2 eval {
185 PRAGMA data_version;
drh3da9c042014-12-22 18:41:21 +0000186 BEGIN;
drhd7107b32014-12-20 14:34:02 +0000187 INSERT INTO t3(a,b,c) VALUES('abc','def','ghi');
188 SELECT * FROM t3;
189 PRAGMA data_version;
190 }
drh3da9c042014-12-22 18:41:21 +0000191 } {2 abc def ghi 2}
192 # The transaction in db2 has not yet committed, so the data_version in
193 # db is unchanged.
drhd7107b32014-12-20 14:34:02 +0000194 do_test pragma3-320 {
195 db eval {
196 PRAGMA data_version;
drh3da9c042014-12-22 18:41:21 +0000197 SELECT * FROM t4;
drhd7107b32014-12-20 14:34:02 +0000198 }
drh3da9c042014-12-22 18:41:21 +0000199 } {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}
drhd7107b32014-12-20 14:34:02 +0000214 db2 close
drh59871fe2014-12-20 14:50:28 +0000215 db close
drhd7107b32014-12-20 14:34:02 +0000216 sqlite3_enable_shared_cache $::enable_shared_cache
217}
218
drh59871fe2014-12-20 14:50:28 +0000219# Make sure this also works in WAL mode
220#
dan55e115f2014-12-30 18:07:34 +0000221# This will not work with the in-memory journal permutation, as opening
222# [db2] switches the journal mode back to "memory"
223#
dan05accd22016-04-27 18:54:49 +0000224if {[wal_is_capable]} {
dan55e115f2014-12-30 18:07:34 +0000225if {[permutation]!="inmemory_journal"} {
226
drh59871fe2014-12-20 14:50:28 +0000227 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 }
drh3da9c042014-12-22 18:41:21 +0000236 } {2 wal 101 201}
drh59871fe2014-12-20 14:50:28 +0000237 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}
drh3da9c042014-12-22 18:41:21 +0000246 } {2 111 222}
drh59871fe2014-12-20 14:50:28 +0000247 do_test pragma3-430 {
248 db2 eval {PRAGMA data_version; SELECT * FROM t1;}
249 } {3 111 222}
250 db2 close
251}
dan55e115f2014-12-30 18:07:34 +0000252}
drh59871fe2014-12-20 14:50:28 +0000253
drh0d339e42014-12-19 20:27:02 +0000254finish_test