feat(mysql): 支持查看和修改表存储引擎
This commit is contained in:
21 files changed
+485
-5
No files matched your search
@@ -0,0 +1,59 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { QueryResult } from "@/types/database";
|
||||
import { mysqlTableEngineDraft, mysqlTableEngineSqlOption, mysqlTableEngineSql, parseMysqlTableEngineMetadata, refreshMysqlTableEngineDraft, supportsMysqlTableEngine } from "@/lib/table/mysqlTableEngine";
|
||||
|
||||
function result(columns: string[], rows: QueryResult["rows"]): QueryResult {
|
||||
return { columns, rows, affected_rows: 0, execution_time_ms: 1 };
|
||||
}
|
||||
|
||||
describe("mysqlTableEngine", () => {
|
||||
it("builds mode-independent metadata SQL for quoted and Unicode names", () => {
|
||||
const sql = mysqlTableEngineSql("db'x", "订单\\archive");
|
||||
expect(sql).toContain("TABLE_SCHEMA = CONVERT(X'64622778' USING utf8mb4)");
|
||||
expect(sql).toContain("TABLE_NAME = CONVERT(X'e8aea2e58d955c61726368697665' USING utf8mb4)");
|
||||
expect(sql).not.toContain("db'x");
|
||||
});
|
||||
|
||||
it("keeps supported engines, the server default, and the current table engine", () => {
|
||||
const engines = result(
|
||||
["Engine", "Support", "Comment"],
|
||||
[
|
||||
["InnoDB", "DEFAULT", ""],
|
||||
["MyISAM", "YES", ""],
|
||||
["FEDERATED", "NO", ""],
|
||||
],
|
||||
);
|
||||
const table = result(["engine"], [["FEDERATED"]]);
|
||||
|
||||
expect(parseMysqlTableEngineMetadata(engines, table)).toEqual({
|
||||
currentEngine: "FEDERATED",
|
||||
defaultEngine: "InnoDB",
|
||||
engines: ["FEDERATED", "InnoDB", "MyISAM"],
|
||||
});
|
||||
});
|
||||
|
||||
it("uses the default for a new table and preserves a dirty edit across refresh", () => {
|
||||
const initial = { currentEngine: "MyISAM", defaultEngine: "InnoDB", engines: ["InnoDB", "MyISAM"] };
|
||||
expect(mysqlTableEngineDraft(initial, true)).toEqual({ value: "InnoDB", originalValue: "InnoDB" });
|
||||
expect(mysqlTableEngineDraft(initial, false)).toEqual({ value: "MyISAM", originalValue: "MyISAM" });
|
||||
|
||||
expect(refreshMysqlTableEngineDraft({ ...initial, currentEngine: "InnoDB" }, { value: "ARCHIVE", originalValue: "MyISAM" }, false, true)).toEqual({
|
||||
value: "ARCHIVE",
|
||||
originalValue: "InnoDB",
|
||||
});
|
||||
});
|
||||
|
||||
it("sends the selected engine for creates and only changed engines for edits", () => {
|
||||
expect(mysqlTableEngineSqlOption({ value: "InnoDB", originalValue: "InnoDB" }, true, true)).toBe("InnoDB");
|
||||
expect(mysqlTableEngineSqlOption({ value: "innodb", originalValue: "InnoDB" }, false, true)).toBeUndefined();
|
||||
expect(mysqlTableEngineSqlOption({ value: "MyISAM", originalValue: "InnoDB" }, false, true)).toBe("MyISAM");
|
||||
expect(mysqlTableEngineSqlOption({ value: "MyISAM", originalValue: "InnoDB" }, false, false)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("is limited to native MySQL connections", () => {
|
||||
expect(supportsMysqlTableEngine({ db_type: "mysql", driver_profile: "mysql" })).toBe(true);
|
||||
expect(supportsMysqlTableEngine({ db_type: "mysql", driver_profile: undefined })).toBe(true);
|
||||
expect(supportsMysqlTableEngine({ db_type: "jdbc", driver_profile: "mysql" })).toBe(false);
|
||||
expect(supportsMysqlTableEngine({ db_type: "mysql", driver_profile: "goldendb" })).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,86 @@
|
||||
import type { ConnectionConfig, QueryResult } from "@/types/database";
|
||||
|
||||
export interface MysqlTableEngineMetadata {
|
||||
currentEngine?: string;
|
||||
defaultEngine?: string;
|
||||
engines: string[];
|
||||
}
|
||||
|
||||
export interface MysqlTableEngineDraft {
|
||||
value: string;
|
||||
originalValue: string;
|
||||
}
|
||||
|
||||
export const MYSQL_STORAGE_ENGINES_SQL = "SHOW ENGINES";
|
||||
|
||||
function mysqlUtf8Literal(value: string): string {
|
||||
const bytes = new TextEncoder().encode(value);
|
||||
const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
||||
return `CONVERT(X'${hex}' USING utf8mb4)`;
|
||||
}
|
||||
|
||||
export function mysqlTableEngineSql(database: string, table: string): string {
|
||||
return ["SELECT ENGINE AS engine", "FROM information_schema.TABLES", `WHERE TABLE_SCHEMA = ${mysqlUtf8Literal(database)}`, ` AND TABLE_NAME = ${mysqlUtf8Literal(table)}`, "LIMIT 1"].join("\n");
|
||||
}
|
||||
|
||||
function columnIndex(result: QueryResult, name: string): number {
|
||||
return result.columns.findIndex((column) => column.trim().toLowerCase() === name.toLowerCase());
|
||||
}
|
||||
|
||||
function cellText(result: QueryResult, rowIndex: number, index: number): string {
|
||||
if (index < 0) return "";
|
||||
const value = result.rows[rowIndex]?.[index];
|
||||
return value === null || value === undefined ? "" : String(value).trim();
|
||||
}
|
||||
|
||||
export function parseMysqlTableEngineMetadata(enginesResult: QueryResult, tableResult?: QueryResult): MysqlTableEngineMetadata {
|
||||
const engineIndex = columnIndex(enginesResult, "Engine");
|
||||
const supportIndex = columnIndex(enginesResult, "Support");
|
||||
const engines: string[] = [];
|
||||
let defaultEngine: string | undefined;
|
||||
const seen = new Set<string>();
|
||||
|
||||
for (let rowIndex = 0; rowIndex < enginesResult.rows.length; rowIndex += 1) {
|
||||
const engine = cellText(enginesResult, rowIndex, engineIndex);
|
||||
const support = cellText(enginesResult, rowIndex, supportIndex).toUpperCase();
|
||||
if (!engine || (support !== "YES" && support !== "DEFAULT")) continue;
|
||||
const key = engine.toLowerCase();
|
||||
if (!seen.has(key)) {
|
||||
engines.push(engine);
|
||||
seen.add(key);
|
||||
}
|
||||
if (support === "DEFAULT") defaultEngine = engine;
|
||||
}
|
||||
|
||||
let currentEngine: string | undefined;
|
||||
if (tableResult) {
|
||||
currentEngine = cellText(tableResult, 0, columnIndex(tableResult, "engine")) || undefined;
|
||||
if (currentEngine && !seen.has(currentEngine.toLowerCase())) engines.unshift(currentEngine);
|
||||
}
|
||||
|
||||
return { currentEngine, defaultEngine, engines };
|
||||
}
|
||||
|
||||
export function mysqlTableEngineDraft(metadata: MysqlTableEngineMetadata, isCreateMode: boolean): MysqlTableEngineDraft {
|
||||
const current = (isCreateMode ? metadata.defaultEngine || metadata.engines[0] : metadata.currentEngine) || "";
|
||||
return { value: current, originalValue: current };
|
||||
}
|
||||
|
||||
export function refreshMysqlTableEngineDraft(metadata: MysqlTableEngineMetadata, current: MysqlTableEngineDraft, isCreateMode: boolean, preserveDraft: boolean): MysqlTableEngineDraft {
|
||||
const server = mysqlTableEngineDraft(metadata, isCreateMode);
|
||||
if (!preserveDraft || current.value === current.originalValue) return server;
|
||||
return { value: current.value, originalValue: server.originalValue };
|
||||
}
|
||||
|
||||
export function mysqlTableEngineSqlOption(draft: MysqlTableEngineDraft, isCreateMode: boolean, supported: boolean): string | undefined {
|
||||
const value = draft.value.trim();
|
||||
if (!supported || !value) return undefined;
|
||||
if (!isCreateMode && value.toLowerCase() === draft.originalValue.trim().toLowerCase()) return undefined;
|
||||
return value;
|
||||
}
|
||||
|
||||
export function supportsMysqlTableEngine(connection: Pick<ConnectionConfig, "db_type" | "driver_profile"> | undefined): boolean {
|
||||
if (connection?.db_type !== "mysql") return false;
|
||||
const profile = connection.driver_profile?.trim().toLowerCase();
|
||||
return !profile || profile === "mysql";
|
||||
}
|
||||
@@ -83,6 +83,7 @@ export interface BuildTableStructureChangeSqlOptions {
|
||||
triggers?: EditableStructureTrigger[];
|
||||
tableComment?: string;
|
||||
originalTableComment?: string;
|
||||
mysqlEngine?: string;
|
||||
/** The target table is a PostgreSQL partitioned parent (`relkind = 'p'`);
|
||||
* the backend rejects `CREATE INDEX CONCURRENTLY` on such tables (fail
|
||||
* closed) instead of downgrading to a blocking `CREATE INDEX`. */
|
||||
|
||||
Reference in new issue
Block a user