rename_index.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 RenameIndex(Check):
  13. def initialize(self) -> Testdrive:
  14. return Testdrive(
  15. dedent(
  16. """
  17. > CREATE TABLE rename_index_table (f1 INTEGER,f2 INTEGER);
  18. > CREATE INDEX rename_index_index1 ON rename_index_table (f2);
  19. > INSERT INTO rename_index_table VALUES (1,1);
  20. > CREATE MATERIALIZED VIEW rename_index_view1 AS SELECT f2 FROM rename_index_table WHERE f2 > 0;
  21. """
  22. )
  23. )
  24. def manipulate(self) -> list[Testdrive]:
  25. return [
  26. Testdrive(dedent(s))
  27. for s in [
  28. """
  29. > INSERT INTO rename_index_table VALUES (2,2);
  30. > ALTER INDEX rename_index_index1 RENAME TO rename_index_index2;
  31. > CREATE INDEX rename_index_index1 ON rename_index_table (f2);
  32. > CREATE MATERIALIZED VIEW rename_index_view2 AS SELECT f2 FROM rename_index_table WHERE f2 > 0;
  33. > INSERT INTO rename_index_table VALUES (3,3);
  34. """,
  35. """
  36. > INSERT INTO rename_index_table VALUES (4,4);
  37. > CREATE MATERIALIZED VIEW rename_index_view3 AS SELECT f2 FROM rename_index_table WHERE f2 > 0;
  38. > ALTER INDEX rename_index_index2 RENAME TO rename_index_index3;
  39. > ALTER INDEX rename_index_index1 RENAME TO rename_index_index2;
  40. > INSERT INTO rename_index_table VALUES (5,5);
  41. """,
  42. ]
  43. ]
  44. def validate(self) -> Testdrive:
  45. return Testdrive(
  46. dedent(
  47. f"""
  48. > SHOW INDEXES ON rename_index_table;
  49. rename_index_index2 rename_index_table {self._default_cluster()} {{f2}} ""
  50. rename_index_index3 rename_index_table {self._default_cluster()} {{f2}} ""
  51. > SELECT * FROM rename_index_view1;
  52. 1
  53. 2
  54. 3
  55. 4
  56. 5
  57. > SELECT * FROM rename_index_view2;
  58. 1
  59. 2
  60. 3
  61. 4
  62. 5
  63. > SELECT * FROM rename_index_view3;
  64. 1
  65. 2
  66. 3
  67. 4
  68. 5
  69. """
  70. )
  71. )