feat(schema-diff): compare routines across same-structure databases
This commit is contained in:
1 parent
bd2ac50ac1
commit
1191118b5c
31 files changed
+2256
-281
No files matched your search
@@ -1,8 +1,13 @@
|
||||
import { strict as assert } from "node:assert";
|
||||
import { test, vi } from "vitest";
|
||||
import { DEFAULT_MYSQL_OPTIONS, getDefaultOptionsForDbType } from "@/types/schemaDiff";
|
||||
|
||||
const apiMock = vi.hoisted(() => ({
|
||||
prepareSchemaDiff: vi.fn(),
|
||||
listFunctions: vi.fn(),
|
||||
listSequences: vi.fn(),
|
||||
listRules: vi.fn(),
|
||||
listOwners: vi.fn(),
|
||||
}));
|
||||
const openMock = vi.hoisted(() => vi.fn());
|
||||
const trackerMock = vi.hoisted(() => ({
|
||||
@@ -17,6 +22,23 @@ vi.mock("@/lib/schema/schemaDiffMetadataLoad", () => ({ loadSchemaDetails: vi.fn
|
||||
|
||||
const { startSchemaDiffSession } = await import("../useSchemaDiffSession.ts");
|
||||
|
||||
async function waitForSession(session: { status: string }) {
|
||||
for (let attempt = 0; attempt < 40 && session.status === "running"; attempt += 1) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
}
|
||||
}
|
||||
|
||||
test("defaults enable tables and functions compare for common targets", () => {
|
||||
assert.equal(DEFAULT_MYSQL_OPTIONS.tables, true);
|
||||
assert.equal(DEFAULT_MYSQL_OPTIONS.functions, true);
|
||||
assert.equal(getDefaultOptionsForDbType("oracle").tables, true);
|
||||
assert.equal(getDefaultOptionsForDbType("oracle").functions, true);
|
||||
assert.equal(getDefaultOptionsForDbType("mysql").tables, true);
|
||||
assert.equal(getDefaultOptionsForDbType("mysql").functions, true);
|
||||
assert.equal(getDefaultOptionsForDbType("postgres").tables, true);
|
||||
assert.equal(getDefaultOptionsForDbType("postgres").functions, true);
|
||||
});
|
||||
|
||||
test("runs a schema diff session after the dialog is closed and retains the prepared result", async () => {
|
||||
apiMock.prepareSchemaDiff.mockResolvedValue({
|
||||
diffs: [],
|
||||
@@ -28,6 +50,7 @@ test("runs a schema diff session after the dialog is closed and retains the prep
|
||||
syncSql: "",
|
||||
rollbackSyncSql: "",
|
||||
});
|
||||
apiMock.listFunctions.mockResolvedValue([]);
|
||||
|
||||
const tableListLoader = {
|
||||
load: vi.fn().mockResolvedValue([]),
|
||||
@@ -49,9 +72,7 @@ test("runs a schema diff session after the dialog is closed and retains the prep
|
||||
{ tableListLoader },
|
||||
);
|
||||
|
||||
for (let attempt = 0; attempt < 20 && session.status === "running"; attempt += 1) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
}
|
||||
await waitForSession(session);
|
||||
|
||||
assert.equal(session.status, "completed");
|
||||
assert.deepEqual(session.result?.diffs, []);
|
||||
@@ -59,8 +80,245 @@ test("runs a schema diff session after the dialog is closed and retains the prep
|
||||
assert.equal(tableListLoader.load.mock.calls[0]?.[1]?.refresh, true);
|
||||
assert.equal(trackerMock.addSchemaDiffTask.mock.calls.length, 1);
|
||||
assert.equal(trackerMock.updateCompareTask.mock.calls.at(-1)?.[1].status, "Done");
|
||||
// MySQL defaults now enable functions compare for same-dialect pairs.
|
||||
assert.equal(apiMock.listFunctions.mock.calls.length, 2);
|
||||
|
||||
const onOpen = trackerMock.addSchemaDiffTask.mock.calls[0]?.[2] as (() => void) | undefined;
|
||||
onOpen?.();
|
||||
assert.equal(openMock.mock.calls.at(-1)?.[0], session.id);
|
||||
});
|
||||
|
||||
test("loads routines for mysql↔mysql when functions is enabled", async () => {
|
||||
apiMock.prepareSchemaDiff.mockClear();
|
||||
apiMock.listFunctions.mockClear();
|
||||
apiMock.prepareSchemaDiff.mockResolvedValue({
|
||||
diffs: [],
|
||||
functionDiffs: [{ diff_type: "added", name: "p1", source: { name: "p1", function_type: "PROCEDURE", data_type: "", definition: "body", arguments: "" }, target: null, changes: [] }],
|
||||
sequenceDiffs: [],
|
||||
ruleDiffs: [],
|
||||
ownerDiffs: [],
|
||||
renameCandidates: [],
|
||||
syncSql: "",
|
||||
rollbackSyncSql: "",
|
||||
});
|
||||
apiMock.listFunctions.mockResolvedValue([{ name: "p1", function_type: "PROCEDURE", data_type: "", definition: "body", arguments: "" }]);
|
||||
|
||||
const session = startSchemaDiffSession(
|
||||
{
|
||||
sourceConnectionId: "mysql-src",
|
||||
sourceDatabase: "gd_ebdata",
|
||||
sourceSchema: "",
|
||||
targetConnectionId: "mysql-dst",
|
||||
targetDatabase: "gd_ebdata_copy",
|
||||
targetSchema: "",
|
||||
sourceDbType: "mysql",
|
||||
targetDbType: "mysql",
|
||||
options: { functions: true },
|
||||
ignoreComments: false,
|
||||
label: "mysql → mysql",
|
||||
},
|
||||
{ tableListLoader: { load: vi.fn().mockResolvedValue([]) } },
|
||||
);
|
||||
|
||||
await waitForSession(session);
|
||||
|
||||
assert.equal(session.status, "completed");
|
||||
assert.equal(apiMock.listFunctions.mock.calls.length, 2);
|
||||
assert.equal(apiMock.prepareSchemaDiff.mock.calls[0]?.[0]?.sourceFunctions?.length, 1);
|
||||
assert.equal(apiMock.prepareSchemaDiff.mock.calls[0]?.[0]?.targetFunctions?.length, 1);
|
||||
});
|
||||
|
||||
test("skips listFunctions for cross-family pairs even when functions is enabled", async () => {
|
||||
apiMock.prepareSchemaDiff.mockClear();
|
||||
apiMock.listFunctions.mockClear();
|
||||
apiMock.prepareSchemaDiff.mockResolvedValue({
|
||||
diffs: [],
|
||||
functionDiffs: [],
|
||||
sequenceDiffs: [],
|
||||
ruleDiffs: [],
|
||||
ownerDiffs: [],
|
||||
renameCandidates: [],
|
||||
syncSql: "",
|
||||
rollbackSyncSql: "",
|
||||
});
|
||||
|
||||
const session = startSchemaDiffSession(
|
||||
{
|
||||
sourceConnectionId: "mysql",
|
||||
sourceDatabase: "gd_ebdata",
|
||||
sourceSchema: "",
|
||||
targetConnectionId: "oracle",
|
||||
targetDatabase: "ARISK",
|
||||
targetSchema: "SYSTEM",
|
||||
sourceDbType: "mysql",
|
||||
targetDbType: "oracle",
|
||||
options: { functions: true },
|
||||
ignoreComments: false,
|
||||
label: "mysql → oracle",
|
||||
},
|
||||
{ tableListLoader: { load: vi.fn().mockResolvedValue([]) } },
|
||||
);
|
||||
|
||||
await waitForSession(session);
|
||||
|
||||
assert.equal(session.status, "completed");
|
||||
assert.equal(apiMock.listFunctions.mock.calls.length, 0);
|
||||
assert.deepEqual(apiMock.prepareSchemaDiff.mock.calls[0]?.[0]?.sourceFunctions, []);
|
||||
assert.deepEqual(apiMock.prepareSchemaDiff.mock.calls[0]?.[0]?.targetFunctions, []);
|
||||
});
|
||||
|
||||
test("skips listFunctions when functions is disabled and routines are unrestricted", async () => {
|
||||
apiMock.prepareSchemaDiff.mockClear();
|
||||
apiMock.listFunctions.mockClear();
|
||||
apiMock.prepareSchemaDiff.mockResolvedValue({
|
||||
diffs: [],
|
||||
functionDiffs: [],
|
||||
sequenceDiffs: [],
|
||||
ruleDiffs: [],
|
||||
ownerDiffs: [],
|
||||
renameCandidates: [],
|
||||
syncSql: "",
|
||||
rollbackSyncSql: "",
|
||||
});
|
||||
|
||||
const session = startSchemaDiffSession(
|
||||
{
|
||||
sourceConnectionId: "mysql-src",
|
||||
sourceDatabase: "gd_ebdata",
|
||||
sourceSchema: "",
|
||||
targetConnectionId: "mysql-dst",
|
||||
targetDatabase: "gd_ebdata_copy",
|
||||
targetSchema: "",
|
||||
sourceDbType: "mysql",
|
||||
targetDbType: "mysql",
|
||||
options: { functions: false, selectedRoutines: undefined },
|
||||
ignoreComments: false,
|
||||
label: "mysql → mysql",
|
||||
},
|
||||
{ tableListLoader: { load: vi.fn().mockResolvedValue([]) } },
|
||||
);
|
||||
|
||||
await waitForSession(session);
|
||||
|
||||
assert.equal(session.status, "completed");
|
||||
assert.equal(apiMock.listFunctions.mock.calls.length, 0);
|
||||
assert.deepEqual(apiMock.prepareSchemaDiff.mock.calls[0]?.[0]?.sourceFunctions, []);
|
||||
assert.deepEqual(apiMock.prepareSchemaDiff.mock.calls[0]?.[0]?.targetFunctions, []);
|
||||
});
|
||||
|
||||
test("skips listFunctions when functions is disabled even if selectedRoutines is set", async () => {
|
||||
apiMock.prepareSchemaDiff.mockClear();
|
||||
apiMock.listFunctions.mockClear();
|
||||
apiMock.prepareSchemaDiff.mockResolvedValue({
|
||||
diffs: [],
|
||||
functionDiffs: [],
|
||||
sequenceDiffs: [],
|
||||
ruleDiffs: [],
|
||||
ownerDiffs: [],
|
||||
renameCandidates: [],
|
||||
syncSql: "",
|
||||
rollbackSyncSql: "",
|
||||
});
|
||||
|
||||
const session = startSchemaDiffSession(
|
||||
{
|
||||
sourceConnectionId: "mysql-src",
|
||||
sourceDatabase: "gd_ebdata",
|
||||
sourceSchema: "",
|
||||
targetConnectionId: "mysql-dst",
|
||||
targetDatabase: "gd_ebdata_copy",
|
||||
targetSchema: "",
|
||||
sourceDbType: "mysql",
|
||||
targetDbType: "mysql",
|
||||
options: { functions: false, selectedRoutines: ["p1"] },
|
||||
ignoreComments: false,
|
||||
label: "mysql → mysql routines off",
|
||||
},
|
||||
{ tableListLoader: { load: vi.fn().mockResolvedValue([]) } },
|
||||
);
|
||||
|
||||
await waitForSession(session);
|
||||
|
||||
assert.equal(session.status, "completed");
|
||||
assert.equal(apiMock.listFunctions.mock.calls.length, 0);
|
||||
assert.deepEqual(apiMock.prepareSchemaDiff.mock.calls[0]?.[0]?.sourceFunctions, []);
|
||||
assert.deepEqual(apiMock.prepareSchemaDiff.mock.calls[0]?.[0]?.targetFunctions, []);
|
||||
});
|
||||
|
||||
test("skips listFunctions when only one side supports routines", async () => {
|
||||
apiMock.prepareSchemaDiff.mockClear();
|
||||
apiMock.listFunctions.mockClear();
|
||||
apiMock.prepareSchemaDiff.mockResolvedValue({
|
||||
diffs: [],
|
||||
functionDiffs: [],
|
||||
sequenceDiffs: [],
|
||||
ruleDiffs: [],
|
||||
ownerDiffs: [],
|
||||
renameCandidates: [],
|
||||
syncSql: "",
|
||||
rollbackSyncSql: "",
|
||||
});
|
||||
|
||||
const session = startSchemaDiffSession(
|
||||
{
|
||||
sourceConnectionId: "mysql",
|
||||
sourceDatabase: "app",
|
||||
sourceSchema: "",
|
||||
targetConnectionId: "sqlite",
|
||||
targetDatabase: "local",
|
||||
targetSchema: "",
|
||||
sourceDbType: "mysql",
|
||||
targetDbType: "sqlite",
|
||||
options: { functions: true },
|
||||
ignoreComments: false,
|
||||
label: "mysql → sqlite",
|
||||
},
|
||||
{ tableListLoader: { load: vi.fn().mockResolvedValue([]) } },
|
||||
);
|
||||
|
||||
await waitForSession(session);
|
||||
|
||||
assert.equal(session.status, "completed");
|
||||
assert.equal(apiMock.listFunctions.mock.calls.length, 0);
|
||||
});
|
||||
|
||||
test("skips table list loading for routines-only compares", async () => {
|
||||
apiMock.prepareSchemaDiff.mockClear();
|
||||
apiMock.listFunctions.mockClear();
|
||||
apiMock.prepareSchemaDiff.mockResolvedValue({
|
||||
diffs: [],
|
||||
functionDiffs: [],
|
||||
sequenceDiffs: [],
|
||||
ruleDiffs: [],
|
||||
ownerDiffs: [],
|
||||
renameCandidates: [],
|
||||
syncSql: "",
|
||||
rollbackSyncSql: "",
|
||||
});
|
||||
apiMock.listFunctions.mockResolvedValue([]);
|
||||
|
||||
const tableListLoader = { load: vi.fn().mockResolvedValue([{ name: "should_not_load", table_type: "BASE TABLE" }]) };
|
||||
const session = startSchemaDiffSession(
|
||||
{
|
||||
sourceConnectionId: "mysql-src",
|
||||
sourceDatabase: "gd_ebdata",
|
||||
sourceSchema: "",
|
||||
targetConnectionId: "mysql-dst",
|
||||
targetDatabase: "gd_ebdata_copy",
|
||||
targetSchema: "",
|
||||
sourceDbType: "mysql",
|
||||
targetDbType: "mysql",
|
||||
options: { tables: false, views: true, functions: true },
|
||||
ignoreComments: false,
|
||||
label: "routines only",
|
||||
},
|
||||
{ tableListLoader },
|
||||
);
|
||||
|
||||
await waitForSession(session);
|
||||
|
||||
assert.equal(session.status, "completed");
|
||||
assert.equal(tableListLoader.load.mock.calls.length, 0);
|
||||
assert.equal(apiMock.listFunctions.mock.calls.length, 2);
|
||||
assert.deepEqual(apiMock.prepareSchemaDiff.mock.calls[0]?.[0]?.sourceTables, []);
|
||||
});
|
||||
+25
@@ -10,6 +10,7 @@ function setup(node: Partial<TreeNode>, options: { treeNodes?: TreeNode[]; selec
|
||||
docsSource: null as unknown,
|
||||
diagramSource: null as unknown,
|
||||
databaseExportSource: null as unknown,
|
||||
schemaDiffSource: null as unknown,
|
||||
treeNodes: options.treeNodes ?? [],
|
||||
selectedTreeNodeIds: options.selectedTreeNodeIds ?? [],
|
||||
};
|
||||
@@ -107,3 +108,27 @@ describe("useSidebarTreeToolRuntime diagram and database export", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("useSidebarTreeToolRuntime openSchemaDiffForRoutine", () => {
|
||||
it("prefills schema diff with a signature-aware routine key", () => {
|
||||
const { connectionStore, runtime } = setup({
|
||||
type: "function",
|
||||
label: "add(integer)",
|
||||
objectName: "add",
|
||||
signature: "integer",
|
||||
connectionId: "conn-1",
|
||||
database: "shop",
|
||||
schema: "public",
|
||||
});
|
||||
|
||||
runtime.openSchemaDiffForRoutine();
|
||||
|
||||
expect(connectionStore.schemaDiffSource).toEqual({
|
||||
connectionId: "conn-1",
|
||||
database: "shop",
|
||||
schema: "public",
|
||||
selectedRoutines: ["add(integer)"],
|
||||
preferredResultTab: "routines",
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -45,6 +45,8 @@ const transferPrefillTargetSchema = ref("");
|
||||
const schemaDiffPrefillConnectionId = ref("");
|
||||
const schemaDiffPrefillDatabase = ref("");
|
||||
const schemaDiffPrefillSchema = ref("");
|
||||
const schemaDiffPrefillSelectedRoutines = ref<string[] | undefined>(undefined);
|
||||
const schemaDiffPrefillResultTab = ref<"tables" | "routines" | "">("");
|
||||
const schemaDiffSessionId = ref<string | null>(null);
|
||||
const dataComparePrefillConnectionId = ref("");
|
||||
const dataComparePrefillDatabase = ref("");
|
||||
@@ -146,6 +148,8 @@ export function useDialogSources() {
|
||||
schemaDiffPrefillConnectionId.value = v.connectionId;
|
||||
schemaDiffPrefillDatabase.value = v.database;
|
||||
schemaDiffPrefillSchema.value = v.schema ?? "";
|
||||
schemaDiffPrefillSelectedRoutines.value = v.selectedRoutines;
|
||||
schemaDiffPrefillResultTab.value = v.preferredResultTab ?? "";
|
||||
schemaDiffSessionId.value = null;
|
||||
showSchemaDiffDialog.value = true;
|
||||
connectionStore.schemaDiffSource = null;
|
||||
@@ -524,6 +528,8 @@ export function useDialogSources() {
|
||||
schemaDiffPrefillConnectionId,
|
||||
schemaDiffPrefillDatabase,
|
||||
schemaDiffPrefillSchema,
|
||||
schemaDiffPrefillSelectedRoutines,
|
||||
schemaDiffPrefillResultTab,
|
||||
schemaDiffSessionId,
|
||||
dataComparePrefillConnectionId,
|
||||
dataComparePrefillDatabase,
|
||||
|
||||
@@ -5,12 +5,14 @@ import { openSchemaDiffSession } from "@/composables/useDialogSources";
|
||||
import { useExportTracker } from "@/composables/useExportTracker";
|
||||
import { filterSchemaDiffTables } from "@/lib/schema/schemaDiffTableFilter";
|
||||
import { compileSchemaDiffTableFilter } from "@/lib/schema/schemaDiffTableFilter";
|
||||
import { filterSchemaDiffFunctions } from "@/lib/schema/schemaDiffRoutine";
|
||||
import { loadSchemaDetails } from "@/lib/schema/schemaDiffMetadataLoad";
|
||||
import type { SchemaDiffTableIdentity, SchemaDiffTableListLoader } from "@/lib/schema/schemaDiffTableList";
|
||||
import { getSchemaDiffNextProgressStep, isSchemaDiffPostgresLike, shouldLoadSchemaDiffExtraObjects, type SchemaDiffProgressPhase } from "@/lib/schema/schemaDiffProgress";
|
||||
import { getSchemaDiffNextProgressStep, isSchemaDiffPostgresLike, shouldLoadSchemaDiffExtraObjectPhase, shouldLoadSchemaDiffRoutines, type SchemaDiffProgressPhase } from "@/lib/schema/schemaDiffProgress";
|
||||
import { schemaDiffRoutineObjectTypesIntersection } from "@/lib/database/databaseObjectCapabilities";
|
||||
import { convertToSchemaDiffObjects, databaseTypeToDialectKind, normalizeDialectKind, schemaDiffDeployTargetSchema, type SchemaDiffObject } from "@/lib/schema/schemaDiff";
|
||||
import { normalizeSchemaDiffCompareOptions, type SchemaDiffCompareOptions } from "@/types/schemaDiff";
|
||||
import type { DatabaseType } from "@/types/database";
|
||||
import type { DatabaseType, FunctionInfo, TableInfo } from "@/types/database";
|
||||
import type { SchemaDiffPreparation } from "@/lib/schema/schemaDiff";
|
||||
|
||||
export interface SchemaDiffSessionProgress {
|
||||
@@ -59,6 +61,8 @@ function cloneOptions(options: SchemaDiffCompareOptions): SchemaDiffCompareOptio
|
||||
...options,
|
||||
selectedTables: options.selectedTables ? [...options.selectedTables] : options.selectedTables,
|
||||
tableMappings: options.tableMappings?.map((mapping) => ({ ...mapping })),
|
||||
selectedRoutines: options.selectedRoutines ? [...options.selectedRoutines] : options.selectedRoutines,
|
||||
routineMappings: options.routineMappings?.map((mapping) => ({ ...mapping })),
|
||||
fieldMappings: options.fieldMappings?.map((mapping) => ({ ...mapping })),
|
||||
};
|
||||
}
|
||||
@@ -71,6 +75,19 @@ function sessionError(error: unknown): string {
|
||||
return error instanceof Error ? error.message : (error as { message?: string } | null)?.message || String(error);
|
||||
}
|
||||
|
||||
function filterFunctionsByRoutineKinds(functions: FunctionInfo[], kinds: Array<"PROCEDURE" | "FUNCTION">): FunctionInfo[] {
|
||||
if (kinds.length === 0) return [];
|
||||
if (kinds.length === 2) return functions;
|
||||
const allowProcedure = kinds.includes("PROCEDURE");
|
||||
const allowFunction = kinds.includes("FUNCTION");
|
||||
return functions.filter((fn) => {
|
||||
const type = (fn.function_type || "").toUpperCase();
|
||||
if (allowProcedure && type.includes("PROC")) return true;
|
||||
if (allowFunction && (type.includes("FUNC") || type === "" || type === "FUNCTION")) return true;
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
function publishProgress(session: SchemaDiffSession, progress: SchemaDiffSessionProgress): void {
|
||||
session.version += 1;
|
||||
session.progress = progress;
|
||||
@@ -87,68 +104,79 @@ async function runSchemaDiffSession(session: SchemaDiffSession, dependencies: Sc
|
||||
const input = session.config;
|
||||
const tracker = useExportTracker();
|
||||
const options = normalizeSchemaDiffCompareOptions(input.options, input.targetDbType);
|
||||
const routineKinds = schemaDiffRoutineObjectTypesIntersection(input.sourceDbType as DatabaseType, input.targetDbType);
|
||||
const loadRoutines = shouldLoadSchemaDiffRoutines(input.sourceDbType, input.targetDbType, options);
|
||||
const sessionOptions = options;
|
||||
const isPostgresLike = isSchemaDiffPostgresLike(input.targetDbType);
|
||||
const hasExtraObjectPhase = shouldLoadSchemaDiffExtraObjects(input.targetDbType, options);
|
||||
const tableFilter = compileSchemaDiffTableFilter(options);
|
||||
const hasExtraObjectPhase = shouldLoadSchemaDiffExtraObjectPhase(input.sourceDbType, input.targetDbType, sessionOptions);
|
||||
const tableFilter = compileSchemaDiffTableFilter(sessionOptions);
|
||||
const loadTableMetadata = !!(sessionOptions.tables || sessionOptions.views);
|
||||
|
||||
try {
|
||||
publishProgress(session, { phase: "loading-table-lists" });
|
||||
|
||||
const sourceTableIdentity: SchemaDiffTableIdentity = {
|
||||
connectionId: input.sourceConnectionId,
|
||||
database: input.sourceDatabase,
|
||||
schema: input.sourceSchema,
|
||||
};
|
||||
const targetTableIdentity: SchemaDiffTableIdentity = {
|
||||
connectionId: input.targetConnectionId,
|
||||
database: input.targetDatabase,
|
||||
schema: input.targetSchema,
|
||||
};
|
||||
const [sourceTableList, targetTableList] = await Promise.all([dependencies.tableListLoader.load(sourceTableIdentity, { refresh: true }), dependencies.tableListLoader.load(targetTableIdentity, { refresh: true })]);
|
||||
const { sourceTables, targetTables } = filterSchemaDiffTables(sourceTableList, targetTableList, tableFilter, options, options.selectedTables);
|
||||
let sourceTables: TableInfo[] = [];
|
||||
let targetTables: TableInfo[] = [];
|
||||
let sourceDetails: Awaited<ReturnType<typeof loadSchemaDetails>> = [];
|
||||
let targetDetails: Awaited<ReturnType<typeof loadSchemaDetails>> = [];
|
||||
|
||||
publishProgress(session, { phase: "loading-source-details", current: 0, total: sourceTables.length });
|
||||
const sourceDetails = await loadSchemaDetails(
|
||||
sourceTables,
|
||||
{
|
||||
if (loadTableMetadata) {
|
||||
const sourceTableIdentity: SchemaDiffTableIdentity = {
|
||||
connectionId: input.sourceConnectionId,
|
||||
database: input.sourceDatabase,
|
||||
schema: input.sourceSchema,
|
||||
dbType: input.sourceDbType,
|
||||
options,
|
||||
onProgress: (progress) => publishProgress(session, { phase: "loading-source-details", ...progress }),
|
||||
},
|
||||
api,
|
||||
);
|
||||
|
||||
publishProgress(session, { phase: "loading-target-details", current: 0, total: targetTables.length });
|
||||
const targetDetails = await loadSchemaDetails(
|
||||
targetTables,
|
||||
{
|
||||
};
|
||||
const targetTableIdentity: SchemaDiffTableIdentity = {
|
||||
connectionId: input.targetConnectionId,
|
||||
database: input.targetDatabase,
|
||||
schema: input.targetSchema,
|
||||
dbType: input.targetDbType,
|
||||
options,
|
||||
onProgress: (progress) => publishProgress(session, { phase: "loading-target-details", ...progress }),
|
||||
},
|
||||
api,
|
||||
);
|
||||
};
|
||||
const [sourceTableList, targetTableList] = await Promise.all([dependencies.tableListLoader.load(sourceTableIdentity, { refresh: true }), dependencies.tableListLoader.load(targetTableIdentity, { refresh: true })]);
|
||||
({ sourceTables, targetTables } = filterSchemaDiffTables(sourceTableList, targetTableList, tableFilter, sessionOptions, sessionOptions.selectedTables));
|
||||
|
||||
publishProgress(session, { phase: "loading-source-details", current: 0, total: sourceTables.length });
|
||||
sourceDetails = await loadSchemaDetails(
|
||||
sourceTables,
|
||||
{
|
||||
connectionId: input.sourceConnectionId,
|
||||
database: input.sourceDatabase,
|
||||
schema: input.sourceSchema,
|
||||
dbType: input.sourceDbType,
|
||||
options: sessionOptions,
|
||||
onProgress: (progress) => publishProgress(session, { phase: "loading-source-details", ...progress }),
|
||||
},
|
||||
api,
|
||||
);
|
||||
|
||||
publishProgress(session, { phase: "loading-target-details", current: 0, total: targetTables.length });
|
||||
targetDetails = await loadSchemaDetails(
|
||||
targetTables,
|
||||
{
|
||||
connectionId: input.targetConnectionId,
|
||||
database: input.targetDatabase,
|
||||
schema: input.targetSchema,
|
||||
dbType: input.targetDbType,
|
||||
options: sessionOptions,
|
||||
onProgress: (progress) => publishProgress(session, { phase: "loading-target-details", ...progress }),
|
||||
},
|
||||
api,
|
||||
);
|
||||
}
|
||||
|
||||
const promises: Promise<unknown>[] = [];
|
||||
if (isPostgresLike && options.functions) {
|
||||
if (loadRoutines) {
|
||||
promises.push(api.listFunctions(input.sourceConnectionId, input.sourceDatabase, input.sourceSchema));
|
||||
promises.push(api.listFunctions(input.targetConnectionId, input.targetDatabase, input.targetSchema));
|
||||
}
|
||||
if (isPostgresLike && options.sequences) {
|
||||
promises.push(api.listSequences(input.sourceConnectionId, input.sourceDatabase, input.sourceSchema, !!options.sequenceLastValues));
|
||||
promises.push(api.listSequences(input.targetConnectionId, input.targetDatabase, input.targetSchema, !!options.sequenceLastValues));
|
||||
if (isPostgresLike && sessionOptions.sequences) {
|
||||
promises.push(api.listSequences(input.sourceConnectionId, input.sourceDatabase, input.sourceSchema, !!sessionOptions.sequenceLastValues));
|
||||
promises.push(api.listSequences(input.targetConnectionId, input.targetDatabase, input.targetSchema, !!sessionOptions.sequenceLastValues));
|
||||
}
|
||||
if (isPostgresLike && options.rules) {
|
||||
if (isPostgresLike && sessionOptions.rules) {
|
||||
promises.push(api.listRules(input.sourceConnectionId, input.sourceDatabase, input.sourceSchema));
|
||||
promises.push(api.listRules(input.targetConnectionId, input.targetDatabase, input.targetSchema));
|
||||
}
|
||||
if (isPostgresLike && options.owners) {
|
||||
if (isPostgresLike && sessionOptions.owners) {
|
||||
promises.push(api.listOwners(input.sourceConnectionId, input.sourceDatabase, input.sourceSchema));
|
||||
promises.push(api.listOwners(input.targetConnectionId, input.targetDatabase, input.targetSchema));
|
||||
}
|
||||
@@ -156,14 +184,15 @@ async function runSchemaDiffSession(session: SchemaDiffSession, dependencies: Sc
|
||||
if (hasExtraObjectPhase) publishProgress(session, { phase: "loading-extra-objects" });
|
||||
const extraObjects = await Promise.all(promises);
|
||||
let index = 0;
|
||||
const sourceFunctions = options.functions && isPostgresLike ? (extraObjects[index++] as Awaited<ReturnType<typeof api.listFunctions>>) : [];
|
||||
const targetFunctions = options.functions && isPostgresLike ? (extraObjects[index++] as Awaited<ReturnType<typeof api.listFunctions>>) : [];
|
||||
const sourceSequences = options.sequences && isPostgresLike ? (extraObjects[index++] as Awaited<ReturnType<typeof api.listSequences>>) : [];
|
||||
const targetSequences = options.sequences && isPostgresLike ? (extraObjects[index++] as Awaited<ReturnType<typeof api.listSequences>>) : [];
|
||||
const sourceRules = options.rules && isPostgresLike ? (extraObjects[index++] as Awaited<ReturnType<typeof api.listRules>>) : [];
|
||||
const targetRules = options.rules && isPostgresLike ? (extraObjects[index++] as Awaited<ReturnType<typeof api.listRules>>) : [];
|
||||
const sourceOwners = options.owners && isPostgresLike ? (extraObjects[index++] as Awaited<ReturnType<typeof api.listOwners>>) : [];
|
||||
const targetOwners = options.owners && isPostgresLike ? (extraObjects[index++] as Awaited<ReturnType<typeof api.listOwners>>) : [];
|
||||
const listedSourceFunctions = loadRoutines ? filterFunctionsByRoutineKinds(extraObjects[index++] as FunctionInfo[], routineKinds) : [];
|
||||
const listedTargetFunctions = loadRoutines ? filterFunctionsByRoutineKinds(extraObjects[index++] as FunctionInfo[], routineKinds) : [];
|
||||
const { sourceFunctions, targetFunctions } = filterSchemaDiffFunctions(listedSourceFunctions, listedTargetFunctions, sessionOptions.selectedRoutines, sessionOptions.routineMappings ?? []);
|
||||
const sourceSequences = sessionOptions.sequences && isPostgresLike ? (extraObjects[index++] as Awaited<ReturnType<typeof api.listSequences>>) : [];
|
||||
const targetSequences = sessionOptions.sequences && isPostgresLike ? (extraObjects[index++] as Awaited<ReturnType<typeof api.listSequences>>) : [];
|
||||
const sourceRules = sessionOptions.rules && isPostgresLike ? (extraObjects[index++] as Awaited<ReturnType<typeof api.listRules>>) : [];
|
||||
const targetRules = sessionOptions.rules && isPostgresLike ? (extraObjects[index++] as Awaited<ReturnType<typeof api.listRules>>) : [];
|
||||
const sourceOwners = sessionOptions.owners && isPostgresLike ? (extraObjects[index++] as Awaited<ReturnType<typeof api.listOwners>>) : [];
|
||||
const targetOwners = sessionOptions.owners && isPostgresLike ? (extraObjects[index++] as Awaited<ReturnType<typeof api.listOwners>>) : [];
|
||||
|
||||
publishProgress(session, { phase: "comparing" });
|
||||
const result = await api.prepareSchemaDiff({
|
||||
@@ -179,29 +208,29 @@ async function runSchemaDiffSession(session: SchemaDiffSession, dependencies: Sc
|
||||
targetRules,
|
||||
sourceOwners,
|
||||
targetOwners,
|
||||
tableMappings: options.selectedTables === undefined ? undefined : options.tableMappings,
|
||||
tableMappings: sessionOptions.selectedTables === undefined ? undefined : sessionOptions.tableMappings,
|
||||
databaseType: input.targetDbType,
|
||||
targetSchema: schemaDiffDeployTargetSchema(input.targetDbType, input.targetDatabase, input.targetSchema),
|
||||
ignoreComments: input.ignoreComments,
|
||||
cascadeDelete: options.cascadeDelete ?? false,
|
||||
compareColumnOrder: options.compareColumnOrder,
|
||||
ignoreTableNameCase: options.ignoreTableNameCase,
|
||||
ignoreColumnNameCase: options.ignoreColumnNameCase,
|
||||
detectRenames: options.detectRenames ?? false,
|
||||
detectTableRenames: options.detectTableRenames ?? false,
|
||||
renameThreshold: options.renameThreshold ?? 0.5,
|
||||
enableRollback: options.enableRollback ?? false,
|
||||
batchPatterns: options.batchPatterns
|
||||
? options.batchPatterns
|
||||
cascadeDelete: sessionOptions.cascadeDelete ?? false,
|
||||
compareColumnOrder: sessionOptions.compareColumnOrder,
|
||||
ignoreTableNameCase: sessionOptions.ignoreTableNameCase,
|
||||
ignoreColumnNameCase: sessionOptions.ignoreColumnNameCase,
|
||||
detectRenames: sessionOptions.detectRenames ?? false,
|
||||
detectTableRenames: sessionOptions.detectTableRenames ?? false,
|
||||
renameThreshold: sessionOptions.renameThreshold ?? 0.5,
|
||||
enableRollback: sessionOptions.enableRollback ?? false,
|
||||
batchPatterns: sessionOptions.batchPatterns
|
||||
? sessionOptions.batchPatterns
|
||||
.split(",")
|
||||
.map((value) => value.trim())
|
||||
.filter(Boolean)
|
||||
: undefined,
|
||||
sourceDialect: options.sourceDialect ? normalizeDialectKind(options.sourceDialect) : input.sourceDbType ? databaseTypeToDialectKind(input.sourceDbType as DatabaseType) : undefined,
|
||||
targetDialect: options.targetDialect ? normalizeDialectKind(options.targetDialect) : input.targetDbType ? databaseTypeToDialectKind(input.targetDbType) : undefined,
|
||||
compatibilityThreshold: options.compatibilityThreshold ?? 0.5,
|
||||
sourceDialect: sessionOptions.sourceDialect ? normalizeDialectKind(sessionOptions.sourceDialect) : input.sourceDbType ? databaseTypeToDialectKind(input.sourceDbType as DatabaseType) : undefined,
|
||||
targetDialect: sessionOptions.targetDialect ? normalizeDialectKind(sessionOptions.targetDialect) : input.targetDbType ? databaseTypeToDialectKind(input.targetDbType) : undefined,
|
||||
compatibilityThreshold: sessionOptions.compatibilityThreshold ?? 0.5,
|
||||
fieldMappings:
|
||||
options.fieldMappings?.map((mapping) => ({
|
||||
sessionOptions.fieldMappings?.map((mapping) => ({
|
||||
sourceType: mapping.sourceType,
|
||||
targetType: mapping.targetType,
|
||||
paramStrategy: mapping.paramStrategy ?? "preserve",
|
||||
@@ -275,7 +304,7 @@ export function removeSchemaDiffSession(id: string): boolean {
|
||||
}
|
||||
|
||||
export function schemaDiffSessionNextProgressStep(session: SchemaDiffSession): string | null {
|
||||
return getSchemaDiffNextProgressStep(session.progress?.phase, shouldLoadSchemaDiffExtraObjects(session.config.targetDbType, session.config.options));
|
||||
return getSchemaDiffNextProgressStep(session.progress?.phase, shouldLoadSchemaDiffExtraObjectPhase(session.config.sourceDbType, session.config.targetDbType, session.config.options));
|
||||
}
|
||||
|
||||
export function schemaDiffSessionObjects(result: SchemaDiffPreparation | null): SchemaDiffObject[] {
|
||||
|
||||
@@ -4,6 +4,7 @@ import type { useQueryStore } from "@/stores/queryStore";
|
||||
import type { useSettingsStore } from "@/stores/settingsStore";
|
||||
import type { TreeNode } from "@/types/database";
|
||||
import { allDatabasesExportSourceForNode, databaseExportSourceForNode, sidebarSameSchemaStructureTargets } from "@/lib/sidebar/sidebarExportRuntime";
|
||||
import { schemaDiffRoutineKey } from "@/lib/schema/schemaDiffRoutine";
|
||||
|
||||
interface SidebarTreeToolRuntimeOptions {
|
||||
activeNode: ShallowRef<TreeNode>;
|
||||
@@ -26,15 +27,26 @@ export function useSidebarTreeToolRuntime(options: SidebarTreeToolRuntimeOptions
|
||||
};
|
||||
}
|
||||
|
||||
function openSchemaDiff() {
|
||||
function openSchemaDiff(options?: { selectedRoutines?: string[]; preferredResultTab?: "tables" | "routines" }) {
|
||||
if (!activeNode.value.connectionId) return;
|
||||
connectionStore.schemaDiffSource = {
|
||||
connectionId: activeNode.value.connectionId,
|
||||
database: activeNode.value.database ?? "",
|
||||
schema: activeNode.value.schema,
|
||||
selectedRoutines: options?.selectedRoutines,
|
||||
preferredResultTab: options?.preferredResultTab,
|
||||
};
|
||||
}
|
||||
|
||||
function openSchemaDiffForRoutine() {
|
||||
const node = activeNode.value;
|
||||
if (!node.connectionId || (node.type !== "procedure" && node.type !== "function")) return;
|
||||
openSchemaDiff({
|
||||
selectedRoutines: [schemaDiffRoutineKey(node.objectName || node.label, node.signature ?? "")],
|
||||
preferredResultTab: "routines",
|
||||
});
|
||||
}
|
||||
|
||||
function openDataCompare() {
|
||||
if (!activeNode.value.connectionId) return;
|
||||
connectionStore.dataCompareSource = {
|
||||
@@ -170,6 +182,7 @@ export function useSidebarTreeToolRuntime(options: SidebarTreeToolRuntimeOptions
|
||||
openFieldLineage,
|
||||
openScheduledBackups,
|
||||
openSchemaDiff,
|
||||
openSchemaDiffForRoutine,
|
||||
openSqlFileExecution,
|
||||
openStructureEditor,
|
||||
openTableImport,
|
||||
|
||||
Reference in new issue
Block a user