fix(schema-diff): focus modified table DDL changes

Closes #10110
This commit is contained in:
t8y2 committed 2026-09-25 19:20:11 +08:00
1 parent 7265e03817
commit c29ec0d58e
3 files changed
+52 -5

No files matched your search

@@ -1,5 +1,6 @@
import { describe, expect, it } from "vitest";
import { findSchemaDiffDdlLineNumber } from "@/lib/schema/schemaDiffDdlLocate";
import { buildHunks } from "@/components/diff/DiffHunkBuilder";
import { findFirstSchemaDiffChangedLineNumber, findSchemaDiffDdlLineNumber } from "@/lib/schema/schemaDiffDdlLocate";
import type { SchemaDiffObject } from "@/lib/schema/schemaDiff";
const field: SchemaDiffObject = {
@@ -28,4 +29,25 @@ describe("schema diff DDL location", () => {
expect(findSchemaDiffDdlLineNumber(source, createdField, "source")).toBe(2);
expect(findSchemaDiffDdlLineNumber(target, createdField, "target")).toBeNull();
});
it("finds the first changed line when a modified table row is selected", () => {
const source = ["CREATE TABLE `agents` (", " `id` bigint NOT NULL,", " `context_window` int NOT NULL", ");"].join("\n");
const target = ["CREATE TABLE `agents` (", " `id` bigint NOT NULL,", " `context_window` int DEFAULT NULL", ");"].join("\n");
const hunks = buildHunks(source, target);
expect(findFirstSchemaDiffChangedLineNumber(hunks, "source")).toBe(3);
expect(findFirstSchemaDiffChangedLineNumber(hunks, "target")).toBe(3);
});
it("skips padding when a table change exists on only one side", () => {
const hunks = [
{
leftLines: [{ type: "equal", lineNumber: null, isPadding: true }],
rightLines: [{ type: "insert", lineNumber: 4, isPadding: false }],
},
];
expect(findFirstSchemaDiffChangedLineNumber(hunks, "source")).toBeNull();
expect(findFirstSchemaDiffChangedLineNumber(hunks, "target")).toBe(4);
});
});
@@ -1,5 +1,16 @@
import type { SchemaDiffObject } from "@/lib/schema/schemaDiff";
interface SchemaDiffDdlLine {
type: string;
lineNumber: number | null;
isPadding: boolean;
}
interface SchemaDiffDdlHunk {
leftLines: readonly SchemaDiffDdlLine[];
rightLines: readonly SchemaDiffDdlLine[];
}
function readSqlIdentifier(value: string): string | null {
const input = value.trimStart();
const opening = input[0];
@@ -59,3 +70,12 @@ export function findSchemaDiffDdlLineNumber(ddl: string, object: SchemaDiffObjec
const index = lines.findIndex((line) => lineMatchesObject(line, object, objectName));
return index >= 0 ? index + 1 : null;
}
export function findFirstSchemaDiffChangedLineNumber(hunks: readonly SchemaDiffDdlHunk[], side: "source" | "target"): number | null {
const linesKey = side === "source" ? "leftLines" : "rightLines";
for (const hunk of hunks) {
const line = hunk[linesKey].find((candidate) => !candidate.isPadding && candidate.type !== "equal" && candidate.lineNumber !== null);
if (line?.lineNumber !== null && line?.lineNumber !== undefined) return line.lineNumber;
}
return null;
}