sidedelete.test 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # 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. #
  18. # $Id: sidedelete.test,v 1.2 2008/08/04 03:51:24 danielk1977 Exp $
  19. #
  20. set testdir [file dirname $argv0]
  21. source $testdir/tester.tcl
  22. # The sequence table is created to store a sequence of integers
  23. # starting with 1. This is used to reinitialize other tables
  24. # as part of other tests.
  25. #
  26. do_test sidedelete-1.1 {
  27. execsql {
  28. CREATE TABLE sequence(a INTEGER PRIMARY KEY);
  29. INSERT INTO sequence VALUES(1);
  30. INSERT INTO sequence VALUES(2);
  31. }
  32. for {set i 0} {$i<8} {incr i} {
  33. execsql {
  34. INSERT INTO sequence SELECT a+(SELECT max(a) FROM sequence) FROM sequence;
  35. }
  36. }
  37. execsql {SELECT count(*) FROM sequence}
  38. } {512}
  39. # Make a series of changes using an UPDATE OR REPLACE and a
  40. # correlated subquery. This would cause database corruption
  41. # prior to the fix for ticket #2832.
  42. #
  43. do_test sidedelete-2.0 {
  44. execsql {
  45. CREATE TABLE t1(a PRIMARY KEY, b);
  46. CREATE TABLE chng(a PRIMARY KEY, b);
  47. SELECT count(*) FROM t1;
  48. SELECT count(*) FROM chng;
  49. }
  50. } {0 0}
  51. for {set i 2} {$i<=100} {incr i} {
  52. set n [expr {($i+2)/2}]
  53. do_test sidedelete-2.$i.1 {
  54. execsql {
  55. DELETE FROM t1;
  56. INSERT INTO t1 SELECT a, a FROM sequence WHERE a<=$i;
  57. DELETE FROM chng;
  58. INSERT INTO chng SELECT a*2, a*2+1 FROM sequence WHERE a<=$i/2;
  59. UPDATE OR REPLACE t1 SET a=(SELECT b FROM chng WHERE a=t1.a);
  60. SELECT count(*), sum(a) FROM t1;
  61. }
  62. } [list $n [expr {$n*$n-1}]]
  63. integrity_check sidedelete-2.$i.2
  64. }
  65. # This will cause stacks leaks but not database corruption prior
  66. # to the #2832 fix.
  67. #
  68. do_test sidedelete-3.0 {
  69. execsql {
  70. DROP TABLE t1;
  71. CREATE TABLE t1(a PRIMARY KEY);
  72. SELECT * FROM t1;
  73. }
  74. } {}
  75. for {set i 1} {$i<=100} {incr i} {
  76. set n [expr {($i+1)/2}]
  77. do_test sidedelete-3.$i.1 {
  78. execsql {
  79. DELETE FROM t1;
  80. INSERT INTO t1 SELECT a FROM sequence WHERE a<=$i;
  81. UPDATE OR REPLACE t1 SET a=a+1;
  82. SELECT count(*), sum(a) FROM t1;
  83. }
  84. } [list $n [expr {$n*($n+1)}]]
  85. integrity_check sidedelete-3.$i.2
  86. }
  87. finish_test