alter-table-after-source.td 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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
  86. FROM POSTGRES CONNECTION pg (PUBLICATION 'mz_source')
  87. FOR ALL TABLES;
  88. #
  89. # Add column
  90. > SELECT * FROM add_columns;
  91. 1
  92. $ postgres-execute connection=postgres://postgres:postgres@postgres
  93. ALTER TABLE add_columns ADD COLUMN f2 varchar(2);
  94. INSERT INTO add_columns VALUES (2, 'ab');
  95. > SELECT * from add_columns;
  96. 1
  97. 2
  98. #
  99. # Remove column
  100. > SELECT * from remove_column;
  101. 2 ab
  102. $ postgres-execute connection=postgres://postgres:postgres@postgres
  103. ALTER TABLE remove_column DROP COLUMN f2;
  104. ! SELECT * from remove_column;
  105. contains:altered
  106. #
  107. # Alter column type
  108. > SELECT * from alter_column;
  109. 2 ab
  110. $ postgres-execute connection=postgres://postgres:postgres@postgres
  111. ALTER TABLE alter_column ALTER COLUMN f2 TYPE CHAR(2);
  112. ! SELECT * from alter_column;
  113. contains:altered
  114. #
  115. # Drop NOT NULL
  116. > SELECT * from alter_drop_nullability
  117. 1
  118. $ postgres-execute connection=postgres://postgres:postgres@postgres
  119. ALTER TABLE alter_drop_nullability ALTER COLUMN f1 DROP NOT NULL;
  120. ! SELECT * FROM alter_drop_nullability WHERE f1 IS NOT NULL;
  121. contains:altered
  122. # We have guaranteed that this column is not null so the optimizer eagerly
  123. # returns the empty set.
  124. > SELECT * FROM alter_drop_nullability WHERE f1 IS NULL;
  125. #
  126. # Add NOT NULL
  127. > SELECT * from alter_add_nullability
  128. 1
  129. $ postgres-execute connection=postgres://postgres:postgres@postgres
  130. ALTER TABLE alter_add_nullability ALTER COLUMN f1 SET NOT NULL;
  131. INSERT INTO alter_add_nullability VALUES (1);
  132. > SELECT * FROM alter_add_nullability;
  133. 1
  134. 1
  135. #
  136. # Drop PK
  137. > SELECT * from alter_drop_pk
  138. 1
  139. $ postgres-execute connection=postgres://postgres:postgres@postgres
  140. ALTER TABLE alter_drop_pk DROP CONSTRAINT alter_drop_pk_pkey;
  141. ! SELECT f1 FROM alter_drop_pk;
  142. contains:altered
  143. #
  144. # Add PK
  145. > SELECT * from alter_add_pk
  146. 1
  147. $ postgres-execute connection=postgres://postgres:postgres@postgres
  148. ALTER TABLE alter_add_pk ADD PRIMARY KEY(f1);
  149. INSERT INTO alter_add_pk VALUES (2);
  150. > SELECT * FROM alter_add_pk;
  151. 1
  152. 2
  153. #
  154. # Cycle PK
  155. > SELECT * from alter_cycle_pk
  156. 1
  157. $ postgres-execute connection=postgres://postgres:postgres@postgres
  158. ALTER TABLE alter_cycle_pk DROP CONSTRAINT alter_cycle_pk_pkey;
  159. ALTER TABLE alter_cycle_pk ADD PRIMARY KEY(f1);
  160. ! SELECT * FROM alter_cycle_pk;
  161. contains:altered
  162. #
  163. # Cycle PK off (no pk, pk, no pk)
  164. > SELECT * from alter_cycle_pk_off
  165. 1
  166. $ postgres-execute connection=postgres://postgres:postgres@postgres
  167. ALTER TABLE alter_cycle_pk_off ADD PRIMARY KEY(f1);
  168. ALTER TABLE alter_cycle_pk_off DROP CONSTRAINT alter_cycle_pk_off_pkey;
  169. INSERT INTO alter_cycle_pk_off VALUES (1);
  170. > SELECT * FROM alter_cycle_pk_off;
  171. 1
  172. 1
  173. #
  174. # Drop unique
  175. > SELECT * from alter_drop_unique
  176. 1
  177. $ postgres-execute connection=postgres://postgres:postgres@postgres
  178. ALTER TABLE alter_drop_unique DROP CONSTRAINT alter_drop_unique_f1_key;
  179. ! SELECT f1 FROM alter_drop_unique;
  180. contains:altered
  181. #
  182. # Add unique
  183. > SELECT * from alter_add_unique
  184. 1
  185. $ postgres-execute connection=postgres://postgres:postgres@postgres
  186. ALTER TABLE alter_add_unique ADD UNIQUE(f1);
  187. INSERT INTO alter_add_unique VALUES (2);
  188. > SELECT * FROM alter_add_unique;
  189. 1
  190. 2
  191. #
  192. # Extend column
  193. > SELECT * from alter_extend_column
  194. ab
  195. $ postgres-execute connection=postgres://postgres:postgres@postgres
  196. ALTER TABLE alter_extend_column ALTER COLUMN f1 TYPE VARCHAR(20);
  197. ! SELECT * FROM alter_extend_column;
  198. contains:altered
  199. #
  200. # Alter decimal
  201. > SELECT * from alter_decimal
  202. 123.45
  203. $ postgres-execute connection=postgres://postgres:postgres@postgres
  204. ALTER TABLE alter_decimal ALTER COLUMN f1 TYPE DECIMAL(6,1);
  205. ! SELECT * FROM alter_decimal;
  206. contains:altered
  207. #
  208. # Alter table rename
  209. > SELECT * from alter_table_rename;
  210. 1
  211. $ postgres-execute connection=postgres://postgres:postgres@postgres
  212. ALTER TABLE alter_table_rename RENAME TO alter_table_renamed;
  213. ! SELECT * FROM alter_table_rename;
  214. contains:altered
  215. #
  216. # Alter table rename column
  217. > SELECT * FROM alter_table_rename_column;
  218. f1_orig f2_orig
  219. $ postgres-execute connection=postgres://postgres:postgres@postgres
  220. ALTER TABLE alter_table_rename_column RENAME COLUMN f1 TO f3;
  221. ALTER TABLE alter_table_rename_column RENAME COLUMN f2 TO f1;
  222. ALTER TABLE alter_table_rename_column RENAME COLUMN f3 TO f2;
  223. ! SELECT * FROM alter_table_rename_column;
  224. contains:altered
  225. #
  226. # Change column attnum
  227. > SELECT * from alter_table_change_attnum;
  228. f1_orig f2_orig
  229. # Ensure simpl name swap doesn't fool schema detection
  230. $ postgres-execute connection=postgres://postgres:postgres@postgres
  231. ALTER TABLE alter_table_change_attnum DROP COLUMN f2;
  232. ALTER TABLE alter_table_change_attnum ADD COLUMN f2 VARCHAR(10);
  233. ! SELECT * FROM alter_table_change_attnum;
  234. contains:altered
  235. > SELECT * from alter_table_supported;
  236. 1 1
  237. $ postgres-execute connection=postgres://postgres:postgres@postgres
  238. ALTER TABLE alter_table_supported ADD COLUMN f3 int;
  239. INSERT INTO alter_table_supported (f1, f2, f3) VALUES (2, 2, 2);
  240. > SELECT * from alter_table_supported;
  241. 1 1
  242. 2 2
  243. $ postgres-execute connection=postgres://postgres:postgres@postgres
  244. ALTER TABLE alter_table_supported DROP COLUMN f3;
  245. INSERT INTO alter_table_supported (f1, f2) VALUES (3, 3);
  246. > SELECT * from alter_table_supported;
  247. 1 1
  248. 2 2
  249. 3 3
  250. $ postgres-execute connection=postgres://postgres:postgres@postgres
  251. ALTER TABLE alter_table_supported DROP COLUMN f2;
  252. ! SELECT * from alter_table_supported;
  253. contains:altered
  254. #
  255. # Truncate table
  256. > SELECT * from truncate_table;
  257. 1 1
  258. $ postgres-execute connection=postgres://postgres:postgres@postgres
  259. TRUNCATE truncate_table;
  260. ! SELECT * FROM truncate_table;
  261. contains:table was truncated
  262. #
  263. # Drop table
  264. > SELECT * from drop_table;
  265. 1 1
  266. $ postgres-execute connection=postgres://postgres:postgres@postgres
  267. DROP TABLE drop_table;
  268. # Table is dropped
  269. ! SELECT * FROM drop_table;
  270. regex:(table was dropped|incompatible schema change)