fix(schema-diff): correct Oracle/JDBC column, index and view diffs

This commit is contained in:
zipg authored and GitHub committed 2026-09-25 09:38:29 +08:00
1 parent ba422f96f8
commit 70569e556f
7 files changed
+1095 -95

No files matched your search

@@ -0,0 +1,41 @@
import { describe, expect, it } from "vitest";
import { schemaDiffEngineDatabaseType } from "@/lib/schema/schemaDiff";
describe("schemaDiffEngineDatabaseType", () => {
it("resolves the product type of a JDBC connection from the driver profile", () => {
expect(schemaDiffEngineDatabaseType({ db_type: "jdbc", driver_profile: "jdbc-oracle" })).toBe("oracle");
expect(schemaDiffEngineDatabaseType({ db_type: "jdbc", driver_profile: "jdbc-mysql" })).toBe("mysql");
expect(schemaDiffEngineDatabaseType({ db_type: "jdbc", driver_profile: "jdbc-postgresql" })).toBe("postgres");
});
it("resolves the product type from the JDBC URL, driver class and labels", () => {
expect(
schemaDiffEngineDatabaseType({
db_type: "jdbc",
connection_string: "jdbc:oracle:thin:@192.168.58.103:15211:XE",
jdbc_driver_class: "oracle.jdbc.OracleDriver",
}),
).toBe("oracle");
expect(
schemaDiffEngineDatabaseType({
db_type: "jdbc",
jdbc_driver_paths: ["/plugins/jdbc/drivers/local/ojdbc8-21.9.0.0.jar"],
jdbc_driver_class: "com.mysql.cj.jdbc.Driver",
connection_string: "jdbc:mysql://127.0.0.1:3306/db",
}),
).toBe("mysql");
expect(schemaDiffEngineDatabaseType({ db_type: "jdbc", driver_label: "Apache Hive" })).toBe("hive");
});
it("keeps generic JDBC connections on the generic type", () => {
expect(schemaDiffEngineDatabaseType({ db_type: "jdbc" })).toBe("jdbc");
expect(schemaDiffEngineDatabaseType({ db_type: "jdbc", driver_profile: "jdbcx" })).toBe("jdbc");
});
it("leaves non-JDBC connections untouched", () => {
expect(schemaDiffEngineDatabaseType({ db_type: "oracle" })).toBe("oracle");
expect(schemaDiffEngineDatabaseType({ db_type: "mysql", driver_profile: "starrocks" })).toBe("mysql");
expect(schemaDiffEngineDatabaseType({ db_type: "postgres" })).toBe("postgres");
expect(schemaDiffEngineDatabaseType(undefined)).toBeUndefined();
});
});
+20 -1
View File
@@ -1,5 +1,6 @@
import type { ColumnInfo, IndexInfo, ForeignKeyInfo, TriggerInfo, FunctionInfo, SequenceInfo, RuleInfo, OwnerInfo, DatabaseType, TableInfo } from "@/types/database";
import type { ColumnInfo, IndexInfo, ForeignKeyInfo, TriggerInfo, FunctionInfo, SequenceInfo, RuleInfo, OwnerInfo, DatabaseType, TableInfo, ConnectionConfig } from "@/types/database";
import type { SchemaDiffTableMapping } from "@/types/schemaDiff";
import { effectiveDatabaseTypeForConnection } from "@/lib/database/jdbcDialect";
import { splitSqlStatementRanges } from "@/lib/sql/sqlStatementRanges";
const DIALECT_KIND_MAP: Record<string, string> = {
@@ -44,6 +45,24 @@ export function databaseTypeToDialectKind(dbType: DatabaseType): string {
return DIALECT_KIND_MAP[dbType] ?? "unsupported";
}
/** The connection fields the JDBC product inference reads. */
export type SchemaDiffDialectConnection = Partial<Pick<ConnectionConfig, "db_type" | "driver_profile" | "driver_label" | "connection_string" | "url_params" | "jdbc_driver_class" | "jdbc_driver_paths" | "database_info" | "external_config" | "username">>;
/**
* Product type the schema-diff engine must treat a connection as.
*
* A JDBC connection stores the generic `jdbc` in `db_type`, but the engine keys its clause
* shapes, index rules and view comparison off the product type — diffing a JDBC Oracle
* connection as `jdbc` emits ANSI/MySQL clauses for it and never compares its view text.
* The product is resolved from the driver profile/URL exactly like the metadata and query
* paths already resolve it; every other connection keeps its own `db_type`.
*/
export function schemaDiffEngineDatabaseType(connection: SchemaDiffDialectConnection | undefined): DatabaseType | undefined {
if (!connection?.db_type) return undefined;
if (connection.db_type !== "jdbc") return connection.db_type as DatabaseType;
return effectiveDatabaseTypeForConnection(connection) ?? (connection.db_type as DatabaseType);
}
const DIALECT_ALIAS_MAP: Record<string, string> = {
access: "sql_server",
mssql: "sql_server",