25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(oscar): preserve comments when duplicating tables
lxk955f3872d9c8444
diff --git a/apps/desktop/src/lib/__tests__/database/dbAdminSql.spec.ts b/apps/desktop/src/lib/__tests__/database/dbAdminSql.spec.tsindex e00452f77e..c870b7b653 100644--- a/apps/desktop/src/lib/__tests__/database/dbAdminSql.spec.ts+++ b/apps/desktop/src/lib/__tests__/database/dbAdminSql.spec.ts@@ -486,6 +486,73 @@ describe("buildDuplicateTableStructurePlan", () => {expect(apiMock.buildDuplicateTableStructureSql).not.toHaveBeenCalled();});+ it("loads Oscar source column comments and keeps table and column comments in the clone plan", async () => {+ const columns = [+ { name: "ID", comment: null, data_type: "int", is_nullable: false, column_default: null, is_primary_key: true },+ { name: "NAME", comment: " 用户's;姓名 ", data_type: "varchar(50)", is_nullable: true, column_default: null, is_primary_key: false },+ { name: "BLANK", comment: " \t\n ", data_type: "varchar(50)", is_nullable: true, column_default: null, is_primary_key: false },+ ];+ const sql = 'CREATE TABLE "SYSDBA"."USERS_COPY" AS SELECT * FROM "SYSDBA"."USERS" WHERE 1=0;\nCOMMENT ON TABLE "SYSDBA"."USERS_COPY" IS \' 用户\'\'s;表 \';\nCOMMENT ON COLUMN "SYSDBA"."USERS_COPY"."NAME" IS \' 用户\'\'s;姓名 \';';+ apiMock.getColumns.mockResolvedValue(columns);+ apiMock.buildDuplicateTableStructureSql.mockResolvedValue(sql);++ const plan = await buildDuplicateTableStructurePlan({+ connectionId: "oscar-1",+ database: "osrdb",+ catalog: "catalog",+ databaseType: "oscar",+ schema: "SYSDBA",+ sourceName: "USERS",+ targetName: "USERS_COPY",+ tableComment: " 用户's;表 ",+ identifierQuote: '"',+ });++ expect(apiMock.getColumns).toHaveBeenCalledExactlyOnceWith("oscar-1", "osrdb", "SYSDBA", "USERS", "catalog");+ expect(apiMock.buildDuplicateTableStructureSql).toHaveBeenCalledExactlyOnceWith({+ databaseType: "oscar",+ schema: "SYSDBA",+ sourceName: "USERS",+ targetName: "USERS_COPY",+ tableComment: " 用户's;表 ",+ columnComments: [{ name: "NAME", comment: " 用户's;姓名 " }],+ identifierQuote: '"',+ });+ expect(plan).toEqual({ sql, sourceColumns: columns, executeAsScript: true });+ expect(apiMock.listIndexes).not.toHaveBeenCalled();+ expect(apiMock.buildCreateTableSql).not.toHaveBeenCalled();+ expect(apiMock.getTableComment).not.toHaveBeenCalled();+ });++ it.each([null, "", " \t\n "])("reuses Oscar columns and keeps absent comments optional: %j", async (comment) => {+ const columns = [{ name: "note", comment, data_type: "text", is_nullable: true, column_default: null, is_primary_key: false }];+ const sql = 'CREATE TABLE "copy" AS SELECT * FROM "source" WHERE 1=0;';+ apiMock.buildDuplicateTableStructureSql.mockResolvedValue(sql);++ const plan = await buildDuplicateTableStructurePlan({+ connectionId: "oscar-1",+ database: "osrdb",+ databaseType: "oscar",+ sourceName: "source",+ targetName: "copy",+ tableComment: comment,+ sourceColumns: columns,+ });++ expect(apiMock.getColumns).not.toHaveBeenCalled();+ expect(apiMock.buildDuplicateTableStructureSql).toHaveBeenCalledWith(expect.objectContaining({ tableComment: comment, columnComments: [] }));+ expect(plan).toEqual({ sql, sourceColumns: columns, executeAsScript: false });+ });++ it("does not silently clone Oscar without comments when column metadata fails", async () => {+ const error = new Error("column metadata unavailable");+ apiMock.getColumns.mockRejectedValueOnce(error);++ await expect(buildDuplicateTableStructurePlan({ connectionId: "oscar-1", database: "osrdb", databaseType: "oscar", sourceName: "source", targetName: "copy" })).rejects.toBe(error);+ expect(apiMock.getColumns).toHaveBeenCalledExactlyOnceWith("oscar-1", "osrdb", "", "source", undefined);+ expect(apiMock.buildDuplicateTableStructureSql).not.toHaveBeenCalled();+ });+it.each(["postgres", "mysql", "highgo", "kingbase", undefined] as const)("keeps %s clones on the generic path", async (databaseType) => {apiMock.buildDuplicateTableStructureSql.mockResolvedValue('CREATE TABLE "copy" (LIKE "source" INCLUDING ALL);');diff --git a/apps/desktop/src/lib/database/dbAdminSql.ts b/apps/desktop/src/lib/database/dbAdminSql.tsindex 3e45649b1d..a40f1c5b37 100644--- a/apps/desktop/src/lib/database/dbAdminSql.ts+++ b/apps/desktop/src/lib/database/dbAdminSql.ts@@ -330,7 +330,7 @@ export async function buildDuplicateTableStructurePlan(options: DuplicateTableSt}let sourceColumns = options.sourceColumns;- if (options.databaseType === "vastbase") {+ if (options.databaseType === "vastbase" || options.databaseType === "oscar") {sourceColumns ??= await api.getColumns(options.connectionId, options.database, options.schema || "", options.sourceName, options.catalog);}const sql = await buildDuplicateTableStructureSql({@@ -339,7 +339,7 @@ export async function buildDuplicateTableStructurePlan(options: DuplicateTableStsourceName: options.sourceName,targetName: options.targetName,tableComment: options.tableComment,- columnComments: options.databaseType === "vastbase" ? collectDuplicateTableColumnComments(sourceColumns ?? []) : [],+ columnComments: options.databaseType === "vastbase" || options.databaseType === "oscar" ? collectDuplicateTableColumnComments(sourceColumns ?? []) : [],identifierQuote: options.identifierQuote,});return { sql, sourceColumns, executeAsScript: duplicateTableStructureRequiresScript(sql) };diff --git a/crates/dbx-sql-schema/src/db_admin_sql.rs b/crates/dbx-sql-schema/src/db_admin_sql.rsindex 114571a6bd..a1a8be9676 100644--- a/crates/dbx-sql-schema/src/db_admin_sql.rs+++ b/crates/dbx-sql-schema/src/db_admin_sql.rs@@ -848,7 +848,9 @@ pub fn build_duplicate_table_structure_sql(options: DuplicateTableStructureSqlOp));}}- if let Some(database_type @ (DatabaseType::Dameng | DatabaseType::Vastbase)) = options.database_type {+ if let Some(database_type @ (DatabaseType::Dameng | DatabaseType::Vastbase | DatabaseType::Oscar)) =+ options.database_type+ {comment_sql.extend(options.column_comments.iter().filter_map(|column| {if column.comment.trim().is_empty() {return None;@@ -1212,6 +1214,7 @@ fn supports_duplicate_table_comment(database_type: DatabaseType) -> bool {| DatabaseType::OpenGauss| DatabaseType::Dameng| DatabaseType::Vastbase+ | DatabaseType::Oscar)}@@ -1325,9 +1328,9 @@ fn quote_sql_string(value: &str) -> String {}fn quote_duplicate_table_comment(database_type: DatabaseType, value: &str) -> String {- // Dameng rejects Postgres E'...' escape strings; keep standard '' escaping- // (same as DamengAgent COMMENT ON / COMMENT ON COLUMN generation).- if database_type == DatabaseType::Dameng {+ // Dameng and Oscar reject Postgres E'...' escape strings; keep standard '' escaping+ // (same as DamengAgent/Oscar COMMENT ON / COMMENT ON COLUMN generation).+ if matches!(database_type, DatabaseType::Dameng | DatabaseType::Oscar) {return quote_sql_string(value);}if !value.contains('\\') && !value.chars().any(|character| character.is_ascii_control()) {@@ -2586,6 +2589,53 @@ mod tests {}}+ #[test]+ fn duplicate_table_structure_oscar_includes_table_and_column_comments() {+ let sql = build_duplicate_table_structure_sql(DuplicateTableStructureSqlOptions {+ database_type: Some(DatabaseType::Oscar),+ schema: Some("SYSDBA".to_string()),+ source_name: "USERS".to_string(),+ target_name: "USERS_COPY".to_string(),+ table_comment: Some("用户表'测试".to_string()),+ column_comments: vec![+ DuplicateTableColumnComment { name: "NAME".to_string(), comment: "姓名'测试".to_string() },+ DuplicateTableColumnComment { name: "AGE".to_string(), comment: "年龄".to_string() },+ DuplicateTableColumnComment { name: "EMPTY".to_string(), comment: " \t\n".to_string() },+ ],+ primary_key_columns: vec![],+ primary_key_constraint_name: None,+ identifier_quote: Some("\"".to_string()),+ });+ let expected_statements = vec![+ "CREATE TABLE \"SYSDBA\".\"USERS_COPY\" AS SELECT * FROM \"SYSDBA\".\"USERS\" WHERE 1=0",+ "COMMENT ON TABLE \"SYSDBA\".\"USERS_COPY\" IS '用户表''测试'",+ "COMMENT ON COLUMN \"SYSDBA\".\"USERS_COPY\".\"NAME\" IS '姓名''测试'",+ "COMMENT ON COLUMN \"SYSDBA\".\"USERS_COPY\".\"AGE\" IS '年龄'",+ ];+ assert_eq!(sql, format!("{};", expected_statements.join(";\n")));+ assert_eq!(crate::sql::split_sql_statements_for_database(&sql, DatabaseType::Oscar), expected_statements);++ // Control chars / backslashes must keep standard '' escaping for Oscar (no Postgres E'...' literals).+ let oscar_escape_sql = build_duplicate_table_structure_sql(DuplicateTableStructureSqlOptions {+ database_type: Some(DatabaseType::Oscar),+ schema: Some("SYSDBA".to_string()),+ source_name: "ORDERS".to_string(),+ target_name: "ORDERS_COPY".to_string(),+ table_comment: Some("line1\\path\nline2".to_string()),+ column_comments: vec![DuplicateTableColumnComment {+ name: "PATH".to_string(),+ comment: "C:\\data\t'file'".to_string(),+ }],+ primary_key_columns: vec![],+ primary_key_constraint_name: None,+ identifier_quote: None,+ });+ assert_eq!(+ oscar_escape_sql,+ "CREATE TABLE \"SYSDBA\".\"ORDERS_COPY\" AS SELECT * FROM \"SYSDBA\".\"ORDERS\" WHERE 1=0;\nCOMMENT ON TABLE \"SYSDBA\".\"ORDERS_COPY\" IS 'line1\\path\nline2';\nCOMMENT ON COLUMN \"SYSDBA\".\"ORDERS_COPY\".\"PATH\" IS 'C:\\data\t''file''';"+ );+ }+#[test]fn duplicate_table_structure_comments_keep_other_dialects_unchanged() {for (database_type, expected) in [