exists.test 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # 2011 April 9
  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. # This file implements regression tests for SQLite library. The
  12. # focus of this file is testing the various schema modification statements
  13. # that feature "IF EXISTS" or "IF NOT EXISTS" clauses.
  14. #
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. source $testdir/lock_common.tcl
  18. foreach jm {rollback wal} {
  19. set testprefix exists-$jm
  20. # This block of tests is targeted at CREATE XXX IF NOT EXISTS statements.
  21. #
  22. do_multiclient_test tn {
  23. # TABLE objects.
  24. #
  25. do_test 1.$tn.1.1 {
  26. if {$jm == "wal"} { sql2 { PRAGMA journal_mode = WAL } }
  27. sql2 { CREATE TABLE t1(x) }
  28. sql1 { CREATE TABLE IF NOT EXISTS t1(a, b) }
  29. sql2 { DROP TABLE t1 }
  30. sql1 { CREATE TABLE IF NOT EXISTS t1(a, b) }
  31. sql2 { SELECT name FROM sqlite_master WHERE type = 'table' }
  32. } {t1}
  33. do_test 1.$tn.1.2 {
  34. sql2 { CREATE TABLE t2(x) }
  35. sql1 { CREATE TABLE IF NOT EXISTS t2 AS SELECT * FROM t1 }
  36. sql2 { DROP TABLE t2 }
  37. sql1 { CREATE TABLE IF NOT EXISTS t2 AS SELECT * FROM t1 }
  38. sql2 { SELECT name FROM sqlite_master WHERE type = 'table' }
  39. } {t1 t2}
  40. # INDEX objects.
  41. #
  42. do_test 1.$tn.2 {
  43. sql2 { CREATE INDEX i1 ON t1(a) }
  44. sql1 { CREATE INDEX IF NOT EXISTS i1 ON t1(a, b) }
  45. sql2 { DROP INDEX i1 }
  46. sql1 { CREATE INDEX IF NOT EXISTS i1 ON t1(a, b) }
  47. sql2 { SELECT name FROM sqlite_master WHERE type = 'index' }
  48. } {i1}
  49. # VIEW objects.
  50. #
  51. do_test 1.$tn.3 {
  52. sql2 { CREATE VIEW v1 AS SELECT * FROM t1 }
  53. sql1 { CREATE VIEW IF NOT EXISTS v1 AS SELECT * FROM t1 }
  54. sql2 { DROP VIEW v1 }
  55. sql1 { CREATE VIEW IF NOT EXISTS v1 AS SELECT * FROM t1 }
  56. sql2 { SELECT name FROM sqlite_master WHERE type = 'view' }
  57. } {v1}
  58. # TRIGGER objects.
  59. #
  60. do_test $tn.4 {
  61. sql2 { CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN SELECT 1; END }
  62. sql1 { CREATE TRIGGER IF NOT EXISTS tr1 AFTER INSERT ON t1 BEGIN SELECT 1; END }
  63. sql2 { DROP TRIGGER tr1 }
  64. sql1 { CREATE TRIGGER IF NOT EXISTS tr1 AFTER INSERT ON t1 BEGIN SELECT 1; END }
  65. sql2 { SELECT name FROM sqlite_master WHERE type = 'trigger' }
  66. } {tr1}
  67. }
  68. # This block of tests is targeted at DROP XXX IF EXISTS statements.
  69. #
  70. do_multiclient_test tn {
  71. # TABLE objects.
  72. #
  73. do_test 2.$tn.1 {
  74. if {$jm == "wal"} { sql1 { PRAGMA journal_mode = WAL } }
  75. sql1 { DROP TABLE IF EXISTS t1 }
  76. sql2 { CREATE TABLE t1(x) }
  77. sql1 { DROP TABLE IF EXISTS t1 }
  78. sql2 { SELECT name FROM sqlite_master WHERE type = 'table' }
  79. } {}
  80. # INDEX objects.
  81. #
  82. do_test 2.$tn.2 {
  83. sql1 { CREATE TABLE t2(x) }
  84. sql1 { DROP INDEX IF EXISTS i2 }
  85. sql2 { CREATE INDEX i2 ON t2(x) }
  86. sql1 { DROP INDEX IF EXISTS i2 }
  87. sql2 { SELECT name FROM sqlite_master WHERE type = 'index' }
  88. } {}
  89. # VIEW objects.
  90. #
  91. do_test 2.$tn.3 {
  92. sql1 { DROP VIEW IF EXISTS v1 }
  93. sql2 { CREATE VIEW v1 AS SELECT * FROM t2 }
  94. sql1 { DROP VIEW IF EXISTS v1 }
  95. sql2 { SELECT name FROM sqlite_master WHERE type = 'view' }
  96. } {}
  97. # TRIGGER objects.
  98. #
  99. do_test 2.$tn.4 {
  100. sql1 { DROP TRIGGER IF EXISTS tr1 }
  101. sql2 { CREATE TRIGGER tr1 AFTER INSERT ON t2 BEGIN SELECT 1; END }
  102. sql1 { DROP TRIGGER IF EXISTS tr1 }
  103. sql2 { SELECT name FROM sqlite_master WHERE type = 'trigger' }
  104. } {}
  105. }
  106. # This block of tests is targeted at DROP XXX IF EXISTS statements with
  107. # attached databases.
  108. #
  109. do_multiclient_test tn {
  110. forcedelete test.db2
  111. do_test 3.$tn.0 {
  112. sql1 { ATTACH 'test.db2' AS aux }
  113. sql2 { ATTACH 'test.db2' AS aux }
  114. } {}
  115. # TABLE objects.
  116. #
  117. do_test 3.$tn.1.1 {
  118. sql1 { DROP TABLE IF EXISTS aux.t1 }
  119. sql2 { CREATE TABLE aux.t1(x) }
  120. sql1 { DROP TABLE IF EXISTS aux.t1 }
  121. sql2 { SELECT name FROM aux.sqlite_master WHERE type = 'table' }
  122. } {}
  123. do_test 3.$tn.1.2 {
  124. sql1 { DROP TABLE IF EXISTS t1 }
  125. sql2 { CREATE TABLE aux.t1(x) }
  126. sql1 { DROP TABLE IF EXISTS t1 }
  127. sql2 { SELECT name FROM aux.sqlite_master WHERE type = 'table' }
  128. } {}
  129. # INDEX objects.
  130. #
  131. do_test 3.$tn.2.1 {
  132. sql1 { CREATE TABLE aux.t2(x) }
  133. sql1 { DROP INDEX IF EXISTS aux.i2 }
  134. sql2 { CREATE INDEX aux.i2 ON t2(x) }
  135. sql1 { DROP INDEX IF EXISTS aux.i2 }
  136. sql2 { SELECT name FROM aux.sqlite_master WHERE type = 'index' }
  137. } {}
  138. do_test 3.$tn.2.2 {
  139. sql1 { DROP INDEX IF EXISTS i2 }
  140. sql2 { CREATE INDEX aux.i2 ON t2(x) }
  141. sql1 { DROP INDEX IF EXISTS i2 }
  142. sql2 { SELECT * FROM aux.sqlite_master WHERE type = 'index' }
  143. } {}
  144. # VIEW objects.
  145. #
  146. do_test 3.$tn.3.1 {
  147. sql1 { DROP VIEW IF EXISTS aux.v1 }
  148. sql2 { CREATE VIEW aux.v1 AS SELECT * FROM t2 }
  149. sql1 { DROP VIEW IF EXISTS aux.v1 }
  150. sql2 { SELECT name FROM aux.sqlite_master WHERE type = 'view' }
  151. } {}
  152. do_test 3.$tn.3.2 {
  153. sql1 { DROP VIEW IF EXISTS v1 }
  154. sql2 { CREATE VIEW aux.v1 AS SELECT * FROM t2 }
  155. sql1 { DROP VIEW IF EXISTS v1 }
  156. sql2 { SELECT name FROM aux.sqlite_master WHERE type = 'view' }
  157. } {}
  158. # TRIGGER objects.
  159. #
  160. do_test 3.$tn.4.1 {
  161. sql1 { DROP TRIGGER IF EXISTS aux.tr1 }
  162. sql2 { CREATE TRIGGER aux.tr1 AFTER INSERT ON t2 BEGIN SELECT 1; END }
  163. sql1 { DROP TRIGGER IF EXISTS aux.tr1 }
  164. sql2 { SELECT name FROM aux.sqlite_master WHERE type = 'trigger' }
  165. } {}
  166. do_test 3.$tn.4.2 {
  167. sql1 { DROP TRIGGER IF EXISTS tr1 }
  168. sql2 { CREATE TRIGGER aux.tr1 AFTER INSERT ON t2 BEGIN SELECT 1; END }
  169. sql1 { DROP TRIGGER IF EXISTS tr1 }
  170. sql2 { SELECT name FROM aux.sqlite_master WHERE type = 'trigger' }
  171. } {}
  172. }
  173. }
  174. finish_test