temptrigger.test 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # 2009 February 27
  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: temptrigger.test,v 1.3 2009/04/15 13:07:19 drh Exp $
  13. set testdir [file dirname $argv0]
  14. source $testdir/tester.tcl
  15. ifcapable {!trigger || !shared_cache} { finish_test ; return }
  16. # Test cases:
  17. #
  18. # temptrigger-1.*: Shared cache problem.
  19. # temptrigger-2.*: A similar shared cache problem.
  20. # temptrigger-3.*: Attached database problem.
  21. #
  22. #-------------------------------------------------------------------------
  23. # Test case temptrigger-1.* demonstrates a problem with temp triggers
  24. # in shared-cache mode. If process 1 connections to a shared-cache and
  25. # creates a temp trigger, the temp trigger is linked into the shared-cache
  26. # schema. If process 2 reloads the shared-cache schema from disk, then
  27. # it does not recreate the temp trigger belonging to process 1. From the
  28. # point of view of process 1, the temp trigger just disappeared.
  29. #
  30. # temptrigger-1.1: In shared cache mode, create a table in the main
  31. # database and add a temp trigger to it.
  32. #
  33. # temptrigger-1.2: Check that the temp trigger is correctly fired. Check
  34. # that the temp trigger is not fired by statements
  35. # executed by a second connection connected to the
  36. # same shared cache.
  37. #
  38. # temptrigger-1.3: Using the second connection to the shared-cache, cause
  39. # the shared-cache schema to be reloaded.
  40. #
  41. # temptrigger-1.4: Check that the temp trigger is still fired correctly.
  42. #
  43. # temptrigger-1.5: Check that the temp trigger can be dropped without error.
  44. #
  45. db close
  46. set ::enable_shared_cache [sqlite3_enable_shared_cache]
  47. sqlite3_enable_shared_cache 1
  48. sqlite3 db test.db
  49. sqlite3 db2 test.db
  50. do_test temptrigger-1.1 {
  51. execsql {
  52. CREATE TABLE t1(a, b);
  53. CREATE TEMP TABLE tt1(a, b);
  54. CREATE TEMP TRIGGER tr1 AFTER INSERT ON t1 BEGIN
  55. INSERT INTO tt1 VALUES(new.a, new.b);
  56. END;
  57. }
  58. } {}
  59. do_test temptrigger-1.2.1 {
  60. execsql { INSERT INTO t1 VALUES(1, 2) }
  61. execsql { SELECT * FROM t1 }
  62. } {1 2}
  63. do_test temptrigger-1.2.2 {
  64. execsql { SELECT * FROM tt1 }
  65. } {1 2}
  66. do_test temptrigger-1.2.3 {
  67. execsql { INSERT INTO t1 VALUES(3, 4) } db2
  68. execsql { SELECT * FROM t1 }
  69. } {1 2 3 4}
  70. do_test temptrigger-1.2.4 {
  71. execsql { SELECT * FROM tt1 }
  72. } {1 2}
  73. # Cause the shared-cache schema to be reloaded.
  74. #
  75. do_test temptrigger-1.3 {
  76. execsql { BEGIN; CREATE TABLE t3(a, b); ROLLBACK; } db2
  77. } {}
  78. do_test temptrigger-1.4 {
  79. execsql { INSERT INTO t1 VALUES(5, 6) }
  80. execsql { SELECT * FROM tt1 }
  81. } {1 2 5 6}
  82. do_test temptrigger-1.5 {
  83. # Before the bug was fixed, the following 'DROP TRIGGER' hit an
  84. # assert if executed.
  85. #execsql { DROP TRIGGER tr1 }
  86. } {}
  87. catch {db close}
  88. catch {db2 close}
  89. #-------------------------------------------------------------------------
  90. # Tests temptrigger-2.* are similar to temptrigger-1.*, except that
  91. # temptrigger-2.3 simply opens and closes a connection to the shared-cache.
  92. # It does not do anything special to cause the schema to be reloaded.
  93. #
  94. do_test temptrigger-2.1 {
  95. sqlite3 db test.db
  96. execsql {
  97. DELETE FROM t1;
  98. CREATE TEMP TABLE tt1(a, b);
  99. CREATE TEMP TRIGGER tr1 AFTER INSERT ON t1 BEGIN
  100. INSERT INTO tt1 VALUES(new.a, new.b);
  101. END;
  102. }
  103. } {}
  104. do_test temptrigger-2.2 {
  105. execsql {
  106. INSERT INTO t1 VALUES(10, 20);
  107. SELECT * FROM tt1;
  108. }
  109. } {10 20}
  110. do_test temptrigger-2.3 {
  111. sqlite3 db2 test.db
  112. db2 close
  113. } {}
  114. do_test temptrigger-2.4 {
  115. execsql {
  116. INSERT INTO t1 VALUES(30, 40);
  117. SELECT * FROM tt1;
  118. }
  119. } {10 20 30 40}
  120. do_test temptrigger-2.5 {
  121. #execsql { DROP TRIGGER tr1 }
  122. } {}
  123. catch {db close}
  124. catch {db2 close}
  125. sqlite3_enable_shared_cache $::enable_shared_cache
  126. #-------------------------------------------------------------------------
  127. # Test case temptrigger-3.* demonstrates a problem with temp triggers
  128. # on tables located in attached databases. At one point when SQLite reloaded
  129. # the schema of an attached database (because some other connection had
  130. # changed the schema cookie) it was not re-creating temp triggers attached
  131. # to tables located within the attached database.
  132. #
  133. # temptrigger-3.1: Attach database 'test2.db' to connection [db]. Add a
  134. # temp trigger to a table in 'test2.db'.
  135. #
  136. # temptrigger-3.2: Check that the temp trigger is correctly fired.
  137. #
  138. # temptrigger-3.3: Update the schema of 'test2.db' using an external
  139. # connection. This forces [db] to reload the 'test2.db'
  140. # schema. Check that the temp trigger is still fired
  141. # correctly.
  142. #
  143. # temptrigger-3.4: Check that the temp trigger can be dropped without error.
  144. #
  145. do_test temptrigger-3.1 {
  146. catch { forcedelete test2.db test2.db-journal }
  147. catch { forcedelete test.db test.db-journal }
  148. sqlite3 db test.db
  149. sqlite3 db2 test2.db
  150. execsql { CREATE TABLE t2(a, b) } db2
  151. execsql {
  152. ATTACH 'test2.db' AS aux;
  153. CREATE TEMP TABLE tt2(a, b);
  154. CREATE TEMP TRIGGER tr2 AFTER INSERT ON aux.t2 BEGIN
  155. INSERT INTO tt2 VALUES(new.a, new.b);
  156. END;
  157. }
  158. } {}
  159. do_test temptrigger-3.2.1 {
  160. execsql {
  161. INSERT INTO aux.t2 VALUES(1, 2);
  162. SELECT * FROM aux.t2;
  163. }
  164. } {1 2}
  165. do_test temptrigger-3.2.2 {
  166. execsql { SELECT * FROM tt2 }
  167. } {1 2}
  168. do_test temptrigger-3.3.1 {
  169. execsql { CREATE TABLE t3(a, b) } db2
  170. execsql {
  171. INSERT INTO aux.t2 VALUES(3, 4);
  172. SELECT * FROM aux.t2;
  173. }
  174. } {1 2 3 4}
  175. do_test temptrigger-3.3.2 {
  176. execsql { SELECT * FROM tt2 }
  177. } {1 2 3 4}
  178. do_test temptrigger-3.4 {
  179. # Before the bug was fixed, the following 'DROP TRIGGER' hit an
  180. # assert if executed.
  181. #execsql { DROP TRIGGER tr2 }
  182. } {}
  183. catch { db close }
  184. catch { db2 close }
  185. finish_test