rename_database.slt 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. # Copyright 2015 - 2019 The Cockroach Authors. All rights reserved.
  2. # Copyright Materialize, Inc. and contributors. All rights reserved.
  3. #
  4. # Use of this software is governed by the Business Source License
  5. # included in the LICENSE file at the root of this repository.
  6. #
  7. # As of the Change Date specified in that file, in accordance with
  8. # the Business Source License, use of this software will be governed
  9. # by the Apache License, Version 2.0.
  10. #
  11. # This file is derived from the logic test suite in CockroachDB. The
  12. # original file was retrieved on June 10, 2019 from:
  13. #
  14. # https://github.com/cockroachdb/cockroach/blob/d2f7fbf5dd1fc1a099bbad790a2e1f7c60a66cc3/pkg/sql/logictest/testdata/logic_test/rename_database
  15. #
  16. # The original source code is subject to the terms of the Apache
  17. # 2.0 license, a copy of which can be found in the LICENSE file at the
  18. # root of this repository.
  19. # not supported yet
  20. halt
  21. mode cockroach
  22. query T
  23. SHOW DATABASES
  24. ----
  25. materialize
  26. postgres
  27. system
  28. test
  29. query TTTT
  30. SHOW GRANTS ON DATABASE test
  31. ----
  32. test crdb_internal admin ALL
  33. test crdb_internal root ALL
  34. test information_schema admin ALL
  35. test information_schema root ALL
  36. test pg_catalog admin ALL
  37. test pg_catalog root ALL
  38. test public admin ALL
  39. test public root ALL
  40. statement ok
  41. CREATE TABLE kv (
  42. k INT PRIMARY KEY,
  43. v INT
  44. )
  45. statement ok
  46. INSERT INTO kv VALUES (1, 2), (3, 4), (5, 6), (7, 8)
  47. query II rowsort
  48. SELECT * FROM kv
  49. ----
  50. 1 2
  51. 3 4
  52. 5 6
  53. 7 8
  54. statement ok
  55. SET sql_safe_updates = TRUE;
  56. statement error RENAME DATABASE on current database
  57. ALTER DATABASE test RENAME TO u
  58. statement ok
  59. SET sql_safe_updates = FALSE;
  60. ALTER DATABASE test RENAME TO u
  61. statement error pgcode 42P01 relation "kv" does not exist
  62. SELECT * FROM kv
  63. statement error target database or schema does not exist
  64. SHOW GRANTS ON DATABASE test
  65. query T
  66. SHOW DATABASES
  67. ----
  68. materialize
  69. postgres
  70. system
  71. u
  72. # check the name in descriptor is also changed
  73. query TTTT
  74. SHOW GRANTS ON DATABASE u
  75. ----
  76. u crdb_internal admin ALL
  77. u crdb_internal root ALL
  78. u information_schema admin ALL
  79. u information_schema root ALL
  80. u pg_catalog admin ALL
  81. u pg_catalog root ALL
  82. u public admin ALL
  83. u public root ALL
  84. statement ok
  85. SET DATABASE = u
  86. query II rowsort
  87. SELECT * FROM kv
  88. ----
  89. 1 2
  90. 3 4
  91. 5 6
  92. 7 8
  93. statement error empty database name
  94. ALTER DATABASE "" RENAME TO u
  95. statement error empty database name
  96. ALTER DATABASE u RENAME TO ""
  97. statement ok
  98. ALTER DATABASE u RENAME TO u
  99. statement ok
  100. CREATE DATABASE t
  101. statement error the new database name "u" already exists
  102. ALTER DATABASE t RENAME TO u
  103. statement ok
  104. GRANT ALL ON DATABASE t TO testuser
  105. user testuser
  106. statement error only superusers are allowed to ALTER DATABASE ... RENAME
  107. ALTER DATABASE t RENAME TO v
  108. query T
  109. SHOW DATABASES
  110. ----
  111. t
  112. user root
  113. # Test that renames aren't allowed while views refer to any of a DB's tables,
  114. # both for views in that database and for views in a different database.
  115. statement ok
  116. CREATE VIEW t.v AS SELECT k,v FROM u.kv
  117. query T
  118. SHOW TABLES FROM u
  119. ----
  120. kv
  121. statement error cannot rename database because view "t.public.v" depends on table "kv"
  122. ALTER DATABASE u RENAME TO v
  123. statement ok
  124. DROP VIEW t.v
  125. statement ok
  126. ALTER DATABASE u RENAME TO v
  127. statement ok
  128. CREATE VIEW v.v AS SELECT k,v FROM v.kv
  129. statement error cannot rename database because view "v" depends on table "kv"
  130. ALTER DATABASE v RENAME TO u
  131. # Check that the default databases can be renamed like any other.
  132. statement ok
  133. ALTER DATABASE materialize RENAME TO w;
  134. ALTER DATABASE postgres RENAME TO materialize;
  135. ALTER DATABASE w RENAME TO postgres
  136. query T
  137. SHOW DATABASES
  138. ----
  139. materialize
  140. postgres
  141. system
  142. t
  143. v
  144. query TTT
  145. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR ALTER DATABASE v RENAME TO x
  146. ----
  147. rename database · ·
  148. # Verify that the EXPLAIN above does not actually rename the database (cockroach#30543)
  149. query T colnames
  150. SHOW DATABASES
  151. ----
  152. database_name
  153. materialize
  154. postgres
  155. system
  156. t
  157. v