xref: /sqlite-3.40.0/test/rollback.test (revision 3d9cf517)
1# 2004 June 30
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.  The
12# focus of this file is verifying that a rollback in one statement
13# caused by an ON CONFLICT ROLLBACK clause aborts any other pending
14# statements.
15#
16# $Id: rollback.test,v 1.8 2008/10/07 15:00:09 danielk1977 Exp $
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
21set DB [sqlite3_connection_pointer db]
22
23do_test rollback-1.1 {
24  execsql {
25    CREATE TABLE t1(a);
26    INSERT INTO t1 VALUES(1);
27    INSERT INTO t1 VALUES(2);
28    INSERT INTO t1 VALUES(3);
29    INSERT INTO t1 VALUES(4);
30    SELECT * FROM t1;
31  }
32} {1 2 3 4}
33
34ifcapable conflict {
35  do_test rollback-1.2 {
36    execsql {
37      CREATE TABLE t3(a unique on conflict rollback);
38      INSERT INTO t3 SELECT a FROM t1;
39      BEGIN;
40      INSERT INTO t1 SELECT * FROM t1;
41    }
42  } {}
43}
44do_test rollback-1.3 {
45  set STMT [sqlite3_prepare $DB "SELECT a FROM t1" -1 TAIL]
46  sqlite3_step $STMT
47} {SQLITE_ROW}
48
49ifcapable conflict {
50  # This causes a ROLLBACK, which deletes the table out from underneath the
51  # SELECT statement.
52  #
53  do_test rollback-1.4 {
54    catchsql {
55      INSERT INTO t3 SELECT a FROM t1;
56    }
57  } {1 {column a is not unique}}
58
59  # Try to continue with the SELECT statement
60  #
61  do_test rollback-1.5 {
62    sqlite3_step $STMT
63  } {SQLITE_ERROR}
64
65  # Restart the SELECT statement
66  #
67  do_test rollback-1.6 { sqlite3_reset $STMT } {SQLITE_ABORT}
68} else {
69  do_test rollback-1.6 { sqlite3_reset $STMT } {SQLITE_OK}
70}
71
72do_test rollback-1.7 {
73  sqlite3_step $STMT
74} {SQLITE_ROW}
75do_test rollback-1.8 {
76  sqlite3_step $STMT
77} {SQLITE_ROW}
78do_test rollback-1.9 {
79  sqlite3_finalize $STMT
80} {SQLITE_OK}
81
82set permutation ""
83catch {set permutation $::permutations_test_prefix}
84if {$tcl_platform(platform) == "unix" && $permutation ne "onefile"} {
85  do_test rollback-2.1 {
86    execsql {
87      BEGIN;
88      INSERT INTO t3 VALUES('hello world');
89    }
90    file copy -force test.db testA.db
91    file copy -force test.db-journal testA.db-journal
92    execsql {
93      COMMIT;
94    }
95  } {}
96
97  # At this point files testA.db and testA.db-journal are present in the
98  # file system. This block adds a master-journal file pointer to the
99  # end of testA.db-journal. The master-journal file does not exist.
100  #
101  set mj [file normalize testA.db-mj-123]
102  binary scan $mj c* a
103  set cksum 0
104  foreach i $a { incr cksum $i }
105  set mj_pgno [expr $sqlite_pending_byte / 1024]
106  set zAppend [binary format Ia*IIa8 $mj_pgno $mj [string length $mj] $cksum \
107    "\xd9\xd5\x05\xf9\x20\xa1\x63\xd7"
108  ]
109  set iOffset [expr (([file size testA.db-journal] + 511)/512)*512]
110  set fd [open testA.db-journal a+]
111  fconfigure $fd -encoding binary -translation binary
112  seek $fd $iOffset
113  puts -nonewline $fd $zAppend
114  close $fd
115
116  # Open a handle on testA.db and use it to query the database. At one
117  # point the first query would attempt a hot rollback, attempt to open
118  # the master-journal file and return SQLITE_CANTOPEN when it could not
119  # be opened. This is incorrect, it should simply delete the journal
120  # file and proceed with the query.
121  #
122  do_test rollback-2.2 {
123    sqlite3 db2 testA.db
124    execsql {
125      SELECT distinct tbl_name FROM sqlite_master;
126    } db2
127  } {t1 t3}
128  do_test rollback-2.3 {
129    file exists testA.db-journal
130  } 0
131  do_test rollback-2.4 {
132    execsql {
133      SELECT distinct tbl_name FROM sqlite_master;
134    } db2
135  } {t1 t3}
136
137  db2 close
138}
139
140finish_test
141