xref: /sqlite-3.40.0/test/pragma3.test (revision 7a1d7c39)
10d339e44Sdrh# 2014-12-19
20d339e44Sdrh#
30d339e44Sdrh# The author disclaims copyright to this source code.  In place of
40d339e44Sdrh# a legal notice, here is a blessing:
50d339e44Sdrh#
60d339e44Sdrh#    May you do good and not evil.
70d339e44Sdrh#    May you find forgiveness for yourself and forgive others.
80d339e44Sdrh#    May you share freely, never taking more than you give.
90d339e44Sdrh#
100d339e44Sdrh#***********************************************************************
110d339e44Sdrh# This file implements regression tests for SQLite library.
120d339e44Sdrh#
130d339e44Sdrh# This file implements tests for PRAGMA data_version command.
140d339e44Sdrh#
150d339e44Sdrh
160d339e44Sdrhset testdir [file dirname $argv0]
170d339e44Sdrhsource $testdir/tester.tcl
18bafad061Sdrh
19bafad061Sdrhif {[sqlite3 -has-codec]} {
20bafad061Sdrh  finish_test
21bafad061Sdrh  return
22bafad061Sdrh}
230d339e44Sdrh
240d339e44Sdrhdo_execsql_test pragma3-100 {
250d339e44Sdrh  PRAGMA data_version;
260d339e44Sdrh} {1}
270d339e44Sdrhdo_execsql_test pragma3-101 {
280d339e44Sdrh  PRAGMA temp.data_version;
290d339e44Sdrh} {1}
300d339e44Sdrh
31d7107b38Sdrh# Writing to the pragma is a no-op
320d339e44Sdrhdo_execsql_test pragma3-102 {
330d339e44Sdrh  PRAGMA main.data_version=1234;
340d339e44Sdrh  PRAGMA main.data_version;
350d339e44Sdrh} {1 1}
360d339e44Sdrh
37d7107b38Sdrh# EVIDENCE-OF: R-27726-60934 The "PRAGMA data_version" command provides
38d7107b38Sdrh# an indication that the database file has been modified.
39d7107b38Sdrh#
404a86d001Sdrh# EVIDENCE-OF: R-47505-58569 The "PRAGMA data_version" value is
414a86d001Sdrh# unchanged for commits made on the same database connection.
42d7107b38Sdrh#
430d339e44Sdrhdo_execsql_test pragma3-110 {
443da9c047Sdrh  PRAGMA data_version;
453da9c047Sdrh  BEGIN IMMEDIATE;
463da9c047Sdrh  PRAGMA data_version;
470d339e44Sdrh  CREATE TABLE t1(a);
480d339e44Sdrh  INSERT INTO t1 VALUES(100),(200),(300);
493da9c047Sdrh  PRAGMA data_version;
503da9c047Sdrh  COMMIT;
510d339e44Sdrh  SELECT * FROM t1;
520d339e44Sdrh  PRAGMA data_version;
533da9c047Sdrh} {1 1 1 100 200 300 1}
540d339e44Sdrh
550d339e44Sdrhsqlite3 db2 test.db
560d339e44Sdrhdo_test pragma3-120 {
570d339e44Sdrh  db2 eval {
580d339e44Sdrh    SELECT * FROM t1;
590d339e44Sdrh    PRAGMA data_version;
600d339e44Sdrh  }
610d339e44Sdrh} {100 200 300 1}
620d339e44Sdrh
630d339e44Sdrhdo_execsql_test pragma3-130 {
643da9c047Sdrh  PRAGMA data_version;
653da9c047Sdrh  BEGIN IMMEDIATE;
663da9c047Sdrh  PRAGMA data_version;
670d339e44Sdrh  INSERT INTO t1 VALUES(400),(500);
683da9c047Sdrh  PRAGMA data_version;
693da9c047Sdrh  COMMIT;
700d339e44Sdrh  SELECT * FROM t1;
710d339e44Sdrh  PRAGMA data_version;
72542d5586Sdrh  PRAGMA shrink_memory;
733da9c047Sdrh} {1 1 1 100 200 300 400 500 1}
740d339e44Sdrh
753da9c047Sdrh# EVIDENCE-OF: R-63005-41812 The integer values returned by two
763da9c047Sdrh# invocations of "PRAGMA data_version" from the same connection will be
773da9c047Sdrh# different if changes were committed to the database by any other
783da9c047Sdrh# connection in the interim.
79d7107b38Sdrh#
803da9c047Sdrh# Value went from 1 in pragma3-120 to 2 here.
81d7107b38Sdrh#
820d339e44Sdrhdo_test pragma3-140 {
830d339e44Sdrh  db2 eval {
840d339e44Sdrh    SELECT * FROM t1;
850d339e44Sdrh    PRAGMA data_version;
863da9c047Sdrh    BEGIN IMMEDIATE;
873da9c047Sdrh    PRAGMA data_version;
880d339e44Sdrh    UPDATE t1 SET a=a+1;
893da9c047Sdrh    COMMIT;
900d339e44Sdrh    SELECT * FROM t1;
910d339e44Sdrh    PRAGMA data_version;
920d339e44Sdrh  }
933da9c047Sdrh} {100 200 300 400 500 2 2 101 201 301 401 501 2}
940d339e44Sdrhdo_execsql_test pragma3-150 {
950d339e44Sdrh  SELECT * FROM t1;
960d339e44Sdrh  PRAGMA data_version;
973da9c047Sdrh} {101 201 301 401 501 2}
980d339e44Sdrh
99d7107b38Sdrh#
1003da9c047Sdrhdo_test pragma3-160 {
1013da9c047Sdrh  db eval {
1023da9c047Sdrh    BEGIN;
1033da9c047Sdrh    PRAGMA data_version;
1043da9c047Sdrh    UPDATE t1 SET a=555 WHERE a=501;
1053da9c047Sdrh    PRAGMA data_version;
1063da9c047Sdrh    SELECT * FROM t1 ORDER BY a;
1073da9c047Sdrh    PRAGMA data_version;
1083da9c047Sdrh  }
1093da9c047Sdrh} {2 2 101 201 301 401 555 2}
1103da9c047Sdrhdo_test pragma3-170 {
1113da9c047Sdrh  db2 eval {
1123da9c047Sdrh    PRAGMA data_version;
1133da9c047Sdrh  }
1143da9c047Sdrh} {2}
1153da9c047Sdrhdo_test pragma3-180 {
1163da9c047Sdrh  db eval {
1173da9c047Sdrh    COMMIT;
1183da9c047Sdrh    PRAGMA data_version;
1193da9c047Sdrh  }
1203da9c047Sdrh} {2}
1213da9c047Sdrhdo_test pragma3-190 {
1223da9c047Sdrh  db2 eval {
1233da9c047Sdrh    PRAGMA data_version;
1243da9c047Sdrh  }
1253da9c047Sdrh} {3}
1263da9c047Sdrh
1273da9c047Sdrh# EVIDENCE-OF: R-19326-44825 The "PRAGMA data_version" value is a local
1283da9c047Sdrh# property of each database connection and so values returned by two
1293da9c047Sdrh# concurrent invocations of "PRAGMA data_version" on separate database
1303da9c047Sdrh# connections are often different even though the underlying database is
1313da9c047Sdrh# identical.
1323da9c047Sdrh#
1333da9c047Sdrhdo_test pragma3-195 {
1343da9c047Sdrh  expr {[db eval {PRAGMA data_version}]!=[db2 eval {PRAGMA data_version}]}
1353da9c047Sdrh} {1}
1363da9c047Sdrh
1373da9c047Sdrh# EVIDENCE-OF: R-54562-06892 The behavior of "PRAGMA data_version" is
1383da9c047Sdrh# the same for all database connections, including database connections
1393da9c047Sdrh# in separate processes and shared cache database connections.
1403da9c047Sdrh#
1413da9c047Sdrh# The next block checks the behavior for separate processes.
142d7107b38Sdrh#
143d7107b38Sdrhdo_test pragma3-200 {
1443da9c047Sdrh  db eval {PRAGMA data_version; SELECT * FROM t1;}
1453da9c047Sdrh} {2 101 201 301 401 555}
1463da9c047Sdrhdo_test pragma3-201 {
147d7107b38Sdrh  set fd [open pragma3.txt wb]
148d7107b38Sdrh  puts $fd {
149d7107b38Sdrh     sqlite3 db test.db;
150d7107b38Sdrh     db eval {DELETE FROM t1 WHERE a>300};
151d7107b38Sdrh     db close;
152d7107b38Sdrh     exit;
153d7107b38Sdrh  }
154d7107b38Sdrh  close $fd
155d7107b38Sdrh  exec [info nameofexec] pragma3.txt
156d7107b38Sdrh  forcedelete pragma3.txt
157d7107b38Sdrh  db eval {
158d7107b38Sdrh    PRAGMA data_version;
159d7107b38Sdrh    SELECT * FROM t1;
160d7107b38Sdrh  }
1613da9c047Sdrh} {3 101 201}
1620d339e44Sdrhdb2 close
163d7107b38Sdrhdb close
164d7107b38Sdrh
1653da9c047Sdrh# EVIDENCE-OF: R-54562-06892 The behavior of "PRAGMA data_version" is
1663da9c047Sdrh# the same for all database connections, including database connections
1673da9c047Sdrh# in separate processes and shared cache database connections.
168d7107b38Sdrh#
1693da9c047Sdrh# The next block checks that behavior is the same for shared-cache.
170d7107b38Sdrh#
171d7107b38Sdrhifcapable shared_cache {
172d7107b38Sdrh  set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
173d7107b38Sdrh  sqlite3 db test.db
174d7107b38Sdrh  sqlite3 db2 test.db
175d7107b38Sdrh  do_test pragma3-300 {
176d7107b38Sdrh    db eval {
177d7107b38Sdrh      PRAGMA data_version;
1783da9c047Sdrh      BEGIN;
179d7107b38Sdrh      CREATE TABLE t3(a,b,c);
1803da9c047Sdrh      CREATE TABLE t4(x,y,z);
1813da9c047Sdrh      INSERT INTO t4 VALUES(123,456,789);
1823da9c047Sdrh      PRAGMA data_version;
1833da9c047Sdrh      COMMIT;
184d7107b38Sdrh      PRAGMA data_version;
185d7107b38Sdrh    }
1863da9c047Sdrh  } {1 1 1}
187d7107b38Sdrh  do_test pragma3-310 {
188d7107b38Sdrh    db2 eval {
189d7107b38Sdrh      PRAGMA data_version;
1903da9c047Sdrh      BEGIN;
191d7107b38Sdrh      INSERT INTO t3(a,b,c) VALUES('abc','def','ghi');
192d7107b38Sdrh      SELECT * FROM t3;
193d7107b38Sdrh      PRAGMA data_version;
194d7107b38Sdrh    }
1953da9c047Sdrh  } {2 abc def ghi 2}
1963da9c047Sdrh  # The transaction in db2 has not yet committed, so the data_version in
1973da9c047Sdrh  # db is unchanged.
198d7107b38Sdrh  do_test pragma3-320 {
199d7107b38Sdrh    db eval {
200d7107b38Sdrh      PRAGMA data_version;
2013da9c047Sdrh      SELECT * FROM t4;
202d7107b38Sdrh    }
2033da9c047Sdrh  } {1 123 456 789}
2043da9c047Sdrh  do_test pragma3-330 {
2053da9c047Sdrh    db2 eval {
2063da9c047Sdrh      COMMIT;
2073da9c047Sdrh      PRAGMA data_version;
2083da9c047Sdrh      SELECT * FROM t4;
2093da9c047Sdrh    }
2103da9c047Sdrh  } {2 123 456 789}
2113da9c047Sdrh  do_test pragma3-340 {
2123da9c047Sdrh    db eval {
2133da9c047Sdrh      PRAGMA data_version;
2143da9c047Sdrh      SELECT * FROM t3;
2153da9c047Sdrh      SELECT * FROM t4;
2163da9c047Sdrh    }
2173da9c047Sdrh  } {2 abc def ghi 123 456 789}
218d7107b38Sdrh  db2 close
21959871fe7Sdrh  db close
220d7107b38Sdrh  sqlite3_enable_shared_cache $::enable_shared_cache
221d7107b38Sdrh}
222d7107b38Sdrh
22359871fe7Sdrh# Make sure this also works in WAL mode
22459871fe7Sdrh#
22555e115f0Sdan# This will not work with the in-memory journal permutation, as opening
22655e115f0Sdan# [db2] switches the journal mode back to "memory"
22755e115f0Sdan#
22805accd22Sdanif {[wal_is_capable]} {
22955e115f0Sdanif {[permutation]!="inmemory_journal"} {
23055e115f0Sdan
23159871fe7Sdrh  sqlite3 db test.db
23259871fe7Sdrh  db eval {PRAGMA journal_mode=WAL}
23359871fe7Sdrh  sqlite3 db2 test.db
23459871fe7Sdrh  do_test pragma3-400 {
23559871fe7Sdrh    db eval {
23659871fe7Sdrh      PRAGMA data_version;
23759871fe7Sdrh      PRAGMA journal_mode;
23859871fe7Sdrh      SELECT * FROM t1;
23959871fe7Sdrh    }
2403da9c047Sdrh  } {2 wal 101 201}
24159871fe7Sdrh  do_test pragma3-410 {
24259871fe7Sdrh    db2 eval {
24359871fe7Sdrh      PRAGMA data_version;
24459871fe7Sdrh      PRAGMA journal_mode;
24559871fe7Sdrh      SELECT * FROM t1;
24659871fe7Sdrh    }
24759871fe7Sdrh  } {2 wal 101 201}
24859871fe7Sdrh  do_test pragma3-420 {
24959871fe7Sdrh    db eval {UPDATE t1 SET a=111*(a/100); PRAGMA data_version; SELECT * FROM t1}
2503da9c047Sdrh  } {2 111 222}
25159871fe7Sdrh  do_test pragma3-430 {
25259871fe7Sdrh    db2 eval {PRAGMA data_version; SELECT * FROM t1;}
25359871fe7Sdrh  } {3 111 222}
25459871fe7Sdrh  db2 close
25559871fe7Sdrh}
25655e115f0Sdan}
25759871fe7Sdrh
258*7a1d7c39Sdan#-------------------------------------------------------------------------
259*7a1d7c39Sdan# Check that empty write transactions do not cause the return of "PRAGMA
260*7a1d7c39Sdan# data_version" to be decremented with journal_mode=PERSIST and
261*7a1d7c39Sdan# locking_mode=EXCLUSIVE
262*7a1d7c39Sdan#
263*7a1d7c39Sdanforeach {tn sql} {
264*7a1d7c39Sdan  A {
265*7a1d7c39Sdan  }
266*7a1d7c39Sdan  B {
267*7a1d7c39Sdan    PRAGMA journal_mode = PERSIST;
268*7a1d7c39Sdan    PRAGMA locking_mode = EXCLUSIVE;
269*7a1d7c39Sdan  }
270*7a1d7c39Sdan} {
271*7a1d7c39Sdan  reset_db
272*7a1d7c39Sdan  execsql $sql
273*7a1d7c39Sdan
274*7a1d7c39Sdan  do_execsql_test pragma3-510$tn {
275*7a1d7c39Sdan    CREATE TABLE t1(x, y);
276*7a1d7c39Sdan    INSERT INTO t1 VALUES(1, 2);
277*7a1d7c39Sdan    PRAGMA data_version;
278*7a1d7c39Sdan  } {1}
279*7a1d7c39Sdan
280*7a1d7c39Sdan  do_execsql_test pragma3-520$tn {
281*7a1d7c39Sdan    BEGIN EXCLUSIVE;
282*7a1d7c39Sdan    COMMIT;
283*7a1d7c39Sdan    PRAGMA data_version;
284*7a1d7c39Sdan  } {1}
285*7a1d7c39Sdan}
286*7a1d7c39Sdan
2870d339e44Sdrhfinish_test
288