savepoint2.test 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # 2008 December 15
  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. # $Id: savepoint2.test,v 1.5 2009/06/05 17:09:12 drh Exp $
  13. set testdir [file dirname $argv0]
  14. source $testdir/tester.tcl
  15. # Tests in this file are quite similar to those run by trans.test and
  16. # avtrans.test.
  17. #
  18. proc signature {} {
  19. return [db eval {SELECT count(*), md5sum(x) FROM t3}]
  20. }
  21. do_test savepoint2-1 {
  22. wal_set_journal_mode
  23. execsql {
  24. PRAGMA cache_size=10;
  25. BEGIN;
  26. CREATE TABLE t3(x TEXT);
  27. INSERT INTO t3 VALUES(randstr(10,400));
  28. INSERT INTO t3 VALUES(randstr(10,400));
  29. INSERT INTO t3 SELECT randstr(10,400) FROM t3;
  30. INSERT INTO t3 SELECT randstr(10,400) FROM t3;
  31. INSERT INTO t3 SELECT randstr(10,400) FROM t3;
  32. INSERT INTO t3 SELECT randstr(10,400) FROM t3;
  33. INSERT INTO t3 SELECT randstr(10,400) FROM t3;
  34. INSERT INTO t3 SELECT randstr(10,400) FROM t3;
  35. INSERT INTO t3 SELECT randstr(10,400) FROM t3;
  36. INSERT INTO t3 SELECT randstr(10,400) FROM t3;
  37. INSERT INTO t3 SELECT randstr(10,400) FROM t3;
  38. COMMIT;
  39. SELECT count(*) FROM t3;
  40. }
  41. } {1024}
  42. wal_check_journal_mode savepoint2-1.1
  43. unset -nocomplain ::sig
  44. unset -nocomplain SQL
  45. set iterations 20
  46. set SQL(1) {
  47. DELETE FROM t3 WHERE random()%10!=0;
  48. INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
  49. INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
  50. }
  51. set SQL(2) {
  52. DELETE FROM t3 WHERE random()%10!=0;
  53. INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
  54. DELETE FROM t3 WHERE random()%10!=0;
  55. INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
  56. }
  57. set SQL(3) {
  58. UPDATE t3 SET x = randstr(10, 400) WHERE random()%10;
  59. INSERT INTO t3 SELECT x FROM t3 WHERE random()%10;
  60. DELETE FROM t3 WHERE random()%10;
  61. }
  62. set SQL(4) {
  63. INSERT INTO t3 SELECT randstr(10,400) FROM t3 WHERE (random()%10 == 0);
  64. }
  65. for {set ii 2} {$ii < ($iterations+2)} {incr ii} {
  66. # Record the database signature. Optionally (every second run) open a
  67. # transaction. In all cases open savepoint "one", which may or may
  68. # not be a transaction savepoint, depending on whether or not a real
  69. # transaction has been opened.
  70. #
  71. do_test savepoint2-$ii.1 {
  72. if {$ii % 2} { execsql BEGIN }
  73. set ::sig(one) [signature]
  74. execsql "SAVEPOINT one"
  75. } {}
  76. # Execute some SQL on the database. Then rollback to savepoint "one".
  77. # Check that the database signature is as it was when "one" was opened.
  78. #
  79. do_test savepoint2-$ii.2 {
  80. execsql $SQL(1)
  81. execsql "ROLLBACK to one"
  82. signature
  83. } $::sig(one)
  84. integrity_check savepoint2-$ii.2.1
  85. # Execute some SQL. Then open savepoint "two". Savepoint "two" is therefore
  86. # nested in savepoint "one".
  87. #
  88. do_test savepoint2-$ii.3 {
  89. execsql $SQL(1)
  90. set ::sig(two) [signature]
  91. execsql "SAVEPOINT two"
  92. } {}
  93. # More SQL changes. The rollback to savepoint "two". Check that the
  94. # signature is as it was when savepoint "two" was opened.
  95. #
  96. do_test savepoint2-$ii.4 {
  97. execsql $SQL(2)
  98. execsql "ROLLBACK to two"
  99. signature
  100. } $::sig(two)
  101. integrity_check savepoint2-$ii.4.1
  102. # More SQL changes. The rollback to savepoint "two". Check that the
  103. # signature is as it was when savepoint "two" was opened.
  104. #
  105. do_test savepoint2-$ii.5 {
  106. execsql $SQL(2)
  107. execsql "SAVEPOINT three"
  108. execsql $SQL(3)
  109. execsql "RELEASE three"
  110. execsql "ROLLBACK to one"
  111. signature
  112. } $::sig(one)
  113. # By this point the database is in the same state as it was at the
  114. # top of the for{} loop (everything having been rolled back by the
  115. # "ROLLBACK TO one" command above). So make a few changes to the
  116. # database and COMMIT the open transaction, so that the next iteration
  117. # of the for{} loop works on a different dataset.
  118. #
  119. # The transaction being committed here may have been opened normally using
  120. # "BEGIN", or may have been opened using a transaction savepoint created
  121. # by the "SAVEPOINT one" statement.
  122. #
  123. do_test savepoint2-$ii.6 {
  124. execsql $SQL(4)
  125. execsql COMMIT
  126. sqlite3_get_autocommit db
  127. } {1}
  128. integrity_check savepoint2-$ii.6.1
  129. # Check that the connection is still running in WAL mode.
  130. wal_check_journal_mode savepoint2-$ii.7
  131. }
  132. unset -nocomplain ::sig
  133. unset -nocomplain SQL
  134. finish_test