feat(database): create and drop databases and schemas from the tree (#4201)

The Database tree gets New > Database... and New > Schema..., and Drop... on database nodes (PostgreSQL, MySQL/MariaDB schemas, SQL Server), each with a preview of the statement it runs.

Creating closes the IDE's own connections to the database the server copies (the template on PostgreSQL, model on SQL Server), which both refuse to copy while it has sessions. Dropping closes the IDE's pools and sessions of that database first and runs DROP DATABASE on the data source's default database; "Close the sessions of other clients" adds WITH (FORCE) on PostgreSQL 13+ and single-user mode on SQL Server. Drop is refused for the data source's own database and read-only data sources. Both go through withConnection, so a password the data source does not save is asked for first.

Closes #3189

Co-authored-by: EjaYF <[email protected]>
Цей коміт міститься в:
Ernstjan FreriksтаEjaYF authored and GitHub committed 2026-11-18 18:17:18 +08:00
1 джерело bb243909bf
коміт e689c95e93
19 files changed
+581 -10

No files matched your search

+50
Переглянути файл
@@ -4,8 +4,12 @@ import {
alterTableSql,
canDrop,
canModifyTable,
createDatabaseSql,
createSchemaSql,
createTableSql,
draftOf,
dropDatabaseSql,
templateOf,
dropSql,
ident,
reference,
@@ -341,3 +345,49 @@ describe("snowflake object SQL", () => {
expect(alterTableSql("snowflake", "HR", "EMP", "EMP", dropped)).toEqual(["alter table HR.EMP drop column NAME;"]);
});
});
describe("database and schema SQL", () => {
it("creates a PostgreSQL database with only the options given", () => {
expect(createDatabaseSql("postgres", "shop", {})).toBe("create database shop;");
expect(
createDatabaseSql("postgres", "Shop Copy", {
owner: "app",
template: "template0",
encoding: "UTF8",
tablespace: " ",
}),
).toBe(`create database "Shop Copy" owner app template template0 encoding 'UTF8';`);
expect(createDatabaseSql("postgres", "shop", { encoding: "it's" })).toBe("create database shop encoding 'it''s';");
});
it("names the database the server copies for a new one", () => {
expect(templateOf("postgres", {})).toBe("template1");
expect(templateOf("postgres", { template: "template0" })).toBe("template0");
expect(templateOf("sqlServer", { template: "ignored" })).toBe("model");
});
it("creates a SQL Server database with a collation", () => {
expect(createDatabaseSql("sqlServer", "shop", { collation: "Latin1_General_CI_AS", owner: "ignored" })).toBe(
"create database shop collate Latin1_General_CI_AS;",
);
});
it("creates a schema with an owner, and a MySQL schema as a database", () => {
expect(createSchemaSql("postgres", "sales", {})).toBe("create schema sales;");
expect(createSchemaSql("postgres", "sales", { owner: "app" })).toBe("create schema sales authorization app;");
expect(createSchemaSql("sqlServer", "sales", { owner: "dbo" })).toBe("create schema sales authorization dbo;");
expect(createSchemaSql("mysql", "shop", { charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci" })).toBe(
"create database shop character set utf8mb4 collate utf8mb4_0900_ai_ci;",
);
expect(createSchemaSql("mysql", "shop", {})).toBe("create database shop;");
});
it("drops a database, forcing out other sessions when asked", () => {
expect(dropDatabaseSql("postgres", "shop", false)).toBe("drop database shop;");
expect(dropDatabaseSql("postgres", "shop", true)).toBe("drop database shop with (force);");
expect(dropDatabaseSql("sqlServer", "shop", false)).toBe("drop database shop;");
expect(dropDatabaseSql("sqlServer", "shop", true)).toBe(
"alter database shop set single_user with rollback immediate;\ndrop database shop;",
);
});
});