feat(export-tracker): 增强导出任务跟踪功能 (#1958)

- 添加了对数据库导出、SQL 文件导出和数据传输任务的支持
- 引入了新的状态和错误处理机制
- 实现了任务的重叠检查,防止同时进行相同目标表的数据传输
- 更新了 API 调用以支持新的任务类型
- 增加了相应的国际化文本
- 添加了单元测试以确保新功能的正确性
This commit is contained in:
二丫讲梵
2026-06-26 18:50:25 +08:00
committed by GitHub
parent 79c2cd8bdc
commit 10aff74072
15 changed files with 838 additions and 208 deletions
@@ -17,9 +17,11 @@ import { isTauriRuntime } from "@/lib/tauriRuntime";
import { useToast } from "@/composables/useToast";
import { Input } from "@/components/ui/input";
import { Download, Square, CheckSquare, Search, X } from "@lucide/vue";
import { useExportTracker } from "@/composables/useExportTracker";
const { t } = useI18n();
const { toast } = useToast();
const { addDatabaseExportTask, updateDatabaseExportTask } = useExportTracker();
const open = defineModel<boolean>("open", { default: false });
const store = useConnectionStore();
@@ -203,9 +205,12 @@ async function startExport() {
batchSize: 1000,
};
addDatabaseExportTask(exportId.value, database.value || "database", filePath);
try {
await api.exportDatabaseSql(request, (progress) => {
exportProgress.value = { ...progress };
updateDatabaseExportTask(progress.exportId, progress);
if (progress.status === "Done") {
exportDone.value = true;
isExporting.value = false;
@@ -220,6 +225,18 @@ async function startExport() {
});
} catch (e: any) {
exportError.value = e?.message || String(e);
const lastProgress = exportProgress.value as api.ExportProgress | null;
const fallbackProgress: api.ExportProgress = {
exportId: exportId.value,
currentObject: database.value || "database",
objectIndex: lastProgress?.objectIndex ?? 0,
totalObjects: lastProgress?.totalObjects ?? 0,
rowsExported: lastProgress?.rowsExported ?? 0,
totalRows: lastProgress?.totalRows ?? null,
status: "Error",
error: exportError.value,
};
updateDatabaseExportTask(exportId.value, fallbackProgress);
isExporting.value = false;
}
}
@@ -489,6 +506,9 @@ watch(
</Button>
</template>
<template v-else-if="isExporting">
<Button variant="outline" size="sm" @click="open = false">
{{ t("databaseExport.runInBackground") }}
</Button>
<Button variant="destructive" size="sm" @click="cancelExport">
<X class="w-3.5 h-3.5 mr-1.5" />
{{ t("transfer.cancel") }}
@@ -3,8 +3,8 @@ import { computed, ref } from "vue";
import { useI18n } from "vue-i18n";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { Button } from "@/components/ui/button";
import { Loader2, CheckCircle2, XCircle, AlertCircle, X, FileDown } from "@lucide/vue";
import { useExportTracker } from "@/composables/useExportTracker";
import { Loader2, CheckCircle2, XCircle, AlertCircle, X, FileDown, DatabaseBackup, FileCode2, ArrowRightLeft } from "@lucide/vue";
import { useExportTracker, type ExportTask } from "@/composables/useExportTracker";
const { t } = useI18n();
const { tasks, activeCount, hasActive, clearFinished, cancelTask, removeTask } = useExportTracker();
@@ -26,21 +26,77 @@ const isActive = (status: string) => status === "Running" || status === "Writing
const isFinished = (status: string) => status === "Done" || status === "Error" || status === "Cancelled";
const finishedCount = computed(() => tasks.value.filter((t) => isFinished(t.status)).length);
const failedCount = computed(() => tasks.value.filter((t) => t.status === "Error").length);
const triggerTitle = computed(() => (failedCount.value > 0 ? t("exportProgress.failedTooltip", { count: failedCount.value }) : t("exportProgress.tooltip")));
const progressPercent = (totalRows: number | null, rowsExported: number) => {
if (!totalRows || totalRows <= 0) return 0;
return Math.min(100, Math.round((rowsExported / totalRows) * 100));
};
const rowsText = (task: { totalRows: number | null; rowsExported: number }) => {
if (task.totalRows) {
return `${task.rowsExported.toLocaleString()} / ${task.totalRows.toLocaleString()}`;
const progressValue = (task: ExportTask) => {
if (task.kind === "database-export") {
if (!task.totalObjects || task.totalObjects <= 0) return 0;
return Math.min(100, Math.round(((task.objectIndex ?? 0) / task.totalObjects) * 100));
}
if (task.kind === "sql-file") {
const total = task.totalRows ?? task.statementIndex ?? 0;
if (task.status === "Done") return 100;
if (total <= 0) return 0;
return Math.min(95, Math.round((task.rowsExported / total) * 100));
}
if (task.kind === "data-transfer") {
if (!task.totalTables || task.totalTables <= 0) return 0;
if (task.status === "Done") return 100;
return Math.min(95, Math.round(((task.tableIndex ?? 0) / task.totalTables) * 100));
}
return progressPercent(task.totalRows, task.rowsExported);
};
const taskTitle = (task: ExportTask) => {
if (task.kind === "database-export") return t("exportProgress.databaseExportTitle", { name: task.tableName });
if (task.kind === "sql-file") return t("exportProgress.sqlFileTitle", { name: task.tableName });
if (task.kind === "data-transfer") return t("exportProgress.dataTransferTitle", { name: task.tableName });
return `${task.tableName}.${task.format}`;
};
const rowsText = (task: ExportTask) => {
if (task.kind === "database-export") {
if (task.totalObjects) {
return t("exportProgress.objectsCount", {
current: (task.objectIndex ?? 0).toLocaleString(),
total: task.totalObjects.toLocaleString(),
});
}
return t("exportProgress.rowsExported", { count: task.rowsExported.toLocaleString() });
}
if (task.kind === "sql-file") {
return t("exportProgress.statementsCount", {
done: (task.successCount ?? 0).toLocaleString(),
failed: (task.failureCount ?? 0).toLocaleString(),
});
}
if (task.kind === "data-transfer") {
const tableText = task.totalTables
? t("exportProgress.tablesCount", {
current: (task.tableIndex ?? 0).toLocaleString(),
total: task.totalTables.toLocaleString(),
})
: task.currentTable || "";
const rowText = task.totalRows ? `${task.rowsExported.toLocaleString()} / ${task.totalRows.toLocaleString()} ${t("exportProgress.rowsShort")}` : `${task.rowsExported.toLocaleString()} ${t("exportProgress.rowsShort")}`;
return task.currentTable ? `${tableText} · ${task.currentTable} · ${rowText}` : tableText;
}
if (task.totalRows) return `${task.rowsExported.toLocaleString()} / ${task.totalRows.toLocaleString()}`;
return `${task.rowsExported.toLocaleString()} ${t("exportProgress.rowsShort")}`;
};
const statusIcon = (status: string) => {
switch (status) {
const statusIcon = (task: ExportTask) => {
if (isActive(task.status)) {
if (task.kind === "database-export") return DatabaseBackup;
if (task.kind === "sql-file") return FileCode2;
if (task.kind === "data-transfer") return ArrowRightLeft;
}
switch (task.status) {
case "Running":
case "Writing":
return Loader2;
@@ -79,30 +135,31 @@ function toggleShowAll() {
<template>
<Popover v-if="tasks.length > 0" v-model:open="open">
<PopoverTrigger as-child>
<Button variant="ghost" size="icon" class="relative h-8 w-8" :title="t('exportProgress.tooltip')" :class="{ 'bg-accent text-primary': hasActive }">
<Button variant="ghost" size="icon" class="relative h-8 w-8" :title="triggerTitle" :class="{ 'bg-destructive/10 text-destructive hover:bg-destructive/15': failedCount > 0, 'bg-accent text-primary': failedCount === 0 && hasActive }">
<FileDown class="h-4 w-4" />
<span v-if="hasActive" class="absolute -right-0.5 -top-0.5 flex h-4 min-w-4 items-center justify-center rounded-full bg-primary px-1 text-[10px] font-medium leading-none text-primary-foreground">
<span v-if="failedCount > 0" class="absolute -right-0.5 -top-0.5 flex h-4 min-w-4 items-center justify-center rounded-full bg-destructive px-1 text-[10px] font-bold leading-none text-destructive-foreground"> ! </span>
<span v-else-if="hasActive" class="absolute -right-0.5 -top-0.5 flex h-4 min-w-4 items-center justify-center rounded-full bg-primary px-1 text-[10px] font-medium leading-none text-primary-foreground">
{{ activeCount > 9 ? "9+" : activeCount }}
</span>
</Button>
</PopoverTrigger>
<PopoverContent align="start" class="w-80 p-0 gap-0 overflow-hidden" :side-offset="8">
<div class="border-b bg-muted/40 px-3 py-2">
<PopoverContent align="start" class="w-[min(92vw,30rem)] p-0 gap-0 overflow-hidden" :side-offset="8">
<div class="border-b bg-muted/40 px-4 py-3">
<div class="text-sm font-semibold">{{ t("exportProgress.popoverTitle") }}</div>
</div>
<div class="max-h-80 overflow-y-auto">
<div v-for="task in visibleTasks" :key="task.exportId" class="flex items-center gap-2 border-b px-3 py-2.5 text-xs last:border-b-0">
<div class="flex-1 min-w-0 flex flex-col gap-1">
<div class="max-h-96 overflow-y-auto">
<div v-for="task in visibleTasks" :key="task.exportId" class="flex items-start gap-3 border-b px-4 py-3 text-xs last:border-b-0">
<div class="flex-1 min-w-0 flex flex-col gap-1.5">
<div class="flex items-center gap-1.5">
<component :is="statusIcon(task.status)" :class="[statusColor(task.status), isActive(task.status) ? 'animate-spin' : '']" class="h-3.5 w-3.5 shrink-0" />
<span class="truncate font-medium">{{ task.tableName }}.{{ task.format }}</span>
<component :is="statusIcon(task)" :class="[statusColor(task.status), task.kind === 'table-export' && isActive(task.status) ? 'animate-spin' : '']" class="h-3.5 w-3.5 shrink-0" />
<span class="truncate font-medium">{{ taskTitle(task) }}</span>
</div>
<!-- Progress bar -->
<div v-if="isActive(task.status)" class="w-full bg-muted rounded-full h-1.5 overflow-hidden">
<div v-if="task.totalRows" class="h-full bg-primary rounded-full transition-[width] duration-300" :style="{ width: `${progressPercent(task.totalRows, task.rowsExported)}%` }" />
<div v-if="task.totalRows || (task.kind === 'database-export' && task.totalObjects) || (task.kind === 'data-transfer' && task.totalTables)" class="h-full bg-primary rounded-full transition-[width] duration-300" :style="{ width: `${progressValue(task)}%` }" />
<div v-else class="h-full w-full overflow-hidden rounded-full">
<div class="export-progress-indeterminate h-full rounded-full bg-primary" />
</div>
@@ -111,16 +168,16 @@ function toggleShowAll() {
<div class="h-full bg-green-500 rounded-full" style="width: 100%" />
</div>
<div class="flex items-center justify-between text-muted-foreground">
<span class="tabular-nums">{{ rowsText(task) }}</span>
<span v-if="task.status === 'Error' && task.errorMessage" class="truncate ml-2 text-destructive" :title="task.errorMessage">
<div class="min-w-0 text-muted-foreground">
<span class="break-words tabular-nums">{{ rowsText(task) }}</span>
<span v-if="task.status === 'Error' && task.errorMessage" class="mt-1 block whitespace-normal break-words text-destructive" :title="task.errorMessage">
{{ task.errorMessage }}
</span>
</div>
</div>
<!-- Actions: stop/cancel for active, delete for finished -->
<div class="flex shrink-0 self-center">
<div class="flex shrink-0 pt-4">
<button v-if="isActive(task.status)" class="flex h-6 w-6 items-center justify-center rounded hover:bg-muted" :title="t('exportProgress.cancel')" @click="cancelTask(task.exportId)">
<X class="h-3.5 w-3.5 text-muted-foreground hover:text-destructive" />
</button>
@@ -126,7 +126,7 @@ watch(
@confirm="emit('dangerConfirm')"
/>
<SqlParameterDialog v-if="showSqlParameterDialog" :open="showSqlParameterDialog" :sql="sqlParameterSourceSql" :parameters="sqlParameterNames" @update:open="emit('update:showSqlParameterDialog', $event)" @execute="emit('sqlParametersConfirm', $event)" />
<DataTransferDialog v-if="dialogs.showTransferDialog.value" v-model:open="dialogs.showTransferDialog.value" :prefill-connection-id="dialogs.transferPrefillConnectionId.value" :prefill-database="dialogs.transferPrefillDatabase.value" />
<DataTransferDialog v-model:open="dialogs.showTransferDialog.value" :prefill-connection-id="dialogs.transferPrefillConnectionId.value" :prefill-database="dialogs.transferPrefillDatabase.value" />
<SchemaDiffDialog v-if="dialogs.showSchemaDiffDialog.value" v-model:open="dialogs.showSchemaDiffDialog.value" :prefill-connection-id="dialogs.schemaDiffPrefillConnectionId.value" :prefill-database="dialogs.schemaDiffPrefillDatabase.value" :prefill-schema="dialogs.schemaDiffPrefillSchema.value" />
<DataCompareDialog
v-if="dialogs.showDataCompareDialog.value"
@@ -136,7 +136,7 @@ watch(
:prefill-schema="dialogs.dataComparePrefillSchema.value"
:prefill-table="dialogs.dataComparePrefillTable.value"
/>
<SqlFileExecutionDialog v-if="dialogs.showSqlFileDialog.value" v-model:open="dialogs.showSqlFileDialog.value" :prefill-connection-id="dialogs.sqlFilePrefillConnectionId.value" :prefill-database="dialogs.sqlFilePrefillDatabase.value" />
<SqlFileExecutionDialog v-model:open="dialogs.showSqlFileDialog.value" :prefill-connection-id="dialogs.sqlFilePrefillConnectionId.value" :prefill-database="dialogs.sqlFilePrefillDatabase.value" />
<SchemaDiagramDialog
v-if="dialogs.showDiagramDialog.value"
v-model:open="dialogs.showDiagramDialog.value"
@@ -14,11 +14,13 @@ import { useToast } from "@/composables/useToast";
import { useConnectionStore } from "@/stores/connectionStore";
import { databaseOptionsForConnection } from "@/composables/useDatabaseOptions";
import { cancelSqlFileExecution, executeSqlFile, listenSqlFileProgress, listDatabases, previewSqlFile, type SqlFilePreview, type SqlFileProgress, type SqlFileStatus } from "@/lib/api";
import { useExportTracker } from "@/composables/useExportTracker";
import { Check, CheckSquare, FileCode, FolderOpen, Loader2, Play, Square, X } from "@lucide/vue";
const { t } = useI18n();
const { toast } = useToast();
const { highlight } = useSqlHighlighter();
const { addSqlFileTask, updateSqlFileTask } = useExportTracker();
const open = defineModel<boolean>("open", { default: false });
const props = defineProps<{
@@ -282,6 +284,7 @@ async function startExecution() {
terminalStatus.value = "running";
terminalError.value = "";
progress.value = null;
addSqlFileTask(id, preview.value.fileName, preview.value.filePath);
let unlisten: (() => void) | undefined;
try {
@@ -296,6 +299,7 @@ async function startExecution() {
progress.value = next;
terminalStatus.value = next.status;
terminalError.value = next.error ?? terminalError.value;
updateSqlFileTask(id, next);
if (isTerminalStatus(next.status)) {
running.value = false;
cancelling.value = false;
@@ -320,6 +324,18 @@ async function startExecution() {
});
if (!isTerminalStatus(terminalStatus.value)) {
terminalStatus.value = cancelRequested.value ? "cancelled" : "done";
const lastProgress = progress.value as SqlFileProgress | null;
updateSqlFileTask(id, {
executionId: id,
status: terminalStatus.value,
statementIndex: lastProgress?.statementIndex ?? 0,
successCount: lastProgress?.successCount ?? 0,
failureCount: lastProgress?.failureCount ?? 0,
affectedRows: lastProgress?.affectedRows ?? 0,
elapsedMs: lastProgress?.elapsedMs ?? 0,
statementSummary: lastProgress?.statementSummary ?? "",
error: lastProgress?.error ?? null,
});
if (terminalStatus.value === "done") {
await refreshTargetAfterImport();
}
@@ -327,6 +343,18 @@ async function startExecution() {
} catch (e: any) {
terminalStatus.value = cancelRequested.value ? "cancelled" : "error";
terminalError.value = e?.message || String(e);
const lastProgress = progress.value as SqlFileProgress | null;
updateSqlFileTask(id, {
executionId: id,
status: terminalStatus.value,
statementIndex: lastProgress?.statementIndex ?? 0,
successCount: lastProgress?.successCount ?? 0,
failureCount: lastProgress?.failureCount ?? 0,
affectedRows: lastProgress?.affectedRows ?? 0,
elapsedMs: lastProgress?.elapsedMs ?? 0,
statementSummary: lastProgress?.statementSummary ?? "",
error: terminalError.value,
});
if (!cancelRequested.value) {
toast(terminalError.value, 5000);
}
@@ -356,7 +384,6 @@ async function cancelExecution() {
}
function handleOpenChange(nextOpen: boolean) {
if (!nextOpen && running.value) return;
open.value = nextOpen;
}
@@ -373,6 +400,7 @@ watch(
open,
(value) => {
if (!value) return;
if (running.value) return;
resetState();
if (connectionId.value) {
loadDatabasesForConnection(connectionId.value);
@@ -543,6 +571,9 @@ watch(
<DialogFooter>
<template v-if="running">
<Button variant="outline" size="sm" @click="open = false">
{{ t("sqlFile.runInBackground") }}
</Button>
<Button variant="destructive" size="sm" :disabled="cancelling" @click="cancelExecution">
<Loader2 v-if="cancelling" class="w-3.5 h-3.5 mr-1.5 animate-spin" />
<X v-else class="w-3.5 h-3.5 mr-1.5" />
@@ -10,14 +10,15 @@ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@
import { useConnectionStore } from "@/stores/connectionStore";
import DatabaseIcon from "@/components/icons/DatabaseIcon.vue";
import * as api from "@/lib/api";
import type { TransferProgress, TransferMode, TransferTableNameCase } from "@/lib/api";
import type { TransferMode, TransferTableNameCase } from "@/lib/api";
import type { DatabaseType } from "@/types/database";
import { isSchemaAware, supportsTransfer } from "@/lib/databaseCapabilities";
import { databaseOptionsForConnection } from "@/composables/useDatabaseOptions";
import { nextTransferTerminalState } from "@/lib/transferProgressState";
import { ArrowRightLeft, Check, X, Loader2, Square, CheckSquare } from "@lucide/vue";
import { useExportTracker } from "@/composables/useExportTracker";
import { ArrowRightLeft, Loader2, Square, CheckSquare } from "@lucide/vue";
const { t } = useI18n();
const { startDataTransferTask } = useExportTracker();
const open = defineModel<boolean>("open", { default: false });
const props = defineProps<{
@@ -52,15 +53,7 @@ const createTable = ref(true);
const transferMode = ref<TransferMode>("append");
const targetTableNameCase = ref<TransferTableNameCase>("preserve");
const batchSize = ref(1000);
// Transfer state
const isTransferring = ref(false);
const transferProgress = ref<Map<string, TransferProgress>>(new Map());
const currentTable = ref("");
const overallDone = ref(false);
const overallError = ref(false);
const overallCancelled = ref(false);
const transferId = ref("");
const isSubmitting = ref(false);
const filteredTables = computed(() => {
const q = tableSearch.value.toLowerCase();
@@ -252,33 +245,27 @@ function resetState() {
transferMode.value = "append";
targetTableNameCase.value = "preserve";
batchSize.value = 1000;
isTransferring.value = false;
transferProgress.value.clear();
currentTable.value = "";
overallDone.value = false;
overallError.value = false;
overallCancelled.value = false;
isSubmitting.value = false;
}
async function startTransfer() {
isTransferring.value = true;
overallDone.value = false;
overallError.value = false;
overallCancelled.value = false;
transferProgress.value.clear();
transferId.value = uuid();
if (!canStart.value || isSubmitting.value) return;
isSubmitting.value = true;
const effectiveSourceSchema = sourceSchema.value || sourceDatabase.value;
const effectiveTargetSchema = targetSchema.value || targetDatabase.value;
const sourceDatabaseName = sourceDatabase.value;
const targetConnection = targetConnectionId.value;
const targetDatabaseName = targetDatabase.value;
const shouldRefreshTargetTree = createTable.value;
const request: api.TransferRequest = {
transferId: transferId.value,
transferId: uuid(),
sourceConnectionId: sourceConnectionId.value,
sourceDatabase: sourceDatabase.value,
sourceDatabase: sourceDatabaseName,
sourceSchema: effectiveSourceSchema,
targetConnectionId: targetConnectionId.value,
targetDatabase: targetDatabase.value,
targetConnectionId: targetConnection,
targetDatabase: targetDatabaseName,
targetSchema: effectiveTargetSchema,
tables: [...selectedTables.value],
createTable: createTable.value,
@@ -287,38 +274,16 @@ async function startTransfer() {
batchSize: batchSize.value,
};
try {
await api.startTransfer(request, (progress) => {
if (progress.table) {
transferProgress.value.set(progress.table, progress);
transferProgress.value = new Map(transferProgress.value);
currentTable.value = progress.table;
startDataTransferTask(request, `${sourceDatabaseName} → ${targetDatabaseName}`, {
formatOverlapError: (tables) => t("transfer.targetTableBusy", { tables: tables.join(", ") }),
onDone: async () => {
if (shouldRefreshTargetTree) {
await store.refreshObjectListTreeNode(targetConnection, targetDatabaseName, effectiveTargetSchema);
}
const nextState = nextTransferTerminalState(
{
done: overallDone.value,
cancelled: overallCancelled.value,
error: overallError.value,
},
progress,
);
overallDone.value = nextState.done;
overallCancelled.value = nextState.cancelled;
overallError.value = nextState.error;
});
if (createTable.value && !overallError.value && !overallCancelled.value) {
await store.refreshObjectListTreeNode(targetConnectionId.value, targetDatabase.value, targetSchema.value);
}
} catch (e: any) {
overallError.value = true;
}
}
async function cancelTransfer() {
if (transferId.value) {
await api.cancelTransfer(transferId.value);
}
},
});
open.value = false;
resetState();
}
function getConnectionName(id: string) {
@@ -328,29 +293,6 @@ function getConnectionName(id: string) {
function getConnectionType(id: string): DatabaseType {
return store.connections.find((c) => c.id === id)?.db_type ?? "mysql";
}
const processedStatuses = new Set<TransferProgress["status"]>(["tableDone", "done", "error", "cancelled"]);
function formatRowCount(count: number) {
return count.toLocaleString();
}
function formatTableRows(progress: TransferProgress) {
if (typeof progress.totalRows === "number") {
return `${formatRowCount(progress.rowsTransferred)} / ${formatRowCount(progress.totalRows)}`;
}
return formatRowCount(progress.rowsTransferred);
}
const completedTables = computed(() => [...transferProgress.value.values()].filter((p) => processedStatuses.has(p.status)).length);
const failedTables = computed(() => [...transferProgress.value.values()].filter((p) => p.status === "error").length);
const totalTransferred = computed(() => [...transferProgress.value.values()].reduce((sum, p) => sum + p.rowsTransferred, 0));
const knownTotalRows = computed(() => [...transferProgress.value.values()].reduce((sum, p) => sum + (typeof p.totalRows === "number" ? p.totalRows : 0), 0));
const overallRowsLabel = computed(() => (knownTotalRows.value > 0 ? `${formatRowCount(totalTransferred.value)} / ${formatRowCount(knownTotalRows.value)}` : formatRowCount(totalTransferred.value)));
</script>
<template>
@@ -364,8 +306,7 @@ const overallRowsLabel = computed(() => (knownTotalRows.value > 0 ? `${formatRow
</DialogHeader>
<div class="flex-1 min-h-0 overflow-auto">
<!-- Config View -->
<div v-if="!isTransferring" class="grid gap-4 py-3">
<div class="grid gap-4 py-3">
<!-- Source Section -->
<div class="space-y-3">
<div class="text-xs font-medium text-muted-foreground uppercase tracking-wider">
@@ -546,86 +487,17 @@ const overallRowsLabel = computed(() => (knownTotalRows.value > 0 ? `${formatRow
</div>
</div>
</div>
<!-- Progress View -->
<div v-else class="py-3 space-y-3">
<div class="flex items-center justify-between text-xs text-muted-foreground">
<span>
{{ t("transfer.overallProgress") }}: {{ completedTables }} / {{ selectedTables.size }} {{ t("transfer.tables").toLowerCase() }} · {{ overallRowsLabel }}
{{ t("grid.rows", { count: "" }).trim() }}
</span>
<span v-if="overallDone && !failedTables" class="text-green-600 font-medium">{{ t("transfer.completed") }}</span>
<span v-else-if="overallDone && failedTables" class="text-amber-600 font-medium">
{{ t("transfer.completedWithErrors", { count: failedTables }) }}
</span>
<span v-else-if="overallCancelled" class="text-yellow-600 font-medium">{{ t("transfer.cancelled") }}</span>
<span v-else-if="overallError" class="text-destructive font-medium">{{ t("transfer.failed") }}</span>
</div>
<div class="w-full bg-muted rounded-full h-2 overflow-hidden">
<div
class="h-full rounded-full transition-[width] duration-300"
:class="overallError ? 'bg-destructive' : overallCancelled ? 'bg-yellow-500' : overallDone && failedTables ? 'bg-amber-500' : 'bg-primary'"
:style="{
width: `${selectedTables.size ? (completedTables / selectedTables.size) * 100 : 0}%`,
}"
/>
</div>
<div class="border rounded-md max-h-[280px] overflow-y-auto">
<div v-for="table in [...selectedTables]" :key="table" class="flex items-center justify-between px-2.5 py-1.5 text-xs border-b last:border-b-0">
<span class="truncate">{{ table }}</span>
<div class="flex items-center gap-1.5 shrink-0 text-muted-foreground">
<template v-if="transferProgress.get(table)">
<template v-if="transferProgress.get(table)!.status === 'running'">
<Loader2 class="w-3 h-3 animate-spin text-primary" />
<span>{{ formatTableRows(transferProgress.get(table)!) }}</span>
</template>
<template v-else-if="transferProgress.get(table)!.status === 'tableDone' || transferProgress.get(table)!.status === 'done'">
<Check class="w-3 h-3 text-green-500" />
<span>{{ formatTableRows(transferProgress.get(table)!) }}</span>
</template>
<template v-else-if="transferProgress.get(table)!.status === 'error'">
<X class="w-3 h-3 text-destructive" />
<span>{{ formatTableRows(transferProgress.get(table)!) }}</span>
<span class="max-w-[520px] whitespace-normal break-words text-destructive" :title="transferProgress.get(table)!.error ?? ''">
{{ transferProgress.get(table)!.error }}
</span>
</template>
<template v-else-if="transferProgress.get(table)!.status === 'cancelled'">
<X class="w-3 h-3 text-yellow-500" />
<span>{{ t("transfer.cancelled") }}</span>
</template>
</template>
<span v-else class="text-muted-foreground/40">—</span>
</div>
</div>
</div>
<!-- Status message -->
</div>
</div>
<DialogFooter>
<template v-if="!isTransferring">
<Button variant="outline" size="sm" @click="open = false">
{{ t("transfer.cancel") }}
</Button>
<Button size="sm" :disabled="!canStart" @click="startTransfer">
<ArrowRightLeft class="w-3.5 h-3.5 mr-1.5" />
{{ t("transfer.start") }}
</Button>
</template>
<template v-else-if="overallDone || overallCancelled || overallError">
<Button size="sm" @click="open = false">
{{ t("common.close") }}
</Button>
</template>
<template v-else>
<Button variant="destructive" size="sm" @click="cancelTransfer">
{{ t("transfer.cancel") }}
</Button>
</template>
<Button variant="outline" size="sm" @click="open = false">
{{ t("transfer.cancel") }}
</Button>
<Button size="sm" :disabled="!canStart || isSubmitting" @click="startTransfer">
<Loader2 v-if="isSubmitting" class="w-3.5 h-3.5 mr-1.5 animate-spin" />
<ArrowRightLeft v-else class="w-3.5 h-3.5 mr-1.5" />
{{ t("transfer.start") }}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
@@ -1,18 +1,79 @@
import { reactive, computed } from "vue";
import * as api from "@/lib/api";
export type BackgroundTaskKind = "table-export" | "database-export" | "sql-file" | "data-transfer";
export type BackgroundTaskStatus = "Running" | "Writing" | "Done" | "Error" | "Cancelled";
export interface ExportTask {
exportId: string;
kind: BackgroundTaskKind;
tableName: string;
format: "csv" | "xlsx";
format: string;
filePath: string;
rowsExported: number;
totalRows: number | null;
status: string;
status: BackgroundTaskStatus;
errorMessage: string | null;
objectIndex?: number;
totalObjects?: number;
statementIndex?: number;
successCount?: number;
failureCount?: number;
affectedRows?: number;
elapsedMs?: number;
statementSummary?: string;
tableIndex?: number;
totalTables?: number;
currentTable?: string;
targetConnectionId?: string;
targetDatabase?: string;
targetSchema?: string;
targetTables?: string[];
}
const taskMap = reactive<Map<string, ExportTask>>(new Map());
const activeTransferRuns = new Set<string>();
function normalizeExportStatus(status: string): BackgroundTaskStatus {
if (status === "Writing" || status === "Done" || status === "Error" || status === "Cancelled") return status;
return "Running";
}
function normalizeSqlFileStatus(status: api.SqlFileStatus): BackgroundTaskStatus {
if (status === "done") return "Done";
if (status === "error") return "Error";
if (status === "cancelled") return "Cancelled";
return "Running";
}
function normalizeTransferStatus(status: api.TransferProgress["status"]): BackgroundTaskStatus {
if (status === "done") return "Done";
if (status === "error") return "Error";
if (status === "cancelled") return "Cancelled";
return "Running";
}
function targetTableName(table: string, nameCase: api.TransferTableNameCase): string {
if (nameCase === "lower") return table.toLowerCase();
if (nameCase === "upper") return table.toUpperCase();
return table;
}
function findActiveOverlappingTransfer(request: api.TransferRequest): string[] {
const requestedTables = new Set(request.tables.map((table) => targetTableName(table, request.targetTableNameCase)));
for (const task of taskMap.values()) {
if (task.exportId === request.transferId) continue;
if (task.kind !== "data-transfer") continue;
if (task.status !== "Running" && task.status !== "Writing") continue;
if (task.targetConnectionId !== request.targetConnectionId) continue;
if (task.targetDatabase !== request.targetDatabase) continue;
if (task.targetSchema !== request.targetSchema) continue;
const overlappingTables = (task.targetTables ?? []).filter((table) => requestedTables.has(table));
if (overlappingTables.length > 0) return overlappingTables;
}
return [];
}
export function useExportTracker() {
const tasks = computed(() => Array.from(taskMap.values()));
@@ -33,10 +94,11 @@ export function useExportTracker() {
.replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/, "$1-$2-$3-$4-$5");
}
function addTask(tableName: string, format: "csv" | "xlsx", filePath: string): ExportTask {
function addTask(tableName: string, format: string, filePath: string): ExportTask {
const exportId = generateUUID();
const task = reactive<ExportTask>({
exportId,
kind: "table-export",
tableName,
format,
filePath,
@@ -49,6 +111,174 @@ export function useExportTracker() {
return task;
}
function addDatabaseExportTask(exportId: string, label: string, filePath: string): ExportTask {
const task = reactive<ExportTask>({
exportId,
kind: "database-export",
tableName: label,
format: "sql",
filePath,
rowsExported: 0,
totalRows: null,
status: "Running",
errorMessage: null,
objectIndex: 0,
totalObjects: 0,
});
taskMap.set(exportId, task);
return task;
}
function addSqlFileTask(executionId: string, fileName: string, filePath: string): ExportTask {
const task = reactive<ExportTask>({
exportId: executionId,
kind: "sql-file",
tableName: fileName,
format: "sql",
filePath,
rowsExported: 0,
totalRows: null,
status: "Running",
errorMessage: null,
statementIndex: 0,
successCount: 0,
failureCount: 0,
affectedRows: 0,
elapsedMs: 0,
statementSummary: "",
});
taskMap.set(executionId, task);
return task;
}
function addDataTransferTask(transferId: string, label: string, totalTables: number): ExportTask {
const task = reactive<ExportTask>({
exportId: transferId,
kind: "data-transfer",
tableName: label,
format: "transfer",
filePath: "",
rowsExported: 0,
totalRows: null,
status: "Running",
errorMessage: null,
tableIndex: 0,
totalTables,
currentTable: "",
});
taskMap.set(transferId, task);
return task;
}
function startDataTransferTask(
request: api.TransferRequest,
label: string,
options: {
onDone?: () => void | Promise<void>;
formatOverlapError?: (tables: string[]) => string;
} = {},
): ExportTask {
const existingTask = taskMap.get(request.transferId);
const task = existingTask ?? addDataTransferTask(request.transferId, label, request.tables.length);
task.targetConnectionId = request.targetConnectionId;
task.targetDatabase = request.targetDatabase;
task.targetSchema = request.targetSchema;
task.targetTables = request.tables.map((table) => targetTableName(table, request.targetTableNameCase));
if (activeTransferRuns.has(request.transferId)) return task;
const overlappingTables = findActiveOverlappingTransfer(request);
if (overlappingTables.length > 0) {
task.status = "Error";
const visibleTables = overlappingTables.slice(0, 5);
task.errorMessage = options.formatOverlapError?.(visibleTables) ?? `Another data transfer is already running for target table(s): ${visibleTables.join(", ")}`;
return task;
}
activeTransferRuns.add(request.transferId);
let terminalStatus: api.TransferProgress["status"] | null = null;
void (async () => {
try {
await api.startTransfer(request, (progress) => {
terminalStatus = progress.status === "done" || progress.status === "error" || progress.status === "cancelled" ? progress.status : terminalStatus;
updateDataTransferTask(progress.transferId, progress);
});
if (terminalStatus === "done" && task.status === "Done") {
await options.onDone?.();
}
} catch (e: any) {
updateDataTransferTask(request.transferId, {
transferId: request.transferId,
table: task.currentTable || "",
tableIndex: task.tableIndex ?? 0,
totalTables: task.totalTables ?? request.tables.length,
rowsTransferred: task.rowsExported,
totalRows: task.totalRows,
status: "error",
error: e?.message || String(e),
});
} finally {
activeTransferRuns.delete(request.transferId);
}
})();
return task;
}
function updateTableExportTask(exportId: string, progress: api.TableExportProgress) {
const task = taskMap.get(exportId);
if (!task) return;
task.tableName = progress.tableName || task.tableName;
task.rowsExported = progress.rowsExported;
task.totalRows = progress.totalRows;
task.status = normalizeExportStatus(progress.status);
task.errorMessage = progress.errorMessage || null;
}
function updateDatabaseExportTask(exportId: string, progress: api.ExportProgress) {
const task = taskMap.get(exportId);
if (!task) return;
task.tableName = progress.currentObject || task.tableName;
task.rowsExported = progress.rowsExported;
task.totalRows = progress.totalRows;
task.status = normalizeExportStatus(progress.status);
task.errorMessage = progress.error || null;
task.objectIndex = progress.objectIndex;
task.totalObjects = progress.totalObjects;
}
function updateSqlFileTask(executionId: string, progress: api.SqlFileProgress) {
const task = taskMap.get(executionId);
if (!task) return;
task.status = normalizeSqlFileStatus(progress.status);
task.errorMessage = progress.error || null;
task.statementIndex = progress.statementIndex;
task.successCount = progress.successCount;
task.failureCount = progress.failureCount;
task.affectedRows = progress.affectedRows;
task.elapsedMs = progress.elapsedMs;
task.statementSummary = progress.statementSummary;
task.rowsExported = progress.successCount + progress.failureCount;
task.totalRows = Math.max(progress.statementIndex, progress.successCount + progress.failureCount) || null;
}
function updateDataTransferTask(transferId: string, progress: api.TransferProgress) {
const task = taskMap.get(transferId);
if (!task) return;
const nextStatus = normalizeTransferStatus(progress.status);
const hadError = task.status === "Error";
task.status = hadError && nextStatus === "Done" ? "Error" : nextStatus;
task.errorMessage = progress.error || task.errorMessage || null;
task.tableIndex = progress.tableIndex;
task.totalTables = progress.totalTables;
task.currentTable = progress.table || task.currentTable;
if (progress.table || progress.rowsTransferred > 0 || task.rowsExported === 0) {
task.rowsExported = progress.rowsTransferred;
}
task.totalRows = progress.totalRows ?? task.totalRows;
}
function removeTask(exportId: string) {
taskMap.delete(exportId);
}
@@ -62,8 +292,17 @@ export function useExportTracker() {
}
async function cancelTask(exportId: string) {
const task = taskMap.get(exportId);
try {
await api.cancelTableExport(exportId);
if (task?.kind === "database-export") {
await api.cancelDatabaseExport(exportId);
} else if (task?.kind === "sql-file") {
await api.cancelSqlFileExecution(exportId);
} else if (task?.kind === "data-transfer") {
await api.cancelTransfer(exportId);
} else {
await api.cancelTableExport(exportId);
}
} catch {
// ignore
}
@@ -74,6 +313,14 @@ export function useExportTracker() {
activeCount,
hasActive,
addTask,
addDatabaseExportTask,
addSqlFileTask,
addDataTransferTask,
startDataTransferTask,
updateTableExportTask,
updateDatabaseExportTask,
updateSqlFileTask,
updateDataTransferTask,
removeTask,
clearFinished,
cancelTask,
+17 -3
View File
@@ -846,13 +846,20 @@ export default {
rowsShort: "rows",
cancel: "Cancel",
close: "Close",
tooltip: "Export Progress",
popoverTitle: "Exports",
noTasks: "No exports",
tooltip: "Background Tasks",
failedTooltip: "{count} background task failed | {count} background tasks failed",
popoverTitle: "Background Tasks",
noTasks: "No background tasks",
clearFinished: "Clear finished",
showMore: "Show {count} more",
showLess: "Show less",
delete: "Remove",
databaseExportTitle: "Database backup: {name}",
sqlFileTitle: "SQL file: {name}",
dataTransferTitle: "Data transfer: {name}",
objectsCount: "{current} / {total} objects",
tablesCount: "{current} / {total} tables",
statementsCount: "{done} succeeded, {failed} failed",
},
welcome: {
title: "Database Workspace",
@@ -1965,6 +1972,11 @@ export default {
tableError: "Table {table} failed: {error}",
overallProgress: "Overall progress",
dataTransfer: "Data Transfer",
runInBackground: "Run in background",
newTask: "New transfer",
editConfig: "Edit config",
retry: "Retry",
targetTableBusy: "Another transfer is already writing target table(s): {tables}",
},
tableImport: {
title: "Import Table Data",
@@ -2141,6 +2153,7 @@ export default {
databasePlaceholder: "Database name",
options: "Options",
continueOnError: "Continue on error",
runInBackground: "Run in background",
cancelling: "Cancelling...",
cancel: "Cancel",
execute: "Execute",
@@ -2877,6 +2890,7 @@ export default {
exportSuccess: "Database exported successfully",
exportError: "Export failed: {error}",
exportCancelled: "Export cancelled",
runInBackground: "Run in background",
},
nacos: {
configs: "Configs",
+17 -3
View File
@@ -847,13 +847,20 @@ export default {
rowsShort: "行",
cancel: "取消",
close: "关闭",
tooltip: "导出进度",
popoverTitle: "导出任务",
noTasks: "暂无导出任务",
tooltip: "后台任务",
failedTooltip: "{count} 个后台任务失败",
popoverTitle: "后台任务",
noTasks: "暂无后台任务",
clearFinished: "清除已完成",
showMore: "展开其余 {count} 个",
showLess: "收起",
delete: "删除",
databaseExportTitle: "数据库备份:{name}",
sqlFileTitle: "SQL 文件:{name}",
dataTransferTitle: "数据传输:{name}",
objectsCount: "{current} / {total} 个对象",
tablesCount: "{current} / {total} 张表",
statementsCount: "成功 {done},失败 {failed}",
},
welcome: {
title: "数据库工作台",
@@ -1964,6 +1971,11 @@ export default {
tableError: "表 {table} 传输失败:{error}",
overallProgress: "整体进度",
dataTransfer: "数据传输",
runInBackground: "后台运行",
newTask: "新建传输",
editConfig: "编辑配置",
retry: "重新传输",
targetTableBusy: "已有传输任务正在写入目标表:{tables}",
},
tableImport: {
title: "导入表数据",
@@ -2140,6 +2152,7 @@ export default {
databasePlaceholder: "数据库名称",
options: "选项",
continueOnError: "出错后继续",
runInBackground: "后台运行",
cancelling: "正在取消...",
cancel: "取消",
execute: "执行",
@@ -2901,6 +2914,7 @@ export default {
exportSuccess: "数据库导出成功",
exportError: "导出失败: {error}",
exportCancelled: "导出已取消",
runInBackground: "后台运行",
},
nacos: {
configs: "配置",
+1 -1
View File
@@ -1147,7 +1147,7 @@ export async function startTransfer(request: TransferRequest, onProgress: (progr
es.onmessage = (e) => {
const progress: TransferProgress = JSON.parse(e.data);
onProgress(progress);
if (progress.status === "done" || progress.status === "cancelled") {
if (progress.status === "done" || progress.status === "error" || progress.status === "cancelled") {
es.close();
resolve();
}
+1 -1
View File
@@ -1611,7 +1611,7 @@ export async function startTransfer(request: TransferRequest, onProgress: (progr
unlisten = await listen<TransferProgress>("transfer-progress", (event) => {
if (event.payload.transferId !== request.transferId) return;
onProgress(event.payload);
if (event.payload.status === "done" || event.payload.status === "cancelled") {
if (event.payload.status === "done" || event.payload.status === "error" || event.payload.status === "cancelled") {
unlisten?.();
resolve();
}
+18 -1
View File
@@ -8,8 +8,25 @@ export interface TransferStatusLike {
status: "running" | "tableDone" | "done" | "error" | "cancelled";
}
export interface TransferDialogRunState extends TransferTerminalState {
transferring: boolean;
}
export function nextTransferTerminalState(state: TransferTerminalState, progress: TransferStatusLike): TransferTerminalState {
if (progress.status === "done") return { ...state, done: true };
if (progress.status === "error") return { ...state, error: true };
if (progress.status === "done") return { ...state, done: true, error: state.error };
if (progress.status === "cancelled") return { ...state, cancelled: true };
return state;
}
export function isTransferRunActive(state: TransferDialogRunState): boolean {
return state.transferring && !state.done && !state.cancelled && !state.error;
}
export function shouldResetTransferDialogOnOpen(state: TransferDialogRunState): boolean {
return state.transferring && !isTransferRunActive(state);
}
export function shouldKeepTransferDraftOnOpen(preserveDraft: boolean, state: TransferDialogRunState): boolean {
return preserveDraft || shouldResetTransferDialogOnOpen(state);
}
+12 -2
View File
@@ -94,6 +94,7 @@ pub async fn start_transfer(
log::warn!("[transfer] failed to sort tables by FK dependency, using original order: {e}");
tables
});
let mut failed_tables: Vec<String> = Vec::new();
for (i, table) in tables.iter().enumerate() {
if transfer::is_cancelled(&req.transfer_id).await {
let progress = transfer::TransferProgress {
@@ -153,6 +154,7 @@ pub async fn start_transfer(
}
}
Err(e) => {
failed_tables.push(table.clone());
let progress = transfer::TransferProgress {
transfer_id: req.transfer_id.clone(),
table: table.clone(),
@@ -178,8 +180,16 @@ pub async fn start_transfer(
total_tables: tables.len(),
rows_transferred: 0,
total_rows: None,
status: TransferStatus::Done,
error: None,
status: if failed_tables.is_empty() { TransferStatus::Done } else { TransferStatus::Error },
error: if failed_tables.is_empty() {
None
} else {
Some(format!(
"{} table(s) failed: {}",
failed_tables.len(),
failed_tables.iter().take(5).cloned().collect::<Vec<_>>().join(", ")
))
},
};
if let Ok(json) = serde_json::to_string(&done) {
let _ = tx.send(json);
@@ -1,11 +1,11 @@
import { strict as assert } from "node:assert";
import { test } from "vitest";
import { nextTransferTerminalState } from "../../apps/desktop/src/lib/transferProgressState.ts";
import { isTransferRunActive, nextTransferTerminalState, shouldKeepTransferDraftOnOpen, shouldResetTransferDialogOnOpen } from "../../apps/desktop/src/lib/transferProgressState.ts";
test("does not mark transfer as failed when only a table progress event reports error", () => {
test("marks transfer as failed when a table progress event reports error", () => {
const state = nextTransferTerminalState({ done: false, cancelled: false, error: false }, { status: "error" });
assert.deepEqual(state, { done: false, cancelled: false, error: false });
assert.deepEqual(state, { done: false, cancelled: false, error: true });
});
test("keeps terminal flags for done and cancelled progress events", () => {
@@ -13,12 +13,26 @@ test("keeps terminal flags for done and cancelled progress events", () => {
assert.deepEqual(nextTransferTerminalState({ done: false, cancelled: false, error: false }, { status: "cancelled" }), { done: false, cancelled: true, error: false });
});
test("still marks transfer as done after earlier table errors", () => {
test("keeps transfer failed after a later done event", () => {
const afterError = nextTransferTerminalState({ done: false, cancelled: false, error: false }, { status: "error" });
assert.deepEqual(nextTransferTerminalState(afterError, { status: "done" }), {
done: true,
cancelled: false,
error: false,
error: true,
});
});
test("keeps running transfer dialog state but resets terminal result on reopen", () => {
assert.equal(isTransferRunActive({ transferring: true, done: false, cancelled: false, error: false }), true);
assert.equal(shouldResetTransferDialogOnOpen({ transferring: true, done: false, cancelled: false, error: false }), false);
assert.equal(shouldResetTransferDialogOnOpen({ transferring: true, done: false, cancelled: false, error: true }), true);
assert.equal(shouldResetTransferDialogOnOpen({ transferring: true, done: true, cancelled: false, error: false }), true);
assert.equal(shouldResetTransferDialogOnOpen({ transferring: true, done: false, cancelled: true, error: false }), true);
});
test("keeps editable transfer draft across repeated dialog opens until user starts a new task", () => {
assert.equal(shouldKeepTransferDraftOnOpen(true, { transferring: false, done: false, cancelled: false, error: false }), true);
assert.equal(shouldKeepTransferDraftOnOpen(false, { transferring: true, done: false, cancelled: false, error: true }), true);
assert.equal(shouldKeepTransferDraftOnOpen(false, { transferring: false, done: false, cancelled: false, error: false }), false);
});
+323
View File
@@ -0,0 +1,323 @@
import { strict as assert } from "node:assert";
import { test, vi } from "vitest";
const apiMock = vi.hoisted(() => ({
cancelDatabaseExport: vi.fn(),
cancelSqlFileExecution: vi.fn(),
cancelTableExport: vi.fn(),
cancelTransfer: vi.fn(),
startTransfer: vi.fn(),
}));
vi.mock("@/lib/api", () => apiMock);
const { useExportTracker } = await import("../../apps/desktop/src/composables/useExportTracker.ts");
test("tracks database export progress and cancels through database export API", async () => {
const tracker = useExportTracker();
const task = tracker.addDatabaseExportTask("db-export-1", "app", "/tmp/app.sql");
tracker.updateDatabaseExportTask("db-export-1", {
exportId: "db-export-1",
currentObject: "users",
objectIndex: 2,
totalObjects: 5,
rowsExported: 120,
totalRows: null,
status: "Running",
error: null,
});
assert.equal(task.kind, "database-export");
assert.equal(task.tableName, "users");
assert.equal(task.objectIndex, 2);
assert.equal(task.totalObjects, 5);
assert.equal(tracker.activeCount.value, 1);
await tracker.cancelTask("db-export-1");
assert.equal(apiMock.cancelDatabaseExport.mock.calls[0][0], "db-export-1");
tracker.clearFinished();
assert.equal(
tracker.tasks.value.some((item) => item.exportId === "db-export-1"),
true,
);
});
test("tracks SQL file progress and clears terminal tasks", () => {
const tracker = useExportTracker();
const task = tracker.addSqlFileTask("sql-1", "init.sql", "/tmp/init.sql");
tracker.updateSqlFileTask("sql-1", {
executionId: "sql-1",
status: "statementDone",
statementIndex: 3,
successCount: 2,
failureCount: 1,
affectedRows: 12,
elapsedMs: 1500,
statementSummary: "insert into users...",
error: null,
});
assert.equal(task.kind, "sql-file");
assert.equal(task.status, "Running");
assert.equal(task.rowsExported, 3);
assert.equal(task.totalRows, 3);
assert.equal(task.affectedRows, 12);
tracker.updateSqlFileTask("sql-1", {
executionId: "sql-1",
status: "done",
statementIndex: 3,
successCount: 3,
failureCount: 0,
affectedRows: 16,
elapsedMs: 2000,
statementSummary: "",
error: null,
});
assert.equal(task.status, "Done");
tracker.clearFinished();
assert.equal(
tracker.tasks.value.some((item) => item.exportId === "sql-1"),
false,
);
});
test("keeps the legacy table export task API and routes cancel to table export API", async () => {
const tracker = useExportTracker();
const task = tracker.addTask("orders", "csv", "/tmp/orders.csv");
tracker.updateTableExportTask(task.exportId, {
exportId: task.exportId,
tableName: "orders",
rowsExported: 10,
totalRows: 20,
status: "Writing",
errorMessage: null,
});
assert.equal(task.kind, "table-export");
assert.equal(task.status, "Writing");
assert.equal(task.rowsExported, 10);
await tracker.cancelTask(task.exportId);
assert.equal(apiMock.cancelTableExport.mock.calls[0][0], task.exportId);
});
test("tracks data transfer progress and routes cancel to transfer API", async () => {
const tracker = useExportTracker();
const task = tracker.addDataTransferTask("transfer-1", "source → target", 4);
tracker.updateDataTransferTask("transfer-1", {
transferId: "transfer-1",
table: "users",
tableIndex: 2,
totalTables: 4,
rowsTransferred: 250,
totalRows: 500,
status: "running",
error: null,
});
assert.equal(task.kind, "data-transfer");
assert.equal(task.currentTable, "users");
assert.equal(task.tableIndex, 2);
assert.equal(task.totalTables, 4);
assert.equal(task.rowsExported, 250);
assert.equal(task.totalRows, 500);
assert.equal(task.status, "Running");
await tracker.cancelTask("transfer-1");
assert.equal(apiMock.cancelTransfer.mock.calls[0][0], "transfer-1");
});
test("keeps data transfer failed when final progress arrives after a table error", () => {
const tracker = useExportTracker();
const task = tracker.addDataTransferTask("transfer-error-1", "source → target", 2);
tracker.updateDataTransferTask("transfer-error-1", {
transferId: "transfer-error-1",
table: "users",
tableIndex: 0,
totalTables: 2,
rowsTransferred: 0,
totalRows: null,
status: "error",
error: "permission denied",
});
tracker.updateDataTransferTask("transfer-error-1", {
transferId: "transfer-error-1",
table: "",
tableIndex: 2,
totalTables: 2,
rowsTransferred: 0,
totalRows: null,
status: "done",
error: null,
});
assert.equal(task.status, "Error");
assert.equal(task.errorMessage, "permission denied");
assert.equal(task.tableIndex, 2);
assert.equal(task.rowsExported, 0);
});
test("keeps data transfer row counts when terminal summary does not include rows", () => {
const tracker = useExportTracker();
const task = tracker.addDataTransferTask("transfer-rows-1", "source → target", 1);
tracker.updateDataTransferTask("transfer-rows-1", {
transferId: "transfer-rows-1",
table: "users",
tableIndex: 0,
totalTables: 1,
rowsTransferred: 200,
totalRows: 200,
status: "tableDone",
error: null,
});
tracker.updateDataTransferTask("transfer-rows-1", {
transferId: "transfer-rows-1",
table: "",
tableIndex: 1,
totalTables: 1,
rowsTransferred: 0,
totalRows: null,
status: "done",
error: null,
});
assert.equal(task.status, "Done");
assert.equal(task.rowsExported, 200);
assert.equal(task.totalRows, 200);
assert.equal(task.tableIndex, 1);
});
test("starts independent data transfer background tasks and routes progress by transfer id", async () => {
const tracker = useExportTracker();
const callbacks = new Map<string, (progress: any) => void>();
const resolvers = new Map<string, () => void>();
const onDone = vi.fn();
apiMock.startTransfer.mockImplementation((request: any, onProgress: (progress: any) => void) => {
callbacks.set(request.transferId, onProgress);
return new Promise<void>((resolve) => resolvers.set(request.transferId, resolve));
});
const firstRequest = {
transferId: "parallel-transfer-1",
sourceConnectionId: "source-a",
sourceDatabase: "app_a",
sourceSchema: "public",
targetConnectionId: "target-a",
targetDatabase: "warehouse_a",
targetSchema: "public",
tables: ["users"],
createTable: true,
mode: "append",
targetTableNameCase: "preserve",
batchSize: 1000,
};
const secondRequest = {
...firstRequest,
transferId: "parallel-transfer-2",
sourceDatabase: "app_b",
targetDatabase: "warehouse_b",
tables: ["orders"],
};
const firstTask = tracker.startDataTransferTask(firstRequest, "app_a → warehouse_a", { onDone });
const secondTask = tracker.startDataTransferTask(secondRequest, "app_b → warehouse_b");
assert.equal(apiMock.startTransfer.mock.calls.at(-2)?.[0].transferId, "parallel-transfer-1");
assert.equal(apiMock.startTransfer.mock.calls.at(-1)?.[0].transferId, "parallel-transfer-2");
callbacks.get("parallel-transfer-2")?.({
transferId: "parallel-transfer-2",
table: "orders",
tableIndex: 0,
totalTables: 1,
rowsTransferred: 40,
totalRows: 100,
status: "running",
error: null,
});
callbacks.get("parallel-transfer-1")?.({
transferId: "parallel-transfer-1",
table: "users",
tableIndex: 0,
totalTables: 1,
rowsTransferred: 10,
totalRows: 50,
status: "running",
error: null,
});
assert.equal(firstTask.currentTable, "users");
assert.equal(firstTask.rowsExported, 10);
assert.equal(secondTask.currentTable, "orders");
assert.equal(secondTask.rowsExported, 40);
callbacks.get("parallel-transfer-1")?.({
transferId: "parallel-transfer-1",
table: "",
tableIndex: 1,
totalTables: 1,
rowsTransferred: 50,
totalRows: 50,
status: "done",
error: null,
});
resolvers.get("parallel-transfer-1")?.();
await new Promise((resolve) => setTimeout(resolve, 0));
assert.equal(firstTask.status, "Done");
assert.equal(secondTask.status, "Running");
assert.equal(onDone.mock.calls.length, 1);
});
test("blocks concurrent data transfers that write the same target table", () => {
const tracker = useExportTracker();
apiMock.startTransfer.mockImplementation(() => new Promise<void>(() => {}));
const firstRequest = {
transferId: "same-target-transfer-1",
sourceConnectionId: "source-a",
sourceDatabase: "app_a",
sourceSchema: "public",
targetConnectionId: "target-a",
targetDatabase: "warehouse",
targetSchema: "public",
tables: ["users"],
createTable: true,
mode: "append",
targetTableNameCase: "preserve",
batchSize: 1000,
};
const secondRequest = {
...firstRequest,
transferId: "same-target-transfer-2",
sourceDatabase: "app_b",
};
const firstTask = tracker.startDataTransferTask(firstRequest, "app_a → warehouse");
const secondTask = tracker.startDataTransferTask(secondRequest, "app_b → warehouse");
assert.equal(firstTask.status, "Running");
assert.equal(secondTask.status, "Error");
assert.match(secondTask.errorMessage ?? "", /already running/);
tracker.updateDataTransferTask("same-target-transfer-1", {
transferId: "same-target-transfer-1",
table: "",
tableIndex: 1,
totalTables: 1,
rowsTransferred: 0,
totalRows: null,
status: "done",
error: null,
});
});
+13 -2
View File
@@ -102,6 +102,7 @@ pub async fn start_transfer(
}
}
let mut failed_tables: Vec<String> = Vec::new();
for (i, table) in sorted_tables.iter().enumerate() {
if dbx_core::transfer::is_cancelled(&transfer_id).await {
emit_progress(
@@ -176,6 +177,7 @@ pub async fn start_transfer(
dbx_core::transfer::clear_cancelled(&transfer_id).await;
return;
}
failed_tables.push(table.clone());
emit_progress(
&app,
TransferProgress {
@@ -224,6 +226,7 @@ pub async fn start_transfer(
return;
}
Err(e) => {
failed_tables.push("schema objects".to_string());
emit_progress(
&app,
TransferProgress {
@@ -250,8 +253,16 @@ pub async fn start_transfer(
total_tables,
rows_transferred: 0,
total_rows: None,
status: TransferStatus::Done,
error: None,
status: if failed_tables.is_empty() { TransferStatus::Done } else { TransferStatus::Error },
error: if failed_tables.is_empty() {
None
} else {
Some(format!(
"{} table(s) failed: {}",
failed_tables.len(),
failed_tables.iter().take(5).cloned().collect::<Vec<_>>().join(", ")
))
},
},
);
dbx_core::transfer::clear_cancelled(&transfer_id).await;