jrnlmode3.test 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # 2009 April 20
  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. # Test cases inspired by ticket #3811. Tests to make sure that
  13. # the journal_mode can only be changed at appropriate times and that
  14. # all reported changes are effective.
  15. #
  16. # $Id: jrnlmode3.test,v 1.5 2009/04/20 17:43:03 drh Exp $
  17. set testdir [file dirname $argv0]
  18. source $testdir/tester.tcl
  19. ifcapable {!pager_pragmas} {
  20. finish_test
  21. return
  22. }
  23. #
  24. # Verify that journal_mode=OFF works as long as it occurs before the first
  25. # transaction, even if locking_mode=EXCLUSIVE is enabled. The behavior if
  26. # journal_mode is changed after the first transaction is undefined and hence
  27. # untested.
  28. #
  29. do_test jrnlmode3-1.1 {
  30. db eval {
  31. PRAGMA journal_mode=OFF;
  32. PRAGMA locking_mode=EXCLUSIVE;
  33. CREATE TABLE t1(x);
  34. INSERT INTO t1 VALUES(1);
  35. SELECT * FROM t1;
  36. }
  37. } {off exclusive 1}
  38. do_test jrnlmode3-1.2 {
  39. db eval {
  40. BEGIN;
  41. INSERT INTO t1 VALUES(2);
  42. ROLLBACK;
  43. SELECT * FROM t1;
  44. }
  45. } {1}
  46. db close
  47. forcedelete test.db test.db-journal
  48. sqlite3 db test.db
  49. do_test jrnlmode3-2.1 {
  50. db eval {
  51. PRAGMA locking_mode=EXCLUSIVE;
  52. PRAGMA journal_mode=OFF;
  53. CREATE TABLE t1(x);
  54. INSERT INTO t1 VALUES(1);
  55. SELECT * FROM t1;
  56. }
  57. } {exclusive off 1}
  58. do_test jrnlmode3-2.2 {
  59. db eval {
  60. BEGIN;
  61. INSERT INTO t1 VALUES(2);
  62. ROLLBACK;
  63. SELECT * FROM t1;
  64. }
  65. } {1}
  66. # Test cases to verify that we can move from any journal_mode
  67. # to any other, as long as we are not in a transaction. Verify
  68. # that we cannot change journal_mode while a transaction is active.
  69. #
  70. set all_journal_modes {delete persist truncate memory off}
  71. set cnt 0
  72. foreach fromjmode $all_journal_modes {
  73. foreach tojmode $all_journal_modes {
  74. # Skip the no-change cases
  75. if {$fromjmode==$tojmode} continue
  76. incr cnt
  77. # Start with a fresh database connection an empty database file.
  78. #
  79. db close
  80. forcedelete test.db test.db-journal
  81. sqlite3 db test.db
  82. # Initialize the journal mode.
  83. #
  84. do_test jrnlmode3-3.$cnt.1-($fromjmode-to-$tojmode) {
  85. db eval "PRAGMA journal_mode = $fromjmode;"
  86. } $fromjmode
  87. # Verify that the initial journal mode takes.
  88. #
  89. do_test jrnlmode3-3.$cnt.2 {
  90. db eval {PRAGMA main.journal_mode}
  91. } $fromjmode
  92. # Start a transaction and try to change the journal mode within
  93. # the transaction. This should fail.
  94. #
  95. do_test jrnlmode3-3.$cnt.3 {
  96. db eval {
  97. CREATE TABLE t1(x);
  98. BEGIN;
  99. INSERT INTO t1 VALUES($cnt);
  100. }
  101. db eval "PRAGMA journal_mode=$tojmode"
  102. } $fromjmode
  103. # Rollback the transaction.
  104. #
  105. do_test jrnlmode3-3.$cnt.4 {
  106. db eval {
  107. ROLLBACK;
  108. SELECT * FROM t1;
  109. }
  110. } {}
  111. # Now change the journal mode again. This time the new mode
  112. # should take.
  113. #
  114. do_test jrnlmode3-3.$cnt.5 {
  115. db eval "PRAGMA journal_mode=$tojmode"
  116. } $tojmode
  117. # Do a the transaction. Verify that the rollback occurred
  118. # if journal_mode!=OFF.
  119. #
  120. do_test jrnlmode3-3.$cnt.6 {
  121. db eval {
  122. DROP TABLE IF EXISTS t1;
  123. CREATE TABLE t1(x);
  124. BEGIN;
  125. INSERT INTO t1 VALUES(1);
  126. }
  127. db eval ROLLBACK
  128. db eval {
  129. SELECT * FROM t1;
  130. }
  131. } {}
  132. }
  133. }
  134. finish_test