swap_cluster.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. class SwapCluster(Check):
  13. def initialize(self) -> Testdrive:
  14. return Testdrive(
  15. dedent(
  16. """
  17. > CREATE TABLE swap_cluster1_table (f1 INTEGER);
  18. > CREATE TABLE swap_cluster2_table (f1 INTEGER);
  19. > CREATE TABLE swap_cluster3_table (f1 INTEGER);
  20. > CREATE TABLE swap_cluster4_table (f1 INTEGER);
  21. > INSERT INTO swap_cluster1_table VALUES (123);
  22. > INSERT INTO swap_cluster2_table VALUES (234);
  23. > INSERT INTO swap_cluster3_table VALUES (345);
  24. > INSERT INTO swap_cluster4_table VALUES (456);
  25. > CREATE CLUSTER swap_cluster1 REPLICAS (replica1 (SIZE '2-2'));
  26. > CREATE CLUSTER swap_cluster2 REPLICAS (replica1 (SIZE '2-2'));
  27. > CREATE CLUSTER swap_cluster3 REPLICAS (replica1 (SIZE '2-2'));
  28. > CREATE CLUSTER swap_cluster4 REPLICAS (replica1 (SIZE '2-2'));
  29. > SET cluster=swap_cluster1
  30. > CREATE DEFAULT INDEX ON swap_cluster1_table;
  31. > CREATE MATERIALIZED VIEW swap_cluster1_view AS SELECT SUM(f1) FROM swap_cluster1_table;
  32. > SET cluster=swap_cluster2
  33. > CREATE DEFAULT INDEX ON swap_cluster2_table;
  34. > CREATE MATERIALIZED VIEW swap_cluster2_view AS SELECT SUM(f1) FROM swap_cluster2_table;
  35. > SET cluster=swap_cluster3
  36. > CREATE DEFAULT INDEX ON swap_cluster3_table;
  37. > CREATE MATERIALIZED VIEW swap_cluster3_view AS SELECT SUM(f1) FROM swap_cluster3_table;
  38. > SET cluster=swap_cluster4
  39. > CREATE DEFAULT INDEX ON swap_cluster4_table;
  40. > CREATE MATERIALIZED VIEW swap_cluster4_view AS SELECT SUM(f1) FROM swap_cluster4_table;
  41. """
  42. )
  43. )
  44. def manipulate(self) -> list[Testdrive]:
  45. return [
  46. Testdrive(dedent(s))
  47. for s in [
  48. """
  49. > ALTER CLUSTER swap_cluster1 SWAP WITH swap_cluster2;
  50. """,
  51. """
  52. > ALTER CLUSTER swap_cluster3 SWAP WITH swap_cluster4;
  53. """,
  54. ]
  55. ]
  56. def validate(self) -> Testdrive:
  57. return Testdrive(
  58. dedent(
  59. """
  60. > SET cluster=swap_cluster1
  61. > SET cluster=swap_cluster2
  62. > SET cluster=swap_cluster3
  63. > SET cluster=swap_cluster4
  64. > SET cluster=default
  65. > SELECT * FROM swap_cluster1_table;
  66. 123
  67. > SELECT * FROM swap_cluster1_view;
  68. 123
  69. > SELECT * FROM swap_cluster2_table;
  70. 234
  71. > SELECT * FROM swap_cluster2_view;
  72. 234
  73. > SELECT * FROM swap_cluster3_table;
  74. 345
  75. > SELECT * FROM swap_cluster3_view;
  76. 345
  77. > SELECT * FROM swap_cluster4_table;
  78. 456
  79. > SELECT * FROM swap_cluster4_view;
  80. 456
  81. """
  82. )
  83. )