sessionalter.test 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # 2023 October 02
  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 that the sessions module interacts well with
  12. # the ALTER TABLE ADD COLUMN command.
  13. #
  14. if {![info exists testdir]} {
  15. set testdir [file join [file dirname [info script]] .. .. test]
  16. }
  17. source [file join [file dirname [info script]] session_common.tcl]
  18. source $testdir/tester.tcl
  19. ifcapable !session {finish_test; return}
  20. set testprefix sessionalter
  21. forcedelete test.db2
  22. sqlite3 db2 test.db2
  23. do_execsql_test 1.0 {
  24. CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
  25. }
  26. do_execsql_test -db db2 1.1 {
  27. CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c DEFAULT 1234);
  28. }
  29. do_then_apply_sql {
  30. INSERT INTO t1 VALUES(1, 'one');
  31. INSERT INTO t1 VALUES(2, 'two');
  32. }
  33. do_execsql_test -db db2 1.2 {
  34. SELECT * FROM t1
  35. } {
  36. 1 one 1234
  37. 2 two 1234
  38. }
  39. do_then_apply_sql {
  40. UPDATE t1 SET b='four' WHERE a=2;
  41. }
  42. do_execsql_test -db db2 1.3 {
  43. SELECT * FROM t1
  44. } {
  45. 1 one 1234
  46. 2 four 1234
  47. }
  48. do_then_apply_sql {
  49. DELETE FROM t1 WHERE a=1;
  50. }
  51. do_execsql_test -db db2 1.4 {
  52. SELECT * FROM t1
  53. } {
  54. 2 four 1234
  55. }
  56. db2 close
  57. #--------------------------------------------------------------------------
  58. reset_db
  59. do_execsql_test 2.0 {
  60. CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
  61. }
  62. do_test 2.1 {
  63. sqlite3session S db main
  64. S attach t1
  65. set {} {}
  66. } {}
  67. do_execsql_test 2.2 {
  68. INSERT INTO t1 VALUES(1, 2);
  69. ALTER TABLE t1 ADD COLUMN c DEFAULT 'abcd';
  70. INSERT INTO t1 VALUES(2, 3, 4);
  71. }
  72. do_changeset_test 2.3 S {
  73. {INSERT t1 0 X.. {} {i 1 i 2 t abcd}}
  74. {INSERT t1 0 X.. {} {i 2 i 3 i 4}}
  75. }
  76. do_iterator_test 2.4 {} {
  77. DELETE FROM t1 WHERE a=2;
  78. ALTER TABLE t1 ADD COLUMN d DEFAULT 'abcd';
  79. ALTER TABLE t1 ADD COLUMN e DEFAULT 5;
  80. ALTER TABLE t1 ADD COLUMN f DEFAULT 7.2;
  81. -- INSERT INTO t1 VALUES(9, 9, 9, 9);
  82. } {
  83. {DELETE t1 0 X..... {i 2 i 3 i 4 t abcd i 5 f 7.2} {}}
  84. }
  85. #-------------------------------------------------------------------------
  86. # Tests of the sqlite3changegroup_xxx() APIs.
  87. #
  88. reset_db
  89. do_execsql_test 3.0 {
  90. CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
  91. CREATE TABLE t2(x PRIMARY KEY, y);
  92. CREATE TABLE t3(x, y);
  93. CREATE TABLE t4(y PRIMARY KEY, x) WITHOUT ROWID;
  94. INSERT INTO t1 VALUES(1, 2), (3, 4), (5, 6);
  95. INSERT INTO t2 VALUES('one', 'two'), ('three', 'four'), ('five', 'six');
  96. INSERT INTO t3 VALUES(1, 2), (3, 4), (5, 6);
  97. INSERT INTO t4(x, y) VALUES(1, 2), (3, 4), (5, 6);
  98. }
  99. db_save_and_close
  100. foreach {tn sql1 at sql2} {
  101. 1 {
  102. INSERT INTO t1(x, y) VALUES(7, 8);
  103. } {
  104. ALTER TABLE t1 ADD COLUMN z DEFAULT 10;
  105. } {
  106. UPDATE t1 SET y=11 WHERE x=7;
  107. }
  108. 2 {
  109. UPDATE t2 SET y='two.two' WHERE x='one';
  110. DELETE FROM t2 WHERE x='five';
  111. INSERT INTO t2(x, y) VALUES('seven', 'eight');
  112. } {
  113. ALTER TABLE t2 ADD COLUMN z;
  114. ALTER TABLE t2 ADD COLUMN zz;
  115. } {
  116. }
  117. 3 {
  118. DELETE FROM t2 WHERE x='five';
  119. } {
  120. ALTER TABLE t2 ADD COLUMN z DEFAULT 'xyz';
  121. } {
  122. }
  123. 4 {
  124. UPDATE t2 SET y='two.two' WHERE x='three';
  125. } {
  126. ALTER TABLE t2 ADD COLUMN z;
  127. } {
  128. UPDATE t2 SET z='abc' WHERE x='one';
  129. }
  130. 5* {
  131. UPDATE t2 SET y='two.two' WHERE x='three';
  132. } {
  133. ALTER TABLE t2 ADD COLUMN z DEFAULT 'defu1';
  134. } {
  135. }
  136. 6* {
  137. INSERT INTO t2(x, y) VALUES('nine', 'ten');
  138. } {
  139. ALTER TABLE t2 ADD COLUMN z;
  140. ALTER TABLE t2 ADD COLUMN a DEFAULT 'eelve';
  141. ALTER TABLE t2 ADD COLUMN b DEFAULT x'1234abcd';
  142. ALTER TABLE t2 ADD COLUMN c DEFAULT 4.2;
  143. ALTER TABLE t2 ADD COLUMN d DEFAULT NULL;
  144. } {
  145. }
  146. 7 {
  147. INSERT INTO t3(x, y) VALUES(7, 8);
  148. UPDATE t3 SET y='fourteen' WHERE x=1;
  149. DELETE FROM t3 WHERE x=3;
  150. } {
  151. ALTER TABLE t3 ADD COLUMN c;
  152. } {
  153. INSERT INTO t3(x, y, c) VALUES(9, 10, 11);
  154. }
  155. 8 {
  156. INSERT INTO t4(x, y) VALUES(7, 8);
  157. UPDATE t4 SET y='fourteen' WHERE x=1;
  158. DELETE FROM t4 WHERE x=3;
  159. } {
  160. ALTER TABLE t4 ADD COLUMN c;
  161. } {
  162. INSERT INTO t4(x, y, c) VALUES(9, 10, 11);
  163. }
  164. } {
  165. foreach {tn2 cmd} {
  166. 1 changeset_from_sql
  167. 2 patchset_from_sql
  168. } {
  169. db_restore_and_reopen
  170. set C1 [$cmd $sql1]
  171. execsql $at
  172. set C2 [$cmd $sql2]
  173. sqlite3changegroup grp
  174. grp schema db main
  175. breakpoint
  176. grp add $C1
  177. grp add $C2
  178. set T1 [grp output]
  179. grp delete
  180. db_restore_and_reopen
  181. execsql $at
  182. set T2 [$cmd "$sql1 ; $sql2"]
  183. if {[string range $tn end end]!="*"} {
  184. do_test 3.1.$tn.$tn2.1 { changeset_to_list $T1 } [changeset_to_list $T2]
  185. set testname "$tn.$tn2"
  186. } else {
  187. set testname "[string range $tn 0 end-1].$tn2"
  188. }
  189. db_restore_and_reopen
  190. proc xConflict {args} { return "REPLACE" }
  191. sqlite3changeset_apply_v2 db $T1 xConflict
  192. set S1 [scksum db main]
  193. db_restore_and_reopen
  194. sqlite3changeset_apply_v2 db $T2 xConflict
  195. set S2 [scksum db main]
  196. # if { $tn==7 } { puts [changeset_to_list $T1] }
  197. do_test 3.1.$tn.2 { set S1 } $S2
  198. }
  199. }
  200. finish_test