swap_schema.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 SwapSchema(Check):
  13. def initialize(self) -> Testdrive:
  14. return Testdrive(
  15. dedent(
  16. """
  17. > CREATE SCHEMA swap_me1;
  18. > CREATE SCHEMA swap_me2;
  19. > CREATE SCHEMA swap_me3;
  20. > CREATE SCHEMA swap_me4;
  21. > CREATE TABLE swap_me1.t1 (f1 INTEGER);
  22. > CREATE TABLE swap_me2.t2 (f1 INTEGER);
  23. > CREATE TABLE swap_me3.t3 (f1 INTEGER);
  24. > CREATE TABLE swap_me4.t4 (f1 INTEGER);
  25. > INSERT INTO swap_me1.t1 VALUES (1);
  26. > INSERT INTO swap_me2.t2 VALUES (2);
  27. > INSERT INTO swap_me3.t3 VALUES (3);
  28. > INSERT INTO swap_me4.t4 VALUES (4);
  29. """
  30. )
  31. )
  32. def manipulate(self) -> list[Testdrive]:
  33. return [
  34. Testdrive(dedent(s))
  35. for s in [
  36. """
  37. > ALTER SCHEMA swap_me1 SWAP WITH swap_me2;
  38. """,
  39. """
  40. > ALTER SCHEMA swap_me3 SWAP WITH swap_me4;
  41. """,
  42. ]
  43. ]
  44. def validate(self) -> Testdrive:
  45. return Testdrive(
  46. dedent(
  47. """
  48. > SHOW SCHEMAS LIKE 'swap_me%';
  49. swap_me1 ""
  50. swap_me2 ""
  51. swap_me3 ""
  52. swap_me4 ""
  53. > SET SCHEMA = swap_me1;
  54. > SELECT * FROM t2;
  55. 2
  56. > SET SCHEMA = swap_me2;
  57. > SELECT * FROM t1;
  58. 1
  59. > SET SCHEMA = swap_me3;
  60. > SELECT * FROM t4;
  61. 4
  62. > SET SCHEMA = swap_me4;
  63. > SELECT * FROM t3;
  64. 3
  65. """
  66. )
  67. )