1# 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 26# Writing to the pragma is a no-op 27do_execsql_test pragma3-102 { 28 PRAGMA main.data_version=1234; 29 PRAGMA main.data_version; 30} {1 1} 31 32# EVIDENCE-OF: R-27726-60934 The "PRAGMA data_version" command provides 33# an indication that the database file has been modified. 34# 35# EVIDENCE-OF: R-47505-58569 The "PRAGMA data_version" value is 36# unchanged for commits made on the same database connection. 37# 38do_execsql_test pragma3-110 { 39 PRAGMA data_version; 40 BEGIN IMMEDIATE; 41 PRAGMA data_version; 42 CREATE TABLE t1(a); 43 INSERT INTO t1 VALUES(100),(200),(300); 44 PRAGMA data_version; 45 COMMIT; 46 SELECT * FROM t1; 47 PRAGMA data_version; 48} {1 1 1 100 200 300 1} 49 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 { 59 PRAGMA data_version; 60 BEGIN IMMEDIATE; 61 PRAGMA data_version; 62 INSERT INTO t1 VALUES(400),(500); 63 PRAGMA data_version; 64 COMMIT; 65 SELECT * FROM t1; 66 PRAGMA data_version; 67 PRAGMA shrink_memory; 68} {1 1 1 100 200 300 400 500 1} 69 70# 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. 74# 75# Value went from 1 in pragma3-120 to 2 here. 76# 77do_test pragma3-140 { 78 db2 eval { 79 SELECT * FROM t1; 80 PRAGMA data_version; 81 BEGIN IMMEDIATE; 82 PRAGMA data_version; 83 UPDATE t1 SET a=a+1; 84 COMMIT; 85 SELECT * FROM t1; 86 PRAGMA data_version; 87 } 88} {100 200 300 400 500 2 2 101 201 301 401 501 2} 89do_execsql_test pragma3-150 { 90 SELECT * FROM t1; 91 PRAGMA data_version; 92} {101 201 301 401 501 2} 93 94# 95do_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. 137# 138do_test pragma3-200 { 139 db eval {PRAGMA data_version; SELECT * FROM t1;} 140} {2 101 201 301 401 555} 141do_test pragma3-201 { 142 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 } 156} {3 101 201} 157db2 close 158db close 159 160# 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. 163# 164# The next block checks that behavior is the same for shared-cache. 165# 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; 173 BEGIN; 174 CREATE TABLE t3(a,b,c); 175 CREATE TABLE t4(x,y,z); 176 INSERT INTO t4 VALUES(123,456,789); 177 PRAGMA data_version; 178 COMMIT; 179 PRAGMA data_version; 180 } 181 } {1 1 1} 182 do_test pragma3-310 { 183 db2 eval { 184 PRAGMA data_version; 185 BEGIN; 186 INSERT INTO t3(a,b,c) VALUES('abc','def','ghi'); 187 SELECT * FROM t3; 188 PRAGMA data_version; 189 } 190 } {2 abc def ghi 2} 191 # The transaction in db2 has not yet committed, so the data_version in 192 # db is unchanged. 193 do_test pragma3-320 { 194 db eval { 195 PRAGMA data_version; 196 SELECT * FROM t4; 197 } 198 } {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} 213 db2 close 214 db close 215 sqlite3_enable_shared_cache $::enable_shared_cache 216} 217 218# Make sure this also works in WAL mode 219# 220# This will not work with the in-memory journal permutation, as opening 221# [db2] switches the journal mode back to "memory" 222# 223ifcapable wal { 224if {[permutation]!="inmemory_journal"} { 225 226 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 } 235 } {2 wal 101 201} 236 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} 245 } {2 111 222} 246 do_test pragma3-430 { 247 db2 eval {PRAGMA data_version; SELECT * FROM t1;} 248 } {3 111 222} 249 db2 close 250} 251} 252 253finish_test 254