source-tables.td 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. # Setup SQL Server state.
  10. #
  11. # Create a table that has CDC enabled.
  12. $ sql-server-connect name=sql-server
  13. server=tcp:sql-server,1433;IntegratedSecurity=true;TrustServerCertificate=true;User ID=${arg.default-sql-server-user};Password=${arg.default-sql-server-password}
  14. $ sql-server-execute name=sql-server
  15. DROP DATABASE IF EXISTS test_source_tables;
  16. CREATE DATABASE test_source_tables COLLATE Latin1_General_100_CI_AI_SC_UTF8;
  17. USE test_source_tables;
  18. EXEC sys.sp_cdc_enable_db;
  19. ALTER DATABASE test_source_tables SET ALLOW_SNAPSHOT_ISOLATION ON;
  20. CREATE TABLE t1_pk (key_col VARCHAR(20) PRIMARY KEY, val_col VARCHAR(1024));
  21. EXEC sys.sp_cdc_enable_table @source_schema = 'dbo', @source_name = 't1_pk', @role_name = 'SA', @capture_instance = N't1_pk_cdc1', @supports_net_changes = 0;
  22. INSERT INTO t1_pk VALUES ('a', 'hello world'), ('b', 'foobar'), ('c', 'anotha one');
  23. CREATE TABLE t2_no_cdc (key_col VARCHAR(20) PRIMARY KEY, val_col VARCHAR(1024));
  24. CREATE TABLE t3_text (value VARCHAR(100));
  25. EXEC sys.sp_cdc_enable_table @source_schema = 'dbo', @source_name = 't3_text', @role_name = 'SA', @supports_net_changes = 0;
  26. CREATE TABLE text_cols (c1 decimal(20, 10), c2 time, c3 money, c4 varbinary(100));
  27. EXEC sys.sp_cdc_enable_table @source_schema = 'dbo', @source_name = 'text_cols', @role_name = 'SA', @supports_net_changes = 0;
  28. INSERT INTO text_cols VALUES (1.444889, '12:00:00', '$100.99', 0x1100AB);
  29. CREATE TABLE dummy (data int);
  30. EXEC sys.sp_cdc_enable_table @source_schema = 'dbo', @source_name = 'dummy', @role_name = 'SA', @supports_net_changes = 0;
  31. # Exercise Materialize.
  32. > CREATE SECRET IF NOT EXISTS sql_server_pass AS '${arg.default-sql-server-password}'
  33. $ postgres-execute connection=postgres://mz_system:materialize@${testdrive.materialize-internal-sql-addr}
  34. ALTER SYSTEM SET enable_sql_server_source = true;
  35. > CREATE CONNECTION ms_conn TO SQL SERVER (
  36. HOST 'sql-server',
  37. PORT 1433,
  38. DATABASE test_source_tables,
  39. USER '${arg.default-sql-server-user}',
  40. PASSWORD = SECRET sql_server_pass
  41. );
  42. # Create a SQL Server Source.
  43. > CREATE CLUSTER src_tables (SIZE = '1', REPLICATION FACTOR = 1);
  44. > SET CLUSTER TO src_tables;
  45. > CREATE SOURCE ms_src FROM SQL SERVER CONNECTION ms_conn;
  46. # Can't create tables from SQL server source where it doesn't EXISTS
  47. ! CREATE TABLE does_not_exist FROM SOURCE ms_src (REFERENCE does_not_exist);
  48. contains:not found in source
  49. # Can't create tables from SQL server source where table doesn't have cdc enabled
  50. ! CREATE TABLE no_cdc FROM SOURCE ms_src (REFERENCE t2_no_cdc);
  51. contains:not found in source
  52. # Can create tables from SQL server source
  53. > CREATE TABLE t1 FROM SOURCE ms_src (REFERENCE t1_pk);
  54. # Wait until snapshot has emitted stats and then insert a new row
  55. # to force LSN in MS SQL to progress.
  56. > SELECT s.name, ss.snapshot_records_known = ss.snapshot_records_staged
  57. FROM mz_internal.mz_source_statistics ss JOIN mz_sources s using (id)
  58. WHERE s.name = 'ms_src' AND ss.snapshot_records_staged > 0;
  59. ms_src true
  60. $ sql-server-execute name=sql-server
  61. INSERT INTO dummy VALUES (1);
  62. > SELECT * FROM t1;
  63. a "hello world"
  64. b "foobar"
  65. c "anotha one"
  66. # using a qualified name should work as well
  67. > CREATE TABLE t2 FROM SOURCE ms_src (REFERENCE dbo.t1_pk);
  68. # Wait until snapshot has emitted stats and then insert a new row
  69. # to force LSN in MS SQL to progress.
  70. > SELECT s.name, ss.snapshot_records_known = ss.snapshot_records_staged
  71. FROM mz_internal.mz_source_statistics ss JOIN mz_sources s using (id)
  72. WHERE s.name = 'ms_src' AND ss.snapshot_records_staged > 0;
  73. ms_src true
  74. $ sql-server-execute name=sql-server
  75. INSERT INTO dummy VALUES (1);
  76. $ sql-server-execute name=sql-server
  77. INSERT INTO dummy VALUES (1);
  78. > SELECT * FROM t2;
  79. a "hello world"
  80. b "foobar"
  81. c "anotha one"
  82. # source tables see updates
  83. $ sql-server-execute name=sql-server
  84. DELETE FROM t1_pk WHERE key_col = 'a';
  85. INSERT INTO t1_pk VALUES ('😊', 'lets see what happens');
  86. UPDATE t1_pk SET val_col = 'beer' WHERE key_col = 'b';
  87. INSERT INTO t2_no_cdc VALUES ('ignored', 'update');
  88. > SELECT * FROM t1;
  89. b "beer"
  90. c "anotha one"
  91. 😊 "lets see what happens"
  92. > SELECT * FROM t2;
  93. b "beer"
  94. c "anotha one"
  95. 😊 "lets see what happens"
  96. > DROP TABLE t1;
  97. # recreate the source table after dropping it
  98. # we should see the same values
  99. > CREATE TABLE t1 FROM SOURCE ms_src (REFERENCE t1_pk);
  100. # Wait until snapshot has emitted stats and then insert a new row
  101. # to force LSN in MS SQL to progress.
  102. > SELECT s.name, ss.snapshot_records_known = ss.snapshot_records_staged
  103. FROM mz_internal.mz_source_statistics ss JOIN mz_sources s using (id)
  104. WHERE s.name = 'ms_src' AND ss.snapshot_records_staged > 0;
  105. ms_src true
  106. $ sql-server-execute name=sql-server
  107. INSERT INTO dummy VALUES (1);
  108. > SELECT * FROM t1;
  109. b "beer"
  110. c "anotha one"
  111. 😊 "lets see what happens"
  112. > DROP TABLE t1;
  113. > DROP TABLE t2;
  114. # source tables support text columns
  115. > CREATE TABLE text_cols FROM SOURCE ms_src (REFERENCE dbo.text_cols) WITH (TEXT COLUMNS = (c1, c2, c3, c4));
  116. # Wait until snapshot has emitted stats and then insert a new row
  117. # to force LSN in MS SQL to progress.
  118. > SELECT s.name, ss.snapshot_records_known = ss.snapshot_records_staged
  119. FROM mz_internal.mz_source_statistics ss JOIN mz_sources s using (id)
  120. WHERE s.name = 'ms_src' AND ss.snapshot_records_staged > 0;
  121. ms_src true
  122. $ sql-server-execute name=sql-server
  123. INSERT INTO dummy VALUES (1);
  124. > SHOW COLUMNS FROM text_cols;
  125. c1 true text ""
  126. c2 true text ""
  127. c3 true text ""
  128. c4 true text ""
  129. > SELECT * FROM text_cols;
  130. 1.4448890000 12:00:00 100.9900 EQCr
  131. $ sql-server-execute name=sql-server
  132. INSERT INTO text_cols VALUES (2.000001, '01:59:32.99901', '$0.89', 0x1122AABBCC00DD), (NULL, NULL, '$99.99', NULL);
  133. > SELECT * FROM text_cols;
  134. 1.4448890000 12:00:00 100.9900 EQCr
  135. 2.0000010000 01:59:32.999010 0.8900 ESKqu8wA3Q==
  136. <null> <null> 99.9900 <null>
  137. > DROP TABLE text_cols;
  138. # source tables support exclude columns
  139. > CREATE TABLE exclude_cols FROM SOURCE ms_src (REFERENCE dbo.t1_pk) WITH (EXCLUDE COLUMNS = (key_col));
  140. # Wait until snapshot has emitted stats and then insert a new row
  141. # to force LSN in MS SQL to progress.
  142. > SELECT s.name, ss.snapshot_records_known = ss.snapshot_records_staged
  143. FROM mz_internal.mz_source_statistics ss JOIN mz_sources s using (id)
  144. WHERE s.name = 'ms_src' AND ss.snapshot_records_staged > 0;
  145. ms_src true
  146. $ sql-server-execute name=sql-server
  147. INSERT INTO dummy VALUES (1);
  148. > SHOW COLUMNS FROM exclude_cols;
  149. val_col true "character varying" ""
  150. > SELECT * FROM exclude_cols;
  151. "beer"
  152. "anotha one"
  153. "lets see what happens"
  154. $ sql-server-execute name=sql-server
  155. INSERT INTO t1_pk(key_col, val_col) VALUES ('d', 'dogs'), ('e', 'elephant');
  156. > SELECT * FROM exclude_cols;
  157. "beer"
  158. "anotha one"
  159. "lets see what happens"
  160. "dogs"
  161. "elephant"
  162. > DROP TABLE exclude_cols;
  163. # Ensure that rewinds are correctly processed
  164. > CREATE TABLE t1 FROM SOURCE ms_src (REFERENCE t1_pk);
  165. # Wait until snapshot has emitted stats and then insert a new row
  166. # to force LSN in MS SQL to progress.
  167. > SELECT s.name, ss.snapshot_records_known = ss.snapshot_records_staged
  168. FROM mz_internal.mz_source_statistics ss JOIN mz_sources s using (id)
  169. WHERE s.name = 'ms_src' AND ss.snapshot_records_staged > 0;
  170. ms_src true
  171. $ sql-server-execute name=sql-server
  172. INSERT INTO dummy VALUES (1);
  173. > SELECT * FROM t1;
  174. b "beer"
  175. c "anotha one"
  176. d "dogs"
  177. e "elephant"
  178. 😊 "lets see what happens"
  179. > ALTER CLUSTER src_tables SET (REPLICATION FACTOR = 0);
  180. $ sql-server-execute name=sql-server
  181. INSERT INTO t1_pk VALUES ('x', 'xeon'), ('y', 'yellow'), ('z', 'zion');
  182. > CREATE TABLE t2 FROM SOURCE ms_src (REFERENCE t1_pk);
  183. > ALTER CLUSTER src_tables SET (REPLICATION FACTOR = 1);
  184. # Wait until snapshot has emitted stats and then insert a new row
  185. # to force LSN in MS SQL to progress.
  186. > SELECT s.name, ss.snapshot_records_known = ss.snapshot_records_staged
  187. FROM mz_internal.mz_source_statistics ss JOIN mz_sources s using (id)
  188. WHERE s.name = 'ms_src' AND ss.snapshot_records_staged > 0;
  189. ms_src true
  190. $ sql-server-execute name=sql-server
  191. INSERT INTO dummy VALUES (1);
  192. > SELECT * FROM t1;
  193. b "beer"
  194. c "anotha one"
  195. d "dogs"
  196. e "elephant"
  197. x "xeon"
  198. y "yellow"
  199. z "zion"
  200. 😊 "lets see what happens"
  201. > SELECT * FROM t2;
  202. b "beer"
  203. c "anotha one"
  204. d "dogs"
  205. e "elephant"
  206. x "xeon"
  207. y "yellow"
  208. z "zion"
  209. 😊 "lets see what happens"
  210. # create source table after adding a column to upstream table
  211. # !!! disabled for now as we don't handle the capture instance switch gracefully.
  212. #$ sql-server-execute name=sql-server
  213. #ALTER TABLE t1_pk ADD extra_1 INT NULL;
  214. #EXEC sys.sp_cdc_enable_table @source_schema = 'dbo', @source_name = 't1_pk', @capture_instance = N't1_pk_cdc2', @role_name = 'SA', @supports_net_changes = 0;
  215. #EXEC sys.sp_cdc_disable_table @source_schema = 'dbo', @source_name = 't1_pk', @capture_instance = N't1_pk_cdc1';
  216. #
  217. #> CREATE TABLE extra_col FROM SOURCE ms_src (REFERENCE t1_pk);
  218. #
  219. #> SELECT * FROM extra_col;
  220. #b "beer" <null>
  221. #c "anotha one" <null>
  222. #😊 "lets see what happens" <null>