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。

8.7 KB
Raw Blame
import { computed, onMounted, onUnmounted, reactive, ref } from "vue";
import * as api from "@/lib/backend/api";
import { appendDebugLog } from "@/lib/backend/debugLog";
import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
import { readDatabaseBackupRuns, readDatabaseBackupSchedules, type DatabaseBackupExecutionConfig, type DatabaseBackupRun, type DatabaseBackupSchedule } from "@/lib/backup/scheduledDatabaseBackup";
import type { DatabaseBackupSnapshot } from "@/lib/backup/backgroundDatabaseBackup";
import { useExportTracker } from "@/composables/useExportTracker";

const schedules = ref<DatabaseBackupSchedule[]>([]);
const runs = ref<DatabaseBackupRun[]>([]);
const activeScheduleIds = reactive(new Set<string>());
const activeRunIds = reactive(new Set<string>());
const cancellingRunIds = reactive(new Set<string>());
const heartbeat = ref<string | null>(null);
const destinationRoot = ref<string | null>(null);
const error = ref("");
const tracked = new Set<string>();
let refreshing: Promise<void> | undefined;
let subscribers = 0;
let timer: ReturnType<typeof setInterval> | undefined;

export function useScheduledDatabaseBackups(_options: { scheduler?: boolean } = {}) {
  const tracker = useExportTracker();
  const activeRuns = computed(() => runs.value.filter((run) => run.status === "running"));

  function update(snapshot: DatabaseBackupSnapshot) {
    schedules.value = snapshot.schedules;
    runs.value = snapshot.runs;
    heartbeat.value = snapshot.heartbeat;
    destinationRoot.value = snapshot.destinationRoot;
    activeScheduleIds.clear();
    activeRunIds.clear();
    for (const run of snapshot.runs) {
      if (run.status === "running") {
        activeRunIds.add(run.id);
        if (run.scheduleId) activeScheduleIds.add(run.scheduleId);
        if (!tracked.has(run.id)) {
          tracked.add(run.id);
          tracker.addDatabaseExportTask(run.id, run.displayName || run.scheduleName, run.destinationDirectory || "", run.source === "scheduled" ? "scheduled" : "manual");
          tracker.registerTaskCancelHandler(run.id, async () => {
            await cancelRun(run.id);
          });
        }
      } else {
        cancellingRunIds.delete(run.id);
        tracker.unregisterTaskCancelHandler(run.id);
      }
      if (tracked.has(run.id)) {
        tracker.updateDatabaseExportTask(run.id, {
          exportId: run.id,
          currentObject: run.displayName || run.scheduleName,
          objectIndex: 0,
          totalObjects: run.files.length,
          rowsExported: 0,
          totalRows: null,
          status: run.status === "running" ? "Running" : run.status === "success" ? "Done" : run.status === "cancelled" ? "Cancelled" : "Error",
          error: run.error || null,
          overallPercent: run.progressPercent || 0,
        });
        if (run.status !== "running") tracked.delete(run.id);
      }
    }
  }

  async function refresh() {
    if (refreshing) return refreshing;
    refreshing = (async () => {
      try {
        let snapshot = await api.databaseBackupCommand<DatabaseBackupSnapshot>({ action: "snapshot" });
        if (!snapshot.migrated) {
          const legacySchedules = isTauriRuntime() ? readDatabaseBackupSchedules().map((schedule) => ({ ...schedule, timeZone: schedule.timeZone || Intl.DateTimeFormat().resolvedOptions().timeZone })) : [];
          await api.databaseBackupCommand({ action: "migrate", migration: { schedules: legacySchedules, runs: isTauriRuntime() ? readDatabaseBackupRuns() : [] } });
          // Keep legacy keys as a migration fallback. The backend marker prevents reimporting.
          snapshot = await api.databaseBackupCommand<DatabaseBackupSnapshot>({ action: "snapshot" });
        }
        update(snapshot);
        error.value = "";
      } catch (reason) {
        error.value = reason instanceof Error ? reason.message : String(reason);
        appendDebugLog("error", "[DBX][database-backup:refresh]", reason);
        throw reason;
      }
    })().finally(() => {
      refreshing = undefined;
    });
    return refreshing;
  }

  async function refreshAfterMutation() {
    // A poll started before a mutation may still return the previous state.
    await refreshing?.catch(() => {});
    await refresh();
  }

  async function saveSchedule(schedule: DatabaseBackupSchedule) {
    const saved = await api.databaseBackupCommand<DatabaseBackupSchedule>({ action: "save", schedule: { ...schedule, timeZone: schedule.timeZone || Intl.DateTimeFormat().resolvedOptions().timeZone } });
    await refreshAfterMutation();
    return saved;
  }

  async function setScheduleEnabled(id: string, enabled: boolean) {
    const schedule = schedules.value.find((s) => s.id === id);
    if (schedule) await saveSchedule({ ...schedule, enabled });
  }

  async function deleteSchedule(id: string) {
    if (activeScheduleIds.has(id)) return false;
    await api.databaseBackupCommand({ action: "deleteSchedule", id });
    await refreshAfterMutation();
    return true;
  }

  async function deleteRuns(ids: readonly string[]) {
    await api.databaseBackupCommand({ action: "deleteRuns", ids: [...ids] });
    await refreshAfterMutation();
  }

  /** Adds backup files created outside DBX to the history; returns the new runs. */
  async function importBackupFiles(files: readonly string[]) {
    const imported = await api.databaseBackupCommand<DatabaseBackupRun[]>({ action: "import", files: [...files] });
    await refreshAfterMutation();
    return imported;
  }

  async function renameRun(id: string, name: string) {
    if (activeRunIds.has(id)) return false;
    await api.databaseBackupCommand({ action: "rename", id, name: name.trim() });
    await refreshAfterMutation();
    return true;
  }

  async function cancelRun(id: string) {
    const accepted = await api.databaseBackupCommand<boolean>({ action: "cancel", id });
    if (accepted) {
      cancellingRunIds.add(id);
      tracker.markDatabaseExportTaskCancelling(id);
    }
    return accepted;
  }

  async function waitForRun(run: DatabaseBackupRun): Promise<DatabaseBackupRun | null> {
    await refreshAfterMutation();
    while (true) {
      const current = runs.value.find((r) => r.id === run.id);
      if (!current || current.status !== "running") return current || null;
      await new Promise((resolve) => setTimeout(resolve, 1000));
      await refresh();
    }
  }

  async function runSchedule(id: string) {
    if (activeScheduleIds.has(id)) return null;
    return waitForRun(await api.databaseBackupCommand<DatabaseBackupRun>({ action: "run", request: { scheduleId: id } }));
  }

  async function runOneShot(config: DatabaseBackupExecutionConfig, displayName = "Database backup") {
    return waitForRun(await api.databaseBackupCommand<DatabaseBackupRun>({ action: "run", request: { config, displayName, timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone } }));
  }

  /**
   * Queues one one-shot run per config and waits for all of them. The backend worker executes queued runs one at a
   * time, so this only fans out the requests; a config that cannot be queued is reported without blocking the rest.
   * `onQueued` lets the caller track run IDs (for example to cancel the whole batch) before the runs finish.
   */
  async function runOneShotBatch(configs: readonly DatabaseBackupExecutionConfig[], displayName = "Database backup", onQueued?: (run: DatabaseBackupRun) => void) {
    const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
    const queued: DatabaseBackupRun[] = [];
    const enqueueErrors: Array<{ connectionId: string; error: unknown }> = [];
    for (const config of configs) {
      try {
        const run = await api.databaseBackupCommand<DatabaseBackupRun>({ action: "run", request: { config, displayName, timeZone } });
        queued.push(run);
        onQueued?.(run);
      } catch (error) {
        enqueueErrors.push({ connectionId: config.connectionId, error });
      }
    }
    const finished = await Promise.all(queued.map((run) => waitForRun(run)));
    return { runs: finished.filter((run): run is DatabaseBackupRun => !!run), enqueueErrors };
  }

  onMounted(() => {
    subscribers += 1;
    if (!timer)
      timer = setInterval(() => {
        void refresh().catch(() => {});
      }, 2000);
    void refresh().catch(() => {});
  });
  onUnmounted(() => {
    subscribers -= 1;
    if (!subscribers && timer) {
      clearInterval(timer);
      timer = undefined;
    }
  });

  return {
    schedules,
    runs,
    activeScheduleIds,
    activeRunIds,
    cancellingRunIds,
    activeRuns,
    heartbeat,
    destinationRoot,
    error,
    saveSchedule,
    setScheduleEnabled,
    deleteSchedule,
    deleteRuns,
    deleteRun: (id: string) => deleteRuns([id]),
    importBackupFiles,
    renameRun,
    runSchedule,
    runOneShot,
    runOneShotBatch,
    cancelRun,
    processDueSchedules: refresh,
  };
}