feat(structure): copy columns between tables

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-08-16 11:39:44 +08:00
1 parent 1d8771a846
commit bdee3a843c
11 files changed
+479 -2

No files matched your search

@@ -1,8 +1,10 @@
import { describe, expect, it } from "vitest";
import {
canEditStructuredTriggerDraft,
cloneColumnDraftAsNew,
combineDataTypeForDatabase,
combineDataTypeForDatabaseWithLengthUnit,
createCopiedColumnDrafts,
createColumnDrafts,
createTriggerDrafts,
dataTypeLengthInputValue,
@@ -23,9 +25,24 @@ import {
resolveInsertColumnIndex,
restoreCharacterLengthUnitsAfterSave,
splitDataType,
tableStructureIdentifierComparisonKey,
} from "@/lib/table/tableStructureEditorState";
describe("tableStructureEditorState", () => {
it("keeps quoted mixed-case identifiers distinct when detecting copied-column duplicates", () => {
const postgresNames = new Set([tableStructureIdentifierComparisonKey("Foo", "postgres")]);
expect(postgresNames.has(tableStructureIdentifierComparisonKey("foo", "postgres"))).toBe(false);
const oracleNames = new Set([tableStructureIdentifierComparisonKey("Foo", "oracle")]);
expect(oracleNames.has(tableStructureIdentifierComparisonKey("FOO", "oracle"))).toBe(false);
const mysqlNames = new Set([tableStructureIdentifierComparisonKey("Foo", "mysql")]);
expect(mysqlNames.has(tableStructureIdentifierComparisonKey("foo", "mysql"))).toBe(true);
const jdbcNames = new Set([tableStructureIdentifierComparisonKey("Foo", "jdbc", { unquotedIdentifierCase: "lower", quotedIdentifierCase: "mixed" })]);
expect(jdbcNames.has(tableStructureIdentifierComparisonKey("foo", "jdbc", { unquotedIdentifierCase: "lower", quotedIdentifierCase: "mixed" }))).toBe(false);
});
it("keeps existing Oracle trigger drafts read-only until full source editing is available", () => {
const [existing] = createTriggerDrafts([{ name: "ORDERS_AUDIT", timing: "AFTER EACH ROW", event: "INSERT OR UPDATE", statement: "BEGIN NULL; END;" }]);
if (!existing) throw new Error("expected an existing trigger draft");
@@ -95,6 +112,81 @@ describe("tableStructureEditorState", () => {
expect(dataTypeLengthInputValue("kingbase", columns[0]?.dataType ?? "")).toBe("255");
});
it("turns copied metadata into new column drafts instead of existing columns", () => {
const copied = createCopiedColumnDrafts(
[
{
name: "display_name",
data_type: "varchar",
is_nullable: false,
column_default: "'anonymous'",
is_primary_key: true,
extra: "auto_increment",
comment: "Visible name",
character_maximum_length: 255,
},
],
"mysql",
() => "copied-column",
);
expect(copied).toEqual([
expect.objectContaining({
id: "new:copied-column",
name: "display_name",
dataType: "varchar(255)",
defaultValue: "'anonymous'",
comment: "Visible name",
isPrimaryKey: false,
extra: {},
}),
]);
expect(copied[0]?.original).toBeUndefined();
expect(copied[0]?.originalPosition).toBeUndefined();
});
it("clones an editable field into an independent new column", () => {
const [source] = createColumnDrafts(
[
{
name: "status",
data_type: "enum",
enum_values: ["draft", "published"],
is_nullable: false,
column_default: "'draft'",
is_primary_key: false,
extra: "on update current_timestamp",
comment: "Publication status",
},
],
"mysql",
);
const copied = cloneColumnDraftAsNew(source!, () => "copied-column");
expect(copied).toMatchObject({
id: "new:copied-column",
name: "status",
enumValues: ["draft", "published"],
extra: { onUpdateCurrentTimestamp: true },
markedForDrop: false,
});
expect(copied.original).toBeUndefined();
expect(copied.originalPosition).toBeUndefined();
source!.isPrimaryKey = true;
source!.extra.autoIncrement = true;
source!.extra.identity = { generation: "ALWAYS" };
const copiedKeyColumn = cloneColumnDraftAsNew(source!, () => "copied-key-column");
expect(copiedKeyColumn.isPrimaryKey).toBe(false);
expect(copiedKeyColumn.extra.autoIncrement).toBeUndefined();
expect(copiedKeyColumn.extra.identity).toBeUndefined();
copied.enumValues?.push("archived");
copied.extra.onUpdateCurrentTimestamp = false;
expect(source?.enumValues).toEqual(["draft", "published"]);
expect(source?.extra.onUpdateCurrentTimestamp).toBe(true);
});
it("parses Kingbase SQLServer compatibility identity metadata", () => {
expect(parseExtraToColumnExtra("identity(10, 2)", "kingbase")).toEqual({
autoIncrement: true,