alter-table-after-source.td 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. #
  10. # Test ALTER TABLE -- source will error out for tables which existed when the source was created
  11. #
  12. $ set-sql-timeout duration=60s
  13. $ postgres-execute connection=postgres://mz_system:materialize@${testdrive.materialize-internal-sql-addr}
  14. ALTER SYSTEM SET pg_schema_validation_interval = '2s';
  15. > CREATE SECRET pgpass AS 'postgres'
  16. > CREATE CONNECTION pg TO POSTGRES (
  17. HOST postgres,
  18. DATABASE postgres,
  19. USER postgres,
  20. PASSWORD SECRET pgpass
  21. )
  22. $ postgres-execute connection=postgres://postgres:postgres@postgres
  23. ALTER USER postgres WITH replication;
  24. DROP SCHEMA IF EXISTS public CASCADE;
  25. DROP PUBLICATION IF EXISTS mz_source;
  26. CREATE SCHEMA public;
  27. CREATE TABLE add_columns (f1 INTEGER);
  28. ALTER TABLE add_columns REPLICA IDENTITY FULL;
  29. INSERT INTO add_columns VALUES (1);
  30. CREATE TABLE remove_column (f1 INTEGER, f2 VARCHAR(2));
  31. ALTER TABLE remove_column REPLICA IDENTITY FULL;
  32. INSERT INTO remove_column VALUES (2, 'ab');
  33. CREATE TABLE alter_column (f1 INTEGER, f2 VARCHAR(2));
  34. ALTER TABLE alter_column REPLICA IDENTITY FULL;
  35. INSERT INTO alter_column VALUES (2, 'ab');
  36. CREATE TABLE alter_drop_nullability (f1 INTEGER NOT NULL);
  37. ALTER TABLE alter_drop_nullability REPLICA IDENTITY FULL;
  38. INSERT INTO alter_drop_nullability VALUES (1);
  39. CREATE TABLE alter_add_nullability (f1 INTEGER);
  40. ALTER TABLE alter_add_nullability REPLICA IDENTITY FULL;
  41. INSERT INTO alter_add_nullability VALUES (1);
  42. CREATE TABLE alter_drop_pk (f1 INTEGER PRIMARY KEY);
  43. ALTER TABLE alter_drop_pk REPLICA IDENTITY FULL;
  44. INSERT INTO alter_drop_pk VALUES (1);
  45. CREATE TABLE alter_add_pk (f1 INTEGER);
  46. ALTER TABLE alter_add_pk REPLICA IDENTITY FULL;
  47. INSERT INTO alter_add_pk VALUES (1);
  48. CREATE TABLE alter_cycle_pk (f1 INTEGER PRIMARY KEY);
  49. ALTER TABLE alter_cycle_pk REPLICA IDENTITY FULL;
  50. INSERT INTO alter_cycle_pk VALUES (1);
  51. CREATE TABLE alter_cycle_pk_off (f1 INTEGER);
  52. ALTER TABLE alter_cycle_pk_off REPLICA IDENTITY FULL;
  53. INSERT INTO alter_cycle_pk_off VALUES (1);
  54. CREATE TABLE alter_drop_unique (f1 INTEGER UNIQUE);
  55. ALTER TABLE alter_drop_unique REPLICA IDENTITY FULL;
  56. INSERT INTO alter_drop_unique VALUES (1);
  57. CREATE TABLE alter_add_unique (f1 INTEGER);
  58. ALTER TABLE alter_add_unique REPLICA IDENTITY FULL;
  59. INSERT INTO alter_add_unique VALUES (1);
  60. CREATE TABLE alter_extend_column (f1 VARCHAR(2));
  61. ALTER TABLE alter_extend_column REPLICA IDENTITY FULL;
  62. INSERT INTO alter_extend_column VALUES ('ab');
  63. CREATE TABLE alter_decimal (f1 DECIMAL(5,2));
  64. ALTER TABLE alter_decimal REPLICA IDENTITY FULL;
  65. INSERT INTO alter_decimal VALUES (123.45);
  66. CREATE TABLE alter_table_rename (f1 INTEGER);
  67. ALTER TABLE alter_table_rename REPLICA IDENTITY FULL;
  68. INSERT INTO alter_table_rename VALUES (1);
  69. CREATE TABLE alter_table_rename_column (f1 VARCHAR(10), f2 VARCHAR(10));
  70. ALTER TABLE alter_table_rename_column REPLICA IDENTITY FULL;
  71. INSERT INTO alter_table_rename_column (f1, f2) VALUES ('f1_orig','f2_orig');
  72. CREATE TABLE alter_table_change_attnum (f1 VARCHAR(10), f2 VARCHAR(10));
  73. ALTER TABLE alter_table_change_attnum REPLICA IDENTITY FULL;
  74. INSERT INTO alter_table_change_attnum (f1, f2) VALUES ('f1_orig','f2_orig');
  75. CREATE TABLE alter_table_supported (f1 int, f2 int);
  76. ALTER TABLE alter_table_supported REPLICA IDENTITY FULL;
  77. INSERT INTO alter_table_supported (f1, f2) VALUES (1, 1);
  78. CREATE TABLE truncate_table (f1 int, f2 int);
  79. ALTER TABLE truncate_table REPLICA IDENTITY FULL;
  80. INSERT INTO truncate_table (f1, f2) VALUES (1, 1);
  81. CREATE TABLE drop_table (f1 int, f2 int);
  82. ALTER TABLE drop_table REPLICA IDENTITY FULL;
  83. INSERT INTO drop_table (f1, f2) VALUES (1, 1);
  84. CREATE PUBLICATION mz_source FOR ALL TABLES;
  85. > CREATE SOURCE mz_source FROM POSTGRES CONNECTION pg (PUBLICATION 'mz_source');
  86. > CREATE TABLE add_columns FROM SOURCE mz_source (REFERENCE add_columns);
  87. > CREATE TABLE remove_column FROM SOURCE mz_source (REFERENCE remove_column);
  88. > CREATE TABLE alter_column FROM SOURCE mz_source (REFERENCE alter_column);
  89. > CREATE TABLE alter_drop_nullability FROM SOURCE mz_source (REFERENCE alter_drop_nullability);
  90. > CREATE TABLE alter_add_nullability FROM SOURCE mz_source (REFERENCE alter_add_nullability);
  91. > CREATE TABLE alter_drop_pk FROM SOURCE mz_source (REFERENCE alter_drop_pk);
  92. > CREATE TABLE alter_add_pk FROM SOURCE mz_source (REFERENCE alter_add_pk);
  93. > CREATE TABLE alter_cycle_pk FROM SOURCE mz_source (REFERENCE alter_cycle_pk);
  94. > CREATE TABLE alter_cycle_pk_off FROM SOURCE mz_source (REFERENCE alter_cycle_pk_off);
  95. > CREATE TABLE alter_drop_unique FROM SOURCE mz_source (REFERENCE alter_drop_unique);
  96. > CREATE TABLE alter_add_unique FROM SOURCE mz_source (REFERENCE alter_add_unique);
  97. > CREATE TABLE alter_extend_column FROM SOURCE mz_source (REFERENCE alter_extend_column);
  98. > CREATE TABLE alter_decimal FROM SOURCE mz_source (REFERENCE alter_decimal);
  99. > CREATE TABLE alter_table_rename FROM SOURCE mz_source (REFERENCE alter_table_rename);
  100. > CREATE TABLE alter_table_rename_column FROM SOURCE mz_source (REFERENCE alter_table_rename_column);
  101. > CREATE TABLE alter_table_change_attnum FROM SOURCE mz_source (REFERENCE alter_table_change_attnum);
  102. > CREATE TABLE alter_table_supported FROM SOURCE mz_source (REFERENCE alter_table_supported);
  103. > CREATE TABLE truncate_table FROM SOURCE mz_source (REFERENCE truncate_table);
  104. > CREATE TABLE drop_table FROM SOURCE mz_source (REFERENCE drop_table);
  105. #
  106. # Add column
  107. > SELECT * FROM add_columns;
  108. 1
  109. $ postgres-execute connection=postgres://postgres:postgres@postgres
  110. ALTER TABLE add_columns ADD COLUMN f2 varchar(2);
  111. INSERT INTO add_columns VALUES (2, 'ab');
  112. > SELECT * from add_columns;
  113. 1
  114. 2
  115. #
  116. # Remove column
  117. > SELECT * from remove_column;
  118. 2 ab
  119. $ postgres-execute connection=postgres://postgres:postgres@postgres
  120. ALTER TABLE remove_column DROP COLUMN f2;
  121. ! SELECT * from remove_column;
  122. contains:altered
  123. #
  124. # Alter column type
  125. > SELECT * from alter_column;
  126. 2 ab
  127. $ postgres-execute connection=postgres://postgres:postgres@postgres
  128. ALTER TABLE alter_column ALTER COLUMN f2 TYPE CHAR(2);
  129. ! SELECT * from alter_column;
  130. contains:altered
  131. #
  132. # Drop NOT NULL
  133. > SELECT * from alter_drop_nullability
  134. 1
  135. $ postgres-execute connection=postgres://postgres:postgres@postgres
  136. ALTER TABLE alter_drop_nullability ALTER COLUMN f1 DROP NOT NULL;
  137. ! SELECT * FROM alter_drop_nullability WHERE f1 IS NOT NULL;
  138. contains:altered
  139. # We have guaranteed that this column is not null so the optimizer eagerly
  140. # returns the empty set.
  141. > SELECT * FROM alter_drop_nullability WHERE f1 IS NULL;
  142. #
  143. # Add NOT NULL
  144. > SELECT * from alter_add_nullability
  145. 1
  146. $ postgres-execute connection=postgres://postgres:postgres@postgres
  147. ALTER TABLE alter_add_nullability ALTER COLUMN f1 SET NOT NULL;
  148. INSERT INTO alter_add_nullability VALUES (1);
  149. > SELECT * FROM alter_add_nullability;
  150. 1
  151. 1
  152. #
  153. # Drop PK
  154. > SELECT * from alter_drop_pk
  155. 1
  156. $ postgres-execute connection=postgres://postgres:postgres@postgres
  157. ALTER TABLE alter_drop_pk DROP CONSTRAINT alter_drop_pk_pkey;
  158. ! SELECT f1 FROM alter_drop_pk;
  159. contains:altered
  160. #
  161. # Add PK
  162. > SELECT * from alter_add_pk
  163. 1
  164. $ postgres-execute connection=postgres://postgres:postgres@postgres
  165. ALTER TABLE alter_add_pk ADD PRIMARY KEY(f1);
  166. INSERT INTO alter_add_pk VALUES (2);
  167. > SELECT * FROM alter_add_pk;
  168. 1
  169. 2
  170. #
  171. # Cycle PK
  172. > SELECT * from alter_cycle_pk
  173. 1
  174. $ postgres-execute connection=postgres://postgres:postgres@postgres
  175. ALTER TABLE alter_cycle_pk DROP CONSTRAINT alter_cycle_pk_pkey;
  176. ALTER TABLE alter_cycle_pk ADD PRIMARY KEY(f1);
  177. ! SELECT * FROM alter_cycle_pk;
  178. contains:altered
  179. #
  180. # Cycle PK off (no pk, pk, no pk)
  181. > SELECT * from alter_cycle_pk_off
  182. 1
  183. $ postgres-execute connection=postgres://postgres:postgres@postgres
  184. ALTER TABLE alter_cycle_pk_off ADD PRIMARY KEY(f1);
  185. ALTER TABLE alter_cycle_pk_off DROP CONSTRAINT alter_cycle_pk_off_pkey;
  186. INSERT INTO alter_cycle_pk_off VALUES (1);
  187. > SELECT * FROM alter_cycle_pk_off;
  188. 1
  189. 1
  190. #
  191. # Drop unique
  192. > SELECT * from alter_drop_unique
  193. 1
  194. $ postgres-execute connection=postgres://postgres:postgres@postgres
  195. ALTER TABLE alter_drop_unique DROP CONSTRAINT alter_drop_unique_f1_key;
  196. ! SELECT f1 FROM alter_drop_unique;
  197. contains:altered
  198. #
  199. # Add unique
  200. > SELECT * from alter_add_unique
  201. 1
  202. $ postgres-execute connection=postgres://postgres:postgres@postgres
  203. ALTER TABLE alter_add_unique ADD UNIQUE(f1);
  204. INSERT INTO alter_add_unique VALUES (2);
  205. > SELECT * FROM alter_add_unique;
  206. 1
  207. 2
  208. #
  209. # Extend column
  210. > SELECT * from alter_extend_column
  211. ab
  212. $ postgres-execute connection=postgres://postgres:postgres@postgres
  213. ALTER TABLE alter_extend_column ALTER COLUMN f1 TYPE VARCHAR(20);
  214. ! SELECT * FROM alter_extend_column;
  215. contains:altered
  216. #
  217. # Alter decimal
  218. > SELECT * from alter_decimal
  219. 123.45
  220. $ postgres-execute connection=postgres://postgres:postgres@postgres
  221. ALTER TABLE alter_decimal ALTER COLUMN f1 TYPE DECIMAL(6,1);
  222. ! SELECT * FROM alter_decimal;
  223. contains:altered
  224. #
  225. # Alter table rename
  226. > SELECT * from alter_table_rename;
  227. 1
  228. $ postgres-execute connection=postgres://postgres:postgres@postgres
  229. ALTER TABLE alter_table_rename RENAME TO alter_table_renamed;
  230. ! SELECT * FROM alter_table_rename;
  231. contains:altered
  232. #
  233. # Alter table rename colum
  234. > SELECT * FROM alter_table_rename_column;
  235. f1_orig f2_orig
  236. $ postgres-execute connection=postgres://postgres:postgres@postgres
  237. ALTER TABLE alter_table_rename_column RENAME COLUMN f1 TO f3;
  238. ALTER TABLE alter_table_rename_column RENAME COLUMN f2 TO f1;
  239. ALTER TABLE alter_table_rename_column RENAME COLUMN f3 TO f2;
  240. ! SELECT * FROM alter_table_rename_column;
  241. contains:altered
  242. #
  243. # Change column attnum
  244. > SELECT * from alter_table_change_attnum;
  245. f1_orig f2_orig
  246. # Ensure simpl name swap doesn't fool schema detection
  247. $ postgres-execute connection=postgres://postgres:postgres@postgres
  248. ALTER TABLE alter_table_change_attnum DROP COLUMN f2;
  249. ALTER TABLE alter_table_change_attnum ADD COLUMN f2 VARCHAR(10);
  250. ! SELECT * FROM alter_table_change_attnum;
  251. contains:altered
  252. > SELECT * from alter_table_supported;
  253. 1 1
  254. $ postgres-execute connection=postgres://postgres:postgres@postgres
  255. ALTER TABLE alter_table_supported ADD COLUMN f3 int;
  256. INSERT INTO alter_table_supported (f1, f2, f3) VALUES (2, 2, 2);
  257. > SELECT * from alter_table_supported;
  258. 1 1
  259. 2 2
  260. $ postgres-execute connection=postgres://postgres:postgres@postgres
  261. ALTER TABLE alter_table_supported DROP COLUMN f3;
  262. INSERT INTO alter_table_supported (f1, f2) VALUES (3, 3);
  263. > SELECT * from alter_table_supported;
  264. 1 1
  265. 2 2
  266. 3 3
  267. $ postgres-execute connection=postgres://postgres:postgres@postgres
  268. ALTER TABLE alter_table_supported DROP COLUMN f2;
  269. ! SELECT * from alter_table_supported;
  270. contains:altered
  271. #
  272. # Truncate table
  273. > SELECT * from truncate_table;
  274. 1 1
  275. $ postgres-execute connection=postgres://postgres:postgres@postgres
  276. TRUNCATE truncate_table;
  277. ! SELECT * FROM truncate_table;
  278. contains:table was truncated
  279. #
  280. # Drop table
  281. > SELECT * from drop_table;
  282. 1 1
  283. $ postgres-execute connection=postgres://postgres:postgres@postgres
  284. DROP TABLE drop_table;
  285. # Table is dropped
  286. ! SELECT * FROM drop_table;
  287. regex:(table was dropped|incompatible schema change)