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。

5.3 KB
Raw Blame
import { computed, reactive, ref } from "vue";
import * as api from "@/lib/backend/api";
import type { MigrationPreflight, MigrationReport } from "@/lib/backend/migration";

type MigrationApi = Pick<typeof api, "migrationStatus" | "migrationStart" | "migrationRetry" | "migrationCleanupBackups">;

// Each startup boundary owns its state; an action failure never erases its report.
export function useMigrationStore(backend: MigrationApi = api) {
  const state = reactive<{
    status: MigrationPreflight | null;
    report: MigrationReport | null;
    loading: boolean;
    busy: boolean;
    error: "statusFailed" | "migrationFailed" | "keyRecoveryRequired" | "cleanupFailed" | null;
    errorCode: string | null;
    errorMessage: string | null;
    entered: boolean;
    step: 1 | 2 | 3;
  }>({ status: null, report: null, loading: true, busy: false, error: null, errorCode: null, errorMessage: null, entered: false, step: 1 });
  const migrationAttempted = ref(false);
  const completed = computed(() => state.status !== null && !state.status.needsMigration && !state.status.keyRecoveryRequired && ["succeeded", "not_required"].includes(state.status.state));
  // A managed data-directory key is intentionally created when migration
  // starts. Its absence during the read-only preflight is actionable, not a
  // migration failure, as long as the backend explicitly allows creation.
  const hasBlockingError = () => Boolean(state.errorCode && state.status?.keyStatus !== "will_create" && !(state.errorCode === "MISSING_MANAGED_KEY" && state.status?.keyCreationAllowed === true));
  // A retained backup is a recovery asset and must not make the success page
  // reappear on later launches. An attempt in this session keeps the wizard
  // visible even when its response is lost, until the user enters the app.
  const blocking = computed(() => state.loading || state.busy || state.error === "statusFailed" || !completed.value || (!state.entered && (migrationAttempted.value || Boolean(state.report))));
  function clearError() {
    state.error = null;
    state.errorCode = null;
    state.errorMessage = null;
  }
  async function refreshStatus(retry = false) {
    // Only this endpoint returns classified, redacted migration errors. Transport
    // exceptions may contain configuration values and must never enter UI state.
    state.status = await backend.migrationStatus(retry);
    state.errorCode = state.status.errorCode ?? null;
    state.errorMessage = state.status.errorMessage ?? null;
  }
  async function checkStatus(retry: boolean) {
    state.loading = true;
    clearError();
    try {
      await refreshStatus(retry);
      state.step = completed.value ? 3 : 1;
      // A completed migration only needs the success page in the session that
      // performed it. On later launches, retained backups are recovery assets,
      // not a reason to block the normal application startup. `entered` is
      // therefore only changed by the explicit success-page action below.
      state.entered = false;
      if (state.status?.keyRecoveryRequired) state.error = "keyRecoveryRequired";
      else if (state.status?.state === "failed" || hasBlockingError()) state.error = "migrationFailed";
    } catch {
      state.error = "statusFailed";
    } finally {
      state.loading = false;
    }
  }
  const initialize = () => checkStatus(false);
  const retryStatus = () => checkStatus(true);
  async function run(retry: boolean) {
    if (state.busy) return;
    state.busy = true;
    migrationAttempted.value = true;
    state.step = 2;
    clearError();
    try {
      state.report = await (retry ? backend.migrationRetry() : backend.migrationStart());
    } catch {
      state.error = "migrationFailed";
    }
    try {
      await refreshStatus();
      if (state.status?.keyRecoveryRequired) {
        state.step = 1;
        state.error = "keyRecoveryRequired";
      } else if (completed.value) {
        state.step = 3;
        state.error = null;
      } else if (hasBlockingError()) {
        state.error = "migrationFailed";
      }
    } catch {
      state.error = "statusFailed";
    } finally {
      state.busy = false;
    }
  }
  async function cleanup() {
    if (state.busy) return;
    state.busy = true;
    clearError();
    try {
      await backend.migrationCleanupBackups();
      if (state.status) state.status.backupPath = null;
      if (state.report) state.report.backupPath = null;
    } catch {
      state.error = "cleanupFailed";
    } finally {
      state.busy = false;
    }
  }
  function diagnostic() {
    const s = state.status;
    return {
      migrationId: s?.migrationId,
      actionError: state.error,
      step: state.step,
      needsMigration: s?.needsMigration,
      keyRecoveryRequired: s?.keyRecoveryRequired,
      legacyJsonFiles: s?.legacyJsonFiles,
      state: s?.state,
      counts: s ? { connections: s.connectionCount, databasePlaintext: s.databasePlaintextCount, plugin: s.pluginSecretCount, ai: s.aiSecretCount, tunnel: s.tunnelSecretCount, sync: s.syncCredentialCount } : {},
      keyProviderAvailable: s?.keyProviderAvailable,
      keyStatus: s?.keyStatus,
      backupPath: s?.backupPath,
      errorCode: state.errorCode,
    };
  }
  function enter() {
    if (completed.value && !state.busy) state.entered = true;
  }
  return { state, blocking, completed, initialize, retryStatus, start: () => run(false), retry: () => run(true), cleanup, diagnostic, enter };
}