blob: 04e457d8504c429b3188d32b3cb59dcc3445195d [file] [log] [blame]
drh9019f4a2007-12-12 22:24:12 +00001# 2007 Dec 12
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#
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.
17#
danielk1977de3e41e2008-08-04 03:51:24 +000018# $Id: sidedelete.test,v 1.2 2008/08/04 03:51:24 danielk1977 Exp $
drh9019f4a2007-12-12 22:24:12 +000019#
20
21set testdir [file dirname $argv0]
22source $testdir/tester.tcl
23
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.
27#
28do_test sidedelete-1.1 {
29 execsql {
30 CREATE TABLE sequence(a INTEGER PRIMARY KEY);
31 INSERT INTO sequence VALUES(1);
32 INSERT INTO sequence VALUES(2);
33 }
34 for {set i 0} {$i<8} {incr i} {
35 execsql {
36 INSERT INTO sequence SELECT a+(SELECT max(a) FROM sequence) FROM sequence;
37 }
38 }
39 execsql {SELECT count(*) FROM sequence}
40} {512}
41
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.
45#
46do_test sidedelete-2.0 {
47 execsql {
48 CREATE TABLE t1(a PRIMARY KEY, b);
49 CREATE TABLE chng(a PRIMARY KEY, b);
danielk1977de3e41e2008-08-04 03:51:24 +000050 SELECT count(*) FROM t1;
51 SELECT count(*) FROM chng;
drh9019f4a2007-12-12 22:24:12 +000052 }
53} {0 0}
54for {set i 2} {$i<=100} {incr i} {
55 set n [expr {($i+2)/2}]
56 do_test sidedelete-2.$i.1 {
57 execsql {
58 DELETE FROM t1;
59 INSERT INTO t1 SELECT a, a FROM sequence WHERE a<=$i;
60 DELETE FROM chng;
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;
64 }
65 } [list $n [expr {$n*$n-1}]]
66 integrity_check sidedelete-2.$i.2
67}
68
69# This will cause stacks leaks but not database corruption prior
70# to the #2832 fix.
71#
72do_test sidedelete-3.0 {
73 execsql {
74 DROP TABLE t1;
75 CREATE TABLE t1(a PRIMARY KEY);
76 SELECT * FROM t1;
77 }
78} {}
79for {set i 1} {$i<=100} {incr i} {
80 set n [expr {($i+1)/2}]
81 do_test sidedelete-3.$i.1 {
82 execsql {
83 DELETE FROM t1;
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;
87 }
88 } [list $n [expr {$n*($n+1)}]]
89 integrity_check sidedelete-3.$i.2
90}
91
92finish_test