fix(sqlserver): preserve table and column comments when cloning
This commit is contained in:
5 files changed
+167
-5
No files matched your search
+63
-1
@@ -486,6 +486,8 @@ describe("buildDuplicateTableStructurePlan", () => {
|
||||
});
|
||||
|
||||
it("recreates the SQL Server primary key that SELECT INTO drops", async () => {
|
||||
apiMock.getColumns.mockResolvedValue([]);
|
||||
apiMock.getTableComment.mockResolvedValue(null);
|
||||
apiMock.listIndexes.mockResolvedValue([
|
||||
{ name: "PK_ORDERS", columns: ["ID", "SEQ"], is_unique: true, is_primary: true },
|
||||
{ name: "IDX_ORDERS_CUSTOMER", columns: ["CUSTOMER"], is_unique: false, is_primary: false },
|
||||
@@ -507,6 +509,8 @@ describe("buildDuplicateTableStructurePlan", () => {
|
||||
});
|
||||
|
||||
it("renames the SQL Server clone PK when it would collide with a source index", async () => {
|
||||
apiMock.getColumns.mockResolvedValue([]);
|
||||
apiMock.getTableComment.mockResolvedValue(null);
|
||||
apiMock.listIndexes.mockResolvedValue([
|
||||
{ name: "PK_ORDERS", columns: ["ID"], is_unique: true, is_primary: true },
|
||||
{ name: "PK_orders_copy", columns: ["FLAG"], is_unique: false, is_primary: false },
|
||||
@@ -549,6 +553,8 @@ describe("buildDuplicateTableStructurePlan", () => {
|
||||
});
|
||||
|
||||
it("keeps primary-key-free SQL Server clones on a single statement", async () => {
|
||||
apiMock.getColumns.mockResolvedValue([]);
|
||||
apiMock.getTableComment.mockResolvedValue(null);
|
||||
apiMock.listIndexes.mockResolvedValue([{ name: "IDX_ORDERS_CUSTOMER", columns: ["CUSTOMER"], is_unique: false, is_primary: false }]);
|
||||
apiMock.buildDuplicateTableStructureSql.mockResolvedValue("SELECT TOP 0 * INTO [orders_copy] FROM [orders];");
|
||||
|
||||
@@ -562,7 +568,63 @@ describe("buildDuplicateTableStructurePlan", () => {
|
||||
});
|
||||
|
||||
expect(apiMock.buildDuplicateTableStructureSql).toHaveBeenCalledWith(expect.objectContaining({ databaseType: "sqlserver", primaryKeyColumns: [] }));
|
||||
expect(plan).toEqual({ sql: "SELECT TOP 0 * INTO [orders_copy] FROM [orders];", sourceColumns: undefined, executeAsScript: false });
|
||||
expect(plan).toEqual({ sql: "SELECT TOP 0 * INTO [orders_copy] FROM [orders];", sourceColumns: [], executeAsScript: false });
|
||||
});
|
||||
|
||||
it("loads SQL Server table and column comments and runs a comment-only clone as a script", async () => {
|
||||
const columns = [
|
||||
{ name: "value field", comment: " 字段 O'Brien;注释 ", data_type: "nvarchar", is_nullable: true, column_default: null, is_primary_key: false },
|
||||
{ name: "id", comment: null, data_type: "int", is_nullable: false, column_default: null, is_primary_key: false },
|
||||
];
|
||||
const sql = "SELECT TOP 0 * INTO [dbo].[copy] FROM [dbo].[source];\nEXEC sys.sp_addextendedproperty ...;";
|
||||
apiMock.getColumns.mockResolvedValue(columns);
|
||||
apiMock.getTableComment.mockResolvedValue(" 表 O'Brien;注释 ");
|
||||
apiMock.listIndexes.mockResolvedValue([]);
|
||||
apiMock.buildDuplicateTableStructureSql.mockResolvedValue(sql);
|
||||
|
||||
const plan = await buildDuplicateTableStructurePlan({ connectionId: "mssql-1", database: "app", catalog: "catalog", databaseType: "sqlserver", schema: "dbo", sourceName: "source", targetName: "copy" });
|
||||
|
||||
expect(apiMock.getColumns).toHaveBeenCalledExactlyOnceWith("mssql-1", "app", "dbo", "source", "catalog");
|
||||
expect(apiMock.getTableComment).toHaveBeenCalledExactlyOnceWith("mssql-1", "app", "dbo", "source", "catalog");
|
||||
expect(apiMock.buildDuplicateTableStructureSql).toHaveBeenCalledWith(expect.objectContaining({ tableComment: " 表 O'Brien;注释 ", columnComments: [{ name: "value field", comment: " 字段 O'Brien;注释 " }], primaryKeyColumns: [] }));
|
||||
expect(plan).toEqual({ sql, sourceColumns: columns, executeAsScript: true });
|
||||
});
|
||||
|
||||
it("reuses supplied SQL Server column and table comments for data-copy callers", async () => {
|
||||
const columns = [{ name: "id", comment: "编号", data_type: "int", is_nullable: false, column_default: null, is_primary_key: true }];
|
||||
apiMock.listIndexes.mockResolvedValue([{ name: "PK_source", columns: ["id"], is_unique: true, is_primary: true }]);
|
||||
apiMock.buildDuplicateTableStructureSql.mockResolvedValue("SELECT TOP 0 * INTO [copy] FROM [source];");
|
||||
const plan = await buildDuplicateTableStructurePlan({ connectionId: "mssql-1", database: "app", databaseType: "sqlserver", sourceName: "source", targetName: "copy", sourceColumns: columns, tableComment: "表注释" });
|
||||
expect(apiMock.getColumns).not.toHaveBeenCalled();
|
||||
expect(apiMock.getTableComment).not.toHaveBeenCalled();
|
||||
expect(apiMock.buildDuplicateTableStructureSql).toHaveBeenCalledWith(expect.objectContaining({ tableComment: "表注释", columnComments: [{ name: "id", comment: "编号" }], primaryKeyColumns: ["id"] }));
|
||||
expect(plan.sourceColumns).toBe(columns);
|
||||
expect(plan.executeAsScript).toBe(true);
|
||||
});
|
||||
|
||||
it("does not create a SQL Server clone when column metadata cannot be loaded", async () => {
|
||||
apiMock.listIndexes.mockResolvedValue([]);
|
||||
apiMock.getColumns.mockResolvedValue([]);
|
||||
apiMock.getTableComment.mockResolvedValue(null);
|
||||
const error = new Error("comment metadata unavailable");
|
||||
apiMock.getColumns.mockRejectedValueOnce(error);
|
||||
await expect(buildDuplicateTableStructurePlan({ connectionId: "mssql-1", database: "app", databaseType: "sqlserver", sourceName: "source", targetName: "copy" })).rejects.toBe(error);
|
||||
expect(apiMock.buildDuplicateTableStructureSql).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("keeps SQL Server cloning available when optional table comment loading fails", async () => {
|
||||
const warning = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
apiMock.listIndexes.mockResolvedValue([]);
|
||||
apiMock.getColumns.mockResolvedValue([]);
|
||||
apiMock.getTableComment.mockRejectedValue(new Error("Table comment lookup is not available in the web backend"));
|
||||
apiMock.buildDuplicateTableStructureSql.mockResolvedValue("SELECT TOP 0 * INTO [copy] FROM [source];");
|
||||
|
||||
const plan = await buildDuplicateTableStructurePlan({ connectionId: "mssql-web", database: "app", databaseType: "sqlserver", sourceName: "source", targetName: "copy" });
|
||||
|
||||
expect(apiMock.buildDuplicateTableStructureSql).toHaveBeenCalledWith(expect.objectContaining({ tableComment: null, columnComments: [], primaryKeyColumns: [] }));
|
||||
expect(plan).toEqual({ sql: "SELECT TOP 0 * INTO [copy] FROM [source];", sourceColumns: [], executeAsScript: false });
|
||||
expect(warning).toHaveBeenCalledOnce();
|
||||
warning.mockRestore();
|
||||
});
|
||||
|
||||
it("forwards the connection identifier quote so dual-dialect clones stay executable", async () => {
|
||||
|
||||
@@ -297,7 +297,19 @@ export async function buildDuplicateTableStructurePlan(options: DuplicateTableSt
|
||||
// cloned table silently loses its primary key (t8y2/dbx#8931). Load the source primary key and
|
||||
// let the backend append an `ALTER TABLE ... ADD CONSTRAINT ... PRIMARY KEY` for it.
|
||||
if (options.databaseType === "sqlserver") {
|
||||
const indexes = await api.listIndexes(options.connectionId, options.database, options.schema || "", options.sourceName, options.catalog);
|
||||
const [columns, indexes, tableComment] = await Promise.all([
|
||||
options.sourceColumns ? Promise.resolve(options.sourceColumns) : api.getColumns(options.connectionId, options.database, options.schema || "", options.sourceName, options.catalog),
|
||||
api.listIndexes(options.connectionId, options.database, options.schema || "", options.sourceName, options.catalog),
|
||||
// The web backend's getTableComment is a throwing placeholder, and SQL Server table lists
|
||||
// report `comment: null` for tables without one, so a hard failure would break web cloning.
|
||||
options.tableComment == null
|
||||
? api.getTableComment(options.connectionId, options.database, options.schema || "", options.sourceName, options.catalog).catch((error) => {
|
||||
console.warn(`Failed to load SQL Server table comment for table clone: ${options.sourceName}`, error);
|
||||
return null;
|
||||
})
|
||||
: Promise.resolve(options.tableComment),
|
||||
]);
|
||||
const columnComments = collectDuplicateTableColumnComments(columns);
|
||||
const primaryKeyColumns = indexes.find((index) => index.is_primary && index.columns.length > 0)?.columns ?? [];
|
||||
const primaryKeyConstraintName = sqlServerClonePrimaryKeyConstraintName(indexes, options.targetName);
|
||||
const sql = await buildDuplicateTableStructureSql({
|
||||
@@ -305,13 +317,13 @@ export async function buildDuplicateTableStructurePlan(options: DuplicateTableSt
|
||||
schema: options.schema,
|
||||
sourceName: options.sourceName,
|
||||
targetName: options.targetName,
|
||||
tableComment: options.tableComment,
|
||||
columnComments: [],
|
||||
tableComment,
|
||||
columnComments,
|
||||
primaryKeyColumns,
|
||||
primaryKeyConstraintName,
|
||||
identifierQuote: options.identifierQuote,
|
||||
});
|
||||
return { sql, sourceColumns: options.sourceColumns, executeAsScript: primaryKeyColumns.length > 0 || duplicateTableStructureRequiresScript(sql) };
|
||||
return { sql, sourceColumns: columns, executeAsScript: primaryKeyColumns.length > 0 || !!tableComment?.trim() || columnComments.length > 0 || duplicateTableStructureRequiresScript(sql) };
|
||||
}
|
||||
|
||||
let sourceColumns = options.sourceColumns;
|
||||
|
||||
Reference in new issue
Block a user