feat(data): run generation and transfer tasks in background

Closes #10390
This commit is contained in:
t8y2 committed 2026-09-27 15:56:53 +00:00
1 parent 1d07cb05e0
commit 78dcb85713
23 files changed
+1014 -247

No files matched your search

@@ -21,6 +21,7 @@ import type { TransferContent, TransferObjectKind, TransferTableNameCase } from
import { crossFamilyTransferableKinds, isSameTransferFamily, transferObjectKindsForDatabase } from "@/lib/database/transferObjectKinds";
import ObjectSelectionTree from "@/components/transfer/ObjectSelectionTree.vue";
import TransferTaskTree from "@/components/transfer/TransferTaskTree.vue";
import DataTransferProgressDialog from "@/components/transfer/DataTransferProgressDialog.vue";
import DangerConfirmDialog from "@/components/editor/DangerConfirmDialog.vue";
import type { DatabaseType } from "@/types/database";
import type { TransferTask, TransferTaskConfig } from "@/types/database";
@@ -31,13 +32,14 @@ import { decodeTransferDatabaseOption, encodeTransferDatabaseOptions, formatTran
import { formatDatabaseLabel } from "@/lib/database/defaultDatabase";
import { databaseOptionsForConnection, fetchCatalogNamespaceOptions, fetchNamespaceOptionsForConnection, namespaceOptionsAreSchemas } from "@/composables/useDatabaseOptions";
import { useExportTracker } from "@/composables/useExportTracker";
import { openDataTransferTask } from "@/composables/useDialogSources";
import { useTransferTaskStore, TransferTaskNameConflictError, nextTransferTaskCopyName } from "@/stores/transferTaskStore";
import { useToast } from "@/composables/useToast";
import type { CatalogInfo } from "@/types/database";
import { ArrowRightLeft, ArrowLeftRight, Loader2 } from "@lucide/vue";
const { t } = useI18n();
const { startDataTransferTask } = useExportTracker();
const { tasks, startDataTransferTask } = useExportTracker();
const { toast } = useToast();
const taskStore = useTransferTaskStore();
const productionSafetyStore = useProductionSafetyStore();
@@ -52,8 +54,14 @@ const props = defineProps<{
prefillTargetConnectionId?: string;
prefillTargetDatabase?: string;
prefillTargetSchema?: string;
taskId?: string | null;
}>();
const trackedTransferTask = computed(() => {
if (!props.taskId) return undefined;
return tasks.value.find((task) => task.exportId === props.taskId && task.kind === "data-transfer");
});
const transferDialogStyle = {
width: "min(1120px, calc(100vw - 2rem))",
height: "min(80vh, calc(var(--dbx-viewport-height) - 2rem))",
@@ -609,9 +617,9 @@ watch(targetDatabase, async (db) => {
});
watch(
open,
async (val) => {
if (val) {
[open, () => props.taskId],
async ([val, taskId]) => {
if (val && !taskId) {
void taskStore.initFromStorage();
resetState();
pendingSourceSchemaPrefill.value = props.prefillSchema ?? "";
@@ -816,6 +824,7 @@ function runTransfer(request: api.TransferRequest, shouldRefreshTargetTree: bool
startDataTransferTask(request, `${request.sourceDatabase} → ${request.targetDatabase}`, {
formatOverlapError: (tables) => t("transfer.targetTableBusy", { tables: tables.join(", ") }),
onStarted: () => toast(t("transfer.backgroundStarted")),
onOpen: () => openDataTransferTask(request.transferId),
onDone: async () => {
if (shouldRefreshTargetTree) {
await store.refreshObjectListTreeNode(request.targetConnectionId, request.targetDatabase, request.targetSchema, request.targetCatalog);
@@ -1155,7 +1164,8 @@ async function saveConfigTask() {
</script>
<template>
<Dialog v-model:open="open">
<DataTransferProgressDialog v-if="trackedTransferTask" v-model:open="open" :task="trackedTransferTask" />
<Dialog v-else v-model:open="open">
<DialogContent class="dbx-transfer-dialog sm:max-w-[1120px] max-h-[80vh] flex flex-col overflow-hidden resize" :style="transferDialogStyle" @interact-outside.prevent>
<DialogHeader class="shrink-0">
<DialogTitle class="flex items-center gap-2">
@@ -0,0 +1,117 @@
<script setup lang="ts">
import { computed, onBeforeUnmount, onMounted, ref } from "vue";
import { useI18n } from "vue-i18n";
import { AlertCircle, ArrowRightLeft, CheckCircle2, Loader2, Minimize2, X, XCircle } from "@lucide/vue";
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import { formatDataTransferDuration, useExportTracker, type ExportTask } from "@/composables/useExportTracker";
import { translateBackendError } from "@/i18n/backend-errors";
const { t } = useI18n();
const open = defineModel<boolean>("open", { default: false });
const props = defineProps<{ task: ExportTask }>();
const { cancelTask } = useExportTracker();
const currentTime = ref(Date.now());
let elapsedTimer: ReturnType<typeof setInterval> | undefined;
const isActive = computed(() => ["Running", "Writing", "Cancelling"].includes(props.task.status));
const progressPercent = computed(() => {
if (props.task.status === "Done") return 100;
if (!props.task.totalTables) return 0;
return Math.min(95, Math.round(((props.task.tableIndex ?? 0) / props.task.totalTables) * 100));
});
const elapsed = computed(() => {
const startedAt = props.task.startedAt ?? currentTime.value;
return formatDataTransferDuration((props.task.finishedAt ?? currentTime.value) - startedAt);
});
const statusText = computed(() => {
if (props.task.status === "Cancelling") return t("databaseBackup.cancelling");
if (props.task.status === "Done") return t("exportProgress.compareDone");
if (props.task.status === "Error") return t("exportProgress.compareError");
if (props.task.status === "Cancelled") return t("exportProgress.compareCancelled");
return t("exportProgress.compareRunning");
});
onMounted(() => {
elapsedTimer = setInterval(() => {
currentTime.value = Date.now();
}, 1000);
});
onBeforeUnmount(() => {
if (elapsedTimer) clearInterval(elapsedTimer);
});
</script>
<template>
<Dialog :open="open" @update:open="open = $event">
<DialogContent class="sm:max-w-lg" data-transfer-progress-dialog @interact-outside.prevent>
<DialogHeader>
<DialogTitle class="flex items-center gap-2">
<ArrowRightLeft class="h-4 w-4" />
{{ t("exportProgress.dataTransferTitle", { name: task.tableName }) }}
</DialogTitle>
</DialogHeader>
<div class="space-y-4 py-3">
<div class="flex items-center gap-2 text-sm">
<Loader2 v-if="isActive" class="h-4 w-4 animate-spin text-primary" />
<CheckCircle2 v-else-if="task.status === 'Done'" class="h-4 w-4 text-green-500" />
<XCircle v-else-if="task.status === 'Error'" class="h-4 w-4 text-destructive" />
<AlertCircle v-else class="h-4 w-4 text-yellow-500" />
<span>{{ statusText }}</span>
</div>
<div class="space-y-1.5">
<div class="h-2 w-full overflow-hidden rounded-full bg-muted">
<div v-if="task.totalTables" class="h-full rounded-full transition-[width] duration-300" :class="task.status === 'Done' ? 'bg-green-500' : 'bg-primary'" :style="{ width: `${progressPercent}%` }" />
<div v-else-if="isActive" class="export-progress-indeterminate h-full rounded-full bg-primary" />
</div>
<div class="flex items-center justify-between gap-3 text-xs text-muted-foreground">
<span class="truncate">{{ task.currentTable || task.tableName }}</span>
<span class="shrink-0 tabular-nums">{{ task.tableIndex ?? 0 }} / {{ task.totalTables ?? 0 }} · {{ task.rowsExported.toLocaleString() }} {{ t("exportProgress.rowsShort") }}</span>
</div>
<div class="text-xs text-muted-foreground">{{ t("exportProgress.elapsed", { duration: elapsed }) }}</div>
</div>
<div v-if="task.errorMessage" class="rounded-md border border-destructive/30 bg-destructive/10 px-3 py-2 text-xs text-destructive">
{{ translateBackendError(t, task.errorMessage) }}
</div>
</div>
<DialogFooter>
<template v-if="isActive">
<Button variant="ghost" size="sm" @click="open = false">
<Minimize2 class="mr-1 h-3.5 w-3.5" />
{{ t("exportProgress.minimize") }}
</Button>
<Button variant="outline" size="sm" :disabled="task.status === 'Cancelling'" @click="cancelTask(task.exportId)">
<Loader2 v-if="task.status === 'Cancelling'" class="mr-1 h-3.5 w-3.5 animate-spin" />
<X v-else class="mr-1 h-3.5 w-3.5" />
{{ task.status === "Cancelling" ? t("databaseBackup.cancelling") : t("exportProgress.cancel") }}
</Button>
</template>
<Button v-else variant="outline" size="sm" @click="open = false">{{ t("exportProgress.close") }}</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</template>
<style scoped>
.export-progress-indeterminate {
width: 42%;
animation: export-progress-slide 1.15s ease-in-out infinite;
}
@keyframes export-progress-slide {
0% {
transform: translateX(-110%);
}
50% {
transform: translateX(70%);
}
100% {
transform: translateX(250%);
}
}
</style>
@@ -0,0 +1,98 @@
// @vitest-environment happy-dom
import { createApp, defineComponent, h, nextTick, ref, type App, type Ref } from "vue";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import i18n from "@/i18n";
const mocks = vi.hoisted(() => ({
cancelTransfer: vi.fn(),
}));
vi.mock("@/lib/backend/api", () => ({
cancelDatabaseExport: vi.fn(),
cancelSqlFileExecution: vi.fn(),
cancelTableExport: vi.fn(),
cancelTransfer: mocks.cancelTransfer,
startTransfer: vi.fn(),
}));
vi.mock("@/components/ui/dialog", async () => {
const { defineComponent, h } = await import("vue");
const passthrough = defineComponent({
setup(_props, { slots }) {
return () => h("div", slots.default?.());
},
});
return { Dialog: passthrough, DialogContent: passthrough, DialogFooter: passthrough, DialogHeader: passthrough, DialogTitle: passthrough };
});
vi.mock("@/components/ui/button", async () => {
const { defineComponent, h } = await import("vue");
return {
Button: defineComponent({
setup(_props, { slots }) {
return () => h("button", slots.default?.());
},
}),
};
});
import DataTransferProgressDialog from "@/components/transfer/DataTransferProgressDialog.vue";
import { useExportTracker } from "@/composables/useExportTracker";
const mountedApps: App[] = [];
function resetTracker() {
const tracker = useExportTracker();
for (const task of tracker.tasks.value) tracker.removeTask(task.exportId);
}
beforeEach(() => {
vi.clearAllMocks();
resetTracker();
i18n.global.locale.value = "en";
});
afterEach(() => {
for (const app of mountedApps.splice(0)) app.unmount();
document.body.innerHTML = "";
resetTracker();
});
describe("DataTransferProgressDialog", () => {
it("minimizes without cancelling and keeps the explicit cancel action", async () => {
const tracker = useExportTracker();
const task = tracker.addDataTransferTask("transfer-progress", "source → target", 2);
let dialogOpen!: Ref<boolean>;
const container = document.createElement("div");
document.body.append(container);
const app = createApp(
defineComponent({
setup() {
dialogOpen = ref(true);
return () => h(DataTransferProgressDialog, { open: dialogOpen.value, "onUpdate:open": (value: boolean) => (dialogOpen.value = value), task });
},
}),
);
mountedApps.push(app);
app.use(i18n);
app.mount(container);
await nextTick();
const buttons = () => [...document.body.querySelectorAll<HTMLButtonElement>("button")];
buttons()
.find((button) => button.textContent?.includes("Minimize to background"))
?.click();
await nextTick();
expect(dialogOpen.value).toBe(false);
expect(mocks.cancelTransfer).not.toHaveBeenCalled();
expect(task.status).toBe("Running");
dialogOpen.value = true;
await nextTick();
buttons()
.find((button) => button.textContent?.trim() === "Cancel")
?.click();
await vi.waitFor(() => expect(mocks.cancelTransfer).toHaveBeenCalledWith(task.exportId));
});
});