CoolFace
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
sidedelete.test93 linesDownload Raw Back to test
1# 2007 Dec 122#3# The author disclaims copyright to this source code. In place of4# 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 database13# changes that involve side effects that delete rows from14# the table being changed.  Ticket #2832 shows that in15# older versions of SQLite that behavior was implemented16# incorrectly and resulted in corrupt database files.17#18# $Id: sidedelete.test,v 1.2 2008/08/04 03:51:24 danielk1977 Exp $19#20 21set testdir [file dirname $argv0]22source $testdir/tester.tcl23 24# The sequence table is created to store a sequence of integers25# starting with 1.  This is used to reinitialize other tables26# 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 a43# correlated subquery.  This would cause database corruption44# 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);50    SELECT count(*) FROM t1;51    SELECT count(*) FROM chng;52  }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.267}68 69# This will cause stacks leaks but not database corruption prior70# 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.290}91 92finish_test93