feat(diff): manual table mapping for schema compare
This commit is contained in:
1 parent
d5a511b719
commit
b5fcb3a832
28 files changed
+885
-122
No files matched your search
@@ -1,24 +0,0 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildSameNameTableMatches } from "@/lib/diff/sameNameTableMatch";
|
||||
|
||||
describe("buildSameNameTableMatches", () => {
|
||||
it("matches same-name target tables and reports missing ones", () => {
|
||||
const result = buildSameNameTableMatches(["a", "b", "c"], ["a", "c", "d"]);
|
||||
expect(result).toEqual({ matched: ["a", "c"], missing: ["b"] });
|
||||
});
|
||||
|
||||
it("reports all missing when no target tables exist", () => {
|
||||
const result = buildSameNameTableMatches(["x", "y"], []);
|
||||
expect(result).toEqual({ matched: [], missing: ["x", "y"] });
|
||||
});
|
||||
|
||||
it("has no missing when every source matches", () => {
|
||||
const result = buildSameNameTableMatches(["a", "b"], ["b", "a"]);
|
||||
expect(result).toEqual({ matched: ["a", "b"], missing: [] });
|
||||
});
|
||||
|
||||
it("handles empty source set", () => {
|
||||
const result = buildSameNameTableMatches([], ["a"]);
|
||||
expect(result).toEqual({ matched: [], missing: [] });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,125 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { loadSchemaDetails, type SchemaDiffMetadataApi } from "@/lib/schema/schemaDiffMetadataLoad";
|
||||
import { compileSchemaDiffTableFilter, filterSchemaDiffTables } from "@/lib/schema/schemaDiffTableFilter";
|
||||
import { availableSchemaDiffTargetTables, buildSchemaDiffTableMatches, pruneSchemaDiffTableMappings, reconcileSchemaDiffTableMappings, swapSchemaDiffTableMappings, updateSchemaDiffTableMapping } from "@/lib/schema/schemaDiffTableMapping";
|
||||
import { DEFAULT_MYSQL_OPTIONS, normalizeSchemaDiffCompareOptions } from "@/types/schemaDiff";
|
||||
import type { TableInfo } from "@/types/database";
|
||||
|
||||
const table = (name: string): TableInfo => ({ name, table_type: "BASE TABLE" });
|
||||
|
||||
describe("schema diff table mappings", () => {
|
||||
it("automatically matches same-name tables", () => {
|
||||
expect(buildSchemaDiffTableMatches(["a", "b"], ["a", "b"], [])).toEqual([
|
||||
{ sourceTable: "a", targetTable: "a", kind: "automatic" },
|
||||
{ sourceTable: "b", targetTable: "b", kind: "automatic" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("leaves a source unmatched when only some names exist on target", () => {
|
||||
expect(buildSchemaDiffTableMatches(["a", "b", "c"], ["a", "x", "c"], [])).toEqual([
|
||||
{ sourceTable: "a", targetTable: "a", kind: "automatic" },
|
||||
{ sourceTable: "b", kind: "unmatched" },
|
||||
{ sourceTable: "c", targetTable: "c", kind: "automatic" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("keeps manual mappings alongside automatic matches", () => {
|
||||
expect(buildSchemaDiffTableMatches(["a", "b", "c"], ["a", "x", "c"], [{ sourceTable: "b", targetTable: "x" }])).toEqual([
|
||||
{ sourceTable: "a", targetTable: "a", kind: "automatic" },
|
||||
{ sourceTable: "b", targetTable: "x", kind: "manual" },
|
||||
{ sourceTable: "c", targetTable: "c", kind: "automatic" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("allows a manual mapping to override an automatic match", () => {
|
||||
expect(buildSchemaDiffTableMatches(["a"], ["a", "a_new"], [{ sourceTable: "a", targetTable: "a_new" }])).toEqual([{ sourceTable: "a", targetTable: "a_new", kind: "manual" }]);
|
||||
});
|
||||
|
||||
it("removes mappings for deselected sources and automatically matches new sources", () => {
|
||||
const mappings = [
|
||||
{ sourceTable: "a", targetTable: "a" },
|
||||
{ sourceTable: "b", targetTable: "b_new" },
|
||||
];
|
||||
expect(reconcileSchemaDiffTableMappings(["a", "c"], ["a", "b_new", "c"], mappings)).toEqual([
|
||||
{ sourceTable: "a", targetTable: "a" },
|
||||
{ sourceTable: "c", targetTable: "c" },
|
||||
]);
|
||||
expect(pruneSchemaDiffTableMappings(["a"], mappings)).toEqual([{ sourceTable: "a", targetTable: "a" }]);
|
||||
});
|
||||
|
||||
it("reconciles a vanished target mapping back to same-name or unmatched", () => {
|
||||
expect(reconcileSchemaDiffTableMappings(["a", "b"], ["a", "b"], [{ sourceTable: "b", targetTable: "b_old" }])).toEqual([
|
||||
{ sourceTable: "a", targetTable: "a" },
|
||||
{ sourceTable: "b", targetTable: "b" },
|
||||
]);
|
||||
expect(reconcileSchemaDiffTableMappings(["b"], ["b_other"], [{ sourceTable: "b", targetTable: "b_old" }])).toEqual([]);
|
||||
});
|
||||
|
||||
it("rejects duplicate target mappings and hides occupied targets", () => {
|
||||
const mappings = [{ sourceTable: "a", targetTable: "x" }];
|
||||
expect(updateSchemaDiffTableMapping(mappings, "b", "x")).toEqual({ mappings, accepted: false, conflictSource: "a" });
|
||||
expect(availableSchemaDiffTargetTables("b", ["x", "y"], mappings)).toEqual(["y"]);
|
||||
expect(
|
||||
reconcileSchemaDiffTableMappings(
|
||||
["a", "b"],
|
||||
["x", "b"],
|
||||
[
|
||||
{ sourceTable: "a", targetTable: "x" },
|
||||
{ sourceTable: "b", targetTable: "x" },
|
||||
],
|
||||
),
|
||||
).toEqual([
|
||||
{ sourceTable: "a", targetTable: "x" },
|
||||
{ sourceTable: "b", targetTable: "b" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("round-trips table mappings through normalized config JSON", () => {
|
||||
const options = normalizeSchemaDiffCompareOptions({ tableMappings: [{ sourceTable: "a", targetTable: "a_new" }] });
|
||||
const restored = normalizeSchemaDiffCompareOptions(JSON.parse(JSON.stringify(options)));
|
||||
expect(restored.tableMappings).toEqual([{ sourceTable: "a", targetTable: "a_new" }]);
|
||||
expect(normalizeSchemaDiffCompareOptions({}).tableMappings).toEqual([]);
|
||||
});
|
||||
|
||||
it("swaps each mapping direction", () => {
|
||||
expect(
|
||||
swapSchemaDiffTableMappings([
|
||||
{ sourceTable: "a", targetTable: "b" },
|
||||
{ sourceTable: "c", targetTable: "d" },
|
||||
]),
|
||||
).toEqual([
|
||||
{ sourceTable: "b", targetTable: "a" },
|
||||
{ sourceTable: "d", targetTable: "c" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("selects mapped target tables for metadata loading instead of same-name tables", async () => {
|
||||
const options = { ...DEFAULT_MYSQL_OPTIONS, tableMappings: [{ sourceTable: "charge_records", targetTable: "charge_record" }] };
|
||||
const filter = compileSchemaDiffTableFilter(options);
|
||||
const filtered = filterSchemaDiffTables([table("charge_records")], [table("charge_records"), table("charge_record")], filter, options, ["charge_records"]);
|
||||
expect(filtered.sourceTables.map((entry) => entry.name)).toEqual(["charge_records"]);
|
||||
expect(filtered.targetTables.map((entry) => entry.name)).toEqual(["charge_record"]);
|
||||
|
||||
const getColumns = vi.fn().mockResolvedValue([]);
|
||||
const api: SchemaDiffMetadataApi = {
|
||||
getTableDdl: vi.fn().mockResolvedValue(""),
|
||||
getColumns,
|
||||
listIndexes: vi.fn().mockResolvedValue([]),
|
||||
listForeignKeys: vi.fn().mockResolvedValue([]),
|
||||
listTriggers: vi.fn().mockResolvedValue([]),
|
||||
};
|
||||
await loadSchemaDetails(
|
||||
filtered.targetTables,
|
||||
{
|
||||
connectionId: "target",
|
||||
database: "app",
|
||||
schema: "public",
|
||||
dbType: "mysql",
|
||||
options,
|
||||
},
|
||||
api,
|
||||
);
|
||||
expect(getColumns).toHaveBeenCalledWith("target", "app", "public", "charge_record");
|
||||
expect(getColumns).not.toHaveBeenCalledWith("target", "app", "public", "charge_records");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user