Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

4.0 KB
Raw Blame
import assert from "node:assert/strict";
import { test } from "vitest";
import { buildAllDatabaseExportPlan } from "../../apps/desktop/src/lib/export/databaseExport.ts";
import { isDatabaseExportTableSelectionValid } from "../../apps/desktop/src/lib/export/databaseExportSelection.ts";

test("database export allows object-only exports without selecting tables", () => {
  assert.equal(
    isDatabaseExportTableSelectionValid({
      allTableCount: 982,
      selectedTableCount: 0,
      includeStructure: false,
      includeData: false,
    }),
    true,
  );
});

test("database export still requires a table when exporting structure or data", () => {
  assert.equal(
    isDatabaseExportTableSelectionValid({
      allTableCount: 982,
      selectedTableCount: 0,
      includeStructure: true,
      includeData: false,
    }),
    false,
  );
  assert.equal(
    isDatabaseExportTableSelectionValid({
      allTableCount: 982,
      selectedTableCount: 1,
      includeStructure: false,
      includeData: true,
    }),
    true,
  );
});

test("all-database export includes every schema for schema-aware databases", () => {
  const plan = buildAllDatabaseExportPlan({
    databases: ["app", "analytics"],
    schemaAware: true,
    schemasByDatabase: {
      app: ["public", "private"],
      analytics: ["reporting"],
    },
  });

  assert.deepEqual(plan, [
    { database: "app", schema: "public", fileStem: "app.public", displayName: "app.public" },
    { database: "app", schema: "private", fileStem: "app.private", displayName: "app.private" },
    { database: "analytics", schema: "reporting", fileStem: "analytics", displayName: "analytics" },
  ]);
});

test("all-database PostgreSQL export keeps all schemas in one database file", () => {
  const plan = buildAllDatabaseExportPlan({
    databases: ["app", "analytics"],
    schemaAware: true,
    schemasByDatabase: {
      app: ["public", "private"],
      analytics: ["reporting"],
    },
    dbType: "postgres",
  });

  assert.deepEqual(plan, [
    { database: "app", schema: "", fileStem: "app", displayName: "app" },
    { database: "analytics", schema: "", fileStem: "analytics", displayName: "analytics" },
  ]);
});

test("all-database export uses the database as schema for non-schema-aware databases", () => {
  const plan = buildAllDatabaseExportPlan({
    databases: ["app", "analytics"],
    schemaAware: false,
    schemasByDatabase: {
      app: ["ignored"],
    },
  });

  assert.deepEqual(plan, [
    { database: "app", schema: "app", fileStem: "app", displayName: "app" },
    { database: "analytics", schema: "analytics", fileStem: "analytics", displayName: "analytics" },
  ]);
});

test("all-database export treats selected items as schemas for single-database types (dameng)", () => {
  // 达梦等单数据库架构:选中的"数据库"就是 schema 本身,不应做笛卡尔积展开
  const plan = buildAllDatabaseExportPlan({
    databases: ["COSIMULATION", "DATAMANAGE"],
    schemaAware: true,
    schemasByDatabase: {
      COSIMULATION: ["COSIMULATION", "DATAMANAGE", "MULTITEST"],
      DATAMANAGE: ["COSIMULATION", "DATAMANAGE", "MULTITEST"],
    },
    dbType: "dameng",
  });

  assert.deepEqual(plan, [
    { database: "", schema: "COSIMULATION", fileStem: "COSIMULATION", displayName: "COSIMULATION" },
    { database: "", schema: "DATAMANAGE", fileStem: "DATAMANAGE", displayName: "DATAMANAGE" },
  ]);
});

test("all-database export preserves real database for non-schema-aware single-database types (firebird)", () => {
  // firebird/questdb/access 是单库但非 schema-aware:不能短路成 database:"",
  // 否则空 database 会覆盖后端 db_config.database 破坏连接。
  const plan = buildAllDatabaseExportPlan({
    databases: ["inventory", "archive"],
    schemaAware: false,
    dbType: "firebird",
  });

  assert.deepEqual(plan, [
    { database: "inventory", schema: "inventory", fileStem: "inventory", displayName: "inventory" },
    { database: "archive", schema: "archive", fileStem: "archive", displayName: "archive" },
  ]);
});