feat(schema-diff): support selecting tables to compare

This commit is contained in:
jearhe authored and GitHub committed 2026-08-18 15:31:00 +08:00
1 parent 0facea2740
commit b3b6d29099
21 files changed
+933 -69

No files matched your search

@@ -0,0 +1,24 @@
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,96 @@
import { describe, expect, it } from "vitest";
import { filterTableNames, isEveryFilteredSelected, toggleSelectFiltered, toggleTableName } from "@/lib/diff/tableMultiSelect";
describe("tableMultiSelect helpers", () => {
const tables = ["users", "active_users", "orders", "audit_log", "payments"];
describe("filterTableNames", () => {
it("returns all tables for an empty/whitespace query", () => {
expect(filterTableNames(tables, "")).toEqual(tables);
expect(filterTableNames(tables, " ")).toEqual(tables);
});
it("filters by case-insensitive substring", () => {
expect(filterTableNames(tables, "USER")).toEqual(["users", "active_users"]);
expect(filterTableNames(tables, "pay")).toEqual(["payments"]);
});
it("returns an empty list when nothing matches", () => {
expect(filterTableNames(tables, "zzz")).toEqual([]);
});
});
describe("toggleTableName", () => {
it("adds a table to the selection", () => {
expect(toggleTableName([], "users")).toEqual(["users"]);
expect(toggleTableName(["orders"], "users")).toEqual(["orders", "users"]);
});
it("removes a table from the selection", () => {
expect(toggleTableName(["users", "orders"], "users")).toEqual(["orders"]);
});
it("returns a new array and never duplicates", () => {
const input = ["users"];
const next = toggleTableName(input, "users");
expect(next).toEqual([]);
expect(next).not.toBe(input);
});
});
describe("isEveryFilteredSelected", () => {
it("is false for an empty filtered set", () => {
expect(isEveryFilteredSelected(["users"], [])).toBe(false);
});
it("is true only when every filtered table is selected", () => {
expect(isEveryFilteredSelected(["users", "orders"], ["users", "orders"])).toBe(true);
expect(isEveryFilteredSelected(["users"], ["users", "orders"])).toBe(false);
expect(isEveryFilteredSelected([], ["users"])).toBe(false);
});
});
describe("toggleSelectFiltered", () => {
it("selects all filtered tables when not all are selected (select-all)", () => {
const result = toggleSelectFiltered(["users"], [...tables]);
expect(result).toEqual([...tables]);
});
it("deselects all filtered tables when all are already selected (deselect-all)", () => {
const result = toggleSelectFiltered([...tables], [...tables]);
expect(result).toEqual([]);
});
it("only affects the filtered subset and preserves other selections", () => {
const filtered = ["users", "orders"];
const result = toggleSelectFiltered(["payments"], filtered);
expect(result).toEqual(["payments", "users", "orders"]);
});
it("after a search, select-all only toggles the current filtered subset", () => {
const filtered = filterTableNames(tables, "user"); // users, active_users
const selected = ["orders", "payments"];
const afterSelect = toggleSelectFiltered(selected, filtered);
expect([...afterSelect].sort()).toEqual(["active_users", "orders", "payments", "users"]);
});
it("keeps already-selected tables untouched by the filtered subset change", () => {
// simulating: selected [users, orders], search narrowed to [users], deselect-all removes only users
const filtered = ["users"];
const result = toggleSelectFiltered(["users", "orders"], filtered);
expect(result).toEqual(["orders"]);
});
});
it("selection persists across a changing search query (search is independent of selection)", () => {
// selection state is external; filtering only selects which rows are visible
const selected = ["users", "audit_log"];
const visibleA = filterTableNames(tables, "user");
const visibleB = filterTableNames(tables, "audit");
expect(visibleA).toEqual(["users", "active_users"]);
expect(visibleB).toEqual(["audit_log"]);
// both still reflect the same underlying selection
expect(isEveryFilteredSelected(selected, ["users"])).toBe(true);
expect(isEveryFilteredSelected(selected, ["audit_log"])).toBe(true);
});
});
@@ -44,4 +44,67 @@ describe("schemaDiffTableFilter", () => {
expect(result.sourceTables.map((table) => table.name)).toEqual(["users", "monthly_users"]);
});
// ---- Visual (explicit) table selection (#6533) ----
const all: TableInfo[] = [
{ name: "users", table_type: "BASE TABLE" },
{ name: "active_users", table_type: "BASE TABLE" },
{ name: "orders", table_type: "BASE TABLE" },
{ name: "orders_bak", table_type: "BASE TABLE" },
];
const baseOptions = { ...DEFAULT_MYSQL_OPTIONS, tables: true, views: true };
it("keeps legacy behavior when no visual selection is set (undefined selection = no restriction)", () => {
const options = { ...baseOptions, tableIncludePattern: "^orders$", tableExcludePattern: "_bak$" };
const result = filterSchemaDiffTables(all, all, compileSchemaDiffTableFilter(options), options, undefined);
expect(result.sourceTables.map((t) => t.name)).toEqual(["orders"]);
});
it("restricts to the explicitly selected tables only", () => {
const result = filterSchemaDiffTables(all, all, compileSchemaDiffTableFilter(baseOptions), baseOptions, ["users", "orders"]);
expect(result.sourceTables.map((t) => t.name)).toEqual(["users", "orders"]);
});
it("intersects visual selection with the include regex", () => {
const options = { ...baseOptions, tableIncludePattern: "^users$" };
const result = filterSchemaDiffTables(all, all, compileSchemaDiffTableFilter(options), options, ["users", "orders"]);
expect(result.sourceTables.map((t) => t.name)).toEqual(["users"]);
});
it("intersects visual selection with the exclude regex", () => {
const options = { ...baseOptions, tableExcludePattern: "_bak$" };
const result = filterSchemaDiffTables(all, all, compileSchemaDiffTableFilter(options), options, ["orders", "orders_bak"]);
expect(result.sourceTables.map((t) => t.name)).toEqual(["orders"]);
});
it("treats an explicitly empty selection ([]) as selecting nothing, distinct from undefined", () => {
const result = filterSchemaDiffTables(all, all, compileSchemaDiffTableFilter(baseOptions), baseOptions, []);
expect(result.sourceTables).toEqual([]);
});
it("keeps selected source tables when same-name targets are missing", () => {
const sourceOnly: TableInfo[] = [
{ name: "a", table_type: "BASE TABLE" },
{ name: "b", table_type: "BASE TABLE" },
{ name: "c", table_type: "BASE TABLE" },
];
const targetPartly: TableInfo[] = [
{ name: "a", table_type: "BASE TABLE" },
{ name: "c", table_type: "BASE TABLE" },
];
const result = filterSchemaDiffTables(sourceOnly, targetPartly, compileSchemaDiffTableFilter(baseOptions), baseOptions, ["a", "b", "c"]);
expect(result.sourceTables.map((t) => t.name)).toEqual(["a", "b", "c"]);
expect(result.targetTables.map((t) => t.name)).toEqual(["a", "c"]);
});
it("does not include unselected target tables that would otherwise look like drops", () => {
const source: TableInfo[] = [{ name: "a", table_type: "BASE TABLE" }];
const target: TableInfo[] = [
{ name: "a", table_type: "BASE TABLE" },
{ name: "b", table_type: "BASE TABLE" },
];
const result = filterSchemaDiffTables(source, target, compileSchemaDiffTableFilter(baseOptions), baseOptions, ["a"]);
expect(result.sourceTables.map((t) => t.name)).toEqual(["a"]);
expect(result.targetTables.map((t) => t.name)).toEqual(["a"]);
});
});
@@ -0,0 +1,117 @@
import { describe, expect, it, vi } from "vitest";
import { createSchemaDiffTableListCoordinator, createSchemaDiffTableListLoader, reconcileSchemaDiffSelectedTables, shouldLoadSchemaDiffTableList, type SchemaDiffTableIdentity, type SchemaDiffTableSide } from "@/lib/schema/schemaDiffTableList";
import type { TableInfo } from "@/types/database";
function table(name: string): TableInfo {
return { name, table_type: "BASE TABLE" };
}
function deferred<T>() {
let resolve!: (value: T) => void;
let reject!: (reason?: unknown) => void;
const promise = new Promise<T>((resolvePromise, rejectPromise) => {
resolve = resolvePromise;
reject = rejectPromise;
});
return { promise, resolve, reject };
}
describe("schemaDiffTableList", () => {
it("keeps the unrestricted config path free of table-list requests", async () => {
const listTables = vi.fn().mockResolvedValue([table("users")]);
const loader = createSchemaDiffTableListLoader({ ensureConnected: vi.fn().mockResolvedValue(undefined), listTables });
const identities: Record<SchemaDiffTableSide, SchemaDiffTableIdentity> = {
source: { connectionId: "source", database: "app", schema: "public" },
target: { connectionId: "target", database: "app", schema: "public" },
};
const coordinator = createSchemaDiffTableListCoordinator({
loader,
getIdentity: (side) => identities[side],
setTables: vi.fn(),
});
await coordinator.refresh("source", shouldLoadSchemaDiffTableList("source", false, 0, true));
await coordinator.refresh("target", shouldLoadSchemaDiffTableList("target", false, 0, true));
expect(listTables).not.toHaveBeenCalled();
expect(shouldLoadSchemaDiffTableList("target", true, 0, true)).toBe(false);
});
it("loads each side at most once and reuses successful results for Compare", async () => {
const listTables = vi.fn(async (connectionId: string) => [table(`${connectionId}_table`)]);
const loader = createSchemaDiffTableListLoader({ ensureConnected: vi.fn().mockResolvedValue(undefined), listTables });
const identities: Record<SchemaDiffTableSide, SchemaDiffTableIdentity> = {
source: { connectionId: "source", database: "app", schema: "public" },
target: { connectionId: "target", database: "app", schema: "public" },
};
const coordinator = createSchemaDiffTableListCoordinator({
loader,
getIdentity: (side) => identities[side],
setTables: vi.fn(),
});
await coordinator.refresh("source", shouldLoadSchemaDiffTableList("source", true, 0, true));
await coordinator.refresh("target", shouldLoadSchemaDiffTableList("target", true, 1, true));
await Promise.all([loader.load(identities.source), loader.load(identities.target)]);
expect(listTables).toHaveBeenCalledTimes(2);
expect(listTables).toHaveBeenCalledWith("source", "app", "public");
expect(listTables).toHaveBeenCalledWith("target", "app", "public");
});
it("prunes deleted selections only after a successful source load", async () => {
const listTables = vi.fn(async (_connectionId: string, database: string) => {
if (database === "unavailable") throw new Error("offline");
return [table("users")];
});
const loader = createSchemaDiffTableListLoader({ ensureConnected: vi.fn().mockResolvedValue(undefined), listTables });
let sourceIdentity: SchemaDiffTableIdentity = { connectionId: "source", database: "app", schema: "public" };
let selectedTables = ["deleted_table"];
const coordinator = createSchemaDiffTableListCoordinator({
loader,
getIdentity: () => sourceIdentity,
setTables: vi.fn(),
onSourceTablesLoaded: (tables) => {
selectedTables = reconcileSchemaDiffSelectedTables(
selectedTables,
tables.map((entry) => entry.name),
);
},
});
await coordinator.refresh("source", true);
expect(selectedTables).toEqual([]);
selectedTables = ["keep_on_error"];
sourceIdentity = { ...sourceIdentity, database: "unavailable" };
await coordinator.refresh("source", true);
expect(selectedTables).toEqual(["keep_on_error"]);
});
it("ignores an older response after the source identity changes", async () => {
const sourceA = deferred<TableInfo[]>();
const sourceB = deferred<TableInfo[]>();
const listTables = vi.fn((_connectionId: string, database: string) => (database === "a" ? sourceA.promise : sourceB.promise));
const loader = createSchemaDiffTableListLoader({ ensureConnected: vi.fn().mockResolvedValue(undefined), listTables });
let sourceIdentity: SchemaDiffTableIdentity = { connectionId: "source", database: "a", schema: "public" };
let visibleTables: TableInfo[] = [];
const coordinator = createSchemaDiffTableListCoordinator({
loader,
getIdentity: () => sourceIdentity,
setTables: (_side, tables) => {
visibleTables = tables;
},
});
const loadA = coordinator.refresh("source", true);
sourceIdentity = { ...sourceIdentity, database: "b" };
const loadB = coordinator.refresh("source", true);
sourceB.resolve([table("b_table")]);
await loadB;
sourceA.resolve([table("a_table")]);
await loadA;
expect(visibleTables.map((entry) => entry.name)).toEqual(["b_table"]);
});
});
@@ -0,0 +1,23 @@
/**
* Same-name target table matching, shared by Data Compare and Schema Diff.
* Given the selected source table set and the available target tables, produces
* which source tables matched a same-name target and which are missing.
*/
export interface SameNameTableMatchResult {
matched: string[];
missing: string[];
}
export function buildSameNameTableMatches(sourceTables: string[], targetTables: string[]): SameNameTableMatchResult {
const targetSet = new Set(targetTables);
const matched: string[] = [];
const missing: string[] = [];
for (const source of sourceTables) {
if (targetSet.has(source)) {
matched.push(source);
} else {
missing.push(source);
}
}
return { matched, missing };
}
@@ -0,0 +1,43 @@
/**
* Pure selection/filtering helpers for the shared TableMultiSelect UI.
*
* These are framework-agnostic (no Vue state) so the behavior of the shared
* multi-table selector can be unit-tested independently of any component
* mount infrastructure, and so the same logic backs both Data Compare and
* Schema Diff table selection.
*/
/** Case-insensitive substring filter over table names. An empty/whitespace query returns all tables. */
export function filterTableNames(tables: string[], query: string): string[] {
const trimmed = query.trim().toLowerCase();
if (!trimmed) return tables;
return tables.filter((table) => table.toLowerCase().includes(trimmed));
}
/** Toggle a single table name within a selection list (preserves order, no duplicates). */
export function toggleTableName(selected: string[], table: string): string[] {
if (selected.includes(table)) {
return selected.filter((name) => name !== table);
}
return [...selected, table];
}
/** True when every entry of `filtered` is already present in `selected`. */
export function isEveryFilteredSelected(selected: string[], filtered: string[]): boolean {
return filtered.length > 0 && filtered.every((table) => selected.includes(table));
}
/**
* Select every table in `filtered` if not all are selected; otherwise deselect them all.
* Only tables within `filtered` are touched — the rest of `selected` is preserved, so
* selecting-all after a search only affects the current filtered subset.
*/
export function toggleSelectFiltered(selected: string[], filtered: string[]): string[] {
const next = new Set(selected);
if (isEveryFilteredSelected(selected, filtered)) {
filtered.forEach((table) => next.delete(table));
} else {
filtered.forEach((table) => next.add(table));
}
return [...next];
}
@@ -49,8 +49,16 @@ function isSchemaDiffObjectEnabled(table: TableInfo, options: Pick<SchemaDiffCom
return isSchemaDiffView(table) ? options.views : options.tables;
}
export function filterSchemaDiffTables(sourceTables: TableInfo[], targetTables: TableInfo[], filter: CompiledSchemaDiffTableFilter, options: Pick<SchemaDiffCompareOptions, "tables" | "views"> = { tables: true, views: true }): FilteredSchemaDiffTables {
const includeTable = (table: TableInfo) => isSchemaDiffObjectEnabled(table, options) && matchesSchemaDiffTableFilter(table.name, filter);
export function filterSchemaDiffTables(sourceTables: TableInfo[], targetTables: TableInfo[], filter: CompiledSchemaDiffTableFilter, options: Pick<SchemaDiffCompareOptions, "tables" | "views"> = { tables: true, views: true }, selectedTables?: string[]): FilteredSchemaDiffTables {
// Visual (explicit) table selection is applied first, then the existing include/exclude
// regex filter. Source and target use the SAME selected table set, so targets are matched
// by the same name and any target missing a same-name table simply drops out (surfaced to
// the user in the UI as "missing"). Semantics:
// - `undefined`: no visual restriction — legacy all-tables behavior + regex filter.
// - `[]`: restriction explicitly enabled with an empty selection — compare nothing.
// - `["a", "b"]`: restrict to exactly those table names.
const selectedSet = selectedTables === undefined ? null : new Set(selectedTables);
const includeTable = (table: TableInfo) => isSchemaDiffObjectEnabled(table, options) && (!selectedSet || selectedSet.has(table.name)) && matchesSchemaDiffTableFilter(table.name, filter);
return {
sourceTables: sourceTables.filter(includeTable),
@@ -0,0 +1,101 @@
import type { TableInfo } from "@/types/database";
export type SchemaDiffTableSide = "source" | "target";
export interface SchemaDiffTableIdentity {
connectionId: string;
database: string;
schema: string;
}
export interface SchemaDiffTableListLoader {
load(identity: SchemaDiffTableIdentity): Promise<TableInfo[]>;
}
interface SchemaDiffTableListLoaderDependencies {
ensureConnected(connectionId: string): Promise<unknown>;
listTables(connectionId: string, database: string, schema: string): Promise<TableInfo[]>;
}
interface SchemaDiffTableListCoordinatorOptions {
loader: SchemaDiffTableListLoader;
getIdentity(side: SchemaDiffTableSide): SchemaDiffTableIdentity;
setTables(side: SchemaDiffTableSide, tables: TableInfo[]): void;
onSourceTablesLoaded?(tables: TableInfo[]): void;
}
function identityKey(identity: SchemaDiffTableIdentity): string {
return JSON.stringify([identity.connectionId, identity.database, identity.schema]);
}
export function sameSchemaDiffTableIdentity(left: SchemaDiffTableIdentity, right: SchemaDiffTableIdentity): boolean {
return left.connectionId === right.connectionId && left.database === right.database && left.schema === right.schema;
}
export function shouldLoadSchemaDiffTableList(side: SchemaDiffTableSide, restricted: boolean, selectedTableCount: number, identityReady: boolean): boolean {
return identityReady && restricted && (side === "source" || selectedTableCount > 0);
}
export function reconcileSchemaDiffSelectedTables(selectedTables: string[], availableTables: string[]): string[] {
const available = new Set(availableTables);
return selectedTables.filter((table) => available.has(table));
}
export function createSchemaDiffTableListLoader(dependencies: SchemaDiffTableListLoaderDependencies): SchemaDiffTableListLoader {
const successful = new Map<string, TableInfo[]>();
const pending = new Map<string, Promise<TableInfo[]>>();
return {
async load(identity) {
const key = identityKey(identity);
const inFlight = pending.get(key);
if (inFlight) return inFlight;
const cached = successful.get(key);
if (cached) {
await dependencies.ensureConnected(identity.connectionId);
return cached;
}
const request = dependencies
.ensureConnected(identity.connectionId)
.then(() => dependencies.listTables(identity.connectionId, identity.database, identity.schema))
.then((tables) => {
const normalized = Array.isArray(tables) ? tables : [];
successful.set(key, normalized);
return normalized;
})
.finally(() => pending.delete(key));
pending.set(key, request);
return request;
},
};
}
export function createSchemaDiffTableListCoordinator(options: SchemaDiffTableListCoordinatorOptions) {
const generations: Record<SchemaDiffTableSide, number> = { source: 0, target: 0 };
return {
async refresh(side: SchemaDiffTableSide, enabled: boolean): Promise<boolean> {
const generation = ++generations[side];
const identity = options.getIdentity(side);
options.setTables(side, []);
if (!enabled) return false;
try {
const tables = await options.loader.load(identity);
if (generation !== generations[side] || !sameSchemaDiffTableIdentity(identity, options.getIdentity(side))) return false;
options.setTables(side, tables);
if (side === "source") options.onSourceTablesLoaded?.(tables);
return true;
} catch {
if (generation === generations[side] && sameSchemaDiffTableIdentity(identity, options.getIdentity(side))) {
options.setTables(side, []);
}
return false;
}
},
};
}