alter_table.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. from textwrap import dedent
  10. from materialize.checks.actions import Testdrive
  11. from materialize.checks.checks import Check
  12. from materialize.checks.executors import Executor
  13. from materialize.mz_version import MzVersion
  14. class AlterTableAddColumn(Check):
  15. def _can_run(self, e: Executor) -> bool:
  16. return self.base_version >= MzVersion.parse_mz("v0.134.0-dev")
  17. def initialize(self) -> Testdrive:
  18. return Testdrive(
  19. # Note(parkmycar): We want to make sure to ALTER a table in the initialize to exercise
  20. # how ALTER-ed tables are handled on a restart of Materialize.
  21. dedent(
  22. """
  23. $postgres-execute connection=postgres://mz_system:materialize@${testdrive.materialize-internal-sql-addr}
  24. ALTER SYSTEM SET enable_alter_table_add_column = true;
  25. > CREATE TABLE alter_table1 (f1 INTEGER, f2 INTEGER NOT NULL DEFAULT 1234);
  26. > INSERT INTO alter_table1 VALUES (100, 100);
  27. > CREATE VIEW alter_table_view_1 AS SELECT * FROM alter_table1;
  28. > CREATE TABLE alter_table2 (f1 text);
  29. > INSERT INTO alter_table2 VALUES ('hello');
  30. > ALTER TABLE alter_table2 ADD COLUMN f2 int;
  31. > INSERT INTO alter_table2 VALUES ('world', 1);
  32. """
  33. )
  34. )
  35. def manipulate(self) -> list[Testdrive]:
  36. return [
  37. Testdrive(dedent(s))
  38. for s in [
  39. """
  40. $postgres-execute connection=postgres://mz_system:materialize@${testdrive.materialize-internal-sql-addr}
  41. ALTER SYSTEM SET enable_alter_table_add_column = true;
  42. > ALTER TABLE alter_table1 ADD COLUMN f3 text;
  43. > INSERT INTO alter_table1 VALUES (200, 200, 'hello'), (NULL, 300, 'world'), (400, 400, NULL), (NULL, 500, NULL);
  44. > CREATE MATERIALIZED VIEW alter_table_mv1 AS SELECT alter_table2.f1 as f1_2, alter_table1.f1 as f1_1 FROM alter_table1, alter_table2;
  45. > CREATE INDEX alter_table1_idx ON alter_table1 (f1);
  46. """,
  47. """
  48. $postgres-execute connection=postgres://mz_system:materialize@${testdrive.materialize-internal-sql-addr}
  49. ALTER SYSTEM SET enable_alter_table_add_column = true;
  50. > INSERT INTO alter_table2 VALUES ('foo', 900), ('bar', 800);
  51. > CREATE VIEW alter_table_view_2 AS SELECT * FROM alter_table2;
  52. """,
  53. ]
  54. ]
  55. def validate(self) -> Testdrive:
  56. return Testdrive(
  57. dedent(
  58. """
  59. > SELECT * FROM alter_table1 ORDER BY f1, f2 ASC;
  60. 100 100 <null>
  61. 200 200 hello
  62. 400 400 <null>
  63. <null> 300 world
  64. <null> 500 <null>
  65. > SELECT * FROM alter_table_view_1 ORDER BY f1, f2 ASC;
  66. 100 100
  67. 200 200
  68. 400 400
  69. <null> 300
  70. <null> 500
  71. > SELECT * FROM alter_table_mv1;
  72. bar 100
  73. bar 200
  74. bar 400
  75. bar <null>
  76. bar <null>
  77. foo 100
  78. foo 200
  79. foo 400
  80. foo <null>
  81. foo <null>
  82. hello 100
  83. hello 200
  84. hello 400
  85. hello <null>
  86. hello <null>
  87. world 100
  88. world 200
  89. world 400
  90. world <null>
  91. world <null>
  92. > SELECT * FROM alter_table2 ORDER BY f1, f2 ASC;
  93. bar 800
  94. foo 900
  95. hello <null>
  96. world 1
  97. """
  98. )
  99. )