-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
52997: sql: introduce a command to convert a database into a schema r=rohany a=rohany Fixes #50885. This commit introduces a command to convert a database into a user defined schema under a desired database. This command can be used by users who are currently emulating a Postgres style set of schemas in CockroachDB with separate databases. Release note (sql change): Users can now convert existing databases into schemas under other databases through the `ALTER DATABASE ... CONVERT TO SCHEMA UNDER PARENT ...` command. This command can only be run by `admin` and is only valid for databases that don't already have any child schemas other than `public`. 53078: stats: use leased types in the stats cache r=rohany a=rohany This commit switches the stats cache to use leased types rather than accessing types directly. This was the final user of the direct type access methods, so that code is removed as well. Release note: None 53137: sql: fix panic when resolving a target on non-existent db r=pbardea a=pbardea When trying to resolve a target on non-existent DB, we would have a nil deference panic due to a missing error check. Some ways that could have triggered this panic: - Resolving {types,sequences} - Renaming a table Release note (bug fix): Fix a crash that may occur when referencing a database that does not exist when trying to create a type, sequence or when renaming a table. Co-authored-by: Rohan Yadav <[email protected]> Co-authored-by: Paul Bardea <[email protected]>
- Loading branch information
Showing
22 changed files
with
462 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
statement ok | ||
SET experimental_enable_user_defined_schemas = true; | ||
SET experimental_enable_enums = true; | ||
|
||
statement ok | ||
CREATE DATABASE pgdatabase; | ||
CREATE TABLE pgdatabase.t1 (x INT PRIMARY KEY); | ||
CREATE DATABASE pgschema; | ||
CREATE TABLE pgschema.t1 (x INT); | ||
CREATE TABLE pgschema.t2 (x INT); | ||
CREATE TABLE pgschema.t3 (x INT PRIMARY KEY); | ||
ALTER TABLE pgschema.t3 ADD FOREIGN KEY (x) REFERENCES pgdatabase.t1 (x); -- references shouldn't be affected by reparenting. | ||
CREATE TYPE pgschema.typ AS ENUM ('schema'); | ||
|
||
let $db_id | ||
SELECT id FROM system.namespace WHERE name = 'pgschema' | ||
|
||
statement ok | ||
ALTER DATABASE pgschema CONVERT TO SCHEMA WITH PARENT pgdatabase | ||
|
||
query I | ||
SELECT * FROM pgdatabase.pgschema.t1 | ||
|
||
query I | ||
SELECT * FROM pgdatabase.pgschema.t2 | ||
|
||
query T | ||
SELECT 'schema'::pgdatabase.pgschema.typ | ||
---- | ||
schema | ||
|
||
statement error pq: insert on table "t3" violates foreign key constraint "fk_x_ref_t1" | ||
INSERT INTO pgdatabase.pgschema.t3 VALUES (1) | ||
|
||
# Assert there aren't any namespace entries left with the old parentID. | ||
query T | ||
SELECT name FROM system.namespace WHERE "parentID" = $db_id | ||
|
||
# We can't reparent a database that has any child schemas. | ||
statement ok | ||
CREATE DATABASE parent; | ||
USE parent; | ||
CREATE SCHEMA child; | ||
USE test; | ||
|
||
statement error pq: cannot convert database with schemas into schema | ||
ALTER DATABASE parent CONVERT TO SCHEMA WITH PARENT pgdatabase | ||
|
||
# We can't reparent a database if it causes a name conflict. | ||
statement ok | ||
CREATE DATABASE pgschema | ||
|
||
statement error pq: schema "pgschema" already exists | ||
ALTER DATABASE pgschema CONVERT TO SCHEMA WITH PARENT pgdatabase | ||
|
||
statement ok | ||
DROP DATABASE pgschema | ||
|
||
# We can't convert a database with an invalid schema name into a schema. | ||
statement ok | ||
CREATE DATABASE pg_temp | ||
|
||
statement error pq: unacceptable schema name "pg_temp" | ||
ALTER DATABASE pg_temp CONVERT TO SCHEMA WITH PARENT pgdatabase | ||
|
||
# We can't reparent a database that has any tables in use by views, | ||
# because we aren't able to rewrite those references. | ||
statement ok | ||
CREATE DATABASE with_views; | ||
CREATE TABLE with_views.t (x INT); | ||
CREATE VIEW with_views.v AS SELECT x FROM with_views.t | ||
|
||
statement error pq: could not convert database "with_views" into schema because "with_views.public.t" has dependent objects \[with_views.public.v\] | ||
ALTER DATABASE with_views CONVERT TO SCHEMA WITH PARENT pgdatabase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.