Update the .help screen in the CLI. Make sure the temporary files for
[sqlite.git] / test / rollback.test
blob60a619031709a3e33531d46bbd90d138fc01d8e4
1 # 2004 June 30
3 # The author disclaims copyright to this source code.  In place of
4 # a legal notice, here is a blessing:
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.
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.
16 # $Id: rollback.test,v 1.11 2009/06/26 07:12:07 danielk1977 Exp $
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
21 set DB [sqlite3_connection_pointer db]
23 do_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}
34 ifcapable 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   } {}
44 do_test rollback-1.3 {
45   set STMT [sqlite3_prepare $DB "SELECT a FROM t1" -1 TAIL]
46   sqlite3_step $STMT
47 } {SQLITE_ROW}
49 ifcapable 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 {UNIQUE constraint failed: t3.a}}
58   
59   # Try to continue with the SELECT statement
60   #
61   do_test rollback-1.5 {
62     sqlite3_step $STMT
63   } {SQLITE_ROW}
65   # Restart the SELECT statement
66   #
67   do_test rollback-1.6 { sqlite3_reset $STMT } {SQLITE_OK}
68 } else {
69   do_test rollback-1.6 { sqlite3_reset $STMT } {SQLITE_OK}
72 do_test rollback-1.7 {
73   sqlite3_step $STMT
74 } {SQLITE_ROW}
75 do_test rollback-1.8 {
76   sqlite3_step $STMT
77 } {SQLITE_ROW}
78 do_test rollback-1.9 {
79   sqlite3_finalize $STMT
80 } {SQLITE_OK}
82 if {$tcl_platform(platform) == "unix" 
83  && [permutation] ne "onefile"
84  && [permutation] ne "inmemory_journal"
85  && [permutation] ne "atomic-batch-write"
86 } {
87   do_test rollback-2.1 {
88     execsql {
89       BEGIN;
90       INSERT INTO t3 VALUES('hello world');
91     }
92     forcecopy test.db testA.db
93     forcecopy test.db-journal testA.db-journal
94     execsql {
95       COMMIT;
96     }
97   } {}
99   # At this point files testA.db and testA.db-journal are present in the
100   # file system. This block adds a master-journal file pointer to the
101   # end of testA.db-journal. The master-journal file does not exist.
102   # 
103   set mj [file normalize testA.db-mj-123]
104   binary scan $mj c* a
105   set cksum 0
106   foreach i $a { incr cksum $i }
107   set mj_pgno [expr $sqlite_pending_byte / 1024]
108   set zAppend [binary format Ia*IIa8 $mj_pgno $mj [string length $mj] $cksum \
109     "\xd9\xd5\x05\xf9\x20\xa1\x63\xd7"
110   ]
111   set iOffset [expr (([file size testA.db-journal] + 511)/512)*512]
112   set fd [open testA.db-journal a+]
113   fconfigure $fd -encoding binary -translation binary
114   seek $fd $iOffset
115   puts -nonewline $fd $zAppend
117   # Also, fix the first journal-header in the journal-file. Because the
118   # journal file has not yet been synced, the 8-byte magic string at the
119   # start of the first journal-header has not been written by SQLite.
120   # So write it now.
121   seek $fd 0
122   puts -nonewline $fd "\xd9\xd5\x05\xf9\x20\xa1\x63\xd7"
123   close $fd
125   # Open a handle on testA.db and use it to query the database. At one
126   # point the first query would attempt a hot rollback, attempt to open
127   # the master-journal file and return SQLITE_CANTOPEN when it could not
128   # be opened. This is incorrect, it should simply delete the journal
129   # file and proceed with the query.
130   # 
131   do_test rollback-2.2 {
132     sqlite3 db2 testA.db
133     execsql {
134       SELECT distinct tbl_name FROM sqlite_master;
135     } db2
136   } {t1 t3}
137   if {[lsearch {exclusive persistent_journal no_journal} [permutation]]<0} {
138     do_test rollback-2.3 {
139       file exists testA.db-journal
140     } 0
141   }
142   do_test rollback-2.4 {
143     execsql {
144       SELECT distinct tbl_name FROM sqlite_master;
145     } db2
146   } {t1 t3}
148   db2 close
151 finish_test