databases.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 CheckDatabaseCreate(Check):
  13. def manipulate(self) -> list[Testdrive]:
  14. return [
  15. Testdrive(dedent(s))
  16. for s in [
  17. """
  18. $ postgres-execute connection=postgres://mz_system@${testdrive.materialize-internal-sql-addr}
  19. GRANT CREATEDB ON SYSTEM TO materialize
  20. > CREATE DATABASE to_be_created1;
  21. > SET DATABASE=to_be_created1;
  22. > CREATE TABLE t1 (f1 INTEGER);
  23. > INSERT INTO t1 VALUES (1);
  24. """,
  25. """
  26. $ postgres-execute connection=postgres://mz_system@${testdrive.materialize-internal-sql-addr}
  27. GRANT CREATEDB ON SYSTEM TO materialize
  28. > CREATE DATABASE to_be_created2;
  29. > SET DATABASE=to_be_created2;
  30. > CREATE TABLE t1 (f1 INTEGER);
  31. > INSERT INTO t1 VALUES (2);
  32. """,
  33. ]
  34. ]
  35. def validate(self) -> Testdrive:
  36. return Testdrive(
  37. dedent(
  38. """
  39. > SHOW DATABASES LIKE 'to_be_created%';
  40. to_be_created1 ""
  41. to_be_created2 ""
  42. > SET DATABASE=to_be_created1;
  43. > SELECT * FROM t1;
  44. 1
  45. > CREATE TABLE t2 (f1 INTEGER);
  46. > INSERT INTO t2 VALUES (1);
  47. > SELECT * FROM t2;
  48. 1
  49. > DROP TABLE t2;
  50. > SET DATABASE=to_be_created2;
  51. > SELECT * FROM t1;
  52. 2
  53. > CREATE TABLE t2 (f1 INTEGER);
  54. > INSERT INTO t2 VALUES (1);
  55. > SELECT * FROM t2;
  56. 1
  57. > DROP TABLE t2;
  58. """
  59. )
  60. )
  61. class CheckDatabaseDrop(Check):
  62. def manipulate(self) -> list[Testdrive]:
  63. return [
  64. Testdrive(dedent(s))
  65. for s in [
  66. """
  67. > CREATE DATABASE to_be_dropped;
  68. > SET DATABASE=to_be_dropped;
  69. > CREATE TABLE t1 (f1 INTEGER);
  70. """,
  71. """
  72. > DROP DATABASE to_be_dropped CASCADE;
  73. """,
  74. ]
  75. ]
  76. def validate(self) -> Testdrive:
  77. return Testdrive(
  78. dedent(
  79. """
  80. > SET DATABASE=to_be_dropped;
  81. ! SELECT * FROM t1;
  82. contains: unknown catalog item
  83. """
  84. )
  85. )