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 #***********************************************************************
12 # This file contains test cases for stressing database
13 # changes that involve side effects that delete rows from
14 # the table being changed. Ticket #2832 shows that in
15 # older versions of SQLite that behavior was implemented
16 # incorrectly and resulted in corrupt database files.
18 # $Id: sidedelete.test,v 1.2 2008/08/04 03:51:24 danielk1977 Exp $
21 set testdir [file dirname $argv0]
22 source $testdir/tester.tcl
24 # The sequence table is created to store a sequence of integers
25 # starting with 1. This is used to reinitialize other tables
26 # as part of other tests.
28 do_test sidedelete-1.1 {
30 CREATE TABLE sequence(a INTEGER PRIMARY KEY);
31 INSERT INTO sequence VALUES(1);
32 INSERT INTO sequence VALUES(2);
34 for {set i 0} {$i<8} {incr i} {
36 INSERT INTO sequence SELECT a+(SELECT max(a) FROM sequence) FROM sequence;
39 execsql {SELECT count(*) FROM sequence}
42 # Make a series of changes using an UPDATE OR REPLACE and a
43 # correlated subquery. This would cause database corruption
44 # prior to the fix for ticket #2832.
46 do_test sidedelete-2.0 {
48 CREATE TABLE t1(a PRIMARY KEY, b);
49 CREATE TABLE chng(a PRIMARY KEY, b);
50 SELECT count(*) FROM t1;
51 SELECT count(*) FROM chng;
54 for {set i 2} {$i<=100} {incr i} {
55 set n [expr {($i+2)/2}]
56 do_test sidedelete-2.$i.1 {
59 INSERT INTO t1 SELECT a, a FROM sequence WHERE a<=$i;
61 INSERT INTO chng SELECT a*2, a*2+1 FROM sequence WHERE a<=$i/2;
62 UPDATE OR REPLACE t1 SET a=(SELECT b FROM chng WHERE a=t1.a);
63 SELECT count(*), sum(a) FROM t1;
65 } [list $n [expr {$n*$n-1}]]
66 integrity_check sidedelete-2.$i.2
69 # This will cause stacks leaks but not database corruption prior
72 do_test sidedelete-3.0 {
75 CREATE TABLE t1(a PRIMARY KEY);
79 for {set i 1} {$i<=100} {incr i} {
80 set n [expr {($i+1)/2}]
81 do_test sidedelete-3.$i.1 {
84 INSERT INTO t1 SELECT a FROM sequence WHERE a<=$i;
85 UPDATE OR REPLACE t1 SET a=a+1;
86 SELECT count(*), sum(a) FROM t1;
88 } [list $n [expr {$n*($n+1)}]]
89 integrity_check sidedelete-3.$i.2