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

@@ -3,7 +3,7 @@ import { computed, onBeforeUnmount, onMounted, ref } from "vue";
import { useI18n } from "vue-i18n";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { Button } from "@/components/ui/button";
import { Loader2, Check, CheckCircle2, XCircle, AlertCircle, X, FileDown, DatabaseBackup, FileCode2, ArrowRightLeft, Layers3, GitCompareArrows, ChevronRight, FolderOpen, Copy } from "@lucide/vue";
import { Loader2, Check, CheckCircle2, XCircle, AlertCircle, X, FileDown, Database, DatabaseBackup, FileCode2, ArrowRightLeft, Layers3, GitCompareArrows, ChevronRight, FolderOpen, Copy } from "@lucide/vue";
import { formatDataTransferDuration, useExportTracker, type ExportTask } from "@/composables/useExportTracker";
import { dataTransferFailureCopyText, sqlFileFailureCopyText } from "@/components/export/failureDetailCopyText";
import SqlFileProgressIndicator from "@/components/sql-file/SqlFileProgressIndicator.vue";
@@ -108,6 +108,7 @@ const taskTitle = (task: ExportTask) => {
}
if (task.kind === "sql-file") return t("exportProgress.sqlFileTitle", { name: task.tableName });
if (task.kind === "data-transfer") return t("exportProgress.dataTransferTitle", { name: task.tableName });
if (task.kind === "data-generation") return t("exportProgress.dataGenerationTitle", { name: task.tableName });
if (task.kind === "multi-db-execution") return t("exportProgress.multiDbExecutionTitle", { name: task.tableName });
if (task.kind === "schema-diff") return t("exportProgress.schemaDiffTitle", { name: task.tableName });
if (task.kind === "data-compare") return t("exportProgress.dataCompareTitle", { name: task.tableName });
@@ -187,6 +188,16 @@ const rowsText = (task: ExportTask) => {
const durationText = t("exportProgress.elapsed", { duration: formatDataTransferDuration(finishedAt - (task.startedAt ?? finishedAt)) });
return task.currentTable ? `${tableText} · ${task.currentTable} · ${rowText} · ${durationText}` : `${tableText} · ${durationText}`;
}
if (task.kind === "data-generation") {
const tableText = task.totalTables
? t("exportProgress.tablesCount", {
current: (task.tableIndex ?? 0).toLocaleString(),
total: task.totalTables.toLocaleString(),
})
: "";
const rowText = task.totalRows ? `${task.rowsExported.toLocaleString()} / ${task.totalRows.toLocaleString()} ${t("exportProgress.rowsShort")}` : `${task.rowsExported.toLocaleString()} ${t("exportProgress.rowsShort")}`;
return [tableText, task.currentTable, rowText].filter(Boolean).join(" · ");
}
if (task.kind === "multi-db-execution") {
return t("exportProgress.multiDbTargets", {
completed: (task.multiDbCompleted ?? 0).toLocaleString(),
@@ -232,6 +243,7 @@ const statusIcon = (task: ExportTask) => {
if (task.kind === "data-dictionary") return FileDown;
if (task.kind === "sql-file") return FileCode2;
if (task.kind === "data-transfer") return ArrowRightLeft;
if (task.kind === "data-generation") return Database;
if (task.kind === "multi-db-execution") return Layers3;
if (task.kind === "schema-diff" || task.kind === "data-compare") return GitCompareArrows;
}
@@ -112,6 +112,31 @@ describe("data dictionary background task", () => {
});
});
describe("data generation background task", () => {
it("shows global progress and opens the resumable task", async () => {
const tracker = useExportTracker();
const onOpen = vi.fn();
const task = tracker.addDataGenerationTask("generate-task", "app.users", 1_000, 2, onOpen);
tracker.updateDataGenerationTask(task.exportId, {
status: "Running",
tableIndex: 1,
totalTables: 2,
currentTable: "users",
rowsGenerated: 250,
totalRows: 1_000,
});
await mountPopover();
expect(document.body.textContent).toContain("Data generation: app.users");
expect(document.body.textContent).toContain("1 / 2 tables · users · 250 / 1,000 rows");
const openButton = document.body.querySelector<HTMLButtonElement>('button[title="Open task"]');
expect(openButton).not.toBeNull();
openButton?.click();
expect(onOpen).toHaveBeenCalledOnce();
});
});
describe("SQL file byte progress", () => {
it("keeps legacy progress indeterminate instead of using successful statements as the total", async () => {
const tracker = useExportTracker();
@@ -1,39 +1,25 @@
<script setup lang="ts">
import { reactive, ref, computed, onMounted, watch, type ComponentPublicInstance } from "vue";
import { reactive, ref, computed, watch, type ComponentPublicInstance } from "vue";
import { useI18n } from "vue-i18n";
import { useConnectionStore } from "@/stores/connectionStore";
import * as api from "@/lib/backend/api";
import type { ColumnGenerateConfig, GenerateResult, TableGenerateConfig } from "@/lib/dataGrid/dataGenerate";
import {
createTableGenerateState,
defaultGeneratorParams,
displayGeneratedValue,
findGeneratorKey,
formatGeneratedRowValues,
formatGeneratedValue,
generateInsertBatches,
generateTableData,
generateTableRowsChunk,
splitValueRowsByByteBudget,
supportsGeneratedMultiRowValues,
UniqueValueGenerationError,
} from "@/lib/dataGrid/dataGenerate";
import { errorMessage, isQueryCanceledError, summarizeBatchResults } from "@/lib/dataGrid/generateInsertAccounting";
import type { ColumnGenerateConfig, TableGenerateConfig } from "@/lib/dataGrid/dataGenerate";
import { defaultGeneratorParams, displayGeneratedValue, findGeneratorKey, formatGeneratedValue, generateTableData, supportsGeneratedMultiRowValues, UniqueValueGenerationError } from "@/lib/dataGrid/dataGenerate";
import { qualifiedTableName, quoteTableIdentifier } from "@/lib/table/tableSelectSql";
import { uniqueConstraintColumns } from "@/lib/table/uniqueConstraintColumns";
import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
import { effectiveDatabaseTypeForConnection } from "@/lib/database/jdbcDialect";
import { executeWithProductionSqlGuard } from "@/lib/database/productionExecutionGuard";
import GeneratorParamsPanel from "./params/GeneratorParamsPanel.vue";
import type { ColumnInfo, QueryResult, TableInfo } from "@/types/database";
import type { ColumnInfo, TableInfo } from "@/types/database";
import { cancelDataGenerateSession, getDataGenerateSession, startDataGenerateSession, type DataGenerateTarget } from "@/composables/useDataGenerateSession";
import { Dialog, DialogHeader, DialogTitle, DialogScrollContent, DialogContent, DialogFooter } from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Database, Table, Columns, Loader2, Save, Upload, Settings, ChevronRight, X, AlertCircle, ArrowUp, ArrowDown } from "@lucide/vue";
import { Database, Table, Columns, Loader2, Save, Upload, Settings, ChevronRight, X, AlertCircle, ArrowUp, ArrowDown, Minimize2 } from "@lucide/vue";
import { ScrollArea } from "@/components/ui/scroll-area";
import { uuid } from "@/lib/common/utils";
const { t } = useI18n();
const store = useConnectionStore();
@@ -45,6 +31,7 @@ const props = defineProps<{
prefillDatabase?: string;
prefillSchema?: string;
prefillTable?: string;
sessionId?: string | null;
}>();
// Left tree state
@@ -72,15 +59,7 @@ const panelColumnName = ref<string | null>(null);
// Step state: config -> preview
const currentStep = ref<"config" | "preview" | "result">("config");
interface GeneratedTableResult extends GenerateResult {
tableName: string;
schema: string;
/** Rows that will actually be inserted. `rows` only holds a preview sample. */
targetRowCount: number;
isSample: boolean;
/** Column config after auto-increment start values have been resolved. */
resolvedColumns: ColumnGenerateConfig[];
}
type GeneratedTableResult = DataGenerateTarget;
const generatedResults = ref<GeneratedTableResult[]>([]);
const generationError = ref("");
@@ -92,12 +71,6 @@ const generationError = ref("");
const PREVIEW_SAMPLE_ROWS = 50;
const MAX_ROW_COUNT = 100_000_000;
const DEFAULT_BATCH_ROWS = 1000;
/**
* Conservative per-statement budget. The backend clamps MySQL batches at 4MB
* and derives a `max_allowed_packet` margin, so staying near 1MB keeps the
* common 4MB / 16MB / 64MB server settings safe without extra round trips.
*/
const MAX_BATCH_BYTES = 1024 * 1024;
const LARGE_ROW_COUNT_HINT = 100_000;
function normalizeRowCount(value: number): number {
@@ -432,6 +405,7 @@ const currentPreview = computed<GeneratedTableResult>(
generatedResults.value[previewTableIndex.value] ?? {
tableName: "",
schema: "",
database: "",
columns: [],
rows: [],
sql: "",
@@ -475,6 +449,8 @@ function buildSampleResult(cfg: TableGenerateConfig, columns: ColumnGenerateConf
return {
tableName: cfg.tableName,
schema: cfg.schema,
database: cfg.database,
tableType: cfg.tableType,
targetRowCount,
isSample: targetRowCount > sampleCount,
resolvedColumns: columns,
@@ -557,17 +533,10 @@ function copySampleSql() {
void navigator.clipboard.writeText(allSql);
}
const executing = ref(false);
interface TableResult {
table: string;
total: number;
ok: number;
err: number;
error?: string;
cancelled?: boolean;
}
const executeResults = ref<TableResult[]>([]);
const activeSessionId = ref<string | null>(props.sessionId ?? null);
const activeSession = computed(() => getDataGenerateSession(activeSessionId.value));
const executing = computed(() => activeSession.value?.status === "running" || activeSession.value?.status === "cancelling");
const executeResults = computed(() => activeSession.value?.results ?? []);
const generateOptions = reactive({
continueOnError: false,
@@ -589,17 +558,7 @@ watch(
const optionsDialogOpen = ref(false);
interface InsertProgress {
tableName: string;
tableIndex: number;
tableCount: number;
insertedRows: number;
totalRows: number;
elapsedMs: number;
}
const insertProgress = ref<InsertProgress | null>(null);
const insertCancelled = ref(false);
let activeExecutionId: string | null = null;
const insertProgress = computed(() => activeSession.value?.progress ?? null);
const insertPercent = computed(() => {
const p = insertProgress.value;
@@ -608,9 +567,8 @@ const insertPercent = computed(() => {
});
function cancelInsert() {
if (!executing.value) return;
insertCancelled.value = true;
if (activeExecutionId) void api.cancelQuery(activeExecutionId);
if (!executing.value || !activeSessionId.value) return;
void cancelDataGenerateSession(activeSessionId.value);
}
function normalizeTimeoutSecs(value: number): number {
@@ -643,119 +601,6 @@ function allSqlStatements(): string[] {
return generatedResults.value.flatMap((r) => sqlStatementsForTable(r));
}
/**
* Streams one table: generate a chunk, insert it, drop the references, repeat.
*
* Peak memory stays proportional to `batchRows` instead of the requested row
* count, and each awaited round trip gives the main thread a chance to paint
* the progress bar.
*/
async function streamInsertTable(cid: string, db: string, r: GeneratedTableResult, executionId: string, onRows: (insertedRows: number) => void): Promise<{ ok: number; attempted: number; error: string; cancelled: boolean }> {
const cfg = configs[tableKey(r.schema, r.tableName)];
const genCfg: TableGenerateConfig = {
tableName: r.tableName,
schema: r.schema,
database: props.prefillDatabase ?? cfg?.database ?? "",
tableType: cfg?.tableType,
rowCount: r.targetRowCount,
columns: r.resolvedColumns,
};
const state = createTableGenerateState(genCfg, dbType.value);
const schema = r.schema || props.prefillSchema;
const forceSingleRow = !generateOptions.extendedInsert;
const timeoutSecs = normalizeTimeoutSecs(generateOptions.timeoutSecs);
const batchRows = Math.max(1, Math.floor(generateOptions.batchRows) || DEFAULT_BATCH_ROWS);
let ok = 0;
let attempted = 0;
let lastError = "";
let cancelled = false;
const run = (sql: string): Promise<QueryResult[]> =>
api.executeMultiWithProgress(cid, db, sql, () => {}, schema, {
timeoutSecs,
useTransaction: generateOptions.useTransaction,
continueOnError: generateOptions.continueOnError,
executionId,
});
/**
* Executes one statement batch and folds the outcome into the running totals.
* Returns false when the caller has to stop the table.
*
* The returned per-statement results are inspected instead of trusting the
* `await`: several backend paths report a failed statement inside the result
* array while still resolving, so counting rows on the await alone would
* report failed batches as inserted.
*/
const executeBatch = async (statements: string[], rowsPerStatement: number[]): Promise<boolean> => {
const expectedRows = rowsPerStatement.reduce((sum, rows) => sum + rows, 0);
attempted += expectedRows;
try {
const results = await run(statements.join("\n"));
// A row-carrying batch that comes back without per-statement results is
// unconfirmed; counting it would over-report inserts.
if (expectedRows > 0 && (!results || results.length === 0)) {
if (!lastError) lastError = "Backend returned no result for the batch";
return generateOptions.continueOnError;
}
const outcome = summarizeBatchResults(results, rowsPerStatement);
ok += outcome.insertedRows;
if (!outcome.failed) return true;
if (!lastError) lastError = outcome.error ?? "Statement failed";
console.error("[startInsert] SQL error:", lastError);
return generateOptions.continueOnError;
} catch (e: unknown) {
// Cancelling a transaction or a single-statement round trip surfaces as
// an error, so it has to be told apart from a real failure.
if (insertCancelled.value || isQueryCanceledError(e)) {
cancelled = true;
return false;
}
const msg = errorMessage(e);
console.error("[startInsert] SQL error:", msg);
if (!lastError) lastError = msg;
return generateOptions.continueOnError;
}
};
const finish = () => ({ ok, attempted, error: cancelled ? "" : lastError, cancelled });
if (generateOptions.truncate) {
const targetTable = qualifiedTableName({ databaseType: dbType.value, schema: r.schema, tableName: r.tableName, database: props.prefillDatabase });
// TRUNCATE carries no generated rows, so it only gates the rest of the run.
const truncateOk = await executeBatch([`TRUNCATE TABLE ${targetTable};`], [0]);
if (!truncateOk) return finish();
}
while (state.nextIndex < r.targetRowCount && !insertCancelled.value) {
let valueRows: string[];
try {
const chunkRows = generateTableRowsChunk(genCfg, state, batchRows);
if (chunkRows.length === 0) break;
valueRows = chunkRows.map((row) => formatGeneratedRowValues(genCfg, dbType.value, state, row));
} catch (e: unknown) {
// Generation failures (unique space exhausted at row 500k, say) must not
// discard the rows that earlier chunks already inserted.
if (!lastError) lastError = generationErrorMessage(e);
return finish();
}
for (const group of splitValueRowsByByteBudget(state, valueRows, MAX_BATCH_BYTES)) {
const { statements, rowsPerStatement } = generateInsertBatches(dbType.value, state, group, forceSingleRow);
if (statements.length === 0) continue;
const keepGoing = await executeBatch(statements, rowsPerStatement);
onRows(ok);
if (!keepGoing) return finish();
}
onRows(ok);
// Yield so the progress bar repaints between chunks.
await new Promise((resolve) => setTimeout(resolve, 0));
}
if (insertCancelled.value) cancelled = true;
return finish();
}
async function startInsert() {
if (executing.value || generationError.value) return;
const cid = props.prefillConnectionId;
@@ -771,67 +616,46 @@ async function startInsert() {
sql: guardSql,
source: t("production.sourceDataGenerate"),
execute: async () => {
executing.value = true;
insertCancelled.value = false;
const executionId = uuid();
activeExecutionId = executionId;
const startedAt = performance.now();
const perTable: TableResult[] = [];
let stopAll = false;
try {
for (let ti = 0; ti < targets.length && !stopAll; ti++) {
const r = targets[ti];
insertProgress.value = {
tableName: r.tableName,
tableIndex: ti + 1,
tableCount: targets.length,
insertedRows: 0,
totalRows: r.targetRowCount,
elapsedMs: 0,
};
let outcome: { ok: number; attempted: number; error: string; cancelled: boolean };
let insertedSoFar = 0;
try {
outcome = await streamInsertTable(cid, db, r, executionId, (insertedRows) => {
insertedSoFar = insertedRows;
if (insertProgress.value) {
insertProgress.value = { ...insertProgress.value, insertedRows, elapsedMs: Math.round(performance.now() - startedAt) };
}
});
} catch (e: unknown) {
// Safety net: even an unexpected throw must not hide the rows that
// earlier batches already inserted.
const wasCancelled = insertCancelled.value || isQueryCanceledError(e);
outcome = { ok: insertedSoFar, attempted: insertedSoFar, error: wasCancelled ? "" : errorMessage(e), cancelled: wasCancelled };
}
const failedRows = Math.max(0, Math.max(outcome.attempted, outcome.ok) - outcome.ok);
perTable.push({
table: r.tableName,
total: r.targetRowCount,
ok: outcome.ok,
err: failedRows,
error: outcome.error || undefined,
cancelled: outcome.cancelled || undefined,
});
if (outcome.ok > 0) {
store.invalidateMetadataCache(cid, db, r.schema || props.prefillSchema || undefined, r.tableName);
}
if ((outcome.error && !generateOptions.continueOnError) || outcome.cancelled) stopAll = true;
}
} finally {
activeExecutionId = null;
}
executeResults.value = perTable;
currentStep.value = "result";
const session = startDataGenerateSession(
{
connectionId: cid,
database: db,
prefillSchema: props.prefillSchema,
databaseType: dbType.value,
label: targets.length === 1 ? `${db}.${targets[0].tableName}` : `${db} (${targets.length})`,
targets,
options: {
continueOnError: generateOptions.continueOnError,
truncate: generateOptions.truncate,
useTransaction: generateOptions.useTransaction,
extendedInsert: generateOptions.extendedInsert,
timeoutSecs: normalizeTimeoutSecs(generateOptions.timeoutSecs),
batchRows: Math.max(1, Math.floor(generateOptions.batchRows) || DEFAULT_BATCH_ROWS),
},
},
{
invalidateMetadataCache: (connectionId, database, schema, table) => store.invalidateMetadataCache(connectionId, database, schema, table),
formatGenerationError: generationErrorMessage,
},
);
activeSessionId.value = session.id;
applyDataGenerateSession(session);
return true;
},
});
} finally {
executing.value = false;
insertProgress.value = null;
} catch (error) {
generationError.value = generationErrorMessage(error);
}
}
function applyDataGenerateSession(session = activeSession.value) {
if (!session) return;
generatedResults.value = session.config.targets;
generationError.value = session.error ?? "";
previewTableIndex.value = Math.min(previewTableIndex.value, Math.max(0, generatedResults.value.length - 1));
currentStep.value = session.status === "running" || session.status === "cancelling" ? "preview" : "result";
}
const orderDialogOpen = ref(false);
const tableOrder = ref<string[]>([]);
@@ -892,15 +716,29 @@ watch(
{ deep: true, flush: "post" },
);
onMounted(() => {
void loadSchemas();
});
watch(open, (val) => {
if (val) {
watch(
[() => open.value, () => props.sessionId],
([isOpen, sessionId]) => {
if (!isOpen) return;
const session = getDataGenerateSession(sessionId);
if (session) {
activeSessionId.value = session.id;
applyDataGenerateSession(session);
return;
}
activeSessionId.value = null;
void loadSchemas();
}
});
},
{ immediate: true },
);
watch(
() => {
const session = getDataGenerateSession(activeSessionId.value);
return session ? `${session.id}:${session.version}` : "";
},
() => applyDataGenerateSession(),
);
interface GenerateProfileJson {
version: 1;
@@ -1296,8 +1134,9 @@ async function onFileSelected(event: Event) {
</div>
<div class="flex items-center gap-2">
<Button variant="outline" size="sm" class="h-7 text-xs" @click="open = false">
<X class="mr-1 h-3 w-3" />
{{ t("dangerDialog.cancel") }}
<Minimize2 v-if="executing" class="mr-1 h-3 w-3" />
<X v-else class="mr-1 h-3 w-3" />
{{ executing ? t("exportProgress.minimize") : t("dangerDialog.cancel") }}
</Button>
<template v-if="currentStep === 'config'">
<Button variant="default" size="sm" class="h-7 text-xs" :disabled="!hasSelectedTables" @click="doGenerate">
@@ -235,6 +235,7 @@ watch(
/>
<DataTransferDialog
v-model:open="dialogs.showTransferDialog.value"
:task-id="dialogs.transferTaskId.value"
:prefill-connection-id="dialogs.transferPrefillConnectionId.value"
:prefill-database="dialogs.transferPrefillDatabase.value"
:prefill-catalog="dialogs.transferPrefillCatalog.value"
@@ -308,6 +309,7 @@ watch(
<DataGenerateDialog
v-if="dialogs.showTableDataGenerateDialog.value"
v-model:open="dialogs.showTableDataGenerateDialog.value"
:session-id="dialogs.tableDataGenerateSessionId.value"
:prefill-connection-id="dialogs.tableDataGeneratePrefillConnectionId.value"
:prefill-database="dialogs.tableDataGeneratePrefillDatabase.value"
:prefill-schema="dialogs.tableDataGeneratePrefillSchema.value"
@@ -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));
});
});