fix: handle MySQL backup SQL import

This commit is contained in:
t8y2 committed 2026-05-16 12:13:40 +08:00
1 parent e9621ef06a
commit f4c7c1d764
10 files changed
+371 -10

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
import type { DatabaseType } from "@/types/database";
import { quoteTableIdentifier } from "./tableSelectSql";
const MYSQL_COMPATIBLE_PROFILES = new Set([
"mysql",
"mariadb",
"tidb",
"oceanbase",
"doris",
"starrocks",
"custom_mysql",
]);
const MYSQL_COMPATIBLE_TYPES = new Set<DatabaseType>(["mysql", "doris", "starrocks", "goldendb"]);
export interface CreateDatabaseSqlOptions {
databaseType?: DatabaseType;
driverProfile?: string | null;
name: string;
charset?: string;
collation?: string;
}
export function supportsCreateDatabaseCharset(databaseType?: DatabaseType, driverProfile?: string | null): boolean {
return (
MYSQL_COMPATIBLE_TYPES.has(databaseType as DatabaseType) ||
(!!driverProfile && MYSQL_COMPATIBLE_PROFILES.has(driverProfile))
);
}
export function buildCreateDatabaseSql(options: CreateDatabaseSqlOptions): string {
const name = quoteTableIdentifier(options.databaseType, options.name);
const charset = cleanSqlOption(options.charset);
const collation = cleanSqlOption(options.collation);
if (!supportsCreateDatabaseCharset(options.databaseType, options.driverProfile) || !charset) {
return `CREATE DATABASE ${name};`;
}
const collateClause = collation ? ` COLLATE ${collation}` : "";
return `CREATE DATABASE ${name} CHARACTER SET ${charset}${collateClause};`;
}
function cleanSqlOption(value: string | undefined): string {
return value?.trim().replace(/[;\s]+/g, "") ?? "";
}