feat(schema-diff): preserve dialect-specific DDL semantics

This commit is contained in:
dbin0123 authored and GitHub committed 2026-07-29 03:47:19 +08:00
1 parent 820b8cd26f
commit 002a2c85c0
146 files changed
+36532 -1375

No files matched your search

+331 -4
View File
@@ -1,5 +1,111 @@
import type { ColumnInfo, IndexInfo, ForeignKeyInfo, TriggerInfo, FunctionInfo, SequenceInfo, RuleInfo, OwnerInfo, DatabaseType, TableInfo } from "@/types/database";
const DIALECT_KIND_MAP: Record<string, string> = {
mysql: "mysql",
doris: "mysql",
starrocks: "mysql",
goldendb: "mysql",
sundb: "mysql",
databend: "mysql",
gbase: "mysql",
postgres: "postgres",
gaussdb: "postgres",
kwdb: "postgres",
opengauss: "postgres",
highgo: "postgres",
vastbase: "postgres",
kingbase: "postgres",
firebird: "postgres",
redshift: "postgres",
vertica: "postgres",
exasol: "postgres",
sqlite: "sqlite",
rqlite: "sqlite",
turso: "sqlite",
duckdb: "duckdb",
sqlserver: "sql_server",
access: "sql_server",
oracle: "oracle",
dameng: "oracle",
"oceanbase-oracle": "oracle",
iris: "oracle",
yashandb: "oracle",
xugu: "oracle",
h2: "h2",
clickhouse: "click_house",
manticoresearch: "manticore_search",
informix: "informix",
questdb: "questdb",
};
export function databaseTypeToDialectKind(dbType: DatabaseType): string {
return DIALECT_KIND_MAP[dbType] ?? "unsupported";
}
const DIALECT_ALIAS_MAP: Record<string, string> = {
access: "sql_server",
mssql: "sql_server",
"sql server": "sql_server",
postgresql: "postgres",
sqlite3: "sqlite",
"oceanbase-oracle": "oracle",
oceanbase: "oracle",
dameng: "oracle",
iris: "oracle",
yashandb: "oracle",
xugu: "oracle",
gaussdb: "postgres",
kwdb: "postgres",
opengauss: "postgres",
highgo: "postgres",
vastbase: "postgres",
kingbase: "postgres",
firebird: "postgres",
redshift: "postgres",
vertica: "postgres",
exasol: "postgres",
doris: "mysql",
starrocks: "mysql",
goldendb: "mysql",
sundb: "mysql",
databend: "mysql",
gbase: "mysql",
rqlite: "sqlite",
turso: "sqlite",
manticore: "manticore_search",
questdb: "questdb",
clickhouse: "click_house",
};
export function normalizeDialectKind(input: string): string {
const lower = input.trim().toLowerCase();
if (DIALECT_KIND_MAP[lower]) return DIALECT_KIND_MAP[lower];
if (DIALECT_ALIAS_MAP[lower]) return DIALECT_ALIAS_MAP[lower];
return lower;
}
function levenshteinDistance(a: string, b: string): number {
const m = a.length;
const n = b.length;
if (m === 0) return n;
if (n === 0) return m;
let prev = Array.from({ length: n + 1 }, (_, i) => i);
for (let i = 1; i <= m; i++) {
const curr = [i];
for (let j = 1; j <= n; j++) {
curr[j] = a[i - 1] === b[j - 1] ? prev[j - 1] : Math.min(prev[j], curr[j - 1], prev[j - 1]) + 1;
}
prev = curr;
}
return prev[n];
}
function nameSimilarity(a: string, b: string): number {
const maxLen = Math.max(a.length, b.length);
if (maxLen === 0) return 1;
return 1 - levenshteinDistance(a, b) / maxLen;
}
export interface ColumnDiff {
type: "added" | "removed" | "modified";
name: string;
@@ -65,7 +171,7 @@ export interface OwnerDiff {
}
export interface TableDiff {
type: "added" | "removed" | "modified";
type: "added" | "removed" | "modified" | "renamed";
objectType?: "table" | "view";
name: string;
columns?: ColumnDiff[];
@@ -88,6 +194,11 @@ export interface TableSchemaDetail {
ddl?: string;
}
export interface FieldMappingEntry {
sourceType: string;
targetType: string;
}
export interface SchemaDiffPreparationOptions {
sourceTables: TableInfo[];
targetTables: TableInfo[];
@@ -106,8 +217,58 @@ export interface SchemaDiffPreparationOptions {
ignoreComments?: boolean;
cascadeDelete?: boolean;
compareColumnOrder?: boolean;
detectRenames?: boolean;
detectTableRenames?: boolean;
renameThreshold?: number;
enableRollback?: boolean;
batchPatterns?: string[];
sourceDialect?: string;
targetDialect?: string;
compatibilityThreshold?: number;
fieldMappings?: FieldMappingEntry[];
}
export interface RenameCandidate {
sourceName: string;
targetName: string;
score: number;
}
export interface CompatibilityWarning {
table: string;
column: string;
sourceType: string;
targetType: string;
risk: string;
message: string;
}
export interface PermissionDiff {
objectName: string;
permissionType: string;
sourcePermission: string | null;
targetPermission: string | null;
}
export interface DependencyNode {
tableName: string;
dependsOn: string[];
dependedBy: string[];
}
export interface DependencyGraph {
nodes: DependencyNode[];
}
export interface MissingRollbackObject {
kind: string;
name: string;
table?: string;
reason: string;
}
export type RollbackCompleteness = "complete" | "incomplete";
export interface SchemaDiffPreparation {
diffs: TableDiff[];
functionDiffs?: FunctionDiff[];
@@ -115,6 +276,15 @@ export interface SchemaDiffPreparation {
ruleDiffs?: RuleDiff[];
ownerDiffs?: OwnerDiff[];
syncSql: string;
rollbackSyncSql?: string;
rollbackCompleteness?: RollbackCompleteness;
missingRollbackObjects?: MissingRollbackObject[];
renameCandidates?: RenameCandidate[];
rollbackGraph?: unknown;
compatibilityWarnings?: CompatibilityWarning[];
permissionDiffs?: PermissionDiff[];
permissionSyncSql?: string;
dependencyGraph?: DependencyGraph;
}
const MYSQL_LIKE_SCHEMA_DIFF_TARGET_TYPES = new Set<DatabaseType>(["mysql", "doris", "starrocks", "goldendb", "sundb", "databend", "gbase"]);
@@ -146,10 +316,17 @@ export interface SchemaDiffObject {
sourceDdl?: string;
targetDdl?: string;
deploySql?: string;
rollbackDdl?: string;
changes?: string[];
children?: SchemaDiffObject[];
/** Function arguments signature (for PostgreSQL overloaded functions) */
arguments?: string;
renameMetadata?: {
confirmed: boolean;
sourceName?: string;
targetName?: string;
score?: number;
};
}
export interface SchemaDiffGroup {
@@ -164,6 +341,7 @@ export interface SchemaDiffGroup {
export function getOperationType(diffType: string): DiffOperationType {
switch (diffType) {
case "modified":
case "renamed":
return "modify";
case "added":
return "create";
@@ -203,23 +381,27 @@ function buildSequenceDdl(seq: SequenceInfo): string {
return parts.join("\n");
}
export function convertToSchemaDiffObjects(tableDiffs: TableDiff[], functionDiffs: FunctionDiff[] = [], sequenceDiffs: SequenceDiff[] = [], ruleDiffs: RuleDiff[] = [], ownerDiffs: OwnerDiff[] = []): SchemaDiffObject[] {
export function convertToSchemaDiffObjects(tableDiffs: TableDiff[], functionDiffs: FunctionDiff[] = [], sequenceDiffs: SequenceDiff[] = [], ruleDiffs: RuleDiff[] = [], ownerDiffs: OwnerDiff[] = [], renameCandidates?: RenameCandidate[]): SchemaDiffObject[] {
const objects: SchemaDiffObject[] = [];
for (const diff of tableDiffs) {
const opType = getOperationType(diff.type);
const isRenamed = diff.type === "renamed";
const newName = isRenamed && renameCandidates ? (renameCandidates.find((rc) => rc.sourceName === diff.name)?.targetName ?? diff.name) : undefined;
const obj: SchemaDiffObject = {
id: `table-${diff.name}`,
operationType: opType,
objectKind: diff.objectType === "view" ? "view" : "table",
name: diff.name,
sourceName: diff.type === "added" ? undefined : diff.name,
targetName: diff.type === "removed" ? undefined : diff.name,
targetName: diff.type === "removed" ? undefined : isRenamed ? newName : diff.name,
selected: opType !== "none",
sourceDdl: diff.ddl,
targetDdl: diff.targetDdl,
deploySql: diff.syncSql,
deploySql: isRenamed && newName ? (diff.objectType === "view" ? `ALTER VIEW ${diff.name} RENAME TO ${newName};` : `RENAME TABLE ${diff.name} TO ${newName};`) : diff.syncSql,
changes: diff.columns?.flatMap((c) => c.changes || []),
renameMetadata: isRenamed && newName ? { confirmed: true, sourceName: diff.name, targetName: newName, score: renameCandidates?.find((rc) => rc.sourceName === diff.name)?.score } : undefined,
children: [
...(diff.columns?.map((c) => ({
id: `col-${diff.name}-${c.name}`,
@@ -324,6 +506,24 @@ export function convertToSchemaDiffObjects(tableDiffs: TableDiff[], functionDiff
});
}
// Pre-mark rename candidates on diff objects (for UI display before user confirms)
if (renameCandidates && renameCandidates.length > 0) {
for (const rc of renameCandidates) {
for (const obj of objects) {
// Backend-detected renames: diff_type = "renamed", already has metadata set above
if (obj.renameMetadata) continue;
// Legacy: mark rename candidates on delete+create pairs (fallback for older backends)
if (obj.operationType === "delete" && obj.name === rc.sourceName) {
obj.renameMetadata = { confirmed: false, targetName: rc.targetName, score: rc.score };
}
if (obj.operationType === "create" && obj.name === rc.targetName) {
obj.renameMetadata = { confirmed: false, sourceName: rc.sourceName, score: rc.score };
obj.sourceName = rc.sourceName;
}
}
}
}
return objects;
}
@@ -382,6 +582,133 @@ function generateDropSql(obj: SchemaDiffObject): string {
return `DROP ${sqlType} IF EXISTS ${obj.name};`;
}
/** Detect column renames in raw SQL and replace DROP+ADD with RENAME COLUMN. */
export function injectColumnRenameSql(sql: string, diffs: TableDiff[], threshold: number, reverse = false): string {
if (!sql || !threshold) return sql;
// Build rename pairs: for each table, match removed columns with added columns by similarity
const replacements: { table: string; oldName: string; newName: string }[] = [];
for (const diff of diffs) {
if (!diff.columns || diff.type !== "modified") continue;
const removedCols = diff.columns.filter((c) => c.type === "removed");
const addedCols = diff.columns.filter((c) => c.type === "added");
if (removedCols.length === 0 || addedCols.length === 0) continue;
const used = new Set<string>();
for (const rc of removedCols) {
let best: (typeof addedCols)[0] | null = null;
let bestSim = 0;
for (const ac of addedCols) {
if (used.has(ac.name)) continue;
const sim = nameSimilarity(rc.name, ac.name);
if (sim > bestSim) {
bestSim = sim;
best = ac;
}
}
if (best && bestSim >= threshold) {
// rc = removed column (exists in source, NOT in target → needs to be ADDED to target)
// best = added column (exists in target, NOT in source → needs to be DROPPED from target)
// To sync target → source: rename target's "best.name" to source's "rc.name"
replacements.push({ table: diff.name, oldName: best.name, newName: rc.name });
used.add(best.name);
}
}
}
if (replacements.length === 0) return sql;
// Process each ALTER TABLE block
const lines = sql.split("\n");
const out: string[] = [];
let currentTable = "";
let inAlter = false;
let alterStart = -1;
const tableRenames = new Map<string, typeof replacements>();
for (const r of replacements) {
const list = tableRenames.get(r.table) || [];
list.push(r);
tableRenames.set(r.table, list);
}
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const trimmed = line.trim();
// Detect ALTER TABLE <table>
const alterMatch = trimmed.match(/^ALTER\s+TABLE\s+(?:`[^`]+`\.)?`?(\w+)`?/i);
if (alterMatch && !inAlter) {
currentTable = alterMatch[1];
const renames = tableRenames.get(currentTable);
if (renames) {
inAlter = true;
alterStart = i;
continue; // skip current ALTER TABLE line, we'll rebuild it
}
}
if (!inAlter) {
out.push(line);
continue;
}
// Inside an ALTER TABLE block being rewritten
const endOfAlter = trimmed === ";" || trimmed.endsWith(";") || (i + 1 < lines.length && lines[i + 1].trim().toUpperCase().startsWith("ALTER")) || i === lines.length - 1;
if (endOfAlter) {
const renames = tableRenames.get(currentTable)!;
// Emit RENAME COLUMN statements
out.push(`-- Alter table: ${currentTable} (column renames detected)`);
for (const r of renames) {
const fromName = reverse ? r.newName : r.oldName;
const toName = reverse ? r.oldName : r.newName;
out.push(`ALTER TABLE ${currentTable} RENAME COLUMN ${fromName} TO ${toName};`);
}
// Collect remaining ALTER clauses (non-renamed columns)
const remaining: string[] = [];
for (let j = alterStart + 1; j <= i; j++) {
const l = lines[j].trim();
if (!l || l === ";") continue;
// Forward: ADD for newName, DROP for oldName. Reverse: ADD for oldName, DROP for newName.
const isAddRename = l.toUpperCase().startsWith("ADD COLUMN") && renames.some((r) => l.includes(reverse ? r.oldName : r.newName));
const isDropRename = l.toUpperCase().startsWith("DROP COLUMN") && renames.some((r) => l.includes(reverse ? r.newName : r.oldName));
if (isAddRename || isDropRename) continue;
// Clean trailing comma if next line is removed
let cleaned = l;
let nextIdx = j + 1;
while (nextIdx <= i) {
const nextLine = lines[nextIdx].trim();
if (!nextLine) {
nextIdx++;
continue;
}
const nextIsAddRename = nextLine.toUpperCase().startsWith("ADD COLUMN") && renames.some((r) => nextLine.includes(reverse ? r.oldName : r.newName));
const nextIsDropRename = nextLine.toUpperCase().startsWith("DROP COLUMN") && renames.some((r) => nextLine.includes(reverse ? r.newName : r.oldName));
if (nextIsAddRename || nextIsDropRename) {
cleaned = cleaned.replace(/,\s*$/, "");
}
break;
}
remaining.push(cleaned);
}
if (remaining.length > 0) {
const last = remaining[remaining.length - 1].replace(/,\s*$/, "").replace(/;\s*$/, "");
remaining[remaining.length - 1] = last;
out.push(`ALTER TABLE ${currentTable}`);
for (const r of remaining) {
out.push(` ${r}`);
}
out.push(";");
}
inAlter = false;
currentTable = "";
}
}
return out.join("\n").trim();
}
export interface ObjectTypeGroup {
kind: DiffObjectKind;
label: string;