Файли
openidea/src/features/database/dumpTools.test.ts
T
Ernstjan FreriksтаEjaYF 318f4a2d82 feat(database): export with mysqldump and restore with mysql (#4001)
MySQL and MariaDB data sources, schemas and tables get Import/Export >
Export with 'mysqldump' and Restore with 'mysql'. ide-db builds the arguments
from typed options and runs the tool connected like the data source (TCP,
the driver's SSL flags, the SSH tunnel); the password goes in a user-only
temporary option file passed as --defaults-extra-file and removed after the
run. Restore pipes the dump into mysql's standard input.

Restore dialogs (MySQL and PostgreSQL) now check the target first: the
Database field lists the source's databases and disables Run for a missing
one, and db_dump_info (formerly db_dump_kind) reports whether an SQL dump
creates its database itself. pg_restore gains --create.

Part of #3188

Co-authored-by: EjaYF <[email protected]>
2026-11-17 23:39:50 +08:00

220 рядки
8.6 KiB
TypeScript

import { describe, expect, it } from "vitest";
import type { DataSource, DbTableInfo } from "@/platform/ipc";
import {
DEFAULT_DUMP,
DEFAULT_MYSQLDUMP,
DEFAULT_RESTORE,
baseName,
dumpMismatch,
dumpOptions,
dumpScope,
formatElapsed,
majorOf,
mysqlDumpScope,
mysqldumpOptions,
outputPath,
patternName,
unquoteName,
restoreTargetProblem,
toggleName,
pickInstall,
restoreOptions,
restoreToolFor,
versionFit,
withExtension,
} from "./dumpTools";
const pg = { id: "s1", name: "Shop / dev", driver: "postgres", host: "localhost", user: "app" } as DataSource;
describe("forms", () => {
it("keep quoted names whole", () => {
const options = dumpOptions({
...DEFAULT_DUMP,
schemas: '"My Schema", audit',
tables: 'public."Order Items" "a,""b"".c"',
excludeTables: "",
});
expect(options.schemas).toEqual(['"My Schema"', "audit"]);
expect(options.tables).toEqual(['public."Order Items"', '"a,""b"".c"']);
});
it("send mysqldump tables only with one database, and masking policies only to a client that knows them", () => {
const one = mysqldumpOptions({ ...DEFAULT_MYSQLDUMP, databases: "shop", tables: "orders, items" }, false);
expect(one).toMatchObject({ databases: ["shop"], tables: ["orders", "items"], maskingPolicies: null });
const two = mysqldumpOptions({ ...DEFAULT_MYSQLDUMP, databases: "a b", tables: "orders" }, true);
expect(two).toMatchObject({ databases: ["a", "b"], tables: [], maskingPolicies: false });
});
it("turn names into pg_dump patterns and back", () => {
for (const name of ["public", "My Schema", 'a"b', "Orders"]) expect(unquoteName(patternName(name))).toBe(name);
expect(patternName("Orders")).toBe('"Orders"');
expect(unquoteName("Public")).toBe("public");
});
it("toggle a name chosen from a list", () => {
expect(toggleName("", "shop")).toBe("shop");
expect(toggleName("shop crm", "audit")).toBe("shop, crm, audit");
expect(toggleName("shop, crm", "shop")).toBe("crm");
});
it("send the typed name lists as lists", () => {
const options = dumpOptions({
...DEFAULT_DUMP,
schemas: "public, audit",
tables: " public.orders ",
excludeTables: "",
});
expect([options.schemas, options.tables, options.excludeTables]).toEqual([
["public", "audit"],
["public.orders"],
[],
]);
expect(restoreOptions({ ...DEFAULT_RESTORE, schemas: "a b" }).schemas).toEqual(["a", "b"]);
});
});
describe("restore", () => {
it("picks psql for .sql files and pg_restore for the rest", () => {
expect(restoreToolFor("/tmp/a.SQL")).toBe("psql");
expect(restoreToolFor("/tmp/a.dump")).toBe("pgRestore");
expect(restoreToolFor("/tmp/dir")).toBe("pgRestore");
});
it("trusts the dump's header over its extension", () => {
expect(restoreToolFor("/tmp/a.backup", "postgresSql")).toBe("psql");
expect(restoreToolFor("/tmp/a.sql", "postgresArchive")).toBe("pgRestore");
});
it("refuses a dump the tool cannot read", () => {
expect(dumpMismatch("psql", "mysqlSql")).toBe("mysqlIntoPostgres");
expect(dumpMismatch("psql", "postgresArchive")).toBe("archiveIntoPsql");
expect(dumpMismatch("pgRestore", "postgresSql")).toBe("sqlIntoPgRestore");
expect(dumpMismatch("psql", "postgresSql")).toBeNull();
expect(dumpMismatch("psql", "unknown")).toBeNull();
expect(dumpMismatch("pgRestore", null)).toBeNull();
expect(dumpMismatch("mysql", "postgresSql")).toBe("postgresIntoMysql");
expect(dumpMismatch("mysql", "postgresArchive")).toBe("postgresIntoMysql");
expect(dumpMismatch("mysql", "mysqlSql")).toBeNull();
});
});
describe("restore target", () => {
const existing = ["postgres", "shop"];
it("needs an existing database", () => {
expect(restoreTargetProblem("shop", existing, false, false)).toBeNull();
expect(restoreTargetProblem(" shop ", existing, true, false)).toBeNull();
expect(restoreTargetProblem("shop_new", existing, false, true)).toBe("missing");
expect(restoreTargetProblem("shop_new", null, false, false)).toBeNull();
});
it("lets MySQL run without a database only for a dump that creates its databases", () => {
expect(restoreTargetProblem("", existing, true, false)).toBe("noDatabase");
expect(restoreTargetProblem("", existing, true, true)).toBeNull();
expect(restoreTargetProblem("", existing, false, false)).toBeNull();
});
});
describe("paths", () => {
it("fills the patterns with file-safe values", () => {
const now = new Date(2026, 2, 7, 9, 5, 2);
expect(
outputPath("/tmp/{data_source}-{database}-{timestamp}.sql", { dataSource: "Shop / dev", database: "shop", now }),
).toBe("/tmp/Shop_dev-shop-2026-03-07_09-05-02.sql");
});
it("follows the format's extension", () => {
expect(withExtension("/tmp/x-{timestamp}.sql", "custom")).toBe("/tmp/x-{timestamp}.dump");
expect(withExtension("/tmp/x.dump", "directory")).toBe("/tmp/x");
expect(withExtension("/tmp/x", "plain")).toBe("/tmp/x.sql");
});
});
describe("dumpScope", () => {
const table = { name: "Orders", kind: "table" } as DbTableInfo;
it("applies to PostgreSQL sources, databases, schemas and tables", () => {
const shop = { ...pg, database: "shop" };
expect(dumpScope({ kind: "source", key: "s1", source: shop }, shop)).toEqual({ target: "s1", database: "shop" });
expect(dumpScope({ kind: "database", key: "k", id: "s1:crm", name: "crm", current: false }, pg)).toEqual({
target: "s1:crm",
database: "crm",
});
expect(dumpScope({ kind: "schema", key: "k", id: "s1:crm", schema: "public" }, pg)).toMatchObject({
schemas: "public",
});
expect(dumpScope({ kind: "table", key: "k", id: "s1:crm", schema: "public", table }, pg)).toMatchObject({
tables: 'public."Orders"',
});
});
it("applies to MySQL sources, schemas (databases) and tables with mysqlDumpScope", () => {
const mysql = { ...pg, driver: "mysql", database: "shop" } as DataSource;
expect(mysqlDumpScope({ kind: "source", key: "s1", source: mysql }, mysql)).toEqual({
target: "s1",
database: "shop",
});
expect(mysqlDumpScope({ kind: "schema", key: "k", id: "s1", schema: "crm" }, mysql)).toEqual({
target: "s1",
database: "crm",
});
expect(mysqlDumpScope({ kind: "table", key: "k", id: "s1", schema: "crm", table }, mysql)).toEqual({
target: "s1",
database: "crm",
tables: "Orders",
});
expect(mysqlDumpScope({ kind: "source", key: "s1", source: pg }, pg)).toBeNull();
});
it("does not apply to other drivers or views", () => {
const mysql = { ...pg, driver: "mysql" } as DataSource;
expect(dumpScope({ kind: "source", key: "s1", source: mysql }, mysql)).toBeNull();
const view = { ...table, kind: "view" } as DbTableInfo;
expect(dumpScope({ kind: "table", key: "k", id: "s1", schema: "public", table: view }, pg)).toBeNull();
});
});
describe("client and server versions", () => {
const installs = [
{ path: "/opt/homebrew/bin/pg_dump", version: "pg_dump (PostgreSQL) 18.4 (Homebrew)" },
{ path: "/opt/homebrew/opt/postgresql@15/bin/pg_dump", version: "pg_dump (PostgreSQL) 15.13 (Homebrew)" },
];
it("reads the major version", () => {
expect(majorOf("pg_dump (PostgreSQL) 18.4 (Homebrew)")).toBe(18);
expect(majorOf("psql (PostgreSQL) 9.6.24")).toBe(9);
expect(majorOf("unknown")).toBeNull();
});
it("flags a pg_dump older than the server, and dumps an older server cannot load", () => {
expect(versionFit("pgDump", 14, 15)).toBe("tooOld");
expect(versionFit("pgDump", 18, 15)).toBe("newer");
expect(versionFit("pgRestore", 18, 15)).toBe("newer");
expect(versionFit("pgRestore", 14, 15)).toBe("ok");
expect(versionFit("psql", 18, 15)).toBe("ok");
expect(versionFit("pgDump", 15, null)).toBe("ok");
});
it("preselects the server's version, else the last used, else the newest", () => {
expect(pickInstall(installs, 15, "/usr/bin/pg_dump")).toBe("/opt/homebrew/opt/postgresql@15/bin/pg_dump");
expect(pickInstall(installs, 16, "/usr/bin/pg_dump")).toBe("/usr/bin/pg_dump");
expect(pickInstall(installs, 16, null)).toBe("/opt/homebrew/bin/pg_dump");
expect(pickInstall([], null, null)).toBe("");
});
});
describe("run summaries", () => {
it("formats how long a run took", () => {
expect(formatElapsed(350)).toBe("< 1 s");
expect(formatElapsed(4_200)).toBe("4 s");
expect(formatElapsed(724_000)).toBe("12 min 4 s");
expect(formatElapsed(7_500_000)).toBe("2 h 5 min");
});
it("shortens paths to their file name", () => {
expect(baseName("/tmp/dumps/shop.sql")).toBe("shop.sql");
expect(baseName("C:\\dumps\\shop.dump")).toBe("shop.dump");
expect(baseName("/tmp/dir/")).toBe("dir");
});
});