Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

feat(sql-file): export and copy failed statement details

Abeautifulsnowac0cecd1d1c7
diff --git a/apps/desktop/src/components/export/ExportProgressPopover.vue b/apps/desktop/src/components/export/ExportProgressPopover.vue
index 7d8035e70e..fc7a03f88c 100644
--- a/apps/desktop/src/components/export/ExportProgressPopover.vue
+++ b/apps/desktop/src/components/export/ExportProgressPopover.vue
@@ -5,8 +5,9 @@ import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover
import { Button } from "@/components/ui/button";
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 { dataTransferFailureCopyText } from "@/components/export/failureDetailCopyText";
import SqlFileProgressIndicator from "@/components/sql-file/SqlFileProgressIndicator.vue";
+import FailureDetailsList from "@/components/sql-file/FailureDetailsList.vue";
import { translateBackendError } from "@/i18n/backend-errors";
import { useToast } from "@/composables/useToast";
import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
@@ -325,10 +326,6 @@ function dataTransferFailureDetailKey(taskId: string, table: string): string {
return `data-transfer:${taskId}:${table}`;
}
-function sqlFileFailureDetailKey(taskId: string, fileIndex: number | undefined, statementIndex: number): string {
- return `sql-file:${taskId}:${fileIndex ?? -1}:${statementIndex}`;
-}
-
async function copyFailureDetail(text: string, key: string): Promise<void> {
try {
await copyToClipboard(text);
@@ -413,15 +410,21 @@ function openTask(task: ExportTask): void {
{{ translateBackendError(t, task.errorMessage!) }}
</span>
<template v-if="(task.kind === 'data-transfer' || task.kind === 'sql-file') && failureDetailCount(task) > 0">
- <button class="mt-1.5 flex items-center gap-1 text-xs font-medium text-foreground hover:text-primary" :aria-expanded="failureDetailsExpanded(task.exportId)" @click="toggleFailureDetails(task.exportId)">
- <ChevronRight class="h-3.5 w-3.5 shrink-0 transition-transform" :class="{ 'rotate-90': failureDetailsExpanded(task.exportId) }" />
- {{ failureDetailsExpanded(task.exportId) ? t("exportProgress.hideFailureDetails") : t("exportProgress.showFailureDetails", { count: failureDetailCount(task) }) }}
- </button>
- <div v-if="failureDetailsExpanded(task.exportId)" class="mt-1.5 max-h-44 overflow-y-auto rounded border border-destructive/20 bg-destructive/5">
- <template v-if="task.kind === 'data-transfer'">
+ <!-- SQL file failures share one implementation with the execution
+ dialog (selection, copy and export of the complete
+ statements); transfer failures keep their own list. -->
+ <FailureDetailsList v-if="task.kind === 'sql-file'" class="mt-1.5" :execution-id="task.exportId" :failures="task.sqlFileFailures ?? []" :omitted="task.sqlFileFailuresOmitted ?? 0" :file-name-hint="task.tableName" variant="popover" />
+ <template v-else>
+ <button class="mt-1.5 flex items-center gap-1 text-xs font-medium text-foreground hover:text-primary" :aria-expanded="failureDetailsExpanded(task.exportId)" @click="toggleFailureDetails(task.exportId)">
+ <ChevronRight class="h-3.5 w-3.5 shrink-0 transition-transform" :class="{ 'rotate-90': failureDetailsExpanded(task.exportId) }" />
+ {{ failureDetailsExpanded(task.exportId) ? t("exportProgress.hideFailureDetails") : t("exportProgress.showFailureDetails", { count: failureDetailCount(task) }) }}
+ </button>
+ <div v-if="failureDetailsExpanded(task.exportId)" class="mt-1.5 max-h-44 overflow-y-auto rounded border border-destructive/20 bg-destructive/5">
<div v-for="failure in task.transferFailures" :key="failure.table" class="border-b border-destructive/15 px-2.5 py-2 last:border-b-0">
<div class="flex min-w-0 items-start justify-between gap-2">
- <div class="min-w-0 break-all font-mono font-medium text-foreground">{{ failure.table }}</div>
+ <div class="flex min-w-0 items-center gap-1.5 font-medium text-foreground">
+ <span class="truncate" :title="failure.table">{{ failure.table }}</span>
+ </div>
<button
type="button"
class="flex h-5 w-5 shrink-0 items-center justify-center rounded hover:bg-muted"
@@ -433,36 +436,12 @@ function openTask(task: ExportTask): void {
<Copy v-else class="h-3.5 w-3.5 text-muted-foreground hover:text-foreground" aria-hidden="true" />
</button>
</div>
- <div class="mt-0.5 select-text whitespace-pre-wrap break-words text-destructive">{{ failure.error }}</div>
- <div v-if="failure.truncated" class="mt-0.5 text-muted-foreground">{{ t("exportProgress.failureDetailTruncated") }}</div>
- </div>
- <div v-if="task.transferFailuresOmitted" class="px-2.5 py-2 text-muted-foreground">{{ t("exportProgress.failureDetailsOmitted", { count: task.transferFailuresOmitted }) }}</div>
- </template>
- <template v-else>
- <div v-for="failure in task.sqlFileFailures" :key="`${failure.fileIndex ?? -1}:${failure.statementIndex}`" class="border-b border-destructive/15 px-2.5 py-2 last:border-b-0">
- <div class="flex min-w-0 items-start justify-between gap-2">
- <div class="flex min-w-0 items-center gap-1.5 font-medium text-foreground">
- <span class="shrink-0">#{{ failure.statementIndex }}</span>
- <span v-if="failure.fileName" class="truncate text-muted-foreground" :title="failure.fileName">{{ failure.fileName }}</span>
- </div>
- <button
- type="button"
- class="flex h-5 w-5 shrink-0 items-center justify-center rounded hover:bg-muted"
- :title="copiedFailureDetailKey === sqlFileFailureDetailKey(task.exportId, failure.fileIndex, failure.statementIndex) ? t('exportProgress.failureDetailCopied') : t('exportProgress.copyFailureDetail')"
- :aria-label="copiedFailureDetailKey === sqlFileFailureDetailKey(task.exportId, failure.fileIndex, failure.statementIndex) ? t('exportProgress.failureDetailCopied') : t('exportProgress.copyFailureDetail')"
- @click.stop="copyFailureDetail(sqlFileFailureCopyText(failure, translateBackendError(t, failure.error)), sqlFileFailureDetailKey(task.exportId, failure.fileIndex, failure.statementIndex))"
- >
- <Check v-if="copiedFailureDetailKey === sqlFileFailureDetailKey(task.exportId, failure.fileIndex, failure.statementIndex)" class="h-3.5 w-3.5 text-green-500" aria-hidden="true" />
- <Copy v-else class="h-3.5 w-3.5 text-muted-foreground hover:text-foreground" aria-hidden="true" />
- </button>
- </div>
- <div v-if="failure.statementSummary" class="mt-0.5 select-text whitespace-pre-wrap break-words font-mono text-foreground">{{ failure.statementSummary }}</div>
<div class="mt-0.5 select-text whitespace-pre-wrap break-words text-destructive">{{ translateBackendError(t, failure.error) }}</div>
<div v-if="failure.truncated" class="mt-0.5 text-muted-foreground">{{ t("exportProgress.failureDetailTruncated") }}</div>
</div>
- <div v-if="task.sqlFileFailuresOmitted" class="px-2.5 py-2 text-muted-foreground">{{ t("exportProgress.failureDetailsOmitted", { count: task.sqlFileFailuresOmitted }) }}</div>
- </template>
- </div>
+ <div v-if="task.transferFailuresOmitted" class="px-2.5 py-2 text-muted-foreground">{{ t("exportProgress.failureDetailsOmitted", { count: task.transferFailuresOmitted }) }}</div>
+ </div>
+ </template>
</template>
</div>
</div>
diff --git a/apps/desktop/src/components/sql-file/FailureDetailsList.spec.ts b/apps/desktop/src/components/sql-file/FailureDetailsList.spec.ts
new file mode 100644
index 0000000000..5e25cdbb23
--- /dev/null
+++ b/apps/desktop/src/components/sql-file/FailureDetailsList.spec.ts
@@ -0,0 +1,280 @@
+// @vitest-environment happy-dom
+
+import { createApp, h, nextTick, reactive, type App } from "vue";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ logInfo: vi.fn(),
+ readText: vi.fn(),
+ exportLog: vi.fn(),
+ reveal: vi.fn(),
+ savePath: vi.fn(),
+ copy: vi.fn(),
+ toast: vi.fn(),
+ desktop: true,
+}));
+
+vi.mock("vue-i18n", () => ({
+ useI18n: () => ({
+ t: (key: string, params?: Record<string, unknown>) => (params ? `${key}|${JSON.stringify(params)}` : key),
+ }),
+}));
+vi.mock("@/lib/backend/api", () => ({
+ sqlFileFailureLogInfo: mocks.logInfo,
+ readSqlFileFailureLogText: mocks.readText,
+ exportSqlFileFailureLog: mocks.exportLog,
+ revealPathInFileManager: mocks.reveal,
+}));
+vi.mock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => mocks.desktop }));
+vi.mock("@/lib/export/exportPath", () => ({ promptExportSavePath: mocks.savePath }));
+vi.mock("@/lib/common/clipboard", () => ({ copyToClipboard: mocks.copy }));
+vi.mock("@/composables/useToast", () => ({ useToast: () => ({ toast: mocks.toast }) }));
+
+import FailureDetailsList from "@/components/sql-file/FailureDetailsList.vue";
+import { failureLogSensitiveNotice } from "@/lib/export/failureLogExport";
+import type { SqlFileFailure } from "@/composables/useExportTracker";
+
+function failure(statementIndex: number, fileName = "a.sql"): SqlFileFailure {
+ return {
+ statementIndex,
+ statementSummary: `INSERT #${statementIndex}`,
+ error: "ORA-12899",
+ fileIndex: 0,
+ fileName,
+ };
+}
+
+let app: App | null = null;
+let root: HTMLElement;
+
+async function mountList(options: { failures: SqlFileFailure[]; omitted?: number; expanded?: boolean } = { failures: [] }) {
+ root = document.createElement("div");
+ document.body.appendChild(root);
+ // Reactive so a test can let a running execution report more failures.
+ const state = reactive({ failures: [...options.failures], expanded: options.expanded ?? true });
+ app = createApp({
+ render: () =>
+ h(FailureDetailsList, {
+ executionId: "exec-1",
+ failures: state.failures,
+ omitted: options.omitted ?? 0,
+ fileNameHint: "dump.sql",
+ expanded: state.expanded,
+ "onUpdate:expanded": (value: boolean) => {
+ state.expanded = value;
+ },
+ }),
+ });
+ app.mount(root);
+ await settle();
+ return { root, app, state };
+}
+
+async function settle() {
+ await nextTick();
+ await Promise.resolve();
+ await nextTick();
+}
+
+function buttons(): HTMLButtonElement[] {
+ return Array.from(root.querySelectorAll("button"));
+}
+
+function buttonWithText(fragment: string): HTMLButtonElement | undefined {
+ return buttons().find((button) => (button.textContent ?? "").includes(fragment));
+}
+
+function checkboxes(): HTMLInputElement[] {
+ return Array.from(root.querySelectorAll('input[type="checkbox"]'));
+}
+
+/** The first checkbox is "select all"; the rest belong to the failure rows. */
+function selectAllBox(): HTMLInputElement {
+ return checkboxes()[0]!;
+}
+
+function rowBox(index: number): HTMLInputElement {
+ return checkboxes()[index + 1]!;
+}
+
+function toggle(box: HTMLInputElement) {
+ box.checked = !box.checked;
+ box.dispatchEvent(new Event("change"));
+}
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ mocks.desktop = true;
+ mocks.logInfo.mockResolvedValue({ status: "available", path: "/home/user/.dbx/sql-file-logs/exec-1.jsonl" });
+ mocks.readText.mockResolvedValue({ text: "FULL LOG TEXT", tooLarge: false, limitBytes: 5 * 1024 * 1024 });
+ mocks.exportLog.mockResolvedValue({ records: 2, bytes: 120, format: "txt" });
+ mocks.savePath.mockResolvedValue("/tmp/out.txt");
+ mocks.copy.mockResolvedValue(undefined);
+ // The sensitive-content notice is once per app run; each test starts a fresh run.
+ failureLogSensitiveNotice.shown = false;
+});
+
+afterEach(() => {
+ app?.unmount();
+ app = null;
+ root?.remove();
+});
+
+describe("FailureDetailsList", () => {
+ it("counts hidden failures and says the export still has them all", async () => {
+ // Collapsed view is what the user sees first, so the counts must be right there.
+ await mountList({ failures: [failure(1), failure(2)], omitted: 98, expanded: false });
+
+ expect(root.textContent).toContain("exportProgress.showFailureDetails|");
+ expect(root.textContent).toContain('"count":100');
+ expect(root.textContent).toContain("exportProgress.failureLog.listTruncated");
+ expect(root.textContent).toContain('"visible":2,"total":100');
+ });
+
+ it("copies the selected rows from the backend log, not the truncated list", async () => {
+ await mountList({ failures: [failure(644), failure(645), failure(646)] });
+
+ toggle(rowBox(0));
+ toggle(rowBox(2));
+ await settle();
+ expect(root.textContent).toContain('"count":2');
+
+ buttonWithText("exportProgress.failureLog.copySelected")!.click();
+ await settle();
+
+ // Selected keys come from the visible rows, and the text comes from the log
+ // so it carries the complete statements.
+ expect(mocks.readText).toHaveBeenCalledWith("exec-1", "txt", [
+ { fileIndex: 0, statementIndex: 644 },
+ { fileIndex: 0, statementIndex: 646 },
+ ]);
+ expect(mocks.copy).toHaveBeenCalledWith("FULL LOG TEXT");
+ expect(mocks.toast).toHaveBeenCalledWith(expect.stringContaining("copySelectedDone"), 2500);
+ });
+
+ it("selects every visible row and exports the full log", async () => {
+ await mountList({ failures: [failure(1), failure(2)] });
+
+ toggle(selectAllBox());
+ await settle();
+
+ buttonWithText("exportProgress.failureLog.exportTxt")!.click();
+ await settle();
+
+ expect(mocks.savePath).toHaveBeenCalledWith(expect.objectContaining({ defaultFileName: "dump-failures.txt" }));
+ // Exporting everything passes no keys, so the backend writes the whole log.
+ expect(mocks.exportLog).toHaveBeenCalledWith("exec-1", "txt", "/tmp/out.txt", undefined);
+ expect(mocks.toast).toHaveBeenCalledWith(expect.stringContaining("exportedRecords"), 3000);
+ });
+
+ it("exports only the selected rows when the user asks for it", async () => {
+ await mountList({ failures: [failure(1), failure(2)] });
+
+ toggle(rowBox(1));
+ await settle();
+ mocks.savePath.mockResolvedValue("/tmp/selected.csv");
+
+ buttonWithText("exportProgress.failureLog.exportSelected")!.click();
+ await settle();
+
+ expect(mocks.exportLog).toHaveBeenCalledWith("exec-1", "txt", "/tmp/selected.csv", [{ fileIndex: 0, statementIndex: 2 }]);
+ });
+
+ it("degrades to the in-memory summaries when the log is unavailable, and says so", async () => {
+ mocks.logInfo.mockResolvedValue({ status: "unavailable", reason: "Cannot create failure log" });
+ await mountList({ failures: [failure(644)] });
+
+ expect(root.textContent).toContain("exportProgress.failureLog.unavailable");
+
+ toggle(rowBox(0));
+ await settle();
+ buttonWithText("exportProgress.failureLog.copySelected")!.click();
+ await settle();
+
+ expect(mocks.readText).not.toHaveBeenCalled();
+ expect(mocks.copy).toHaveBeenCalledWith(expect.stringContaining("INSERT #644"));
+ expect(mocks.toast).toHaveBeenCalledWith(expect.stringContaining("copySummaryFallback"), 5000);
+ });
+
+ it("sends the user to the file export when the selection is too large to copy", async () => {
+ mocks.readText.mockResolvedValue({ text: undefined, tooLarge: true, limitBytes: 5 * 1024 * 1024 });
+ await mountList({ failures: [failure(1)] });
+
+ toggle(rowBox(0));
+ await settle();
+ buttonWithText("exportProgress.failureLog.copySelected")!.click();
+ await settle();
+
+ expect(mocks.copy).not.toHaveBeenCalled();
+ expect(mocks.toast).toHaveBeenCalledWith(expect.stringContaining("copyTooLarge"), 5000);
+ });
+
+ it("keeps the per-row copy button for a single failure", async () => {
+ await mountList({ failures: [failure(644)] });
+
+ const rowCopy = buttons().find((button) => (button.getAttribute("aria-label") ?? "") === "exportProgress.copyFailureDetail");
+ rowCopy!.click();
+ await settle();
+
+ expect(mocks.copy).toHaveBeenCalledWith(expect.stringContaining("INSERT #644"));
+ expect(mocks.copy).toHaveBeenCalledWith(expect.stringContaining("ORA-12899"));
+ });
+
+ it("notices a log that degrades while the execution is still running", async () => {
+ const mounted = await mountList({ failures: [failure(1)] });
+ expect(root.textContent).not.toContain("exportProgress.failureLog.degraded");
+
+ // The disk fills up mid-run: the next failure arrives together with a
+ // degraded status, which must reach the UI without any user action.
+ mocks.logInfo.mockResolvedValue({ status: "degraded", reason: "Cannot write failure log" });
+ mounted.state.failures.push(failure(2));
+ await settle();
+
+ expect(mocks.logInfo.mock.calls.length).toBeGreaterThan(1);
+ expect(root.textContent).toContain("exportProgress.failureLog.degraded");
+ });
+
+ it("re-reads the log state before acting on it", async () => {
+ await mountList({ failures: [failure(1)] });
+ mocks.logInfo.mockClear();
+
+ toggle(rowBox(0));
+ await settle();
+ buttonWithText("exportProgress.failureLog.copySelected")!.click();
+ await settle();
+
+ // Acting on a cached status could export or copy from a log that is gone.
+ expect(mocks.logInfo).toHaveBeenCalledWith("exec-1");
+ });
+
+ it("warns once per app run that the log can contain sensitive data", async () => {
+ await mountList({ failures: [failure(1)] });
+
+ toggle(rowBox(0));
+ await settle();
+ buttonWithText("exportProgress.failureLog.copySelected")!.click();
+ await settle();
+
+ expect(mocks.toast).toHaveBeenCalledWith(expect.stringContaining("sensitiveWarning"), 8000);
+
+ mocks.toast.mockClear();
+ buttonWithText("exportProgress.failureLog.exportTxt")!.click();
+ await settle();
+
+ // Warning once is informative; repeating it on every action is just noise.
+ expect(mocks.toast).not.toHaveBeenCalledWith(expect.stringContaining("sensitiveWarning"), 8000);
+ });
+
+ it("offers the log folder only when the backend reported a path", async () => {
+ await mountList({ failures: [failure(1)] });
+ expect(buttonWithText("exportProgress.failureLog.openFolder")).toBeDefined();
+
+ app?.unmount();
+ root.remove();
+ mocks.logInfo.mockResolvedValue({ status: "degraded", reason: "Cannot write failure log" });
+ await mountList({ failures: [failure(1)] });
+
+ expect(root.textContent).toContain("exportProgress.failureLog.degraded");
+ expect(buttonWithText("exportProgress.failureLog.openFolder")).toBeUndefined();
+ });
+});
diff --git a/apps/desktop/src/components/sql-file/FailureDetailsList.vue b/apps/desktop/src/components/sql-file/FailureDetailsList.vue
new file mode 100644
index 0000000000..3c8c4ea834
--- /dev/null
+++ b/apps/desktop/src/components/sql-file/FailureDetailsList.vue
@@ -0,0 +1,127 @@
+<script setup lang="ts">
+import { computed, onBeforeUnmount, ref } from "vue";
+import { useI18n } from "vue-i18n";
+import { Check, ChevronRight, Copy } from "@lucide/vue";
+
+import { Button } from "@/components/ui/button";
+import { sqlFileFailureCopyText } from "@/components/export/failureDetailCopyText";
+import { useSqlFileFailureLog } from "@/composables/useSqlFileFailureLog";
+import type { SqlFileFailure } from "@/composables/useExportTracker";
+import { useToast } from "@/composables/useToast";
+import { copyToClipboard } from "@/lib/common/clipboard";
+import { failureLogRowKey } from "@/lib/export/failureLogExport";
+import { translateBackendError } from "@/i18n/backend-errors";
+
+const props = withDefaults(
+ defineProps<{
+ executionId?: string;
+ failures: SqlFileFailure[];
+ /** Failures the backend logged but this in-memory list dropped. */
+ omitted: number;
+ fileNameHint: string;
+ /** Only affects paddings/sizes so each surface keeps its own density. */
+ variant?: "dialog" | "popover";
+ }>(),
+ { executionId: undefined, variant: "dialog" },
+);
+
+/** Owned locally unless a parent binds it (the execution dialog auto-expands on failure). */
+const expanded = defineModel<boolean>("expanded", { default: false });
+
+const { t } = useI18n();
+const { toast } = useToast();
+
+const failureLog = useSqlFileFailureLog({
+ executionId: () => props.executionId,
+ failures: () => props.failures,
+ omittedFailures: () => props.omitted,
+ fileNameHint: () => props.fileNameHint,
+});
+const { selectedCount, allSelected, coverage, logWarning, logUnavailable, busy, info } = failureLog;
+
+const totalFailures = computed(() => props.failures.length + props.omitted);
+const listClass = computed(() => (props.variant === "popover" ? "max-h-44 overflow-y-auto rounded border border-destructive/20 bg-destructive/5 text-xs" : "max-h-[min(30vh,280px)] min-w-0 overflow-y-auto rounded-md border border-destructive/20 bg-destructive/5 text-xs"));
+const rowClass = computed(() => (props.variant === "popover" ? "border-b border-destructive/15 px-2.5 py-2 last:border-b-0" : "border-b border-destructive/15 p-2.5 last:border-b-0"));
+
+// Per-row copy keeps the existing single-failure behaviour; the bulk actions
+// above copy/export the full statements from the backend log instead.
+const copiedRowKey = ref("");
+let copiedRowTimer: ReturnType<typeof setTimeout> | undefined;
+
+onBeforeUnmount(() => {
+ if (copiedRowTimer) clearTimeout(copiedRowTimer);
+});
+
+async function copyRow(failure: SqlFileFailure): Promise<void> {
+ const key = failureLogRowKey(failure);
+ try {
+ await copyToClipboard(sqlFileFailureCopyText(failure, translateBackendError(t, failure.error)));
+ copiedRowKey.value = key;
+ if (copiedRowTimer) clearTimeout(copiedRowTimer);
+ copiedRowTimer = setTimeout(() => {
+ if (copiedRowKey.value === key) copiedRowKey.value = "";
+ copiedRowTimer = undefined;
+ }, 2000);
+ toast(t("exportProgress.failureDetailCopied"));
+ } catch (error) {
+ const message = error instanceof Error ? error.message : String(error);
+ toast(t("exportProgress.failureDetailCopyFailed", { message: translateBackendError(t, message) }), 5000);
+ }
+}
+</script>
+
+<template>
+ <div class="min-w-0 space-y-1.5">
+ <div class="flex flex-wrap items-center gap-x-2 gap-y-1">
+ <button type="button" class="flex items-center gap-1 text-xs font-medium text-foreground hover:text-primary" :aria-expanded="expanded" @click="expanded = !expanded">
+ <ChevronRight class="h-3.5 w-3.5 shrink-0 transition-transform" :class="{ 'rotate-90': expanded }" />
+ {{ expanded ? t("exportProgress.hideFailureDetails") : t("exportProgress.showFailureDetails", { count: totalFailures }) }}
+ </button>
+ <div class="ml-auto flex flex-wrap items-center gap-x-2 gap-y-1">
+ <label class="flex cursor-pointer items-center gap-1 text-xs text-muted-foreground">
+ <input type="checkbox" class="h-3.5 w-3.5 accent-primary" :checked="allSelected" @change="failureLog.toggleAll()" />
+ {{ t("exportProgress.failureLog.selectAll") }}
+ </label>
+ <span v-if="selectedCount > 0" class="text-xs text-muted-foreground">{{ t("exportProgress.failureLog.selectedCount", { count: selectedCount }) }}</span>
+ <Button variant="outline" size="sm" class="h-6 px-2 text-xs" :disabled="selectedCount === 0 || busy" @click="failureLog.copySelected()">{{ t("exportProgress.failureLog.copySelected") }}</Button>
+ <Button variant="outline" size="sm" class="h-6 px-2 text-xs" :disabled="busy || logUnavailable" @click="failureLog.exportLog('txt', 'all')">{{ t("exportProgress.failureLog.exportTxt") }}</Button>
+ <Button variant="outline" size="sm" class="h-6 px-2 text-xs" :disabled="busy || logUnavailable" @click="failureLog.exportLog('csv', 'all')">{{ t("exportProgress.failureLog.exportCsv") }}</Button>
+ <Button v-if="selectedCount > 0" variant="outline" size="sm" class="h-6 px-2 text-xs" :disabled="busy || logUnavailable" @click="failureLog.exportLog('txt', 'selected')">{{ t("exportProgress.failureLog.exportSelected") }}</Button>
+ <Button v-if="info?.path" variant="outline" size="sm" class="h-6 px-2 text-xs" @click="failureLog.openLogFolder()">{{ t("exportProgress.failureLog.openFolder") }}</Button>
+ </div>
+ </div>
+ <!-- The list is capped while the log keeps every failure: say so instead of
+ letting the export/UI counts silently disagree. -->
+ <p v-if="coverage.truncated" class="text-xs text-muted-foreground">
+ {{ t("exportProgress.failureLog.listTruncated", { visible: failures.length, total: totalFailures }) }}
+ </p>
+ <p v-if="logWarning" class="text-xs text-destructive">{{ logWarning }}</p>
+ <div v-if="expanded" :class="listClass">
+ <div v-for="failure in failures" :key="failureLogRowKey(failure)" :class="rowClass">
+ <div class="flex min-w-0 items-start justify-between gap-2">
+ <label class="flex min-w-0 items-start gap-2">
+ <input type="checkbox" class="mt-0.5 h-3.5 w-3.5 shrink-0 accent-primary" :checked="failureLog.isSelected(failure)" :aria-label="`#${failure.statementIndex}`" @change="failureLog.toggleSelection(failure)" />
+ <span class="flex min-w-0 items-center gap-1.5 font-medium text-foreground">
+ <span class="shrink-0">#{{ failure.statementIndex }}</span>
+ <span v-if="failure.fileName" class="truncate text-muted-foreground" :title="failure.fileName">{{ failure.fileName }}</span>
+ </span>
+ </label>
+ <button
+ type="button"
+ class="flex h-5 w-5 shrink-0 items-center justify-center rounded hover:bg-muted"
+ :title="copiedRowKey === failureLogRowKey(failure) ? t('exportProgress.failureDetailCopied') : t('exportProgress.copyFailureDetail')"
+ :aria-label="copiedRowKey === failureLogRowKey(failure) ? t('exportProgress.failureDetailCopied') : t('exportProgress.copyFailureDetail')"
+ @click.stop="copyRow(failure)"
+ >
+ <Check v-if="copiedRowKey === failureLogRowKey(failure)" class="h-3.5 w-3.5 text-green-500" aria-hidden="true" />
+ <Copy v-else class="h-3.5 w-3.5 text-muted-foreground hover:text-foreground" aria-hidden="true" />
+ </button>
+ </div>
+ <div v-if="failure.statementSummary" class="mt-1 max-w-full overflow-auto font-mono whitespace-pre-wrap break-words text-foreground">{{ failure.statementSummary }}</div>
+ <div class="mt-1 select-text whitespace-pre-wrap break-words text-destructive">{{ translateBackendError(t, failure.error) }}</div>
+ <div v-if="failure.truncated" class="mt-1 text-muted-foreground">{{ t("exportProgress.failureDetailTruncated") }}</div>
+ </div>
+ <div v-if="omitted" class="p-2.5 text-muted-foreground">{{ t("exportProgress.failureDetailsOmitted", { count: omitted }) }}</div>
+ </div>
+ </div>
+</template>
diff --git a/apps/desktop/src/components/sql-file/SqlFileExecutionDialog.retry.spec.ts b/apps/desktop/src/components/sql-file/SqlFileExecutionDialog.retry.spec.ts
index 52f69ccede..60e2c000db 100644
--- a/apps/desktop/src/components/sql-file/SqlFileExecutionDialog.retry.spec.ts
+++ b/apps/desktop/src/components/sql-file/SqlFileExecutionDialog.retry.spec.ts
@@ -113,7 +113,7 @@ vi.mock("@/lib/backend/api", () => ({
}));
vi.mock("@lucide/vue", () => {
const Icon = passthrough("span");
- return { Check: Icon, CheckSquare: Icon, ChevronRight: Icon, FileCode: Icon, FolderOpen: Icon, Loader2: Icon, Play: Icon, Square: Icon, X: Icon };
+ return { Check: Icon, CheckSquare: Icon, ChevronRight: Icon, Copy: Icon, FileCode: Icon, FolderOpen: Icon, Loader2: Icon, Play: Icon, Square: Icon, X: Icon };
});
vi.mock("@/components/ui/dialog", () => ({
Dialog: defineComponent({
diff --git a/apps/desktop/src/components/sql-file/SqlFileExecutionDialog.vue b/apps/desktop/src/components/sql-file/SqlFileExecutionDialog.vue
index a1551eb0b0..7c12177595 100644
--- a/apps/desktop/src/components/sql-file/SqlFileExecutionDialog.vue
+++ b/apps/desktop/src/components/sql-file/SqlFileExecutionDialog.vue
@@ -13,6 +13,7 @@ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@
import DatabaseIcon from "@/components/icons/DatabaseIcon.vue";
import ConnectionGroupBadge from "@/components/connection/ConnectionGroupBadge.vue";
import { useToast } from "@/composables/useToast";
+import FailureDetailsList from "@/components/sql-file/FailureDetailsList.vue";
import { useConnectionStore } from "@/stores/connectionStore";
import { useQueryStore } from "@/stores/queryStore";
import { useSettingsStore } from "@/stores/settingsStore";
@@ -32,7 +33,7 @@ import { orderConnectionsForSidebarDisplay } from "@/lib/sidebar/connectionListS
import SqlFileProgressIndicator from "./SqlFileProgressIndicator.vue";
import { useExportTracker, type ExportTask } from "@/composables/useExportTracker";
import { translateBackendError } from "@/i18n/backend-errors";
-import { Check, CheckSquare, ChevronRight, FileCode, FolderOpen, Loader2, Play, Square, X } from "@lucide/vue";
+import { Check, CheckSquare, FileCode, FolderOpen, Loader2, Play, Square, X } from "@lucide/vue";
const { t } = useI18n();
const { toast } = useToast();
@@ -339,6 +340,10 @@ const statusIcon = computed(() => {
});
const sqlFileFailures = computed(() => activeExecutionTask.value?.sqlFileFailures ?? []);
+// Default name of an exported failure log: the executed script(s), so a log is
+// recognizable next to the file it came from.
+const failureLogFileNameHint = computed(() => previews.value.map((item) => displayFileNames.value.get(item.filePath) ?? item.fileName).join("-") || "sql-file");
+
const sqlFileFailureCount = computed(() => sqlFileFailures.value.length + (activeExecutionTask.value?.sqlFileFailuresOmitted ?? 0));
const unlistedTerminalError = computed(() => {
const error = progress.value?.error || terminalError.value;
@@ -1158,26 +1163,18 @@ watch(
</div>
</div>
- <div v-if="sqlFileFailureCount > 0" class="min-w-0 space-y-1.5">
- <button type="button" class="flex items-center gap-1 text-xs font-medium text-foreground hover:text-primary" :aria-expanded="failureDetailsExpanded" @click="failureDetailsExpanded = !failureDetailsExpanded">
- <ChevronRight class="h-3.5 w-3.5 shrink-0 transition-transform" :class="{ 'rotate-90': failureDetailsExpanded }" />
- {{ failureDetailsExpanded ? t("exportProgress.hideFailureDetails") : t("exportProgress.showFailureDetails", { count: sqlFileFailureCount }) }}
- </button>
- <div v-if="failureDetailsExpanded" class="max-h-[min(30vh,280px)] min-w-0 overflow-y-auto rounded-md border border-destructive/20 bg-destructive/5 text-xs">
- <div v-for="failure in sqlFileFailures" :key="`${failure.fileIndex ?? -1}:${failure.statementIndex}`" class="border-b border-destructive/15 p-2.5 last:border-b-0">
- <div class="flex min-w-0 items-center gap-1.5 font-medium text-foreground">
- <span class="shrink-0">#{{ failure.statementIndex }}</span>
- <span v-if="failure.fileName" class="truncate text-muted-foreground" :title="failure.fileName">{{ failure.fileName }}</span>
- </div>
- <div v-if="failure.statementSummary" class="mt-1 max-w-full overflow-auto font-mono whitespace-pre-wrap break-words text-foreground">{{ failure.statementSummary }}</div>
- <div class="mt-1 select-text whitespace-pre-wrap break-words text-destructive">{{ translateBackendError(t, failure.error) }}</div>
- <div v-if="failure.truncated" class="mt-1 text-muted-foreground">{{ t("exportProgress.failureDetailTruncated") }}</div>
- </div>
- <div v-if="activeExecutionTask?.sqlFileFailuresOmitted" class="p-2.5 text-muted-foreground">
- {{ t("exportProgress.failureDetailsOmitted", { count: activeExecutionTask.sqlFileFailuresOmitted }) }}
- </div>
- </div>
- </div>
+ <!-- The list is capped in memory while the backend log keeps every
+ failed statement, so the export/copy actions live in the shared
+ component that also renders these rows (dbx#11619). -->
+ <FailureDetailsList
+ v-if="sqlFileFailureCount > 0"
+ v-model:expanded="failureDetailsExpanded"
+ :execution-id="executionId || activeExecutionTask?.exportId || undefined"
+ :failures="sqlFileFailures"
+ :omitted="activeExecutionTask?.sqlFileFailuresOmitted ?? 0"
+ :file-name-hint="failureLogFileNameHint"
+ variant="dialog"
+ />
<div v-if="unlistedTerminalError" class="max-w-full overflow-auto rounded-md border bg-destructive/5 p-2 text-xs text-destructive whitespace-pre-wrap">
{{ translateBackendError(t, unlistedTerminalError) }}
diff --git a/apps/desktop/src/composables/useSqlFileFailureLog.ts b/apps/desktop/src/composables/useSqlFileFailureLog.ts
new file mode 100644
index 0000000000..3b2110c1c2
--- /dev/null
+++ b/apps/desktop/src/composables/useSqlFileFailureLog.ts
@@ -0,0 +1,242 @@
+import { computed, ref, watch } from "vue";
+import { useI18n } from "vue-i18n";
+
+import { sqlFileFailureCopyText } from "@/components/export/failureDetailCopyText";
+import * as api from "@/lib/backend/api";
+import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
+import { copyToClipboard } from "@/lib/common/clipboard";
+import type { SqlFileFailure } from "@/composables/useExportTracker";
+import { useToast } from "@/composables/useToast";
+import { promptExportSavePath } from "@/lib/export/exportPath";
+import { FAILURE_LOG_TEXT_TOO_LARGE, failureLogListCoverage, failureLogRowKey, selectedFailureLogKeys, takeFailureLogSensitiveNotice } from "@/lib/export/failureLogExport";
+import { sanitizeExportBaseName } from "@/lib/export/saveTextFile";
+import { translateBackendError } from "@/i18n/backend-errors";
+
+export type FailureLogExportFormat = "txt" | "csv";
+export type FailureLogExportScope = "all" | "selected";
+
+export interface UseSqlFileFailureLogOptions {
+ executionId: () => string | undefined;
+ /** Failures the UI keeps in memory (at most a few hundred). */
+ failures: () => SqlFileFailure[];
+ /** Failures the backend logged but the UI dropped; they are still exported. */
+ omittedFailures: () => number;
+ /** Used for the default export file name, e.g. the script or task name. */
+ fileNameHint: () => string;
+}
+
+/**
+ * Failure-log actions shared by the execution dialog and the background-task
+ * popover (dbx#11619): selection over the visible failures, copy/export through
+ * the backend log (which holds the complete statements), and honest degradation
+ * when that log is missing or damaged.
+ *
+ * The surfaces render their own lists, so only the behaviour lives here.
+ */
+export function useSqlFileFailureLog(options: UseSqlFileFailureLogOptions) {
+ const { t } = useI18n();
+ const { toast } = useToast();
+
+ const selectedKeys = ref<Set<string>>(new Set());
+ const info = ref<api.SqlFileFailureLogInfo | null>(null);
+ const busy = ref(false);
+
+ const logUnavailable = computed(() => info.value?.status === "unavailable");
+ const logDegraded = computed(() => info.value?.status === "degraded");
+ const logWarning = computed(() => {
+ if (info.value?.status === "unavailable") {
+ return t("exportProgress.failureLog.unavailable", { reason: info.value.reason ?? "" });
+ }
+ if (info.value?.status === "degraded") {
+ return t("exportProgress.failureLog.degraded", { reason: info.value.reason ?? "" });
+ }
+ return "";
+ });
+
+ const selectedCount = computed(() => options.failures().filter((failure) => selectedKeys.value.has(failureLogRowKey(failure))).length);
+ const allSelected = computed(() => {
+ const list = options.failures();
+ return list.length > 0 && selectedCount.value >= list.length;
+ });
+ /** The list is capped while the log keeps everything, so the UI must say so. */
+ const coverage = computed(() => failureLogListCoverage(options.failures().length + options.omittedFailures(), options.failures().length));
+
+ function isSelected(failure: SqlFileFailure): boolean {
+ return selectedKeys.value.has(failureLogRowKey(failure));
+ }
+
+ function toggleSelection(failure: SqlFileFailure): void {
+ const next = new Set(selectedKeys.value);
+ const key = failureLogRowKey(failure);
+ if (next.has(key)) next.delete(key);
+ else next.add(key);
+ selectedKeys.value = next;
+ }
+
+ function toggleAll(): void {
+ selectedKeys.value = allSelected.value ? new Set() : new Set(options.failures().map(failureLogRowKey));
+ }
+
+ function clearSelection(): void {
+ selectedKeys.value = new Set();
+ }
+
+ async function refreshInfo(): Promise<void> {
+ const executionId = options.executionId();
+ if (!executionId) {
+ info.value = null;
+ return;
+ }
+ try {
+ info.value = await api.sqlFileFailureLogInfo(executionId);
+ } catch {
+ // An unreachable status must not block the UI: fall back to the in-memory
+ // failure list, which the copy path already degrades to.
+ info.value = null;
+ }
+ }
+
+ // Refreshes are coalesced: a running execution reports failures per statement,
+ // and one lookup in flight is enough for the UI to notice a log that degraded
+ // mid-run (disk full) and disable the export controls.
+ let refreshInFlight = false;
+
+ function scheduleRefreshInfo(): void {
+ if (refreshInFlight) return;
+ refreshInFlight = true;
+ void refreshInfo().finally(() => {
+ refreshInFlight = false;
+ });
+ }
+
+ watch(
+ () => options.executionId(),
+ () => {
+ clearSelection();
+ void refreshInfo();
+ },
+ { immediate: true },
+ );
+
+ // New failures mean the log is being written to, so its health may have changed.
+ watch(
+ () => options.failures().length,
+ () => scheduleRefreshInfo(),
+ );
+
+ function selectedFailures(): SqlFileFailure[] {
+ return options.failures().filter((failure) => selectedKeys.value.has(failureLogRowKey(failure)));
+ }
+
+ function formatLimit(limitBytes: number): string {
+ return `${Math.max(1, Math.round(limitBytes / (1024 * 1024)))}MB`;
+ }
+
+ function errorMessage(error: unknown): string {
+ return error instanceof Error ? error.message : String(error);
+ }
+
+ // Warn once per app run before the first copy/export: the derived text holds
+ // complete SQL and raw database errors, which can carry sensitive values.
+ function warnSensitiveLogOnce(): void {
+ if (!takeFailureLogSensitiveNotice()) return;
+ toast(t("exportProgress.failureLog.sensitiveWarning"), 8000);
+ }
+
+ async function copySelected(): Promise<void> {
+ const executionId = options.executionId();
+ const failures = selectedFailures();
+ if (!executionId || failures.length === 0) return;
+ await refreshInfo();
+ warnSensitiveLogOnce();
+ busy.value = true;
+ try {
+ if (logUnavailable.value) {
+ // Degrade honestly: only summaries exist in memory, and the toast says so
+ // instead of passing them off as the full log.
+ const text = failures.map((failure) => sqlFileFailureCopyText(failure, translateBackendError(t, failure.error))).join("\n\n");
+ await copyToClipboard(text);
+ toast(t("exportProgress.failureLog.copySummaryFallback"), 5000);
+ return;
+ }
+ const payload = await api.readSqlFileFailureLogText(executionId, "txt", selectedFailureLogKeys(options.failures(), selectedKeys.value));
+ if (payload.tooLarge || payload.text === undefined) {
+ toast(t("exportProgress.failureLog.copyTooLarge", { limit: formatLimit(payload.limitBytes) }), 5000);
+ return;
+ }
+ await copyToClipboard(payload.text);
+ toast(t("exportProgress.failureLog.copySelectedDone", { count: failures.length }), 2500);
+ } catch (error) {
+ toast(t("exportProgress.failureLog.actionFailed", { message: translateBackendError(t, errorMessage(error)) }), 5000);
+ } finally {
+ busy.value = false;
+ }
+ }
+
+ async function exportLog(format: FailureLogExportFormat, scope: FailureLogExportScope): Promise<void> {
+ const executionId = options.executionId();
+ if (!executionId) return;
+ const keys = scope === "selected" ? selectedFailureLogKeys(options.failures(), selectedKeys.value) : [];
+ if (scope === "selected" && keys.length === 0) return;
+ await refreshInfo();
+ warnSensitiveLogOnce();
+ busy.value = true;
+ try {
+ const baseName = sanitizeExportBaseName(options.fileNameHint()) || "sql-file";
+ const defaultFileName = `${baseName}-failures.${format}`;
+ if (isTauriRuntime()) {
+ const targetPath = await promptExportSavePath({
+ defaultFileName,
+ filters: [{ name: format.toUpperCase(), extensions: [format] }],
+ });
+ if (!targetPath) return;
+ const result = await api.exportSqlFileFailureLog(executionId, format, targetPath, keys.length > 0 ? keys : undefined);
+ toast(t("exportProgress.failureLog.exportedRecords", { count: result.records }), 3000);
+ return;
+ }
+ // Web: the whole log streams from the server as an attachment; a selected
+ // subset comes back as capped text and is saved by the transport.
+ const result = await api.exportSqlFileFailureLog(executionId, format, "", keys.length > 0 ? keys : undefined);
+ toast(scope === "selected" ? t("exportProgress.failureLog.exportedRecords", { count: result.records }) : t("exportProgress.failureLog.exportStarted"), 3000);
+ } catch (error) {
+ const message = errorMessage(error);
+ if (message === FAILURE_LOG_TEXT_TOO_LARGE) {
+ toast(t("exportProgress.failureLog.exportSelectionTooLarge"), 5000);
+ return;
+ }
+ toast(t("exportProgress.failureLog.actionFailed", { message: translateBackendError(t, message) }), 5000);
+ } finally {
+ busy.value = false;
+ }
+ }
+
+ async function openLogFolder(): Promise<void> {
+ await refreshInfo();
+ const path = info.value?.path;
+ if (!path) return;
+ try {
+ await api.revealPathInFileManager(path);
+ } catch (error) {
+ toast(t("exportProgress.failureLog.actionFailed", { message: errorMessage(error) }), 3000);
+ }
+ }
+
+ return {
+ selectedCount,
+ allSelected,
+ coverage,
+ logUnavailable,
+ logDegraded,
+ logWarning,
+ busy,
+ info,
+ isSelected,
+ toggleSelection,
+ toggleAll,
+ clearSelection,
+ refreshInfo,
+ copySelected,
+ exportLog,
+ openLogFolder,
+ };
+}
diff --git a/apps/desktop/src/i18n/locales/az.ts b/apps/desktop/src/i18n/locales/az.ts
index 7df79649ec..53b980d1ab 100644
--- a/apps/desktop/src/i18n/locales/az.ts
+++ b/apps/desktop/src/i18n/locales/az.ts
@@ -2618,6 +2618,26 @@ export default withEnglishFallback({
statementsCount: "{done} uğurlu, {failed} uğursuz",
elapsed: "Keçən vaxt: {duration}",
showFailureDetails: "Uğursuzluq təfərrüatları ({count})",
+ failureLog: {
+ selectAll: "Hamısını seç",
+ selectedCount: "{count} seçildi",
+ copySelected: "Seçiləni kopyala",
+ copySelectedDone: "{count} uğursuzluq təfərrüatı kopyalandı",
+ copyTooLarge: "Seçim {limit} həddini aşır; fayla eksport edin",
+ copySummaryFallback: "Tam uğursuzluq jurnalı mövcud deyil; yalnız görünən xülasələr kopyalandı",
+ exportTxt: "TXT eksport et",
+ exportCsv: "CSV eksport et",
+ exportSelected: "Seçiləni eksport et",
+ exportedRecords: "{count} uğursuzluq təfərrüatı eksport edildi",
+ exportStarted: "Jurnalın yüklənməsi başladı",
+ exportSelectionTooLarge: "Seçilmiş uğursuzluq təfərrüatları çox böyükdür; tam jurnal üçün TXT eksportundan istifadə edin",
+ actionFailed: "Əməliyyat alınmadı: {message}",
+ unavailable: "Uğursuzluq jurnalı mövcud deyil: {reason}",
+ degraded: "Uğursuzluq jurnalının yazılması problemi: {reason}",
+ listTruncated: "İlk {visible} göstərilir; eksport bütün {total} qeydi əhatə edir",
+ sensitiveWarning: "Uğursuzluq jurnalları uğursuz ifadələrin tam mətnini və verilənlər bazasının xam xətalarını ehtiva edir; həssas məlumat ola bilər. Paylaşarkən diqqətli olun.",
+ openFolder: "Jurnal qovluğunu aç",
+ },
hideFailureDetails: "Uğursuzluq təfərrüatlarını gizlət",
failureDetailTruncated: "Xəta təfərrüatları kəsildi",
failureDetailsOmitted: "Əlavə {count} uğursuzluq təfərrüatı göstərilmir",
diff --git a/apps/desktop/src/i18n/locales/en.ts b/apps/desktop/src/i18n/locales/en.ts
index 70a91f306d..ab851d7716 100644
--- a/apps/desktop/src/i18n/locales/en.ts
+++ b/apps/desktop/src/i18n/locales/en.ts
@@ -3095,6 +3095,26 @@ export default {
statementsCount: "{done} succeeded, {failed} failed",
elapsed: "Elapsed: {duration}",
showFailureDetails: "Failure details ({count})",
+ failureLog: {
+ selectAll: "Select all",
+ selectedCount: "{count} selected",
+ copySelected: "Copy selected",
+ copySelectedDone: "Copied {count} failure(s)",
+ copyTooLarge: "The selection exceeds {limit}; export it to a file instead",
+ copySummaryFallback: "The full failure log is unavailable, so only the summaries shown here were copied",
+ exportTxt: "Export TXT",
+ exportCsv: "Export CSV",
+ exportSelected: "Export selected",
+ exportedRecords: "Exported {count} failure(s)",
+ exportStarted: "Log download started",
+ exportSelectionTooLarge: "The selected failures are too large; use Export TXT to get the whole log",
+ actionFailed: "Action failed: {message}",
+ unavailable: "Failure log unavailable: {reason}",
+ degraded: "Failure log write problem: {reason}",
+ listTruncated: "Showing the first {visible}; the export contains all {total}",
+ sensitiveWarning: "Failure logs contain the complete failed statements and raw database errors, which may include sensitive data. Share them carefully.",
+ openFolder: "Open log folder",
+ },
hideFailureDetails: "Hide failure details",
failureDetailTruncated: "Error details were truncated",
failureDetailsOmitted: "{count} additional failure detail(s) not shown",
diff --git a/apps/desktop/src/i18n/locales/es.ts b/apps/desktop/src/i18n/locales/es.ts
index 6dce48f293..ce27b0441d 100644
--- a/apps/desktop/src/i18n/locales/es.ts
+++ b/apps/desktop/src/i18n/locales/es.ts
@@ -3004,6 +3004,26 @@ export default withEnglishFallback({
elapsed: "Transcurrido: {duration}",
delete: "Eliminar",
showFailureDetails: "Detalles de fallos ({count})",
+ failureLog: {
+ selectAll: "Seleccionar todo",
+ selectedCount: "{count} seleccionados",
+ copySelected: "Copiar selección",
+ copySelectedDone: "Se copiaron {count} fallos",
+ copyTooLarge: "La selección supera {limit}; exporte a un archivo",
+ copySummaryFallback: "El registro completo no está disponible; solo se copiaron los resúmenes visibles",
+ exportTxt: "Exportar TXT",
+ exportCsv: "Exportar CSV",
+ exportSelected: "Exportar selección",
+ exportedRecords: "Se exportaron {count} fallos",
+ exportStarted: "Descarga del registro iniciada",
+ exportSelectionTooLarge: "La selección es demasiado grande; use Exportar TXT para el registro completo",
+ actionFailed: "La operación falló: {message}",
+ unavailable: "Registro de fallos no disponible: {reason}",
+ degraded: "Problema al escribir el registro de fallos: {reason}",
+ listTruncated: "Se muestran los primeros {visible}; la exportación incluye los {total}",
+ sensitiveWarning: "Los registros de fallos contienen las sentencias fallidas completas y los errores originales de la base de datos, que pueden incluir datos sensibles. Compártalos con cuidado.",
+ openFolder: "Abrir carpeta de registros",
+ },
hideFailureDetails: "Ocultar detalles de fallos",
failureDetailTruncated: "Los detalles del error se han truncado",
failureDetailsOmitted: "No se muestran {count} detalles de fallos adicionales",
diff --git a/apps/desktop/src/i18n/locales/id.ts b/apps/desktop/src/i18n/locales/id.ts
index bb40535a26..b1a1e60a0a 100644
--- a/apps/desktop/src/i18n/locales/id.ts
+++ b/apps/desktop/src/i18n/locales/id.ts
@@ -2915,6 +2915,26 @@ export default withEnglishFallback({
statementsCount: "{done} berhasil, {failed} gagal",
elapsed: "Berlalu: {duration}",
showFailureDetails: "Detail kegagalan ({count})",
+ failureLog: {
+ selectAll: "Pilih semua",
+ selectedCount: "{count} dipilih",
+ copySelected: "Salin yang dipilih",
+ copySelectedDone: "{count} detail kegagalan disalin",
+ copyTooLarge: "Pilihan melebihi {limit}; ekspor ke berkas",
+ copySummaryFallback: "Log kegagalan lengkap tidak tersedia; hanya ringkasan yang terlihat disalin",
+ exportTxt: "Ekspor TXT",
+ exportCsv: "Ekspor CSV",
+ exportSelected: "Ekspor yang dipilih",
+ exportedRecords: "{count} detail kegagalan diekspor",
+ exportStarted: "Unduhan log dimulai",
+ exportSelectionTooLarge: "Detail kegagalan yang dipilih terlalu besar; gunakan Ekspor TXT untuk log lengkap",
+ actionFailed: "Tindakan gagal: {message}",
+ unavailable: "Log kegagalan tidak tersedia: {reason}",
+ degraded: "Masalah penulisan log kegagalan: {reason}",
+ listTruncated: "Menampilkan {visible} pertama; ekspor memuat seluruh {total}",
+ sensitiveWarning: "Log kegagalan memuat pernyataan gagal secara lengkap dan error mentah basis data, yang dapat berisi data sensitif. Bagikan dengan hati-hati.",
+ openFolder: "Buka folder log",
+ },
hideFailureDetails: "Sembunyikan detail kegagalan",
failureDetailTruncated: "Detail galat dipotong",
failureDetailsOmitted: "{count} detail kegagalan tambahan tidak ditampilkan",
diff --git a/apps/desktop/src/i18n/locales/it.ts b/apps/desktop/src/i18n/locales/it.ts
index 7a0ed332a8..315f347886 100644
--- a/apps/desktop/src/i18n/locales/it.ts
+++ b/apps/desktop/src/i18n/locales/it.ts
@@ -2860,6 +2860,26 @@ export default withEnglishFallback({
statementsCount: "{done} riuscite, {failed} non riuscite",
elapsed: "Tempo trascorso: {duration}",
showFailureDetails: "Dettagli errori ({count})",
+ failureLog: {
+ selectAll: "Seleziona tutto",
+ selectedCount: "{count} selezionati",
+ copySelected: "Copia selezione",
+ copySelectedDone: "Copiati {count} errori",
+ copyTooLarge: "La selezione supera {limit}; esporta su file",
+ copySummaryFallback: "Il log completo non è disponibile: sono stati copiati solo i riepiloghi visibili",
+ exportTxt: "Esporta TXT",
+ exportCsv: "Esporta CSV",
+ exportSelected: "Esporta selezione",
+ exportedRecords: "Esportati {count} errori",
+ exportStarted: "Download del log avviato",
+ exportSelectionTooLarge: "La selezione è troppo grande; usa Esporta TXT per il log completo",
+ actionFailed: "Operazione non riuscita: {message}",
+ unavailable: "Log degli errori non disponibile: {reason}",
+ degraded: "Problema di scrittura del log degli errori: {reason}",
+ listTruncated: "Sono mostrati i primi {visible}; l'esportazione include tutti i {total}",
+ sensitiveWarning: "I log degli errori contengono le istruzioni fallite complete e gli errori grezzi del database, che possono includere dati sensibili. Condividili con cautela.",
+ openFolder: "Apri cartella dei log",
+ },
hideFailureDetails: "Nascondi dettagli errori",
failureDetailTruncated: "I dettagli dell'errore sono stati troncati",
failureDetailsOmitted: "Altri {count} dettagli di errore non sono visualizzati",
diff --git a/apps/desktop/src/i18n/locales/ja.ts b/apps/desktop/src/i18n/locales/ja.ts
index 1ad8cabe98..c350a671d0 100644
--- a/apps/desktop/src/i18n/locales/ja.ts
+++ b/apps/desktop/src/i18n/locales/ja.ts
@@ -2881,6 +2881,26 @@ export default withEnglishFallback({
statementsCount: "{done}成功、{failed}失敗",
elapsed: "経過時間: {duration}",
showFailureDetails: "失敗の詳細({count})",
+ failureLog: {
+ selectAll: "すべて選択",
+ selectedCount: "{count} 件選択中",
+ copySelected: "選択項目をコピー",
+ copySelectedDone: "{count} 件の失敗詳細をコピーしました",
+ copyTooLarge: "選択内容が {limit} を超えています。ファイルへのエクスポートを使用してください",
+ copySummaryFallback: "完全な失敗ログを利用できないため、一覧に表示中の概要のみをコピーしました",
+ exportTxt: "TXT をエクスポート",
+ exportCsv: "CSV をエクスポート",
+ exportSelected: "選択項目をエクスポート",
+ exportedRecords: "{count} 件の失敗詳細をエクスポートしました",
+ exportStarted: "ログのダウンロードを開始しました",
+ exportSelectionTooLarge: "選択した失敗詳細が大きすぎます。TXT エクスポートで完全なログを取得してください",
+ actionFailed: "操作に失敗しました:{message}",
+ unavailable: "失敗ログを利用できません:{reason}",
+ degraded: "失敗ログの書き込みに問題があります:{reason}",
+ listTruncated: "一覧は先頭 {visible} 件のみ表示しています。エクスポートには全 {total} 件が含まれます",
+ sensitiveWarning: "失敗ログには完全な失敗ステートメントとデータベースの生のエラーが含まれ、機密情報が含まれる可能性があります。共有には注意してください。",
+ openFolder: "ログフォルダーを開く",
+ },
hideFailureDetails: "失敗の詳細を閉じる",
failureDetailTruncated: "エラー詳細は省略されました",
failureDetailsOmitted: "ほか {count} 件の失敗詳細は表示されていません",
diff --git a/apps/desktop/src/i18n/locales/ko.ts b/apps/desktop/src/i18n/locales/ko.ts
index 8038a741ea..7eab182e91 100644
--- a/apps/desktop/src/i18n/locales/ko.ts
+++ b/apps/desktop/src/i18n/locales/ko.ts
@@ -2809,6 +2809,26 @@ export default withEnglishFallback({
statementsCount: "{done}개 성공, {failed}개 실패",
elapsed: "경과: {duration}",
showFailureDetails: "실패 상세 ({count})",
+ failureLog: {
+ selectAll: "전체 선택",
+ selectedCount: "{count}개 선택됨",
+ copySelected: "선택 항목 복사",
+ copySelectedDone: "실패 상세 {count}건을 복사했습니다",
+ copyTooLarge: "선택한 내용이 {limit}을(를) 초과했습니다. 파일로 내보내세요",
+ copySummaryFallback: "전체 실패 로그를 사용할 수 없어 목록에 보이는 요약만 복사했습니다",
+ exportTxt: "TXT 내보내기",
+ exportCsv: "CSV 내보내기",
+ exportSelected: "선택 항목 내보내기",
+ exportedRecords: "실패 상세 {count}건을 내보냈습니다",
+ exportStarted: "로그 다운로드를 시작했습니다",
+ exportSelectionTooLarge: "선택한 실패 상세가 너무 큽니다. TXT 내보내기로 전체 로그를 받으세요",
+ actionFailed: "작업 실패: {message}",
+ unavailable: "실패 로그를 사용할 수 없습니다: {reason}",
+ degraded: "실패 로그 쓰기 오류: {reason}",
+ listTruncated: "목록은 처음 {visible}건만 표시합니다. 내보내기에는 전체 {total}건이 포함됩니다",
+ sensitiveWarning: "실패 로그에는 전체 실패 구문과 데이터베이스 원본 오류가 포함되어 있어 민감한 정보가 있을 수 있습니다. 공유에 주의하세요.",
+ openFolder: "로그 폴더 열기",
+ },
hideFailureDetails: "실패 상세 숨기기",
failureDetailTruncated: "오류 상세가 잘렸습니다",
failureDetailsOmitted: "추가 실패 상세 {count}개가 표시되지 않았습니다",
diff --git a/apps/desktop/src/i18n/locales/pt-BR.ts b/apps/desktop/src/i18n/locales/pt-BR.ts
index 456f1211c4..df6f02b770 100644
--- a/apps/desktop/src/i18n/locales/pt-BR.ts
+++ b/apps/desktop/src/i18n/locales/pt-BR.ts
@@ -2857,6 +2857,26 @@ export default withEnglishFallback({
statementsCount: "{done} com sucesso, {failed} com falha",
elapsed: "Decorrido: {duration}",
showFailureDetails: "Detalhes das falhas ({count})",
+ failureLog: {
+ selectAll: "Selecionar tudo",
+ selectedCount: "{count} selecionadas",
+ copySelected: "Copiar seleção",
+ copySelectedDone: "{count} falhas copiadas",
+ copyTooLarge: "A seleção excede {limit}; exporte para um arquivo",
+ copySummaryFallback: "O log completo não está disponível; apenas os resumos visíveis foram copiados",
+ exportTxt: "Exportar TXT",
+ exportCsv: "Exportar CSV",
+ exportSelected: "Exportar seleção",
+ exportedRecords: "{count} falhas exportadas",
+ exportStarted: "Download do log iniciado",
+ exportSelectionTooLarge: "A seleção é grande demais; use Exportar TXT para obter o log completo",
+ actionFailed: "Falha na operação: {message}",
+ unavailable: "Log de falhas indisponível: {reason}",
+ degraded: "Problema ao gravar o log de falhas: {reason}",
+ listTruncated: "Exibindo as primeiras {visible}; a exportação inclui todas as {total}",
+ sensitiveWarning: "Os logs de falhas contêm as instruções com falha completas e os erros brutos do banco de dados, que podem incluir dados sensíveis. Compartilhe com cuidado.",
+ openFolder: "Abrir pasta de logs",
+ },
hideFailureDetails: "Ocultar detalhes das falhas",
failureDetailTruncated: "Os detalhes do erro foram truncados",
failureDetailsOmitted: "{count} detalhes adicionais de falha não exibidos",
diff --git a/apps/desktop/src/i18n/locales/ru.ts b/apps/desktop/src/i18n/locales/ru.ts
index 10b7d8d86f..404f155368 100644
--- a/apps/desktop/src/i18n/locales/ru.ts
+++ b/apps/desktop/src/i18n/locales/ru.ts
@@ -2963,6 +2963,26 @@ export default withEnglishFallback({
statementsCount: "успешно {done}, с ошибкой {failed}",
elapsed: "Затрачено: {duration}",
showFailureDetails: "Детали ошибок ({count})",
+ failureLog: {
+ selectAll: "Выбрать все",
+ selectedCount: "Выбрано: {count}",
+ copySelected: "Копировать выбранное",
+ copySelectedDone: "Скопировано ошибок: {count}",
+ copyTooLarge: "Выбор превышает {limit}; экспортируйте в файл",
+ copySummaryFallback: "Полный журнал недоступен, скопированы только видимые сводки",
+ exportTxt: "Экспорт TXT",
+ exportCsv: "Экспорт CSV",
+ exportSelected: "Экспорт выбранного",
+ exportedRecords: "Экспортировано ошибок: {count}",
+ exportStarted: "Загрузка журнала началась",
+ exportSelectionTooLarge: "Выбранные ошибки слишком велики; используйте «Экспорт TXT» для полного журнала",
+ actionFailed: "Не удалось выполнить действие: {message}",
+ unavailable: "Журнал ошибок недоступен: {reason}",
+ degraded: "Ошибка записи журнала ошибок: {reason}",
+ listTruncated: "Показаны первые {visible}; экспорт содержит все {total}",
+ sensitiveWarning: "Журналы ошибок содержат полные неудавшиеся операторы и исходные ошибки базы данных, которые могут включать конфиденциальные данные. Делитесь ими осторожно.",
+ openFolder: "Открыть папку журналов",
+ },
hideFailureDetails: "Скрыть детали ошибок",
failureDetailTruncated: "Детали ошибок были усечены",
failureDetailsOmitted: "Не показано дополнительных деталей ошибок: {count}",
diff --git a/apps/desktop/src/i18n/locales/tr.ts b/apps/desktop/src/i18n/locales/tr.ts
index 4efe7e7409..2ad7f3d5c1 100644
--- a/apps/desktop/src/i18n/locales/tr.ts
+++ b/apps/desktop/src/i18n/locales/tr.ts
@@ -2594,6 +2594,26 @@ export default withEnglishFallback({
statementsCount: "{done} başarılı, {failed} başarısız",
elapsed: "Geçen süre: {duration}",
showFailureDetails: "Hata ayrıntıları ({count})",
+ failureLog: {
+ selectAll: "Tümünü seç",
+ selectedCount: "{count} seçili",
+ copySelected: "Seçileni kopyala",
+ copySelectedDone: "{count} hata ayrıntısı kopyalandı",
+ copyTooLarge: "Seçim {limit} sınırını aşıyor; dosyaya aktarın",
+ copySummaryFallback: "Tam hata günlüğü kullanılamıyor; yalnızca listede görünen özetler kopyalandı",
+ exportTxt: "TXT olarak aktar",
+ exportCsv: "CSV olarak aktar",
+ exportSelected: "Seçileni aktar",
+ exportedRecords: "{count} hata ayrıntısı aktarıldı",
+ exportStarted: "Günlük indirme başladı",
+ exportSelectionTooLarge: "Seçilen hata ayrıntıları çok büyük; tam günlük için TXT olarak aktarın",
+ actionFailed: "İşlem başarısız: {message}",
+ unavailable: "Hata günlüğü kullanılamıyor: {reason}",
+ degraded: "Hata günlüğü yazma sorunu: {reason}",
+ listTruncated: "İlk {visible} kayıt gösteriliyor; aktarım {total} kaydın tamamını içerir",
+ sensitiveWarning: "Hata günlükleri başarısız ifadelerin tamamını ve veritabanının ham hatalarını içerir; hassas veriler barındırabilir. Paylaşırken dikkatli olun.",
+ openFolder: "Günlük klasörünü aç",
+ },
hideFailureDetails: "Hata ayrıntılarını gizle",
failureDetailTruncated: "Hata ayrıntıları kısaltıldı",
failureDetailsOmitted: "Gösterilmeyen {count} ek hata ayrıntısı var",
diff --git a/apps/desktop/src/i18n/locales/zh-CN.ts b/apps/desktop/src/i18n/locales/zh-CN.ts
index 39e87d15cc..6e80638810 100644
--- a/apps/desktop/src/i18n/locales/zh-CN.ts
+++ b/apps/desktop/src/i18n/locales/zh-CN.ts
@@ -3009,6 +3009,26 @@ export default withEnglishFallback({
statementsCount: "成功 {done},失败 {failed}",
elapsed: "耗时:{duration}",
showFailureDetails: "失败详情({count})",
+ failureLog: {
+ selectAll: "全选",
+ selectedCount: "已选 {count} 条",
+ copySelected: "复制选中",
+ copySelectedDone: "已复制 {count} 条失败详情",
+ copyTooLarge: "选中内容超过 {limit},请改用「导出日志」",
+ copySummaryFallback: "完整失败日志不可用,已改为复制列表中可见的摘要信息",
+ exportTxt: "导出 TXT",
+ exportCsv: "导出 CSV",
+ exportSelected: "导出选中",
+ exportedRecords: "已导出 {count} 条失败详情",
+ exportStarted: "已开始下载日志",
+ exportSelectionTooLarge: "选中的失败详情过大,请改用「导出 TXT」获取完整日志",
+ actionFailed: "操作失败:{message}",
+ unavailable: "失败日志不可用:{reason}",
+ degraded: "失败日志写入异常:{reason}",
+ listTruncated: "列表仅显示前 {visible} 条,导出包含全部 {total} 条",
+ sensitiveWarning: "失败日志包含完整的失败语句与数据库原始错误,可能含有敏感信息,请谨慎分享。",
+ openFolder: "打开日志目录",
+ },
hideFailureDetails: "收起失败详情",
failureDetailTruncated: "错误详情已截断",
failureDetailsOmitted: "另有 {count} 条失败详情未显示",
diff --git a/apps/desktop/src/i18n/locales/zh-TW.ts b/apps/desktop/src/i18n/locales/zh-TW.ts
index d8a24ab7cc..84faaf73fc 100644
--- a/apps/desktop/src/i18n/locales/zh-TW.ts
+++ b/apps/desktop/src/i18n/locales/zh-TW.ts
@@ -2862,6 +2862,26 @@ export default withEnglishFallback({
statementsCount: "{done} 成功,{failed} 失敗",
elapsed: "耗時:{duration}",
showFailureDetails: "失敗詳情({count})",
+ failureLog: {
+ selectAll: "全選",
+ selectedCount: "已選 {count} 筆",
+ copySelected: "複製選取",
+ copySelectedDone: "已複製 {count} 筆失敗詳情",
+ copyTooLarge: "選取內容超過 {limit},請改用「匯出日誌」",
+ copySummaryFallback: "完整失敗日誌無法使用,已改為複製清單中可見的摘要",
+ exportTxt: "匯出 TXT",
+ exportCsv: "匯出 CSV",
+ exportSelected: "匯出選取",
+ exportedRecords: "已匯出 {count} 筆失敗詳情",
+ exportStarted: "已開始下載日誌",
+ exportSelectionTooLarge: "選取的失敗詳情過大,請改用「匯出 TXT」取得完整日誌",
+ actionFailed: "操作失敗:{message}",
+ unavailable: "失敗日誌無法使用:{reason}",
+ degraded: "失敗日誌寫入異常:{reason}",
+ listTruncated: "清單僅顯示前 {visible} 筆,匯出包含全部 {total} 筆",
+ sensitiveWarning: "失敗日誌包含完整的失敗語句與資料庫原始錯誤,可能含有敏感資訊,請謹慎分享。",
+ openFolder: "開啟日誌目錄",
+ },
hideFailureDetails: "收合失敗詳情",
failureDetailTruncated: "錯誤詳情已截斷",
failureDetailsOmitted: "另有 {count} 條失敗詳情未顯示",
diff --git a/apps/desktop/src/lib/__tests__/export/failureLogExport.spec.ts b/apps/desktop/src/lib/__tests__/export/failureLogExport.spec.ts
new file mode 100644
index 0000000000..2edc1fe102
--- /dev/null
+++ b/apps/desktop/src/lib/__tests__/export/failureLogExport.spec.ts
@@ -0,0 +1,35 @@
+import { describe, expect, it } from "vitest";
+
+import { failureLogKeyPayload, failureLogListCoverage, failureLogRowKey, selectedFailureLogKeys } from "@/lib/export/failureLogExport";
+
+function failure(fileIndex: number | undefined, statementIndex: number, error = "boom") {
+ return { statementIndex, statementSummary: `summary ${statementIndex}`, error, fileIndex };
+}
+
+describe("failure log export helpers", () => {
+ it("keys rows stably whether or not the run had several files", () => {
+ expect(failureLogRowKey(failure(2, 644))).toBe("2:644");
+ // Single-file runs carry no fileIndex; the key must still be unique.
+ expect(failureLogRowKey(failure(undefined, 644))).toBe("-1:644");
+ });
+
+ it("maps rows to the backend key payload, defaulting the file index", () => {
+ expect(failureLogKeyPayload(failure(1, 7))).toEqual({ fileIndex: 1, statementIndex: 7 });
+ expect(failureLogKeyPayload(failure(undefined, 7))).toEqual({ fileIndex: 0, statementIndex: 7 });
+ });
+
+ it("keeps the list order when building the selection payload", () => {
+ const failures = [failure(0, 1), failure(0, 2), failure(0, 3)];
+ const selected = new Set([failureLogRowKey(failures[2]!), failureLogRowKey(failures[0]!)]);
+
+ expect(selectedFailureLogKeys(failures, selected)).toEqual([
+ { fileIndex: 0, statementIndex: 1 },
+ { fileIndex: 0, statementIndex: 3 },
+ ]);
+ });
+
+ it("reports how many failures the in-memory list hides", () => {
+ expect(failureLogListCoverage(110, 110)).toEqual({ truncated: false, hidden: 0 });
+ expect(failureLogListCoverage(1110, 500)).toEqual({ truncated: true, hidden: 610 });
+ });
+});
diff --git a/apps/desktop/src/lib/backend/api.ts b/apps/desktop/src/lib/backend/api.ts
index 79f1a203e0..df0d370ec5 100644
--- a/apps/desktop/src/lib/backend/api.ts
+++ b/apps/desktop/src/lib/backend/api.ts
@@ -469,6 +469,9 @@ export const listLocalSshKeys = forward("listLocalSshKeys");
export const previewSqlFile = forward("previewSqlFile");
export const releaseSqlFilePreview = forward("releaseSqlFilePreview");
export const executeSqlFile = forward("executeSqlFile");
+export const sqlFileFailureLogInfo = forward("sqlFileFailureLogInfo");
+export const exportSqlFileFailureLog = forward("exportSqlFileFailureLog");
+export const readSqlFileFailureLogText = forward("readSqlFileFailureLogText");
export const executeSqlFiles = forward("executeSqlFiles");
export const cancelSqlFileExecution = forward("cancelSqlFileExecution");
export const inspectSqlFileTables = forward("inspectSqlFileTables");
@@ -981,6 +984,11 @@ export type { AiConfigItem };
export type {
AppSupportInfo,
+ SqlFileFailureLogExportPayload,
+ SqlFileFailureLogInfo,
+ SqlFileFailureLogKey,
+ SqlFileFailureLogStatus,
+ SqlFileFailureLogTextPayload,
AiMessage,
AiToolCallRef,
AiCompletionRequest,
diff --git a/apps/desktop/src/lib/backend/http.ts b/apps/desktop/src/lib/backend/http.ts
index 8ce05cb774..4c9eb5917e 100644
--- a/apps/desktop/src/lib/backend/http.ts
+++ b/apps/desktop/src/lib/backend/http.ts
@@ -2,7 +2,9 @@ import type { MongoDumpFormat, MongoDumpSourceInput, MongoDumpCatalog, MongoRest
import type { MongoRestoreUpload, MongoSourceReadOptions } from "./mongodbDumpTypes";
import type { UserSkillRootSettings, UserSkillsListResult, UserSkillsReadResult } from "@/types/userSkills";
import type { DatabaseBackupCommand, DatabaseBackupBackgroundStatus } from "@/lib/backup/backgroundDatabaseBackup";
-import type { PluginUiStorageItemRef, SyncCatalogItem, SyncSelection, SyncSnapshotCatalog } from "@/lib/backend/tauri";
+import type { PluginUiStorageItemRef, SqlFileFailureLogExportPayload, SqlFileFailureLogInfo, SqlFileFailureLogKey, SqlFileFailureLogTextPayload, SyncCatalogItem, SyncSelection, SyncSnapshotCatalog } from "@/lib/backend/tauri";
+import { FAILURE_LOG_TEXT_TOO_LARGE } from "@/lib/export/failureLogExport";
+import { saveTextFile } from "@/lib/export/saveTextFile";
export function databaseBackupCommand<T = unknown>(command: DatabaseBackupCommand): Promise<T> {
return post("/api/database-backups", command);
@@ -2769,6 +2771,33 @@ export async function releaseSqlFilePreview(cleanupToken: string): Promise<void>
return post("/api/sql-file/preview/release", { cleanupToken });
}
+/**
+ * Failure log of one execution for the Web runtime (dbx#11619). The whole-log
+ * export streams from the server as an attachment; a selected subset travels as
+ * capped text because only the full log has a streaming route.
+ */
+export async function sqlFileFailureLogInfo(executionId: string): Promise<SqlFileFailureLogInfo | null> {
+ return get(`/api/sql-file/failure-log/${encodeURIComponent(executionId)}/info`);
+}
+
+export async function readSqlFileFailureLogText(executionId: string, format?: string, keys?: SqlFileFailureLogKey[]): Promise<SqlFileFailureLogTextPayload> {
+ return post("/api/sql-file/failure-log/text", { executionId, format, keys });
+}
+
+export async function exportSqlFileFailureLog(executionId: string, format: string, targetPath: string, keys?: SqlFileFailureLogKey[]): Promise<SqlFileFailureLogExportPayload> {
+ if (keys && keys.length > 0) {
+ const payload = await readSqlFileFailureLogText(executionId, format, keys);
+ if (payload.tooLarge || payload.text === undefined) throw new Error(FAILURE_LOG_TEXT_TOO_LARGE);
+ const fileName = targetPath.trim() || `sql-file-failures.${format}`;
+ await saveTextFile(payload.text, fileName, format.toUpperCase(), format);
+ return { records: keys.length, bytes: payload.text.length, format };
+ }
+ const anchor = document.createElement("a");
+ anchor.href = apiUrl(`/api/sql-file/failure-log/${encodeURIComponent(executionId)}?format=${encodeURIComponent(format)}`);
+ anchor.click();
+ return { records: 0, bytes: 0, format };
+}
+
export async function executeSqlFile(request: SqlFileRequest): Promise<void> {
return post("/api/sql-file/execute", { request });
}
diff --git a/apps/desktop/src/lib/backend/tauri.ts b/apps/desktop/src/lib/backend/tauri.ts
index ab50980054..ca6e574afd 100644
--- a/apps/desktop/src/lib/backend/tauri.ts
+++ b/apps/desktop/src/lib/backend/tauri.ts
@@ -5587,6 +5587,47 @@ export async function previewSqlFile(filePath: string): Promise<SqlFilePreview>
return invoke("preview_sql_file", { filePath });
}
+export type SqlFileFailureLogStatus = "available" | "degraded" | "unavailable";
+
+/** Health of one execution's failure log (dbx#11619). */
+export interface SqlFileFailureLogInfo {
+ status: SqlFileFailureLogStatus;
+ reason?: string;
+ /** Desktop only: the local log file, used by "open log folder". */
+ path?: string;
+}
+
+/** Stable identity of one failure row, matching what the backend logged. */
+export interface SqlFileFailureLogKey {
+ fileIndex: number;
+ statementIndex: number;
+}
+
+export interface SqlFileFailureLogTextPayload {
+ text?: string;
+ /** The derived text exceeded `limitBytes`; the caller should export to a file. */
+ tooLarge: boolean;
+ limitBytes: number;
+}
+
+export interface SqlFileFailureLogExportPayload {
+ records: number;
+ bytes: number;
+ format: string;
+}
+
+export async function sqlFileFailureLogInfo(executionId: string): Promise<SqlFileFailureLogInfo | null> {
+ return invoke("sql_file_failure_log_info", { executionId });
+}
+
+export async function exportSqlFileFailureLog(executionId: string, format: string, targetPath: string, keys?: SqlFileFailureLogKey[]): Promise<SqlFileFailureLogExportPayload> {
+ return invoke("export_sql_file_failure_log", { executionId, format, targetPath, keys });
+}
+
+export async function readSqlFileFailureLogText(executionId: string, format?: string, keys?: SqlFileFailureLogKey[]): Promise<SqlFileFailureLogTextPayload> {
+ return invoke("read_sql_file_failure_log_text", { executionId, format, keys });
+}
+
export async function executeSqlFile(request: SqlFileRequest): Promise<void> {
return invoke("execute_sql_file", { request });
}
diff --git a/apps/desktop/src/lib/export/failureLogExport.ts b/apps/desktop/src/lib/export/failureLogExport.ts
new file mode 100644
index 0000000000..071c664d4d
--- /dev/null
+++ b/apps/desktop/src/lib/export/failureLogExport.ts
@@ -0,0 +1,60 @@
+/**
+ * Shared helpers for the SQL file failure log (dbx#11619).
+ *
+ * Both the execution dialog and the background-task popover offer the same
+ * actions over the same backend log, so the parts that do not depend on either
+ * layout live here: the error code the Web transport raises when a selected
+ * subset is too large to ship as text, the JS-side view of a failure row, and
+ * the conversion to the stable keys the backend filters on.
+ */
+
+import type { SqlFileFailure } from "@/composables/useExportTracker";
+import type { SqlFileFailureLogKey } from "@/lib/backend/api";
+
+/** Raised when a selected subset exceeds the text cap and must be exported instead. */
+export const FAILURE_LOG_TEXT_TOO_LARGE = "FAILURE_LOG_TEXT_TOO_LARGE";
+
+/**
+ * Per-app-run guard for the sensitive-data notice (dbx#11619).
+ *
+ * A failure log stores the complete failing statements and the raw database
+ * errors, which can contain credentials or personal data — that is the point of
+ * the feature, and DBX deliberately does not redact them. The user is therefore
+ * warned the first time they export or copy, once per app run rather than on
+ * every action, which would only train them to dismiss it.
+ */
+export const failureLogSensitiveNotice = { shown: false };
+
+/** Returns true exactly once per app run, so the caller can warn a single time. */
+export function takeFailureLogSensitiveNotice(): boolean {
+ if (failureLogSensitiveNotice.shown) return false;
+ failureLogSensitiveNotice.shown = true;
+ return true;
+}
+
+/** Selection key of one failure row; `fileIndex` is absent for single-file runs. */
+export function failureLogRowKey(failure: Pick<SqlFileFailure, "fileIndex" | "statementIndex">): string {
+ return `${failure.fileIndex ?? -1}:${failure.statementIndex}`;
+}
+
+/** Backend key payload for a failure row. */
+export function failureLogKeyPayload(failure: Pick<SqlFileFailure, "fileIndex" | "statementIndex">): SqlFileFailureLogKey {
+ return { fileIndex: failure.fileIndex ?? 0, statementIndex: failure.statementIndex };
+}
+
+/**
+ * Keys of the rows the user selected, in the order the list shows them, so the
+ * exported subset keeps the log's original ordering.
+ */
+export function selectedFailureLogKeys(failures: SqlFileFailure[], selected: ReadonlySet<string>): SqlFileFailureLogKey[] {
+ return failures.filter((failure) => selected.has(failureLogRowKey(failure))).map(failureLogKeyPayload);
+}
+
+/**
+ * Whether the UI list is shorter than the log: the backend keeps every logged
+ * failure, while the frontend retains only the newest few hundred.
+ */
+export function failureLogListCoverage(loggedFailures: number, visibleFailures: number): { truncated: boolean; hidden: number } {
+ const hidden = Math.max(0, loggedFailures - visibleFailures);
+ return { truncated: hidden > 0, hidden };
+}
diff --git a/crates/dbx-core/src/data/sql_file_import.rs b/crates/dbx-core/src/data/sql_file_import.rs
index a7935d4004..9b05085869 100644
--- a/crates/dbx-core/src/data/sql_file_import.rs
+++ b/crates/dbx-core/src/data/sql_file_import.rs
@@ -36,6 +36,22 @@ mod table_restore;
pub use table_restore::SqlFileTable;
use table_restore::TableRestoreFilter;
+mod failure_log;
+mod failure_log_derive;
+pub use failure_log::{
+ prune_sql_file_failure_logs, prune_sql_file_failure_logs_with_limits, start_sql_file_failure_log,
+ SqlFileFailureLogFileInfo, SqlFileFailureLogHandle, SqlFileFailureLogInfo, SqlFileFailureLogLimits,
+ SqlFileFailureLogMeta, SqlFileFailureLogRegistry, SqlFileFailureLogRunCounters, SqlFileFailureLogStatus,
+ SqlFileFailureLogStatusProbe, SqlFileFailureLogSummary, SQL_FILE_FAILURE_LOG_DIR,
+ SQL_FILE_FAILURE_LOG_MAX_DIR_BYTES, SQL_FILE_FAILURE_LOG_MAX_FILES, SQL_FILE_FAILURE_LOG_MAX_STATEMENT_BYTES,
+ SQL_FILE_FAILURE_LOG_MAX_TOTAL_BYTES,
+};
+pub use failure_log_derive::{
+ export_sql_file_failure_log, sql_file_failure_log_text, SqlFileFailureLogDeriveError,
+ SqlFileFailureLogDeriveSummary, SqlFileFailureLogFormat, SqlFileFailureLogKey,
+ SQL_FILE_FAILURE_LOG_CLIPBOARD_MAX_BYTES,
+};
+
/// How the database compatibility mode used for SQL statement splitting was
/// resolved. `Option<String>` cannot express the difference between "this
/// database has no compatibility mode concept" and "the probe failed", and
@@ -922,6 +938,7 @@ async fn execute_sql_file_paths_inner(
phase: None,
file_index: Some(file_index),
file_name: Some(file_name.clone()),
+ failure_sql: None,
});
}
@@ -1078,6 +1095,7 @@ async fn execute_sql_file_paths_inner(
phase: None,
file_index: Some(file_index),
file_name: Some(file_name),
+ failure_sql: None,
});
prev_statement_index = progress.statement_index;
prev_success_count = progress.success_count;
@@ -2151,6 +2169,7 @@ pub fn sql_file_progress(
phase: None,
file_index: None,
file_name: None,
+ failure_sql: None,
}
}
@@ -2588,6 +2607,7 @@ async fn execute_statement_with_progress(
*failure_count,
*affected_rows,
&summary,
+ &statement.sql,
error,
);
@@ -2681,6 +2701,7 @@ async fn execute_merged_statement_fallback_with_progress(
*failure_count,
*affected_rows,
&summary,
+ source_sql,
error,
);
@@ -2780,6 +2801,7 @@ fn statement_error_decision(
failure_count: usize,
affected_rows: u64,
summary: &str,
+ statement_sql: &str,
error: String,
) -> StatementErrorDecision {
if token.is_cancelled() {
@@ -2801,7 +2823,7 @@ fn statement_error_decision(
}
let failure_count = failure_count + 1;
- let statement_failed = sql_file_progress(
+ let mut statement_failed = sql_file_progress(
execution_id,
SqlFileStatus::StatementFailed,
statement_index,
@@ -2812,6 +2834,9 @@ fn statement_error_decision(
summary,
Some(error.clone()),
);
+ // The full source statement rides along for the backend failure log (the
+ // field is skipped by serde, so the frontend never receives it).
+ statement_failed.failure_sql = Some(statement_sql.to_string());
if continue_on_error {
return StatementErrorDecision { progress: vec![statement_failed], failure_count, result: Ok(false) };
@@ -2873,6 +2898,7 @@ mod tests {
selected_tables: None,
part_cooldown_ms: 0,
skip_relational_constraints: false,
+ failure_log_enabled: true,
};
let mut events = Vec::new();
let error = execute_sql_file_content(
@@ -3075,6 +3101,7 @@ mod tests {
phase: None,
file_index: None,
file_name: None,
+ failure_sql: None,
}
}
@@ -3226,6 +3253,7 @@ mod tests {
selected_tables: None,
part_cooldown_ms: 0,
skip_relational_constraints: false,
+ failure_log_enabled: true,
};
let token = CancellationToken::new();
let mut events = Vec::new();
@@ -3835,6 +3863,7 @@ mod tests {
selected_tables: None,
part_cooldown_ms: 0,
skip_relational_constraints: false,
+ failure_log_enabled: true,
};
let mut progress = Vec::new();
@@ -3884,6 +3913,7 @@ mod tests {
0,
5,
"bad statement",
+ "INSERT INTO t VALUES (1)",
"syntax error".to_string(),
);
@@ -3910,6 +3940,7 @@ mod tests {
4,
9,
"slow statement",
+ "SELECT pg_sleep(10)",
"Query canceled".to_string(),
);
diff --git a/crates/dbx-core/src/data/sql_file_import/failure_log.rs b/crates/dbx-core/src/data/sql_file_import/failure_log.rs
new file mode 100644
index 0000000000..e56c7b6172
--- /dev/null
+++ b/crates/dbx-core/src/data/sql_file_import/failure_log.rs
@@ -0,0 +1,1242 @@
+//! Durable failure log for SQL file execution (dbx#11619).
+//!
+//! Every failing statement is appended to a JSONL file inside the application
+//! data directory as it happens, so the complete source statement survives even
+//! when the frontend only ever receives the 120-char `statement_summary` and
+//! keeps just the newest 500 failures in memory. TXT/CSV are derived from this
+//! file at export time instead of being written during execution.
+//!
+//! The progress emitter that feeds the frontend is a *synchronous* callback, so
+//! the sink never touches the filesystem itself: [`SqlFileFailureLogHandle`]
+//! serializes records and hands them to a writer task over a channel. That keeps
+//! a slow or failing disk out of the execution hot path — the run must never be
+//! interrupted because logging broke — and still lets an export read a snapshot
+//! mid-run, because each record is written and flushed as one complete line.
+
+use std::path::{Path, PathBuf};
+use std::sync::atomic::{AtomicU64, AtomicU8, Ordering};
+use std::sync::{Arc, Mutex};
+use std::time::Duration;
+
+use serde::Serialize;
+use tokio::io::AsyncWriteExt;
+use tokio::sync::{mpsc, oneshot};
+
+use crate::sql::{SqlFileProgress, SqlFileStatus};
+
+/// Sub-directory of the application data directory holding the logs.
+pub const SQL_FILE_FAILURE_LOG_DIR: &str = "sql-file-logs";
+/// Per-statement cap. A mysqldump extended INSERT can reach ~1MB on its own, so
+/// a total-only cap would let a single statement dominate the whole budget.
+pub const SQL_FILE_FAILURE_LOG_MAX_STATEMENT_BYTES: usize = 256 * 1024;
+/// Per-execution cap: past this the writer stops appending and the summary
+/// reports how many failures were dropped.
+pub const SQL_FILE_FAILURE_LOG_MAX_TOTAL_BYTES: u64 = 200 * 1024 * 1024;
+/// Retention: newest N executions, and never more than the byte budget below.
+pub const SQL_FILE_FAILURE_LOG_MAX_FILES: usize = 20;
+pub const SQL_FILE_FAILURE_LOG_MAX_DIR_BYTES: u64 = 500 * 1024 * 1024;
+/// Upper bound on how long `finish` waits for the writer task.
+const SQL_FILE_FAILURE_LOG_FINISH_TIMEOUT: Duration = Duration::from_secs(5);
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub enum SqlFileFailureLogStatus {
+ /// Logging works.
+ Available,
+ /// The file was created but a later write failed (e.g. the disk filled up).
+ Degraded,
+ /// No file exists at all (directory or file creation failed).
+ Unavailable,
+}
+
+impl SqlFileFailureLogStatus {
+ pub fn as_str(self) -> &'static str {
+ match self {
+ Self::Available => "available",
+ Self::Degraded => "degraded",
+ Self::Unavailable => "unavailable",
+ }
+ }
+}
+
+#[derive(Debug, Clone, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct SqlFileFailureLogFileInfo {
+ pub index: usize,
+ pub name: String,
+ pub path: String,
+ pub size_bytes: Option<u64>,
+}
+
+#[derive(Debug, Clone)]
+pub struct SqlFileFailureLogMeta {
+ pub execution_id: String,
+ pub connection_id: String,
+ pub connection_name: Option<String>,
+ pub database: String,
+ pub schema: Option<String>,
+ pub files: Vec<SqlFileFailureLogFileInfo>,
+ pub limits: SqlFileFailureLogLimits,
+}
+
+#[derive(Debug, Clone, Copy)]
+pub struct SqlFileFailureLogLimits {
+ pub max_statement_bytes: usize,
+ pub max_total_bytes: u64,
+}
+
+impl Default for SqlFileFailureLogLimits {
+ fn default() -> Self {
+ Self {
+ max_statement_bytes: SQL_FILE_FAILURE_LOG_MAX_STATEMENT_BYTES,
+ max_total_bytes: SQL_FILE_FAILURE_LOG_MAX_TOTAL_BYTES,
+ }
+ }
+}
+
+/// Execution outcome written as the last line. Counters come from the execution
+/// itself, not from the log, so a truncated log never makes them wrong.
+#[derive(Debug, Clone, Default)]
+pub struct SqlFileFailureLogSummary {
+ pub success_count: usize,
+ pub failure_count: usize,
+ pub affected_rows: u64,
+ pub elapsed_ms: u64,
+ pub cancelled: bool,
+}
+
+#[derive(Serialize)]
+#[serde(tag = "type", rename_all = "camelCase")]
+enum LogRecord<'a> {
+ #[serde(rename_all = "camelCase")]
+ Meta {
+ execution_id: &'a str,
+ connection_id: &'a str,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ connection_name: Option<&'a str>,
+ database: &'a str,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ schema: Option<&'a str>,
+ started_at_ms: i64,
+ files: &'a [SqlFileFailureLogFileInfo],
+ max_statement_bytes: usize,
+ max_total_bytes: u64,
+ },
+ #[serde(rename_all = "camelCase")]
+ Failure {
+ file_index: usize,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ file_name: Option<&'a str>,
+ file_statement_index: usize,
+ statement_index: usize,
+ sql: &'a str,
+ sql_truncated: bool,
+ error: &'a str,
+ recorded_at_ms: i64,
+ },
+ #[serde(rename_all = "camelCase")]
+ Summary {
+ success_count: usize,
+ failure_count: usize,
+ affected_rows: u64,
+ elapsed_ms: u64,
+ failed_statements_logged: u64,
+ /// Failures dropped because the total budget was exhausted.
+ omitted_failures: u64,
+ /// Records whose SQL text had to be cut at the per-statement cap.
+ truncated_statements: u64,
+ log_truncated: bool,
+ cancelled: bool,
+ log_status: &'a str,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ log_status_reason: Option<&'a str>,
+ finished_at_ms: i64,
+ },
+}
+
+enum LogCommand {
+ /// `failure` marks the lines that count as logged failures, so the summary
+ /// reports what the writer actually persisted rather than what was queued.
+ Line {
+ line: String,
+ failure: bool,
+ },
+ Flush(oneshot::Sender<()>),
+ Finish(String, oneshot::Sender<()>),
+}
+
+#[derive(Default)]
+struct LogStatusCell {
+ status: AtomicU8,
+ reason: Mutex<Option<String>>,
+}
+
+impl LogStatusCell {
+ fn new(status: SqlFileFailureLogStatus, reason: Option<String>) -> Self {
+ Self { status: AtomicU8::new(status as u8), reason: Mutex::new(reason) }
+ }
+
+ fn set(&self, status: SqlFileFailureLogStatus, reason: Option<String>) {
+ self.status.store(status as u8, Ordering::Relaxed);
+ if let Ok(mut slot) = self.reason.lock() {
+ *slot = reason;
+ }
+ }
+
+ fn get(&self) -> (SqlFileFailureLogStatus, Option<String>) {
+ let status = match self.status.load(Ordering::Relaxed) {
+ 1 => SqlFileFailureLogStatus::Degraded,
+ 2 => SqlFileFailureLogStatus::Unavailable,
+ _ => SqlFileFailureLogStatus::Available,
+ };
+ let reason = self.reason.lock().ok().and_then(|slot| slot.clone());
+ (status, reason)
+ }
+}
+
+/// Cloneable view of a log's health. The registry keeps one of these so the
+/// export/copy UI can report `degraded`/`unavailable` without holding (or being
+/// able to disturb) the writer handle owned by the running execution.
+#[derive(Clone)]
+pub struct SqlFileFailureLogStatusProbe {
+ status: Arc<LogStatusCell>,
+}
+
+impl SqlFileFailureLogStatusProbe {
+ pub fn status(&self) -> SqlFileFailureLogStatus {
+ self.status.get().0
+ }
+
+ pub fn reason(&self) -> Option<String> {
+ self.status.get().1
+ }
+}
+
+/// What the frontend needs to render the export affordances of one execution.
+#[derive(Debug, Clone, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct SqlFileFailureLogInfo {
+ pub status: SqlFileFailureLogStatus,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub reason: Option<String>,
+ /// Absent when no log file could be created.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub path: Option<String>,
+}
+
+/// Terminal counters of one run, taken from its terminal progress event.
+///
+/// The emitter throttles intermediate progress, so the totals must come from the
+/// final event — that is also what the UI shows, which keeps the log summary and
+/// the interface from disagreeing. Shared by the desktop and Web runtimes.
+#[derive(Debug, Default, Clone, Copy)]
+pub struct SqlFileFailureLogRunCounters {
+ pub success_count: usize,
+ pub failure_count: usize,
+ pub affected_rows: u64,
+ pub cancelled: bool,
+}
+
+impl SqlFileFailureLogRunCounters {
+ pub fn observe(&mut self, progress: &SqlFileProgress) {
+ match progress.status {
+ SqlFileStatus::Done | SqlFileStatus::Error | SqlFileStatus::Cancelled => {
+ self.success_count = progress.success_count;
+ self.failure_count = progress.failure_count;
+ self.affected_rows = progress.affected_rows;
+ self.cancelled = progress.status == SqlFileStatus::Cancelled;
+ }
+ _ => {}
+ }
+ }
+
+ pub fn summary(&self, elapsed_ms: u64) -> SqlFileFailureLogSummary {
+ SqlFileFailureLogSummary {
+ success_count: self.success_count,
+ failure_count: self.failure_count,
+ affected_rows: self.affected_rows,
+ elapsed_ms,
+ cancelled: self.cancelled,
+ }
+ }
+}
+
+/// Owner-scoped `executionId -> failure log` registry.
+///
+/// The desktop runtime uses a single fixed owner; the Web runtime binds the
+/// authenticated session token, so a session that guesses another session's
+/// execution id still cannot read its log.
+#[derive(Default)]
+pub struct SqlFileFailureLogRegistry {
+ entries: std::sync::RwLock<std::collections::HashMap<String, SqlFileFailureLogRegistryEntry>>,
+}
+
+struct SqlFileFailureLogRegistryEntry {
+ owner: String,
+ path: Option<PathBuf>,
+ probe: SqlFileFailureLogStatusProbe,
+ /// False while the execution is still writing this log. Retention must keep
+ /// finished logs prunable: protecting every log ever registered would let a
+ /// long-lived process (the Web server runs for days) grow without bound.
+ finished: bool,
+}
+
+impl SqlFileFailureLogRegistry {
+ pub fn new() -> Self {
+ Self::default()
+ }
+
+ pub fn register(
+ &self,
+ owner: &str,
+ execution_id: &str,
+ path: Option<PathBuf>,
+ probe: SqlFileFailureLogStatusProbe,
+ ) {
+ let Ok(mut entries) = self.entries.write() else {
+ return;
+ };
+ entries.insert(
+ execution_id.to_string(),
+ SqlFileFailureLogRegistryEntry { owner: owner.to_string(), path, probe, finished: false },
+ );
+ }
+
+ /// Returns the log info only when `owner` is the one that registered it.
+ ///
+ /// A finished log is deliberately prunable, and the user can delete the file
+ /// by hand, so the stored path is checked before it is reported: claiming
+ /// `available` for a file that is gone would keep the export controls enabled
+ /// and hand out a path that cannot be read.
+ pub fn info(&self, owner: &str, execution_id: &str) -> Option<SqlFileFailureLogInfo> {
+ let entries = self.entries.read().ok()?;
+ let entry = entries.get(execution_id)?;
+ if entry.owner != owner {
+ return None;
+ }
+ match entry.path.as_ref() {
+ Some(path) if !path.is_file() => Some(SqlFileFailureLogInfo {
+ status: SqlFileFailureLogStatus::Unavailable,
+ reason: Some("Failure log file no longer exists".to_string()),
+ path: None,
+ }),
+ Some(path) => Some(SqlFileFailureLogInfo {
+ status: entry.probe.status(),
+ reason: entry.probe.reason(),
+ path: Some(path.to_string_lossy().to_string()),
+ }),
+ None => {
+ Some(SqlFileFailureLogInfo { status: entry.probe.status(), reason: entry.probe.reason(), path: None })
+ }
+ }
+ }
+
+ pub fn path(&self, owner: &str, execution_id: &str) -> Option<PathBuf> {
+ let entries = self.entries.read().ok()?;
+ let entry = entries.get(execution_id)?;
+ (entry.owner == owner).then(|| entry.path.clone()).flatten().filter(|path| path.is_file())
+ }
+
+ pub fn remove(&self, owner: &str, execution_id: &str) {
+ let Ok(mut entries) = self.entries.write() else {
+ return;
+ };
+ if entries.get(execution_id).is_some_and(|entry| entry.owner == owner) {
+ entries.remove(execution_id);
+ }
+ }
+
+ /// Marks one execution's log as complete. The entry stays registered so the
+ /// user can still export it, but it stops being protected from retention.
+ pub fn mark_finished(&self, owner: &str, execution_id: &str) {
+ let Ok(mut entries) = self.entries.write() else {
+ return;
+ };
+ if let Some(entry) = entries.get_mut(execution_id) {
+ if entry.owner == owner {
+ entry.finished = true;
+ }
+ }
+ }
+
+ /// Paths of logs that are still being written, across all owners. Used as the
+ /// protection list when pruning so a parallel execution keeps its in-progress
+ /// log; finished logs stay prunable, which is what makes retention work.
+ pub fn active_paths(&self) -> Vec<PathBuf> {
+ let Ok(entries) = self.entries.read() else {
+ return Vec::new();
+ };
+ entries.values().filter(|entry| !entry.finished).filter_map(|entry| entry.path.clone()).collect()
+ }
+
+ /// Drops every log of one owner (e.g. when a Web session ends).
+ pub fn remove_owner(&self, owner: &str) -> Vec<PathBuf> {
+ let Ok(mut entries) = self.entries.write() else {
+ return Vec::new();
+ };
+ let removed_ids: Vec<String> =
+ entries.iter().filter(|(_, entry)| entry.owner == owner).map(|(id, _)| id.clone()).collect();
+ removed_ids.into_iter().filter_map(|id| entries.remove(&id)).filter_map(|entry| entry.path).collect()
+ }
+}
+
+/// Synchronous producer used from the progress emitter callback.
+pub struct SqlFileFailureLogHandle {
+ commands: Option<mpsc::UnboundedSender<LogCommand>>,
+ status: Arc<LogStatusCell>,
+ path: Option<PathBuf>,
+ limits: SqlFileFailureLogLimits,
+ total_bytes: u64,
+ log_truncated: bool,
+ omitted_failures: u64,
+ truncated_statements: u64,
+ /// Bumped by the writer task after a record is on disk. Counting at queue
+ /// time would let the summary claim failures that a failed write dropped.
+ persisted_failures: Arc<AtomicU64>,
+ /// Context of the file currently executing. Statement progress events carry
+ /// no file info (only file-boundary events do), so the sink tracks the last
+ /// boundary itself — the same thing the frontend does to label failures.
+ current_file_index: usize,
+ current_file_name: Option<String>,
+ /// Global statement counter when the current file started; per-file 1-based
+ /// indexes are derived from it.
+ file_start_statement_index: usize,
+ finished: bool,
+}
+
+impl SqlFileFailureLogHandle {
+ /// A handle for an execution whose log could not be created at all.
+ pub fn unavailable(reason: String) -> Self {
+ Self {
+ commands: None,
+ status: Arc::new(LogStatusCell::new(SqlFileFailureLogStatus::Unavailable, Some(reason))),
+ path: None,
+ limits: SqlFileFailureLogLimits::default(),
+ total_bytes: 0,
+ log_truncated: false,
+ omitted_failures: 0,
+ truncated_statements: 0,
+ persisted_failures: Arc::new(AtomicU64::new(0)),
+ current_file_index: 0,
+ current_file_name: None,
+ file_start_statement_index: 0,
+ finished: false,
+ }
+ }
+
+ pub fn status(&self) -> SqlFileFailureLogStatus {
+ self.status.get().0
+ }
+
+ pub fn status_reason(&self) -> Option<String> {
+ self.status.get().1
+ }
+
+ /// Shareable health probe for the registry (see [`SqlFileFailureLogStatusProbe`]).
+ pub fn status_probe(&self) -> SqlFileFailureLogStatusProbe {
+ SqlFileFailureLogStatusProbe { status: self.status.clone() }
+ }
+
+ pub fn path(&self) -> Option<&Path> {
+ self.path.as_deref()
+ }
+
+ pub fn omitted_failures(&self) -> u64 {
+ self.omitted_failures
+ }
+
+ /// Called for every progress event. File-boundary events update the file
+ /// context; `StatementFailed` events carrying the full source SQL are logged.
+ pub fn record(&mut self, progress: &SqlFileProgress) {
+ self.track_file_boundary(progress);
+
+ if progress.status != SqlFileStatus::StatementFailed {
+ return;
+ }
+ let Some(sql) = progress.failure_sql.as_deref() else {
+ return;
+ };
+
+ let (sql, sql_truncated) = truncate_utf8(sql, self.limits.max_statement_bytes);
+ if sql_truncated {
+ self.truncated_statements += 1;
+ }
+ let error = progress.error.as_deref().unwrap_or_default();
+ let file_statement_index = progress.statement_index.saturating_sub(self.file_start_statement_index);
+ let record = LogRecord::Failure {
+ file_index: self.current_file_index,
+ file_name: self.current_file_name.as_deref(),
+ file_statement_index,
+ statement_index: progress.statement_index,
+ sql,
+ sql_truncated,
+ error,
+ recorded_at_ms: now_ms(),
+ };
+ let line = match serde_json::to_string(&record) {
+ Ok(line) => line,
+ Err(_) => {
+ self.status.set(SqlFileFailureLogStatus::Degraded, Some("Serialize failure record failed".to_string()));
+ self.omitted_failures += 1;
+ return;
+ }
+ };
+
+ // The summary line is written even after the budget is exhausted: it is
+ // the only place that reports that truncation happened, so losing it
+ // would make the log silently incomplete.
+ let record_bytes = line.len() as u64 + 1;
+ if self.total_bytes + record_bytes > self.limits.max_total_bytes {
+ self.log_truncated = true;
+ self.omitted_failures += 1;
+ return;
+ }
+ self.total_bytes += record_bytes;
+ self.send(LogCommand::Line { line, failure: true });
+ }
+
+ /// Writes the summary record and waits (bounded) for the writer to flush.
+ pub async fn finish(&mut self, summary: SqlFileFailureLogSummary) {
+ if self.finished {
+ return;
+ }
+ self.finished = true;
+ let (status, reason) = self.status.get();
+
+ // Drain first: the writer owns the persisted counter, and the summary
+ // must describe records that are already on disk.
+ self.flush().await;
+ let persisted_failures = self.persisted_failures.load(Ordering::Relaxed);
+ let line = match serde_json::to_string(&LogRecord::Summary {
+ success_count: summary.success_count,
+ failure_count: summary.failure_count,
+ affected_rows: summary.affected_rows,
+ elapsed_ms: summary.elapsed_ms,
+ failed_statements_logged: persisted_failures,
+ omitted_failures: self.omitted_failures,
+ truncated_statements: self.truncated_statements,
+ log_truncated: self.log_truncated,
+ cancelled: summary.cancelled,
+ log_status: status.as_str(),
+ log_status_reason: reason.as_deref(),
+ finished_at_ms: now_ms(),
+ }) {
+ Ok(line) => line,
+ Err(_) => return,
+ };
+ let Some(commands) = self.commands.take() else {
+ return;
+ };
+ let (ack_tx, ack_rx) = oneshot::channel();
+ if commands.send(LogCommand::Finish(line, ack_tx)).is_err() {
+ return;
+ }
+ // A wedged writer must not hang the end of an execution.
+ let _ = tokio::time::timeout(SQL_FILE_FAILURE_LOG_FINISH_TIMEOUT, ack_rx).await;
+ }
+
+ /// Waits until everything queued so far has been written and flushed. Used
+ /// by the export/copy path, which reads the file while the run continues.
+ pub async fn flush(&mut self) {
+ let Some(commands) = self.commands.as_ref() else {
+ return;
+ };
+ let (ack_tx, ack_rx) = oneshot::channel();
+ if commands.send(LogCommand::Flush(ack_tx)).is_err() {
+ return;
+ }
+ let _ = tokio::time::timeout(SQL_FILE_FAILURE_LOG_FINISH_TIMEOUT, ack_rx).await;
+ }
+
+ fn track_file_boundary(&mut self, progress: &SqlFileProgress) {
+ // Only the *start-of-file* event carries the global statement counter at
+ // the file boundary. The end-of-file summary event sets `file_index` too
+ // but its `statement_index` is a per-file delta, so using it here would
+ // corrupt every later per-file index.
+ if progress.status != SqlFileStatus::Running {
+ return;
+ }
+ let Some(file_index) = progress.file_index else {
+ return;
+ };
+ self.current_file_index = file_index;
+ self.current_file_name = progress.file_name.clone();
+ self.file_start_statement_index = progress.statement_index;
+ }
+
+ fn send(&mut self, command: LogCommand) {
+ let Some(commands) = self.commands.as_ref() else {
+ return;
+ };
+ if commands.send(command).is_err() {
+ // Writer task is gone; keep reporting the degradation instead of
+ // failing the execution that is producing the failures.
+ self.status.set(SqlFileFailureLogStatus::Degraded, Some("Failure log writer stopped".to_string()));
+ }
+ }
+}
+
+/// Creates the log file, writes its `meta` line and spawns the writer task.
+/// Never fails: a log that cannot be created yields an `Unavailable` handle so
+/// the SQL file execution still runs.
+pub async fn start_sql_file_failure_log(dir: &Path, meta: SqlFileFailureLogMeta) -> SqlFileFailureLogHandle {
+ let limits = meta.limits;
+ let file_name = format!("{}-{}.jsonl", sanitize_execution_id(&meta.execution_id), timestamp_suffix());
+ let path = dir.join(file_name);
+
+ match open_log_file(dir, &path).await {
+ Ok(file) => {
+ let meta_record = LogRecord::Meta {
+ execution_id: &meta.execution_id,
+ connection_id: &meta.connection_id,
+ connection_name: meta.connection_name.as_deref(),
+ database: &meta.database,
+ schema: meta.schema.as_deref(),
+ started_at_ms: now_ms(),
+ files: &meta.files,
+ max_statement_bytes: limits.max_statement_bytes,
+ max_total_bytes: limits.max_total_bytes,
+ };
+ let meta_line = serde_json::to_string(&meta_record).unwrap_or_default();
+ let status = Arc::new(LogStatusCell::new(SqlFileFailureLogStatus::Available, None));
+ let (commands, receiver) = mpsc::unbounded_channel();
+ let persisted_failures = Arc::new(AtomicU64::new(0));
+ tokio::spawn(run_writer(file, receiver, status.clone(), persisted_failures.clone()));
+
+ let mut handle = SqlFileFailureLogHandle {
+ commands: Some(commands),
+ status,
+ path: Some(path),
+ limits,
+ total_bytes: 0,
+ log_truncated: false,
+ omitted_failures: 0,
+ truncated_statements: 0,
+ persisted_failures,
+ current_file_index: 0,
+ current_file_name: None,
+ file_start_statement_index: 0,
+ finished: false,
+ };
+ handle.total_bytes = meta_line.len() as u64 + 1;
+ // A single-file run emits no file-boundary event, so seed the context
+ // from the meta file list; multi-file runs overwrite it per file.
+ if meta.files.len() == 1 {
+ handle.current_file_name = Some(meta.files[0].name.clone());
+ }
+ handle.send(LogCommand::Line { line: meta_line, failure: false });
+ handle
+ }
+ Err(reason) => SqlFileFailureLogHandle::unavailable(reason),
+ }
+}
+
+async fn open_log_file(dir: &Path, path: &Path) -> Result<tokio::fs::File, String> {
+ create_log_dir(dir).await?;
+ let mut options = tokio::fs::OpenOptions::new();
+ options.create(true).truncate(true).write(true);
+ // POSIX: the log holds full SQL text and database errors, so keep it private.
+ // On Windows the file inherits the user-profile ACL of the data directory.
+ // `mode` is tokio's own unix-only method, so no std extension trait is needed.
+ #[cfg(unix)]
+ {
+ options.mode(0o600);
+ }
+ options.open(path).await.map_err(|error| format!("Cannot create failure log: {error}"))
+}
+
+async fn create_log_dir(dir: &Path) -> Result<(), String> {
+ if dir.is_dir() {
+ return Ok(());
+ }
+ let mut builder = tokio::fs::DirBuilder::new();
+ builder.recursive(true);
+ #[cfg(unix)]
+ {
+ builder.mode(0o700);
+ }
+ builder.create(dir).await.map_err(|error| format!("Cannot create failure log directory: {error}"))
+}
+
+async fn run_writer(
+ mut file: tokio::fs::File,
+ mut receiver: mpsc::UnboundedReceiver<LogCommand>,
+ status: Arc<LogStatusCell>,
+ persisted_failures: Arc<AtomicU64>,
+) {
+ while let Some(command) = receiver.recv().await {
+ match command {
+ LogCommand::Line { line, failure } => {
+ if write_line(&mut file, &line, &status).await && failure {
+ persisted_failures.fetch_add(1, Ordering::Relaxed);
+ }
+ }
+ LogCommand::Flush(ack) => {
+ let _ = file.flush().await;
+ let _ = ack.send(());
+ }
+ LogCommand::Finish(line, ack) => {
+ // The summary line is not a failure record; only flush it.
+ let _ = write_line(&mut file, &line, &status).await;
+ let _ = file.flush().await;
+ let _ = ack.send(());
+ break;
+ }
+ }
+ }
+}
+
+/// Writes one complete record per call and flushes it, so a concurrent reader
+/// only ever sees whole lines (plus possibly one partial tail line, which the
+/// derivation step tolerates).
+/// Returns whether the record actually reached the disk, which is what the
+/// summary may claim.
+async fn write_line(file: &mut tokio::fs::File, line: &str, status: &Arc<LogStatusCell>) -> bool {
+ let mut buffer = Vec::with_capacity(line.len() + 1);
+ buffer.extend_from_slice(line.as_bytes());
+ buffer.push(b'\n');
+ if let Err(error) = file.write_all(&buffer).await {
+ status.set(SqlFileFailureLogStatus::Degraded, Some(format!("Cannot write failure log: {error}")));
+ return false;
+ }
+ if let Err(error) = file.flush().await {
+ status.set(SqlFileFailureLogStatus::Degraded, Some(format!("Cannot flush failure log: {error}")));
+ return false;
+ }
+ true
+}
+
+/// Retention for the log directory: newest first, keep at most
+/// [`SQL_FILE_FAILURE_LOG_MAX_FILES`] files and [`SQL_FILE_FAILURE_LOG_MAX_DIR_BYTES`]
+/// in total. Every path in `protected` (the logs of executions still registered
+/// in this process) survives regardless of age, because two imports can run in
+/// parallel and the newer one must not prune the older one's in-progress log.
+pub fn prune_sql_file_failure_logs(dir: &Path, protected: &[PathBuf]) -> Result<u64, String> {
+ prune_sql_file_failure_logs_with_limits(
+ dir,
+ protected,
+ SQL_FILE_FAILURE_LOG_MAX_FILES,
+ SQL_FILE_FAILURE_LOG_MAX_DIR_BYTES,
+ )
+}
+
+/// Same policy with injectable limits, so tests can exercise the retention rules
+/// without writing hundreds of megabytes to disk.
+pub fn prune_sql_file_failure_logs_with_limits(
+ dir: &Path,
+ protected: &[PathBuf],
+ max_files: usize,
+ max_bytes: u64,
+) -> Result<u64, String> {
+ if !dir.is_dir() {
+ return Ok(0);
+ }
+ let mut entries = Vec::new();
+ let read_dir = std::fs::read_dir(dir).map_err(|error| format!("Cannot read failure log directory: {error}"))?;
+ for entry in read_dir.flatten() {
+ let path = entry.path();
+ if path.extension().and_then(|value| value.to_str()) != Some("jsonl") {
+ continue;
+ }
+ let Ok(metadata) = entry.metadata() else {
+ continue;
+ };
+ if !metadata.is_file() {
+ continue;
+ }
+ let modified = metadata.modified().unwrap_or(std::time::SystemTime::UNIX_EPOCH);
+ entries.push((path, modified, metadata.len()));
+ }
+ entries.sort_by_key(|entry| std::cmp::Reverse(entry.1));
+
+ let mut removed = 0u64;
+ let mut kept_bytes = 0u64;
+ for (index, (path, _, size)) in entries.iter().enumerate() {
+ let is_active = protected.iter().any(|entry| entry == path);
+ let within_count = index < max_files;
+ let within_bytes = kept_bytes + size <= max_bytes;
+ if is_active || (within_count && within_bytes) {
+ kept_bytes += size;
+ continue;
+ }
+ match std::fs::remove_file(path) {
+ Ok(()) => removed += 1,
+ Err(error) => log::warn!("Failed to prune SQL file failure log {}: {error}", path.display()),
+ }
+ }
+ Ok(removed)
+}
+
+fn sanitize_execution_id(execution_id: &str) -> String {
+ let sanitized: String = execution_id
+ .chars()
+ .map(|value| if value.is_ascii_alphanumeric() || matches!(value, '-' | '_') { value } else { '_' })
+ .collect();
+ let trimmed = sanitized.trim_matches('_');
+ if trimmed.is_empty() {
+ "execution".to_string()
+ } else {
+ trimmed.chars().take(80).collect()
+ }
+}
+
+fn timestamp_suffix() -> String {
+ chrono::Local::now().format("%Y%m%d%H%M%S").to_string()
+}
+
+fn now_ms() -> i64 {
+ chrono::Utc::now().timestamp_millis()
+}
+
+/// Truncates on a UTF-8 boundary so SQL with multi-byte characters stays valid.
+fn truncate_utf8(value: &str, max_bytes: usize) -> (&str, bool) {
+ if value.len() <= max_bytes {
+ return (value, false);
+ }
+ let mut end = max_bytes;
+ while end > 0 && !value.is_char_boundary(end) {
+ end -= 1;
+ }
+ (&value[..end], true)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::sql::SqlFileProgress;
+
+ fn progress(status: SqlFileStatus, statement_index: usize) -> SqlFileProgress {
+ SqlFileProgress {
+ execution_id: "exec-1".to_string(),
+ status,
+ statement_index,
+ success_count: 0,
+ failure_count: 0,
+ affected_rows: 0,
+ elapsed_ms: 0,
+ statement_summary: String::new(),
+ error: None,
+ bytes_read: None,
+ total_bytes: None,
+ phase: None,
+ file_index: None,
+ file_name: None,
+ failure_sql: None,
+ }
+ }
+
+ fn failure_progress(statement_index: usize, sql: &str, error: &str) -> SqlFileProgress {
+ let mut value = progress(SqlFileStatus::StatementFailed, statement_index);
+ value.failure_sql = Some(sql.to_string());
+ value.error = Some(error.to_string());
+ value
+ }
+
+ fn meta(files: Vec<SqlFileFailureLogFileInfo>, limits: SqlFileFailureLogLimits) -> SqlFileFailureLogMeta {
+ SqlFileFailureLogMeta {
+ execution_id: "exec-1".to_string(),
+ connection_id: "conn-1".to_string(),
+ connection_name: Some("MySQL".to_string()),
+ database: "shop".to_string(),
+ schema: None,
+ files,
+ limits,
+ }
+ }
+
+ fn file_info(index: usize, name: &str) -> SqlFileFailureLogFileInfo {
+ SqlFileFailureLogFileInfo { index, name: name.to_string(), path: format!("/tmp/{name}"), size_bytes: Some(10) }
+ }
+
+ fn lines(path: &Path) -> Vec<serde_json::Value> {
+ std::fs::read_to_string(path)
+ .unwrap()
+ .lines()
+ .filter(|line| !line.trim().is_empty())
+ .map(|line| serde_json::from_str(line).unwrap())
+ .collect()
+ }
+
+ #[tokio::test]
+ async fn failure_log_writes_meta_failure_and_summary_records() {
+ let dir = tempfile::tempdir().unwrap();
+ let log_dir = dir.path().join(SQL_FILE_FAILURE_LOG_DIR);
+ let mut handle =
+ start_sql_file_failure_log(&log_dir, meta(vec![file_info(0, "a.sql")], Default::default())).await;
+ assert_eq!(handle.status(), SqlFileFailureLogStatus::Available);
+
+ handle.record(&failure_progress(7, "INSERT INTO t VALUES (1);", "ORA-12899: value too large"));
+ handle
+ .finish(SqlFileFailureLogSummary {
+ success_count: 6,
+ failure_count: 1,
+ affected_rows: 4,
+ elapsed_ms: 120,
+ cancelled: false,
+ })
+ .await;
+
+ let path = handle.path().unwrap().to_path_buf();
+ let records = lines(&path);
+ assert_eq!(records.len(), 3);
+ assert_eq!(records[0]["type"], "meta");
+ assert_eq!(records[0]["files"][0]["name"], "a.sql");
+ assert_eq!(records[1]["type"], "failure");
+ assert_eq!(records[1]["statementIndex"], 7);
+ assert_eq!(records[1]["fileIndex"], 0);
+ assert_eq!(records[1]["fileStatementIndex"], 7);
+ assert_eq!(records[1]["sql"], "INSERT INTO t VALUES (1);");
+ assert_eq!(records[1]["error"], "ORA-12899: value too large");
+ assert_eq!(records[1]["sqlTruncated"], false);
+ assert_eq!(records[2]["type"], "summary");
+ assert_eq!(records[2]["failureCount"], 1);
+ assert_eq!(records[2]["failedStatementsLogged"], 1);
+ assert_eq!(records[2]["omittedFailures"], 0);
+ assert_eq!(records[2]["logTruncated"], false);
+ assert_eq!(records[2]["logStatus"], "available");
+ }
+
+ #[tokio::test]
+ async fn failure_log_derives_per_file_statement_indexes_from_boundary_events() {
+ let dir = tempfile::tempdir().unwrap();
+ let limits = SqlFileFailureLogLimits::default();
+ let mut handle =
+ start_sql_file_failure_log(dir.path(), meta(vec![file_info(0, "a.sql"), file_info(1, "b.sql")], limits))
+ .await;
+
+ // Second file starts after 5 statements of the first file.
+ let mut boundary = progress(SqlFileStatus::Running, 5);
+ boundary.file_index = Some(1);
+ boundary.file_name = Some("b.sql".to_string());
+ handle.record(&boundary);
+ // The end-of-file summary for "a.sql" carries a per-file delta, not a
+ // global counter; it must not become the next file's start offset.
+ let mut file_summary = progress(SqlFileStatus::StatementDone, 5);
+ file_summary.file_index = Some(0);
+ file_summary.file_name = Some("a.sql".to_string());
+ handle.record(&file_summary);
+ handle.record(&failure_progress(8, "INSERT INTO b VALUES (1);", "duplicate key"));
+ handle.finish(SqlFileFailureLogSummary::default()).await;
+
+ let records = lines(handle.path().unwrap());
+ let failure = records.iter().find(|record| record["type"] == "failure").unwrap();
+ assert_eq!(failure["fileIndex"], 1);
+ assert_eq!(failure["fileName"], "b.sql");
+ assert_eq!(failure["statementIndex"], 8);
+ assert_eq!(failure["fileStatementIndex"], 3);
+ }
+
+ #[tokio::test]
+ async fn failure_log_truncates_oversized_statement_and_reports_it() {
+ let dir = tempfile::tempdir().unwrap();
+ let limits = SqlFileFailureLogLimits { max_statement_bytes: 32, ..Default::default() };
+ let mut handle = start_sql_file_failure_log(dir.path(), meta(vec![file_info(0, "a.sql")], limits)).await;
+
+ let sql = format!("INSERT INTO t VALUES ('{}');", "中".repeat(64));
+ handle.record(&failure_progress(1, &sql, "error"));
+ handle.finish(SqlFileFailureLogSummary::default()).await;
+
+ let records = lines(handle.path().unwrap());
+ let failure = records.iter().find(|record| record["type"] == "failure").unwrap();
+ assert_eq!(failure["sqlTruncated"], true);
+ assert!(failure["sql"].as_str().unwrap().len() <= 32);
+ // Truncation must land on a char boundary, so the text stays valid UTF-8.
+ assert!(std::str::from_utf8(failure["sql"].as_str().unwrap().as_bytes()).is_ok());
+ let summary = records.iter().find(|record| record["type"] == "summary").unwrap();
+ assert_eq!(summary["truncatedStatements"], 1);
+ assert_eq!(summary["omittedFailures"], 0);
+ }
+
+ #[tokio::test]
+ async fn failure_log_stops_at_total_budget_and_counts_omitted_failures() {
+ let dir = tempfile::tempdir().unwrap();
+ // Large enough for a couple of records, then the writer must stop.
+ let limits = SqlFileFailureLogLimits { max_statement_bytes: 1024, max_total_bytes: 900 };
+ let mut handle = start_sql_file_failure_log(dir.path(), meta(vec![file_info(0, "a.sql")], limits)).await;
+
+ for index in 1..=20 {
+ handle.record(&failure_progress(index, "INSERT INTO t VALUES (1);", "boom"));
+ }
+ handle.finish(SqlFileFailureLogSummary { failure_count: 20, ..Default::default() }).await;
+
+ let records = lines(handle.path().unwrap());
+ let failures = records.iter().filter(|record| record["type"] == "failure").count();
+ let summary = records.iter().find(|record| record["type"] == "summary").unwrap();
+ assert!(failures as u64 > 0 && failures < 20, "expected partial log, got {failures} records");
+ assert_eq!(summary["logTruncated"], true);
+ assert_eq!(summary["omittedFailures"].as_u64().unwrap() as usize, 20 - failures);
+ assert_eq!(summary["failedStatementsLogged"].as_u64().unwrap() as usize, failures);
+ // The summary line is written even after the budget ran out.
+ assert_eq!(summary["failureCount"], 20);
+ }
+
+ #[tokio::test]
+ async fn failure_log_reports_unavailable_instead_of_failing_the_execution() {
+ let dir = tempfile::tempdir().unwrap();
+ // A regular file where the log directory should be makes creation fail.
+ let blocked = dir.path().join("blocked");
+ std::fs::write(&blocked, b"not a directory").unwrap();
+
+ let mut handle =
+ start_sql_file_failure_log(&blocked, meta(vec![file_info(0, "a.sql")], Default::default())).await;
+ assert_eq!(handle.status(), SqlFileFailureLogStatus::Unavailable);
+ assert!(handle.status_reason().is_some());
+ // Recording and finishing on an unavailable log must stay silent no-ops.
+ handle.record(&failure_progress(1, "SELECT 1", "boom"));
+ handle.finish(SqlFileFailureLogSummary::default()).await;
+ }
+
+ #[tokio::test]
+ async fn failure_log_is_readable_while_the_execution_continues() {
+ let dir = tempfile::tempdir().unwrap();
+ let limits = SqlFileFailureLogLimits { max_statement_bytes: 4096, max_total_bytes: 1024 * 1024 };
+ let mut handle = start_sql_file_failure_log(dir.path(), meta(vec![file_info(0, "a.sql")], limits)).await;
+
+ handle.record(&failure_progress(1, "INSERT INTO t VALUES (1);", "first"));
+ handle.flush().await;
+ let path = handle.path().unwrap().to_path_buf();
+ let snapshot = lines(&path);
+ assert_eq!(snapshot.len(), 2);
+ assert_eq!(snapshot[1]["error"], "first");
+
+ handle.record(&failure_progress(2, "INSERT INTO t VALUES (2);", "second"));
+ handle.finish(SqlFileFailureLogSummary::default()).await;
+ let final_lines = lines(&path);
+ assert_eq!(final_lines.len(), 4);
+ assert_eq!(final_lines[3]["type"], "summary");
+ }
+
+ #[test]
+ fn prune_keeps_newest_logs_within_the_file_count_limit() {
+ let dir = tempfile::tempdir().unwrap();
+ let mut created = Vec::new();
+ for index in 0..SQL_FILE_FAILURE_LOG_MAX_FILES + 3 {
+ let path = dir.path().join(format!("exec-{index}.jsonl"));
+ std::fs::write(&path, b"{}\n").unwrap();
+ created.push(path);
+ }
+ // Unrelated files must survive pruning.
+ let keep_other = dir.path().join("notes.txt");
+ std::fs::write(&keep_other, b"keep me").unwrap();
+
+ let removed =
+ prune_sql_file_failure_logs_with_limits(dir.path(), &[], SQL_FILE_FAILURE_LOG_MAX_FILES, u64::MAX).unwrap();
+ assert_eq!(removed as usize, 3);
+ let remaining: Vec<_> = created.iter().filter(|path| path.exists()).collect();
+ assert_eq!(remaining.len(), SQL_FILE_FAILURE_LOG_MAX_FILES);
+ assert!(keep_other.exists());
+ }
+
+ #[test]
+ fn prune_shrinks_the_directory_to_the_byte_budget() {
+ let dir = tempfile::tempdir().unwrap();
+ let mut created = Vec::new();
+ for index in 0..5 {
+ let path = dir.path().join(format!("byte-{index}.jsonl"));
+ std::fs::write(&path, vec![b'x'; 100]).unwrap();
+ created.push(path);
+ }
+
+ // 250 bytes fits two 100-byte logs and evicts the rest regardless of count.
+ let removed = prune_sql_file_failure_logs_with_limits(dir.path(), &[], 20, 250).unwrap();
+ assert_eq!(removed, 3);
+ assert_eq!(created.iter().filter(|path| path.exists()).count(), 2);
+ }
+
+ #[test]
+ fn prune_protects_the_active_log_and_shrinks_oversized_directories() {
+ let dir = tempfile::tempdir().unwrap();
+ let old = dir.path().join("old.jsonl");
+ let active = dir.path().join("active.jsonl");
+ std::fs::write(&old, vec![b'x'; 200]).unwrap();
+ std::fs::write(&active, vec![b'y'; 200]).unwrap();
+ // Make `old` appear older so the active log is the newest entry.
+ let now = std::time::SystemTime::now();
+ filetime_set(&active, now);
+ filetime_set(&old, now - Duration::from_secs(60));
+
+ // The byte budget on its own would evict the active log.
+ let removed =
+ prune_sql_file_failure_logs_with_limits(dir.path(), std::slice::from_ref(&active), 20, 250).unwrap();
+ assert_eq!(removed, 1);
+ assert!(active.exists(), "the log of the running execution must survive");
+ assert!(!old.exists());
+ }
+
+ #[test]
+ fn prune_protects_every_concurrently_running_log() {
+ let dir = tempfile::tempdir().unwrap();
+ let first = dir.path().join("first.jsonl");
+ let second = dir.path().join("second.jsonl");
+ let stale = dir.path().join("stale.jsonl");
+ for path in [&first, &second, &stale] {
+ std::fs::write(path, vec![b'x'; 200]).unwrap();
+ }
+ // Write order and mtime granularity are not reliable orderings, so pin
+ // the timestamps: `stale` is the oldest, `second` the newest.
+ let now = std::time::SystemTime::now();
+ filetime_set(&second, now);
+ filetime_set(&first, now - Duration::from_secs(30));
+ filetime_set(&stale, now - Duration::from_secs(60));
+ // Both imports are running: pruning for the newer one must not delete the
+ // older one's in-progress log, only the unregistered leftover.
+ let removed =
+ prune_sql_file_failure_logs_with_limits(dir.path(), &[first.clone(), second.clone()], 20, 250).unwrap();
+ assert_eq!(removed, 1);
+ assert!(first.exists() && second.exists());
+ assert!(!stale.exists());
+ }
+
+ #[test]
+ fn registry_scopes_logs_to_their_owner() {
+ let dir = tempfile::tempdir().unwrap();
+ let log_path = dir.path().join("exec-1.jsonl");
+ std::fs::write(
+ &log_path, b"{}
+",
+ )
+ .unwrap();
+ let registry = SqlFileFailureLogRegistry::new();
+ // A still-usable log is registered with a healthy probe here; the probe's
+ // own status is covered by the probe tests below.
+ let handle = SqlFileFailureLogHandle::unavailable("boom".to_string());
+ registry.register("session-a", "exec-1", Some(log_path.clone()), handle.status_probe());
+
+ let info = registry.info("session-a", "exec-1").unwrap();
+ assert_eq!(info.path.as_deref(), Some(log_path.to_string_lossy().as_ref()));
+ assert_eq!(registry.path("session-a", "exec-1"), Some(log_path.clone()));
+
+ // Another session holding the same execution id must be refused.
+ assert!(registry.info("session-b", "exec-1").is_none());
+ assert_eq!(registry.path("session-b", "exec-1"), None);
+ // ...and must not be able to delete it either.
+ registry.remove("session-b", "exec-1");
+ assert!(registry.info("session-a", "exec-1").is_some());
+ assert!(registry.info("session-a", "exec-1").unwrap().path.is_some());
+ }
+
+ #[test]
+ fn registry_stops_protecting_a_log_once_its_execution_finished() {
+ let registry = SqlFileFailureLogRegistry::new();
+ let running = SqlFileFailureLogHandle::unavailable("running".to_string());
+ let done = SqlFileFailureLogHandle::unavailable("done".to_string());
+ registry.register(
+ "desktop",
+ "exec-running",
+ Some(PathBuf::from("/logs/running.jsonl")),
+ running.status_probe(),
+ );
+ registry.register("desktop", "exec-done", Some(PathBuf::from("/logs/done.jsonl")), done.status_probe());
+
+ let mut active = registry.active_paths();
+ active.sort();
+ assert_eq!(active, vec![PathBuf::from("/logs/done.jsonl"), PathBuf::from("/logs/running.jsonl")]);
+
+ registry.mark_finished("desktop", "exec-done");
+ // Finished logs are still exportable...
+ assert!(registry.info("desktop", "exec-done").is_some());
+ // ...but no longer shield themselves from retention.
+ assert_eq!(registry.active_paths(), vec![PathBuf::from("/logs/running.jsonl")]);
+
+ // Another owner cannot mark (or unmark) someone else's log.
+ let other = SqlFileFailureLogHandle::unavailable("other".to_string());
+ registry.register("session-b", "exec-other", Some(PathBuf::from("/logs/other.jsonl")), other.status_probe());
+ registry.mark_finished("session-a", "exec-other");
+ assert!(registry.active_paths().contains(&PathBuf::from("/logs/other.jsonl")));
+ }
+
+ #[test]
+ fn finished_logs_become_prunable_with_the_registry_protection_list() {
+ let dir = tempfile::tempdir().unwrap();
+ let registry = SqlFileFailureLogRegistry::new();
+ let old_log = dir.path().join("old.jsonl");
+ let new_log = dir.path().join("new.jsonl");
+ std::fs::write(&old_log, vec![b'x'; 200]).unwrap();
+ std::fs::write(&new_log, vec![b'y'; 200]).unwrap();
+ let now = std::time::SystemTime::now();
+ filetime_set(&new_log, now);
+ filetime_set(&old_log, now - Duration::from_secs(60));
+
+ let handle = SqlFileFailureLogHandle::unavailable("finished".to_string());
+ registry.register("desktop", "exec-old", Some(old_log.clone()), handle.status_probe());
+ registry.mark_finished("desktop", "exec-old");
+
+ // The byte budget on its own would evict one of the two.
+ let removed = prune_sql_file_failure_logs_with_limits(dir.path(), &registry.active_paths(), 20, 250).unwrap();
+ assert_eq!(removed, 1);
+ assert!(!old_log.exists(), "a finished log must be reclaimable");
+ assert!(new_log.exists());
+ }
+
+ #[tokio::test]
+ async fn registry_reports_a_log_whose_file_is_gone_as_unavailable() {
+ let dir = tempfile::tempdir().unwrap();
+ let mut handle =
+ start_sql_file_failure_log(dir.path(), meta(vec![file_info(0, "a.sql")], Default::default())).await;
+ handle.record(&failure_progress(1, "SELECT 1;", "boom"));
+ // Finishing closes the writer's handle, which is what makes the file
+ // deletable on Windows.
+ handle.finish(SqlFileFailureLogSummary::default()).await;
+ let path = handle.path().unwrap().to_path_buf();
+ let registry = SqlFileFailureLogRegistry::new();
+ registry.register("desktop", "exec-1", Some(path.clone()), handle.status_probe());
+ registry.mark_finished("desktop", "exec-1");
+
+ assert_eq!(registry.info("desktop", "exec-1").unwrap().status, SqlFileFailureLogStatus::Available);
+ assert!(registry.path("desktop", "exec-1").is_some());
+
+ // Retention (or the user) deletes a finished log behind our back.
+ std::fs::remove_file(&path).unwrap();
+
+ let info = registry.info("desktop", "exec-1").unwrap();
+ assert_eq!(info.status, SqlFileFailureLogStatus::Unavailable);
+ assert_eq!(info.reason.as_deref(), Some("Failure log file no longer exists"));
+ assert!(info.path.is_none(), "a vanished file must not be offered for export");
+ assert!(registry.path("desktop", "exec-1").is_none());
+ }
+
+ #[tokio::test]
+ async fn summary_counts_only_records_the_writer_persisted() {
+ let dir = tempfile::tempdir().unwrap();
+ let mut handle =
+ start_sql_file_failure_log(dir.path(), meta(vec![file_info(0, "a.sql")], Default::default())).await;
+ for statement_index in 1..=3 {
+ handle.record(&failure_progress(statement_index, "INSERT INTO t VALUES (1);", "boom"));
+ }
+ handle.finish(SqlFileFailureLogSummary::default()).await;
+
+ let path = handle.path().unwrap().to_path_buf();
+ let contents = std::fs::read_to_string(&path).unwrap();
+ let records: Vec<serde_json::Value> = contents
+ .lines()
+ .filter(|line| !line.trim().is_empty())
+ .map(|line| serde_json::from_str(line).unwrap())
+ .collect();
+ let on_disk = records.iter().filter(|record| record["type"] == "failure").count();
+ let summary = records.iter().find(|record| record["type"] == "summary").unwrap();
+
+ // The summary must describe the file, not the queue.
+ assert_eq!(on_disk, 3);
+ assert_eq!(summary["failedStatementsLogged"].as_u64().unwrap() as usize, on_disk);
+ }
+
+ #[test]
+ fn registry_status_follows_the_live_probe_and_owner_removal_is_scoped() {
+ let registry = SqlFileFailureLogRegistry::new();
+ let unavailable = SqlFileFailureLogHandle::unavailable("first".to_string());
+ registry.register("session-a", "exec-1", None, unavailable.status_probe());
+ registry.register("session-b", "exec-2", Some(PathBuf::from("/logs/exec-2.jsonl")), unavailable.status_probe());
+
+ let removed = registry.remove_owner("session-a");
+ assert_eq!(removed.len(), 0, "the unavailable log has no path to clean up");
+ assert!(registry.info("session-a", "exec-1").is_none());
+ assert!(registry.info("session-b", "exec-2").is_some());
+ }
+
+ /// `std::fs::File::set_modified` is stable for our use, but tests only need
+ /// the ordering to be deterministic, so use the std API directly.
+ fn filetime_set(path: &Path, time: std::time::SystemTime) {
+ let file = std::fs::OpenOptions::new().write(true).open(path).unwrap();
+ file.set_modified(time).unwrap();
+ }
+}
diff --git a/crates/dbx-core/src/data/sql_file_import/failure_log_derive.rs b/crates/dbx-core/src/data/sql_file_import/failure_log_derive.rs
new file mode 100644
index 0000000000..f4a12fe0ce
--- /dev/null
+++ b/crates/dbx-core/src/data/sql_file_import/failure_log_derive.rs
@@ -0,0 +1,687 @@
+//! TXT/CSV derivation for the SQL file failure log (dbx#11619).
+//!
+//! Execution writes the log as JSONL (see [`super::failure_log`]); both the file
+//! export and the clipboard copy read it back through here, so what a user
+//! copies can never drift from what they export.
+//!
+//! Derivation is **single-pass and synchronous**: a log can reach 200MB, so it is
+//! streamed line by line straight into the destination and never buffered whole.
+//! Callers on the async runtime must run this in `spawn_blocking`.
+//!
+//! Text is deliberately English (headers, column names, footer). The backend has
+//! no locale, and these files are handed to DBAs and spreadsheets; the localized
+//! wording lives in the UI around the export button.
+
+use std::collections::HashSet;
+use std::fmt;
+use std::io::{BufRead, BufReader, Write};
+use std::path::Path;
+
+use dbx_formats::csv_export::{push_formula_guard, write_csv_text_row, CsvQuoteMode};
+use serde::Deserialize;
+
+/// Clipboard guard: pasting multiple megabytes of SQL into a clipboard is
+/// already unhelpful, so anything larger must go through the file export.
+pub const SQL_FILE_FAILURE_LOG_CLIPBOARD_MAX_BYTES: u64 = 5 * 1024 * 1024;
+
+const UTF8_BOM: &[u8] = b"\xEF\xBB\xBF";
+/// RFC4180 record separator. The shared `write_csv_text_row` helper only emits
+/// the values, so the terminator is ours (the repo's result exports use LF, but
+/// the failure log is spec'd as CRLF for spreadsheet tooling).
+const CSV_RECORD_SEPARATOR: &[u8] = b"\r\n";
+const CSV_COLUMNS: [&str; 5] = ["file", "statement_index", "error", "statement", "truncated"];
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum SqlFileFailureLogFormat {
+ Txt,
+ Csv,
+}
+
+impl SqlFileFailureLogFormat {
+ pub fn extension(self) -> &'static str {
+ match self {
+ Self::Txt => "txt",
+ Self::Csv => "csv",
+ }
+ }
+
+ /// Accepts the values used by the desktop command and the Web query string.
+ pub fn parse(value: &str) -> Option<Self> {
+ match value.trim().to_ascii_lowercase().as_str() {
+ "txt" | "log" | "text" => Some(Self::Txt),
+ "csv" => Some(Self::Csv),
+ _ => None,
+ }
+ }
+}
+
+/// Stable identity of one failure, used by "copy selected" to name a subset.
+/// `statement_index` is the global counter the UI shows (`#644`), which is what
+/// makes a selection survive a re-read of the log. Serializable because both the
+/// desktop command and the Web route receive it from the frontend.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct SqlFileFailureLogKey {
+ pub file_index: usize,
+ pub statement_index: usize,
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+pub struct SqlFileFailureLogDeriveSummary {
+ pub records: u64,
+ pub bytes: u64,
+ pub sql_truncated: u64,
+ pub omitted_failures: u64,
+ pub log_truncated: bool,
+ /// False when the run never wrote its closing summary record (cancelled, or
+ /// the app stopped): the derived text then says so rather than inventing
+ /// counters.
+ pub run_summary_present: bool,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum SqlFileFailureLogDeriveError {
+ /// The requested text exceeds the caller's cap (clipboard path).
+ TooLarge {
+ limit: u64,
+ },
+ /// A complete line in the middle of the log is not valid JSON. Exporting a
+ /// silently partial list would be worse than failing.
+ Corrupt {
+ line: usize,
+ },
+ Io(String),
+}
+
+impl fmt::Display for SqlFileFailureLogDeriveError {
+ fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ Self::TooLarge { limit } => write!(formatter, "Failure log text exceeds {limit} bytes"),
+ Self::Corrupt { line } => write!(formatter, "Failure log line {line} is not valid JSON"),
+ Self::Io(message) => write!(formatter, "Failure log read failed: {message}"),
+ }
+ }
+}
+
+pub type SqlFileFailureLogDeriveResult<T> = Result<T, SqlFileFailureLogDeriveError>;
+
+/// Streams the derived text of `jsonl_path` into `destination`.
+pub fn export_sql_file_failure_log(
+ jsonl_path: &Path,
+ destination: &Path,
+ format: SqlFileFailureLogFormat,
+ selection: Option<&HashSet<SqlFileFailureLogKey>>,
+) -> SqlFileFailureLogDeriveResult<SqlFileFailureLogDeriveSummary> {
+ let file =
+ std::fs::File::create(destination).map_err(|error| SqlFileFailureLogDeriveError::Io(error.to_string()))?;
+ let mut writer = std::io::BufWriter::new(file);
+ let summary = derive_sql_file_failure_log(jsonl_path, &mut writer, format, selection, None)?;
+ writer.flush().map_err(|error| SqlFileFailureLogDeriveError::Io(error.to_string()))?;
+ Ok(summary)
+}
+
+/// Builds the text for the clipboard, refusing anything over `max_bytes`.
+pub fn sql_file_failure_log_text(
+ jsonl_path: &Path,
+ format: SqlFileFailureLogFormat,
+ selection: Option<&HashSet<SqlFileFailureLogKey>>,
+ max_bytes: u64,
+) -> SqlFileFailureLogDeriveResult<String> {
+ let mut buffer: Vec<u8> = Vec::new();
+ derive_sql_file_failure_log(jsonl_path, &mut buffer, format, selection, Some(max_bytes))?;
+ let text = String::from_utf8(buffer).map_err(|error| SqlFileFailureLogDeriveError::Io(error.to_string()))?;
+ Ok(text)
+}
+
+#[derive(Debug, Deserialize)]
+#[serde(tag = "type")]
+enum LogLine {
+ #[serde(rename = "meta")]
+ Meta(MetaLine),
+ #[serde(rename = "failure")]
+ Failure(FailureLine),
+ #[serde(rename = "summary")]
+ Summary(SummaryLine),
+ /// Forward compatibility: a future record type must not break today's reader.
+ #[serde(other)]
+ Unknown,
+}
+
+#[derive(Debug, Deserialize)]
+#[serde(rename_all = "camelCase")]
+struct MetaLine {
+ execution_id: String,
+ connection_id: String,
+ #[serde(default)]
+ connection_name: Option<String>,
+ database: String,
+ #[serde(default)]
+ schema: Option<String>,
+ started_at_ms: i64,
+ #[serde(default)]
+ files: Vec<MetaFile>,
+ max_statement_bytes: usize,
+ max_total_bytes: u64,
+}
+
+#[derive(Debug, Deserialize)]
+#[serde(rename_all = "camelCase")]
+struct MetaFile {
+ index: usize,
+ name: String,
+ path: String,
+ #[serde(default)]
+ size_bytes: Option<u64>,
+}
+
+#[derive(Debug, Deserialize)]
+#[serde(rename_all = "camelCase")]
+struct FailureLine {
+ file_index: usize,
+ #[serde(default)]
+ file_name: Option<String>,
+ file_statement_index: usize,
+ statement_index: usize,
+ sql: String,
+ #[serde(default)]
+ sql_truncated: bool,
+ error: String,
+}
+
+#[derive(Debug, Deserialize)]
+#[serde(rename_all = "camelCase")]
+struct SummaryLine {
+ success_count: usize,
+ failure_count: usize,
+ affected_rows: u64,
+ elapsed_ms: u64,
+ failed_statements_logged: u64,
+ omitted_failures: u64,
+ truncated_statements: u64,
+ log_truncated: bool,
+ cancelled: bool,
+ log_status: String,
+ #[serde(default)]
+ log_status_reason: Option<String>,
+}
+
+/// Wraps every write so byte counting and the optional cap live in one place.
+struct CountingWriter<'a> {
+ destination: &'a mut dyn Write,
+ bytes: u64,
+ cap: Option<u64>,
+}
+
+impl CountingWriter<'_> {
+ fn write_all(&mut self, bytes: &[u8]) -> SqlFileFailureLogDeriveResult<()> {
+ if let Some(cap) = self.cap {
+ if self.bytes + bytes.len() as u64 > cap {
+ return Err(SqlFileFailureLogDeriveError::TooLarge { limit: cap });
+ }
+ }
+ self.destination.write_all(bytes).map_err(|error| SqlFileFailureLogDeriveError::Io(error.to_string()))?;
+ self.bytes += bytes.len() as u64;
+ Ok(())
+ }
+}
+
+fn derive_sql_file_failure_log(
+ jsonl_path: &Path,
+ destination: &mut dyn Write,
+ format: SqlFileFailureLogFormat,
+ selection: Option<&HashSet<SqlFileFailureLogKey>>,
+ cap: Option<u64>,
+) -> SqlFileFailureLogDeriveResult<SqlFileFailureLogDeriveSummary> {
+ let file = std::fs::File::open(jsonl_path).map_err(|error| SqlFileFailureLogDeriveError::Io(error.to_string()))?;
+ let mut reader = BufReader::new(file);
+ let mut writer = CountingWriter { destination, bytes: 0, cap };
+ let mut summary = SqlFileFailureLogDeriveSummary::default();
+
+ if format == SqlFileFailureLogFormat::Csv {
+ write_csv_header(&mut writer)?;
+ }
+
+ let mut buffer = String::new();
+ let mut line_number = 0usize;
+ loop {
+ buffer.clear();
+ let read =
+ reader.read_line(&mut buffer).map_err(|error| SqlFileFailureLogDeriveError::Io(error.to_string()))?;
+ if read == 0 {
+ break;
+ }
+ line_number += 1;
+ let text = buffer.trim_end_matches(['\n', '\r']);
+ if text.trim().is_empty() {
+ continue;
+ }
+ let parsed: LogLine = match serde_json::from_str(text) {
+ Ok(parsed) => parsed,
+ // A line with no terminator is the record the running execution is
+ // still writing; it is the only line allowed to be unparsable.
+ Err(_) if !buffer.ends_with('\n') => break,
+ Err(_) => return Err(SqlFileFailureLogDeriveError::Corrupt { line: line_number }),
+ };
+ write_log_record(&mut writer, &mut summary, parsed, format, selection)?;
+ }
+
+ if format == SqlFileFailureLogFormat::Txt && !summary.run_summary_present {
+ write_missing_run_summary_notice(&mut writer)?;
+ }
+ summary.bytes = writer.bytes;
+ Ok(summary)
+}
+
+fn write_csv_header(writer: &mut CountingWriter<'_>) -> SqlFileFailureLogDeriveResult<()> {
+ writer.write_all(UTF8_BOM)?;
+ let mut row = Vec::new();
+ write_csv_text_row(&mut row, CSV_COLUMNS.map(str::to_string), CsvQuoteMode::Necessary)
+ .map_err(SqlFileFailureLogDeriveError::Io)?;
+ writer.write_all(&row)?;
+ writer.write_all(CSV_RECORD_SEPARATOR)
+}
+
+fn write_missing_run_summary_notice(writer: &mut CountingWriter<'_>) -> SqlFileFailureLogDeriveResult<()> {
+ writer.write_all(
+ b"\n---\nRun summary: not recorded (the execution was cancelled or DBX stopped before it finished).\n",
+ )
+}
+
+/// Applies one parsed log record: `meta` prints the TXT header, `failure` prints a
+/// record (unless the caller selected a subset), `summary` closes the TXT footer.
+fn write_log_record(
+ writer: &mut CountingWriter<'_>,
+ summary: &mut SqlFileFailureLogDeriveSummary,
+ record: LogLine,
+ format: SqlFileFailureLogFormat,
+ selection: Option<&HashSet<SqlFileFailureLogKey>>,
+) -> SqlFileFailureLogDeriveResult<()> {
+ match record {
+ LogLine::Meta(meta) => {
+ if format == SqlFileFailureLogFormat::Txt {
+ write_txt_header(writer, &meta)?;
+ }
+ }
+ LogLine::Failure(failure) => {
+ if let Some(selection) = selection {
+ let key =
+ SqlFileFailureLogKey { file_index: failure.file_index, statement_index: failure.statement_index };
+ if !selection.contains(&key) {
+ return Ok(());
+ }
+ }
+ if failure.sql_truncated {
+ summary.sql_truncated += 1;
+ }
+ summary.records += 1;
+ match format {
+ SqlFileFailureLogFormat::Txt => write_txt_failure(writer, &failure)?,
+ SqlFileFailureLogFormat::Csv => write_csv_failure(writer, &failure)?,
+ }
+ }
+ LogLine::Summary(run) => {
+ summary.run_summary_present = true;
+ summary.omitted_failures = run.omitted_failures;
+ summary.log_truncated = run.log_truncated;
+ if format == SqlFileFailureLogFormat::Txt {
+ write_txt_footer(writer, &run)?;
+ }
+ }
+ LogLine::Unknown => {}
+ }
+ Ok(())
+}
+
+fn write_txt_header(writer: &mut CountingWriter<'_>, meta: &MetaLine) -> SqlFileFailureLogDeriveResult<()> {
+ let mut text = String::new();
+ text.push_str("DBX SQL file execution failure log\n");
+ text.push_str(&format!("execution: {}\n", meta.execution_id));
+ match meta.connection_name.as_deref() {
+ Some(name) => text.push_str(&format!("connection: {name} ({})\n", meta.connection_id)),
+ None => text.push_str(&format!("connection: {}\n", meta.connection_id)),
+ }
+ match meta.schema.as_deref() {
+ Some(schema) => text.push_str(&format!("database: {} / schema: {schema}\n", meta.database)),
+ None => text.push_str(&format!("database: {}\n", meta.database)),
+ }
+ text.push_str(&format!("started: {}\n", format_timestamp(meta.started_at_ms)));
+ text.push_str(&format!(
+ "limits: statement {} bytes, log {} bytes\n",
+ meta.max_statement_bytes, meta.max_total_bytes
+ ));
+ text.push_str("files:\n");
+ for file in &meta.files {
+ match file.size_bytes {
+ Some(size) => text.push_str(&format!(" [{}] {} {} {} bytes\n", file.index, file.name, file.path, size)),
+ None => text.push_str(&format!(" [{}] {} {}\n", file.index, file.name, file.path)),
+ }
+ }
+ writer.write_all(text.as_bytes())
+}
+
+fn write_txt_failure(writer: &mut CountingWriter<'_>, failure: &FailureLine) -> SqlFileFailureLogDeriveResult<()> {
+ let mut text = String::new();
+ text.push_str("\n---\n");
+ match failure.file_name.as_deref() {
+ Some(name) => text.push_str(&format!(
+ "#{} {} (statement #{} in file)\n",
+ failure.statement_index, name, failure.file_statement_index
+ )),
+ None => text.push_str(&format!(
+ "#{} (statement #{} in file {})\n",
+ failure.statement_index, failure.file_statement_index, failure.file_index
+ )),
+ }
+ text.push_str(&failure.sql);
+ if !failure.sql.ends_with('\n') {
+ text.push('\n');
+ }
+ text.push_str(&format!("error: {}\n", failure.error));
+ if failure.sql_truncated {
+ text.push_str("[statement text truncated at the per-statement limit]\n");
+ }
+ writer.write_all(text.as_bytes())
+}
+
+fn write_csv_failure(writer: &mut CountingWriter<'_>, failure: &FailureLine) -> SqlFileFailureLogDeriveResult<()> {
+ let file = failure.file_name.clone().unwrap_or_else(|| format!("file[{}]", failure.file_index));
+ let values = [
+ file,
+ failure.statement_index.to_string(),
+ neutralize_csv_formula_text(&failure.error),
+ neutralize_csv_formula_text(&failure.sql),
+ if failure.sql_truncated { "yes" } else { "no" }.to_string(),
+ ];
+ // Buffered per row: the shared helper quotes/escapes, while counting and the
+ // clipboard cap stay in `CountingWriter`.
+ let mut row = Vec::new();
+ write_csv_text_row(&mut row, values, CsvQuoteMode::Necessary).map_err(SqlFileFailureLogDeriveError::Io)?;
+ writer.write_all(&row)?;
+ writer.write_all(CSV_RECORD_SEPARATOR)
+}
+
+/// The error and statement columns come from the imported dump file and the
+/// server's failure text; the CSV targets spreadsheet tooling, so both get the
+/// same formula neutralization as the result CSV export.
+fn neutralize_csv_formula_text(value: &str) -> String {
+ let mut guarded = String::new();
+ push_formula_guard(&mut guarded, value);
+ guarded.push_str(value);
+ guarded
+}
+
+fn write_txt_footer(writer: &mut CountingWriter<'_>, run: &SummaryLine) -> SqlFileFailureLogDeriveResult<()> {
+ let mut text = String::new();
+ text.push_str("\n---\n");
+ text.push_str(&format!(
+ "Run summary: {} succeeded, {} failed, {} rows affected, {} ms\n",
+ run.success_count, run.failure_count, run.affected_rows, run.elapsed_ms
+ ));
+ text.push_str(&format!("failures logged: {}\n", run.failed_statements_logged));
+ if run.omitted_failures > 0 || run.truncated_statements > 0 || run.log_truncated {
+ text.push_str(&format!(
+ "truncation: {} statement(s) truncated, {} failure(s) not logged, log truncated: {}\n",
+ run.truncated_statements,
+ run.omitted_failures,
+ if run.log_truncated { "yes" } else { "no" }
+ ));
+ }
+ text.push_str(&format!(
+ "log status: {}{}\n",
+ run.log_status,
+ run.log_status_reason.as_deref().map(|reason| format!(" ({reason})")).unwrap_or_default()
+ ));
+ if run.cancelled {
+ text.push_str("the execution was cancelled\n");
+ }
+ writer.write_all(text.as_bytes())
+}
+
+fn format_timestamp(timestamp_ms: i64) -> String {
+ match chrono::DateTime::from_timestamp_millis(timestamp_ms) {
+ Some(value) => value.format("%Y-%m-%d %H:%M:%S UTC").to_string(),
+ None => timestamp_ms.to_string(),
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn write_log(dir: &Path, body: &str) -> std::path::PathBuf {
+ let path = dir.join("exec-1.jsonl");
+ std::fs::write(&path, body).unwrap();
+ path
+ }
+
+ fn meta_line() -> String {
+ serde_json::json!({
+ "type": "meta",
+ "executionId": "exec-1",
+ "connectionId": "conn-1",
+ "connectionName": "MySQL",
+ "database": "shop",
+ "startedAtMs": 1_760_000_000_000_i64,
+ "files": [{ "index": 0, "name": "a.sql", "path": "/tmp/a.sql", "sizeBytes": 10 }],
+ "maxStatementBytes": 262_144,
+ "maxTotalBytes": 209_715_200_u64
+ })
+ .to_string()
+ }
+
+ fn failure_line(statement_index: usize, sql: &str, error: &str) -> String {
+ serde_json::json!({
+ "type": "failure",
+ "fileIndex": 0,
+ "fileName": "a.sql",
+ "fileStatementIndex": statement_index,
+ "statementIndex": statement_index,
+ "sql": sql,
+ "sqlTruncated": false,
+ "error": error,
+ "recordedAtMs": 1_760_000_000_100_i64
+ })
+ .to_string()
+ }
+
+ fn summary_line(omitted: u64, truncated: u64, log_truncated: bool) -> String {
+ serde_json::json!({
+ "type": "summary",
+ "successCount": 6,
+ "failureCount": 2,
+ "affectedRows": 4,
+ "elapsedMs": 1200,
+ "failedStatementsLogged": 2,
+ "omittedFailures": omitted,
+ "truncatedStatements": truncated,
+ "logTruncated": log_truncated,
+ "cancelled": false,
+ "logStatus": "available",
+ "finishedAtMs": 1_760_000_001_200_i64
+ })
+ .to_string()
+ }
+
+ fn derive_text(path: &Path, format: SqlFileFailureLogFormat) -> String {
+ sql_file_failure_log_text(path, format, None, SQL_FILE_FAILURE_LOG_CLIPBOARD_MAX_BYTES).unwrap()
+ }
+
+ #[test]
+ fn txt_export_renders_meta_failures_and_run_summary() {
+ let dir = tempfile::tempdir().unwrap();
+ let log = write_log(
+ dir.path(),
+ &format!(
+ "{}\n{}\n{}\n{}\n",
+ meta_line(),
+ failure_line(644, "INSERT INTO t VALUES (1);", "ORA-12899: value too large"),
+ failure_line(645, "INSERT INTO t VALUES (2);", "ORA-12899: value too large"),
+ summary_line(0, 0, false)
+ ),
+ );
+
+ let text = derive_text(&log, SqlFileFailureLogFormat::Txt);
+
+ assert!(text.contains("DBX SQL file execution failure log"));
+ assert!(text.contains("connection: MySQL (conn-1)"));
+ assert!(text.contains("database: shop"));
+ assert!(text.contains("[0] a.sql /tmp/a.sql 10 bytes"));
+ assert!(text.contains("#644 a.sql (statement #644 in file)"));
+ assert!(text.contains("INSERT INTO t VALUES (1);"));
+ assert!(text.contains("error: ORA-12899: value too large"));
+ assert!(text.contains("Run summary: 6 succeeded, 2 failed, 4 rows affected, 1200 ms"));
+ assert!(!text.contains("truncation:"));
+ }
+
+ #[test]
+ fn csv_export_writes_bom_crlf_and_rfc4180_quoting() {
+ let dir = tempfile::tempdir().unwrap();
+ let sql = "INSERT INTO t (a, b) VALUES ('x, y', \"quoted\");\nSELECT '中文';";
+ let log = write_log(
+ dir.path(),
+ &format!("{}\n{}\n{}\n", meta_line(), failure_line(7, sql, "duplicate key"), summary_line(0, 0, false)),
+ );
+
+ let text = derive_text(&log, SqlFileFailureLogFormat::Csv);
+
+ assert!(text.starts_with("\u{feff}file,statement_index,error,statement,truncated\r\n"));
+ assert!(text.ends_with("\r\n"), "every CSV record must end with CRLF");
+ // Comma, quote and newline force quoting; the embedded quote is doubled
+ // and the SQL text itself stays byte-for-byte what the user wrote.
+ assert!(text.contains("\"INSERT INTO t (a, b) VALUES ('x, y', \"\"quoted\"\");\nSELECT '中文';\""));
+ // The meta and summary records are not spreadsheet rows.
+ assert!(!text.contains("executionId"));
+ }
+
+ #[test]
+ fn csv_export_neutralizes_formula_text_from_the_dump() {
+ let dir = tempfile::tempdir().unwrap();
+ // Both free-text columns come from untrusted dump content; spreadsheet
+ // tooling must never re-evaluate them as formulas on open.
+ let log = write_log(
+ dir.path(),
+ &format!(
+ "{}\n{}\n{}\n",
+ meta_line(),
+ failure_line(3, "=WEBSERVICE(\"http://evil\")", "=SUM(1)+cmd|' /C calc'!A0"),
+ summary_line(0, 1, true)
+ ),
+ );
+
+ let text = derive_text(&log, SqlFileFailureLogFormat::Csv);
+
+ assert!(text.contains("'=WEBSERVICE"), "statement column must carry the formula guard");
+ assert!(text.contains("'=SUM(1)+cmd"), "error column must carry the formula guard");
+ }
+
+ #[test]
+ fn selection_filters_records_and_keeps_their_order() {
+ let dir = tempfile::tempdir().unwrap();
+ let log = write_log(
+ dir.path(),
+ &format!(
+ "{}\n{}\n{}\n{}\n",
+ meta_line(),
+ failure_line(1, "SELECT 1;", "e1"),
+ failure_line(2, "SELECT 2;", "e2"),
+ failure_line(3, "SELECT 3;", "e3")
+ ),
+ );
+ let selection: HashSet<SqlFileFailureLogKey> = [
+ SqlFileFailureLogKey { file_index: 0, statement_index: 3 },
+ SqlFileFailureLogKey { file_index: 0, statement_index: 1 },
+ ]
+ .into_iter()
+ .collect();
+
+ let text = sql_file_failure_log_text(&log, SqlFileFailureLogFormat::Txt, Some(&selection), u64::MAX).unwrap();
+
+ assert!(text.contains("SELECT 1;") && text.contains("SELECT 3;"));
+ assert!(!text.contains("SELECT 2;"));
+ assert!(text.find("SELECT 1;").unwrap() < text.find("SELECT 3;").unwrap());
+ }
+
+ #[test]
+ fn a_partially_written_trailing_record_is_ignored() {
+ let dir = tempfile::tempdir().unwrap();
+ let log = write_log(
+ dir.path(),
+ &format!("{}\n{}\n{}", meta_line(), failure_line(1, "SELECT 1;", "e1"), "{\"type\":\"fail"),
+ );
+
+ let text = derive_text(&log, SqlFileFailureLogFormat::Txt);
+
+ assert!(text.contains("SELECT 1;"));
+ // The half-written record must not leak into the export in any form.
+ assert!(!text.contains("{\"type\":\"fail"));
+ }
+
+ #[test]
+ fn a_corrupt_complete_line_is_reported_instead_of_silently_dropped() {
+ let dir = tempfile::tempdir().unwrap();
+ let log = write_log(
+ dir.path(),
+ &format!(
+ "{}\n{}\nnot json\n{}\n",
+ meta_line(),
+ failure_line(1, "SELECT 1;", "e1"),
+ summary_line(0, 0, false)
+ ),
+ );
+
+ let error = sql_file_failure_log_text(&log, SqlFileFailureLogFormat::Txt, None, u64::MAX).unwrap_err();
+ assert_eq!(error, SqlFileFailureLogDeriveError::Corrupt { line: 3 });
+ }
+
+ #[test]
+ fn a_missing_run_summary_is_stated_instead_of_invented() {
+ let dir = tempfile::tempdir().unwrap();
+ // Cancelled execution: the log stops after the failures.
+ let log = write_log(dir.path(), &format!("{}\n{}\n", meta_line(), failure_line(1, "SELECT 1;", "e1")));
+
+ let text = derive_text(&log, SqlFileFailureLogFormat::Txt);
+
+ assert!(text.contains("Run summary: not recorded"));
+ assert!(!text.contains("Run summary: 0 succeeded"));
+ }
+
+ #[test]
+ fn truncation_and_omissions_are_reported() {
+ let dir = tempfile::tempdir().unwrap();
+ let log = write_log(
+ dir.path(),
+ &format!("{}\n{}\n{}\n", meta_line(), failure_line(1, "SELECT 1;", "e1"), summary_line(9, 2, true)),
+ );
+
+ let text = derive_text(&log, SqlFileFailureLogFormat::Txt);
+
+ assert!(text.contains("truncation: 2 statement(s) truncated, 9 failure(s) not logged, log truncated: yes"));
+ }
+
+ #[test]
+ fn clipboard_text_is_refused_over_the_byte_cap() {
+ let dir = tempfile::tempdir().unwrap();
+ let log =
+ write_log(dir.path(), &format!("{}\n{}\n", meta_line(), failure_line(1, &"SELECT 1;".repeat(200), "e1")));
+
+ let error = sql_file_failure_log_text(&log, SqlFileFailureLogFormat::Txt, None, 256).unwrap_err();
+ assert_eq!(error, SqlFileFailureLogDeriveError::TooLarge { limit: 256 });
+ }
+
+ #[test]
+ fn file_export_streams_to_the_destination_and_reports_counts() {
+ let dir = tempfile::tempdir().unwrap();
+ let log = write_log(
+ dir.path(),
+ &format!("{}\n{}\n{}\n", meta_line(), failure_line(1, "SELECT 1;", "e1"), summary_line(0, 0, false)),
+ );
+ let destination = dir.path().join("export.csv");
+
+ let summary = export_sql_file_failure_log(&log, &destination, SqlFileFailureLogFormat::Csv, None).unwrap();
+
+ assert_eq!(summary.records, 1);
+ assert!(summary.run_summary_present);
+ assert_eq!(summary.bytes, std::fs::metadata(&destination).unwrap().len());
+ assert!(std::fs::read_to_string(&destination).unwrap().contains("SELECT 1;"));
+ }
+}
diff --git a/crates/dbx-core/src/query/mod.rs b/crates/dbx-core/src/query/mod.rs
index f8b5ecc355..cfab980976 100644
--- a/crates/dbx-core/src/query/mod.rs
+++ b/crates/dbx-core/src/query/mod.rs
@@ -11812,6 +11812,7 @@ for line in sys.stdin:
selected_tables: None,
part_cooldown_ms: 0,
skip_relational_constraints: false,
+ failure_log_enabled: true,
}
}
diff --git a/crates/dbx-core/tests/live_mysql57.rs b/crates/dbx-core/tests/live_mysql57.rs
index a4f161eb0a..74c933889f 100644
--- a/crates/dbx-core/tests/live_mysql57.rs
+++ b/crates/dbx-core/tests/live_mysql57.rs
@@ -1376,6 +1376,7 @@ INSERT INTO install_check (id) VALUES (1), (2);
selected_tables: None,
part_cooldown_ms: 0,
skip_relational_constraints: false,
+ failure_log_enabled: true,
};
let _ = execute_sql_statement(
@@ -1466,6 +1467,7 @@ INSERT INTO children (parent_id) VALUES (LAST_INSERT_ID());
selected_tables: None,
part_cooldown_ms: 0,
skip_relational_constraints: false,
+ failure_log_enabled: true,
};
let _ = execute_sql_statement(
@@ -1540,6 +1542,7 @@ async fn live_sql_file_import_preserves_raw_mysql_binary_literal_bytes() {
selected_tables: None,
part_cooldown_ms: 0,
skip_relational_constraints: false,
+ failure_log_enabled: true,
};
tokio::fs::write(&request.file_path, script).await.unwrap();
diff --git a/crates/dbx-core/tests/live_mysql_database_export.rs b/crates/dbx-core/tests/live_mysql_database_export.rs
index 5e478fc528..ff5587a5e7 100644
--- a/crates/dbx-core/tests/live_mysql_database_export.rs
+++ b/crates/dbx-core/tests/live_mysql_database_export.rs
@@ -78,6 +78,7 @@ async fn live_mysql_selected_table_restore_preserves_unselected_tables() {
selected_tables: Some(vec![SqlFileTable { database: Some(database.clone()), name: "chosen".into() }]),
part_cooldown_ms: 0,
skip_relational_constraints: false,
+ failure_log_enabled: true,
};
execute_sql_file_path(&state, &request, &path, CancellationToken::new(), std::time::Instant::now(), |_| {}).await?;
let rows = execute_sql_statement(&state, &connection_id, &database, "SELECT id, LENGTH(body) FROM chosen", None, None).await?;
@@ -95,6 +96,7 @@ async fn live_mysql_selected_table_restore_preserves_unselected_tables() {
selected_tables: Some(vec![SqlFileTable { database: None, name: "chosen".into() }]),
part_cooldown_ms: 0,
skip_relational_constraints: false,
+ failure_log_enabled: true,
};
let mut events = Vec::new();
assert!(execute_sql_file_path(&state, &request, &path, CancellationToken::new(), std::time::Instant::now(), |event| events.push(event)).await.is_err());
@@ -183,6 +185,7 @@ async fn live_mysql_database_export_restores_dependent_views() {
selected_tables: None,
part_cooldown_ms: 0,
skip_relational_constraints: false,
+ failure_log_enabled: true,
};
execute_sql_file_path(
&state,
diff --git a/crates/dbx-core/tests/live_oceanbase_sql_file_transaction.rs b/crates/dbx-core/tests/live_oceanbase_sql_file_transaction.rs
index 8d5e2ba66c..a6b0240d03 100644
--- a/crates/dbx-core/tests/live_oceanbase_sql_file_transaction.rs
+++ b/crates/dbx-core/tests/live_oceanbase_sql_file_transaction.rs
@@ -67,6 +67,7 @@ async fn live_oceanbase_sql_files_commit_rollback_failure_and_cancel() {
execution_id: "live-manual-file".to_string(), connection_id: "file-writer".to_string(),
database: database.clone(), schema: None, file_path: String::new(), continue_on_error: false,
selected_tables: None, part_cooldown_ms: 0, skip_relational_constraints: false, txn_session_id: None,
+ failure_log_enabled: true,
};
let first = directory.path().join("first.sql");
let second = directory.path().join("second.sql");
diff --git a/crates/dbx-core/tests/live_postgres_all_schema_export.rs b/crates/dbx-core/tests/live_postgres_all_schema_export.rs
index b319cf8d0d..e6a7253989 100644
--- a/crates/dbx-core/tests/live_postgres_all_schema_export.rs
+++ b/crates/dbx-core/tests/live_postgres_all_schema_export.rs
@@ -193,6 +193,7 @@ async fn live_postgres_all_schema_export_restores_one_sql_file() {
selected_tables: None,
part_cooldown_ms: 0,
skip_relational_constraints: false,
+ failure_log_enabled: true,
},
&export_path,
CancellationToken::new(),
@@ -339,6 +340,7 @@ async fn live_postgres_all_schema_export_restores_one_sql_file() {
selected_tables: None,
part_cooldown_ms: 0,
skip_relational_constraints: false,
+ failure_log_enabled: true,
},
&path,
CancellationToken::new(),
diff --git a/crates/dbx-core/tests/live_postgres_export_deferred_foreign_keys.rs b/crates/dbx-core/tests/live_postgres_export_deferred_foreign_keys.rs
index e8ecca2c25..77b633247b 100644
--- a/crates/dbx-core/tests/live_postgres_export_deferred_foreign_keys.rs
+++ b/crates/dbx-core/tests/live_postgres_export_deferred_foreign_keys.rs
@@ -188,6 +188,7 @@ async fn live_postgres_export_with_foreign_key_cycle_restores_into_empty_databas
selected_tables: None,
part_cooldown_ms: 0,
skip_relational_constraints: false,
+ failure_log_enabled: true,
},
&export_path,
tokio_util::sync::CancellationToken::new(),
diff --git a/crates/dbx-core/tests/live_sqlserver_completion.rs b/crates/dbx-core/tests/live_sqlserver_completion.rs
index fca2202a71..cb3148001c 100644
--- a/crates/dbx-core/tests/live_sqlserver_completion.rs
+++ b/crates/dbx-core/tests/live_sqlserver_completion.rs
@@ -1319,6 +1319,7 @@ async fn live_sqlserver_sql_file_import_executes_go_batches() {
selected_tables: None,
part_cooldown_ms: 0,
skip_relational_constraints: false,
+ failure_log_enabled: true,
};
let done_seen = AtomicBool::new(false);
diff --git a/crates/dbx-core/tests/live_sqlserver_split_zip_e2e.rs b/crates/dbx-core/tests/live_sqlserver_split_zip_e2e.rs
index 58d663f330..98693a9d43 100644
--- a/crates/dbx-core/tests/live_sqlserver_split_zip_e2e.rs
+++ b/crates/dbx-core/tests/live_sqlserver_split_zip_e2e.rs
@@ -226,6 +226,7 @@ async fn manual_e2e_sqlserver_default_split_zip_export_then_import_skip_relation
// constraint bypass runs `ALTER TABLE ... NOCHECK CONSTRAINT ALL`
// before the import and `WITH NOCHECK CHECK CONSTRAINT ALL` after.
skip_relational_constraints: true,
+ failure_log_enabled: true,
};
let file_path_refs: Vec<&std::path::Path> = extracted_paths.iter().map(|path| path.as_path()).collect();
diff --git a/crates/dbx-sql-core/src/sql.rs b/crates/dbx-sql-core/src/sql.rs
index 41d2574b72..d0d80ae29d 100644
--- a/crates/dbx-sql-core/src/sql.rs
+++ b/crates/dbx-sql-core/src/sql.rs
@@ -82,6 +82,15 @@ pub struct SqlFileRequest {
/// and (on PostgreSQL) other trigger-enforced constraints.
#[serde(default)]
pub skip_relational_constraints: bool,
+ /// Whether the backend writes a JSONL failure log for this execution
+ /// (dbx#11619). Absent in requests from older frontends, which must keep
+ /// logging enabled, so the serde default is `true`.
+ #[serde(default = "sql_file_failure_log_enabled_by_default")]
+ pub failure_log_enabled: bool,
+}
+
+fn sql_file_failure_log_enabled_by_default() -> bool {
+ true
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -430,6 +439,13 @@ pub struct SqlFileProgress {
/// When processing multiple files, the name of the current file.
#[serde(skip_serializing_if = "Option::is_none")]
pub file_name: Option<String>,
+ /// Full source SQL behind a `StatementFailed` event, used by the backend to
+ /// write the failure log. It is deliberately never serialized: the frontend
+ /// only ever needs the 120-char summary, and shipping every failing statement
+ /// over IPC would bloat the progress stream it already throttles.
+ #[serde(skip)]
+ #[serde(default)]
+ pub failure_sql: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
@@ -3477,6 +3493,26 @@ mod tests {
SqlFileRequest, SqlFileStatementAction, SqlParsingOptions, SqlStatementSplitter,
};
+ #[test]
+ fn sql_file_request_keeps_failure_logging_on_when_the_field_is_absent() {
+ // Older frontends never send the flag; their imports must keep writing
+ // the failure log (dbx#11619), so the serde default is "enabled".
+ let base = serde_json::json!({
+ "executionId": "exec-1",
+ "connectionId": "conn-1",
+ "database": "shop",
+ "filePath": "/tmp/dump.sql",
+ "continueOnError": false
+ });
+ let legacy: SqlFileRequest = serde_json::from_value(base.clone()).unwrap();
+ assert!(legacy.failure_log_enabled);
+
+ let mut explicit_off = base;
+ explicit_off["failureLogEnabled"] = serde_json::Value::Bool(false);
+ let disabled: SqlFileRequest = serde_json::from_value(explicit_off).unwrap();
+ assert!(!disabled.failure_log_enabled);
+ }
+
#[test]
fn sql_file_request_schema_is_optional_and_round_trips_when_present() {
let legacy = serde_json::json!({
diff --git a/crates/dbx-web/src/main.rs b/crates/dbx-web/src/main.rs
index 8e3d15c410..0b436a9215 100644
--- a/crates/dbx-web/src/main.rs
+++ b/crates/dbx-web/src/main.rs
@@ -570,6 +570,7 @@ async fn serve() -> Result<(), String> {
transfer_progress_channels: RwLock::new(HashMap::new()),
table_import_channels: RwLock::new(HashMap::new()),
sql_file_executions: RwLock::new(HashMap::new()),
+ sql_file_failure_logs: Default::default(),
managed_sql_previews: Default::default(),
nacos_imports: RwLock::new(HashMap::new()),
login_rate_limit: tokio::sync::Mutex::new(state::LoginRateLimit { fail_count: 0, locked_until: None }),
@@ -1362,6 +1363,9 @@ async fn serve() -> Result<(), String> {
.route("/sql-file/tables", post(routes::sql_file::inspect_sql_file_tables))
.route("/sql-file/progress/{executionId}", get(routes::sql_file::sql_file_progress))
.route("/sql-file/cancel", post(routes::sql_file::cancel_sql_file))
+ .route("/sql-file/failure-log/text", post(routes::sql_file::sql_file_failure_log_text))
+ .route("/sql-file/failure-log/{executionId}", get(routes::sql_file::sql_file_failure_log_download))
+ .route("/sql-file/failure-log/{executionId}/info", get(routes::sql_file::sql_file_failure_log_info))
// Table import
.route(
"/import/preview",
diff --git a/crates/dbx-web/src/routes/mq.rs b/crates/dbx-web/src/routes/mq.rs
index c4017c8369..ff6a900eb7 100644
--- a/crates/dbx-web/src/routes/mq.rs
+++ b/crates/dbx-web/src/routes/mq.rs
@@ -1617,6 +1617,7 @@ mod tests {
transfer_progress_channels: RwLock::new(HashMap::new()),
table_import_channels: RwLock::new(HashMap::new()),
sql_file_executions: RwLock::new(HashMap::new()),
+ sql_file_failure_logs: Default::default(),
managed_sql_previews: Default::default(),
nacos_imports: RwLock::new(HashMap::new()),
login_rate_limit: Mutex::new(LoginRateLimit { fail_count: 0, locked_until: None }),
diff --git a/crates/dbx-web/src/routes/sql_file.rs b/crates/dbx-web/src/routes/sql_file.rs
index 235e7dcb46..a235f1c441 100644
--- a/crates/dbx-web/src/routes/sql_file.rs
+++ b/crates/dbx-web/src/routes/sql_file.rs
@@ -3,13 +3,20 @@ use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, Weak};
use std::time::{Duration, Instant};
-use axum::extract::{Multipart, Path as AxumPath, State};
+use axum::body::Body;
+use axum::extract::{Multipart, Path as AxumPath, Query, State};
+use axum::http::{header, HeaderMap, StatusCode};
use axum::response::sse::{Event, Sse};
+use axum::response::Response;
use axum::Json;
use dbx_core::sql::{self, SqlFileProgress, SqlFileRequest, SqlFileStatus};
use dbx_core::sql_file_import::{
- execute_sql_file_paths, execute_sql_file_zip_package_paths, sql_file_error_progress,
- sql_file_progress as build_sql_file_progress, SqlFileProgressEmitter,
+ execute_sql_file_paths, execute_sql_file_zip_package_paths, export_sql_file_failure_log as derive_failure_log_file,
+ prune_sql_file_failure_logs, sql_file_error_progress, sql_file_failure_log_text as derive_failure_log_text,
+ sql_file_progress as build_sql_file_progress, start_sql_file_failure_log, SqlFileFailureLogDeriveError,
+ SqlFileFailureLogFileInfo, SqlFileFailureLogFormat, SqlFileFailureLogKey, SqlFileFailureLogMeta,
+ SqlFileFailureLogRunCounters, SqlFileProgressEmitter, SQL_FILE_FAILURE_LOG_CLIPBOARD_MAX_BYTES,
+ SQL_FILE_FAILURE_LOG_DIR,
};
use futures::stream::Stream;
use serde::Deserialize;
@@ -25,6 +32,10 @@ const SQL_FILE_UPLOAD_MAX_AGE: Duration = Duration::from_secs(24 * 60 * 60);
pub const SQL_FILE_UPLOAD_MAX_BYTES: usize = 200 * 1024 * 1024;
const SQL_FILE_UPLOAD_MIN_BYTES: usize = 1024 * 1024;
const SQL_FILE_UPLOAD_MAX_BYTES_LIMIT: usize = 4096 * 1024 * 1024;
+/// Derived exports are streamed to the client and then swept by age: the log
+/// itself stays in `sql-file-logs`, so these are disposable.
+const SQL_FILE_FAILURE_LOG_EXPORT_DIR: &str = "tmp/sql-file-failure-logs";
+const SQL_FILE_FAILURE_LOG_EXPORT_MAX_AGE: Duration = Duration::from_secs(60 * 60);
struct ManagedPreview {
created: Instant,
@@ -92,6 +103,7 @@ pub fn start_sql_file_cleanup(state: &Arc<WebState>) {
loop {
let Some(state) = state.upgrade() else { break };
state.managed_sql_previews.cleanup(&state.data_dir.join("tmp/sql_file"), SQL_FILE_UPLOAD_MAX_AGE, true);
+ sweep_failure_log_exports(&state.data_dir.join(SQL_FILE_FAILURE_LOG_EXPORT_DIR));
drop(state);
tokio::time::sleep(Duration::from_secs(60)).await;
}
@@ -267,9 +279,14 @@ async fn preview_uploaded_sql_file(
pub async fn execute_sql_file(
State(state): State<Arc<WebState>>,
+ headers: HeaderMap,
Json(body): Json<SqlFileExecuteWrapper>,
) -> Result<Json<serde_json::Value>, AppError> {
let req = body.request;
+ // The failure log of this run belongs to the session that started it: the
+ // export/copy routes look it up by this owner, never by path (dbx#11619).
+ let log_owner = crate::auth::session_token_from_headers(&headers)
+ .ok_or_else(|| AppError::from("Not authenticated".to_string()))?;
let requested_paths = if body.file_paths.is_empty() { vec![req.file_path.clone()] } else { body.file_paths };
let file_paths = requested_paths
@@ -311,7 +328,19 @@ pub async fn execute_sql_file(
tokio::spawn(async move {
let _managed_previews = managed_previews;
let started_at = std::time::Instant::now();
+ let mut failure_log = start_web_failure_log(&state_clone, &req, &file_paths).await;
+ state_clone.sql_file_failure_logs.register(
+ &log_owner,
+ &req.execution_id,
+ failure_log.path().map(Path::to_path_buf),
+ failure_log.status_probe(),
+ );
+ let mut counters = SqlFileFailureLogRunCounters::default();
let mut progress_emitter = SqlFileProgressEmitter::new(|progress| {
+ // The sink only queues; the writer task owns the disk, so a failing
+ // log can never break the import.
+ counters.observe(&progress);
+ failure_log.record(&progress);
send_sql_file_progress(&tx, progress);
});
progress_emitter.emit(build_sql_file_progress(
@@ -361,6 +390,9 @@ pub async fn execute_sql_file(
.await
};
+ failure_log.finish(counters.summary(started_at.elapsed().as_millis() as u64)).await;
+ // Keep the log exportable, but let retention reclaim it once nothing writes.
+ state_clone.sql_file_failure_logs.mark_finished(&log_owner, &req.execution_id);
cleanup_sql_file_package_paths(&file_paths);
cleanup_sql_file_execution(&state_clone, &req.execution_id).await;
});
@@ -394,6 +426,201 @@ async fn cleanup_sql_file_execution(state: &WebState, execution_id: &str) {
state.sql_file_executions.write().await.remove(execution_id);
}
+/// Owner-scoped lookup used by both failure-log routes. Kept as its own function
+/// (rather than inlined) so the cross-session rejection is directly testable.
+fn failure_log_path_for(
+ registry: &dbx_core::sql_file_import::SqlFileFailureLogRegistry,
+ owner: &str,
+ execution_id: &str,
+) -> Result<PathBuf, AppError> {
+ if let Some(path) = registry.path(owner, execution_id) {
+ return Ok(path);
+ }
+ // Say why: a log can be missing because it never existed, or because
+ // retention (or the user) removed the file after the run finished.
+ let reason = registry.info(owner, execution_id).and_then(|info| info.reason);
+ Err(AppError::from(match reason {
+ Some(reason) => format!("No failure log to export for this execution: {reason}"),
+ None => "No failure log to export for this execution".to_string(),
+ }))
+}
+
+/// Creates the failure log of one Web execution. Retention runs first, with every
+/// registered log protected so a parallel import cannot delete an in-progress one.
+async fn start_web_failure_log(
+ state: &WebState,
+ request: &SqlFileRequest,
+ file_paths: &[PathBuf],
+) -> dbx_core::sql_file_import::SqlFileFailureLogHandle {
+ if !request.failure_log_enabled {
+ return dbx_core::sql_file_import::SqlFileFailureLogHandle::unavailable(
+ "Failure log disabled for this execution".to_string(),
+ );
+ }
+ let log_dir = state.data_dir.join(SQL_FILE_FAILURE_LOG_DIR);
+ let protected = state.sql_file_failure_logs.active_paths();
+ if let Err(error) = prune_sql_file_failure_logs(&log_dir, &protected) {
+ log::warn!("Failed to prune SQL file failure logs: {error}");
+ }
+ let connection_name = state.app.configs.read().await.get(&request.connection_id).map(|config| config.name.clone());
+ let mut files = Vec::with_capacity(file_paths.len());
+ for (index, file_path) in file_paths.iter().enumerate() {
+ let size_bytes = tokio::fs::metadata(file_path).await.ok().map(|metadata| metadata.len());
+ let name = file_path.file_name().map(|name| name.to_string_lossy().to_string()).unwrap_or_default();
+ files.push(SqlFileFailureLogFileInfo {
+ index,
+ name,
+ path: file_path.to_string_lossy().to_string(),
+ size_bytes,
+ });
+ }
+ let meta = SqlFileFailureLogMeta {
+ execution_id: request.execution_id.clone(),
+ connection_id: request.connection_id.clone(),
+ connection_name,
+ database: request.database.clone(),
+ schema: request.schema.clone(),
+ files,
+ limits: Default::default(),
+ };
+ start_sql_file_failure_log(&log_dir, meta).await
+}
+
+/// Derived TXT/CSV files are served once and re-creatable, so they are swept by
+/// age instead of tracked.
+fn sweep_failure_log_exports(dir: &Path) {
+ let Ok(entries) = std::fs::read_dir(dir) else {
+ return;
+ };
+ for entry in entries.flatten() {
+ let expired = entry
+ .metadata()
+ .ok()
+ .and_then(|metadata| metadata.modified().ok())
+ .and_then(|modified| modified.elapsed().ok())
+ .is_some_and(|age| age >= SQL_FILE_FAILURE_LOG_EXPORT_MAX_AGE);
+ if expired {
+ let _ = std::fs::remove_file(entry.path());
+ }
+ }
+}
+
+/// Status of one execution's failure log, so the Web UI can show the same
+/// `degraded`/`unavailable` states as the desktop. The server-side path is
+/// deliberately omitted here: a browser has no use for it and it would leak the
+/// server's filesystem layout.
+pub async fn sql_file_failure_log_info(
+ State(state): State<Arc<WebState>>,
+ AxumPath(execution_id): AxumPath<String>,
+ headers: HeaderMap,
+) -> Result<Json<Option<serde_json::Value>>, AppError> {
+ let owner = crate::auth::session_token_from_headers(&headers)
+ .ok_or_else(|| AppError::from("Not authenticated".to_string()))?;
+ let info = state.sql_file_failure_logs.info(&owner, &execution_id);
+ Ok(Json(info.map(|info| {
+ serde_json::json!({
+ "status": info.status.as_str(),
+ "reason": info.reason,
+ })
+ })))
+}
+
+#[derive(Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct SqlFileFailureLogDownloadQuery {
+ #[serde(default)]
+ pub format: Option<String>,
+}
+
+/// Streams the full failure log as an attachment. Owner-scoped: the execution id
+/// alone never grants access, and no path is accepted from the client.
+pub async fn sql_file_failure_log_download(
+ State(state): State<Arc<WebState>>,
+ AxumPath(execution_id): AxumPath<String>,
+ Query(query): Query<SqlFileFailureLogDownloadQuery>,
+ headers: HeaderMap,
+) -> Result<Response, AppError> {
+ let owner = crate::auth::session_token_from_headers(&headers)
+ .ok_or_else(|| AppError::from("Not authenticated".to_string()))?;
+ let format = match query.format.as_deref() {
+ Some(value) => SqlFileFailureLogFormat::parse(value)
+ .ok_or_else(|| AppError::from(format!("Unsupported log format: {value}")))?,
+ None => SqlFileFailureLogFormat::Txt,
+ };
+ let log_path = failure_log_path_for(&state.sql_file_failure_logs, &owner, &execution_id)?;
+
+ let export_dir = state.data_dir.join(SQL_FILE_FAILURE_LOG_EXPORT_DIR);
+ tokio::fs::create_dir_all(&export_dir).await.map_err(|error| AppError::from(error.to_string()))?;
+ let export_path = export_dir.join(format!("{}.{}", Uuid::new_v4(), format.extension()));
+ let destination = export_path.clone();
+ tokio::task::spawn_blocking(move || derive_failure_log_file(&log_path, &destination, format, None))
+ .await
+ .map_err(|error| AppError::from(error.to_string()))?
+ .map_err(|error| AppError::from(error.to_string()))?;
+
+ let file = tokio::fs::File::open(&export_path).await.map_err(|error| AppError::from(error.to_string()))?;
+ let content_type = match format {
+ SqlFileFailureLogFormat::Txt => "text/plain; charset=utf-8",
+ SqlFileFailureLogFormat::Csv => "text/csv; charset=utf-8",
+ };
+ let file_name = format!("sql-file-failures.{}", format.extension());
+ Response::builder()
+ .status(StatusCode::OK)
+ .header(header::CONTENT_TYPE, content_type)
+ .header(header::CONTENT_DISPOSITION, super::export_download::attachment_content_disposition(&file_name))
+ .body(Body::from_stream(tokio_util::io::ReaderStream::new(file)))
+ .map_err(|error| AppError::from(error.to_string()))
+}
+
+#[derive(Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct SqlFileFailureLogTextRequest {
+ pub execution_id: String,
+ #[serde(default)]
+ pub format: Option<String>,
+ #[serde(default)]
+ pub keys: Option<Vec<SqlFileFailureLogKey>>,
+}
+
+/// Derived text for the clipboard, capped like the desktop command. Being over
+/// the cap is a normal outcome (the UI suggests the file export), not an error.
+#[derive(serde::Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct SqlFileFailureLogTextResponse {
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub text: Option<String>,
+ pub too_large: bool,
+ pub limit_bytes: u64,
+}
+
+pub async fn sql_file_failure_log_text(
+ State(state): State<Arc<WebState>>,
+ headers: HeaderMap,
+ Json(body): Json<SqlFileFailureLogTextRequest>,
+) -> Result<Json<SqlFileFailureLogTextResponse>, AppError> {
+ let owner = crate::auth::session_token_from_headers(&headers)
+ .ok_or_else(|| AppError::from("Not authenticated".to_string()))?;
+ let format = match body.format.as_deref() {
+ Some(value) => SqlFileFailureLogFormat::parse(value)
+ .ok_or_else(|| AppError::from(format!("Unsupported log format: {value}")))?,
+ None => SqlFileFailureLogFormat::Txt,
+ };
+ let log_path = failure_log_path_for(&state.sql_file_failure_logs, &owner, &body.execution_id)?;
+ let selection = body.keys.filter(|keys| !keys.is_empty()).map(|keys| keys.into_iter().collect());
+ let limit = SQL_FILE_FAILURE_LOG_CLIPBOARD_MAX_BYTES;
+ let derived =
+ tokio::task::spawn_blocking(move || derive_failure_log_text(&log_path, format, selection.as_ref(), limit))
+ .await
+ .map_err(|error| AppError::from(error.to_string()))?;
+ match derived {
+ Ok(text) => Ok(Json(SqlFileFailureLogTextResponse { text: Some(text), too_large: false, limit_bytes: limit })),
+ Err(SqlFileFailureLogDeriveError::TooLarge { limit }) => {
+ Ok(Json(SqlFileFailureLogTextResponse { text: None, too_large: true, limit_bytes: limit }))
+ }
+ Err(error) => Err(AppError::from(error.to_string())),
+ }
+}
+
fn safe_uploaded_sql_path(tmp_dir: &Path, file_name: &str) -> Result<PathBuf, AppError> {
let base_name = file_name.rsplit(['/', '\\']).find(|part| !part.is_empty()).unwrap_or("upload.sql").trim();
if base_name.is_empty() || base_name == "." || base_name == ".." {
@@ -527,6 +754,16 @@ mod tests {
use super::*;
+ /// The execution route now takes the session that owns the failure log, so
+ /// these tests authenticate the same way the Web client does: a session that
+ /// exists in `WebState::sessions` plus its `dbx_session` cookie.
+ async fn authenticated_state(state: &WebState, token: &str) -> HeaderMap {
+ state.sessions.write().await.insert(token.to_string());
+ let mut headers = HeaderMap::new();
+ headers.insert(header::COOKIE, format!("dbx_session={token}").parse().unwrap());
+ headers
+ }
+
async fn restore_state(directory: &Path) -> WebState {
let storage = dbx_core::persistence::test_storage::open(&directory.join("dbx.db")).await.unwrap();
WebState::for_tests(Arc::new(dbx_core::connection::AppState::new(storage)), directory.to_path_buf())
@@ -622,10 +859,14 @@ mod tests {
"filePath": copy, "continueOnError": false
}))
.unwrap();
- let response =
- execute_sql_file(State(state.clone()), Json(SqlFileExecuteWrapper { request, file_paths: vec![] }))
- .await
- .unwrap();
+ let headers = authenticated_state(&state, "session-restore").await;
+ let response = execute_sql_file(
+ State(state.clone()),
+ headers,
+ Json(SqlFileExecuteWrapper { request, file_paths: vec![] }),
+ )
+ .await
+ .unwrap();
assert_eq!(response.0["executionId"], "restore-error");
tokio::time::timeout(Duration::from_secs(5), async {
while copy.exists() {
@@ -653,9 +894,14 @@ mod tests {
"filePath": copy, "continueOnError": false
}))
.unwrap();
- assert!(execute_sql_file(State(state.clone()), Json(SqlFileExecuteWrapper { request, file_paths: vec![] }))
- .await
- .is_err());
+ let headers = authenticated_state(&state, "session-existing").await;
+ assert!(execute_sql_file(
+ State(state.clone()),
+ headers,
+ Json(SqlFileExecuteWrapper { request, file_paths: vec![] })
+ )
+ .await
+ .is_err());
assert!(!copy.exists());
assert!(source.exists());
assert!(!token.is_cancelled());
@@ -751,3 +997,69 @@ mod tests {
let _ = std::fs::remove_dir_all(tmp_dir);
}
}
+
+#[cfg(test)]
+mod failure_log_route_tests {
+ use super::*;
+ use dbx_core::sql_file_import::{SqlFileFailureLogHandle, SqlFileFailureLogRegistry};
+ use std::path::PathBuf;
+
+ /// The registry reports a log whose file is gone as unavailable, so these
+ /// fixtures must point at a file that really exists.
+ fn registry_with_log(dir: &Path, owner: &str, execution_id: &str) -> (SqlFileFailureLogRegistry, PathBuf) {
+ let path = dir.join(format!("{execution_id}.jsonl"));
+ std::fs::write(
+ &path, b"{}
+",
+ )
+ .unwrap();
+ let registry = SqlFileFailureLogRegistry::new();
+ let handle = SqlFileFailureLogHandle::unavailable("test".to_string());
+ registry.register(owner, execution_id, Some(path.clone()), handle.status_probe());
+ (registry, path)
+ }
+
+ #[test]
+ fn failure_log_lookup_serves_the_owning_session_only() {
+ let dir = tempfile::tempdir().unwrap();
+ let (registry, path) = registry_with_log(dir.path(), "session-a", "exec-1");
+
+ assert_eq!(failure_log_path_for(&registry, "session-a", "exec-1").unwrap(), path);
+ // A session that guesses another session's execution id gets nothing.
+ assert!(failure_log_path_for(&registry, "session-b", "exec-1").is_err());
+ // So does an unknown execution id for the owning session.
+ assert!(failure_log_path_for(&registry, "session-a", "exec-2").is_err());
+ }
+
+ #[test]
+ fn failure_log_lookup_explains_a_log_whose_file_disappeared() {
+ let dir = tempfile::tempdir().unwrap();
+ let path = dir.path().join("exec.jsonl");
+ std::fs::write(
+ &path, b"{}
+",
+ )
+ .unwrap();
+ let registry = SqlFileFailureLogRegistry::new();
+ let handle = SqlFileFailureLogHandle::unavailable("test".to_string());
+ registry.register("session-a", "exec-1", Some(path.clone()), handle.status_probe());
+ assert!(failure_log_path_for(&registry, "session-a", "exec-1").is_ok());
+
+ // Retention prunes a finished log: the route must say so instead of
+ // streaming a path that no longer resolves.
+ std::fs::remove_file(&path).unwrap();
+ let error = failure_log_path_for(&registry, "session-a", "exec-1").unwrap_err();
+ assert!(error.message.contains("no longer exists"), "got: {}", error.message);
+ }
+
+ #[test]
+ fn failure_log_lookup_rejects_a_log_without_a_file() {
+ let registry = SqlFileFailureLogRegistry::new();
+ let handle = SqlFileFailureLogHandle::unavailable("no file".to_string());
+ registry.register("session-a", "exec-1", None, handle.status_probe());
+
+ // An unavailable log has no path to export; the route must fail rather
+ // than stream something that does not exist.
+ assert!(failure_log_path_for(&registry, "session-a", "exec-1").is_err());
+ }
+}
diff --git a/crates/dbx-web/src/state.rs b/crates/dbx-web/src/state.rs
index 531aefb624..38c5407baf 100644
--- a/crates/dbx-web/src/state.rs
+++ b/crates/dbx-web/src/state.rs
@@ -45,6 +45,9 @@ pub struct WebState {
pub transfer_progress_channels: RwLock<HashMap<String, Arc<TransferProgressChannel>>>,
pub table_import_channels: RwLock<HashMap<String, watch::Sender<String>>>,
pub sql_file_executions: RwLock<HashMap<String, CancellationToken>>,
+ /// Failure logs of the executions this process ran, scoped to the session
+ /// that started them (dbx#11619).
+ pub sql_file_failure_logs: dbx_core::sql_file_import::SqlFileFailureLogRegistry,
pub managed_sql_previews: crate::routes::sql_file::ManagedSqlPreviews,
pub nacos_imports: RwLock<HashMap<String, NacosImportContext>>,
pub login_rate_limit: Mutex<LoginRateLimit>,
@@ -75,6 +78,7 @@ impl WebState {
sse_channels: RwLock::new(HashMap::new()),
transfer_progress_channels: RwLock::new(HashMap::new()),
table_import_channels: RwLock::new(HashMap::new()),
+ sql_file_failure_logs: Default::default(),
sql_file_executions: RwLock::new(HashMap::new()),
managed_sql_previews: Default::default(),
nacos_imports: RwLock::new(HashMap::new()),
diff --git a/src-tauri/src/commands/sql_file.rs b/src-tauri/src/commands/sql_file.rs
index 69c0787f22..6c75b456c5 100644
--- a/src-tauri/src/commands/sql_file.rs
+++ b/src-tauri/src/commands/sql_file.rs
@@ -1,16 +1,22 @@
-use std::collections::HashMap;
+use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::{Arc, OnceLock};
use std::time::Instant;
+use serde::Serialize;
use tauri::{AppHandle, Emitter, State};
use tokio::sync::RwLock;
use tokio_util::sync::CancellationToken;
use crate::commands::connection::{ensure_connection_writable, AppState};
use dbx_core::sql_file_import::{
- execute_sql_file_paths, execute_sql_file_zip_package_paths, mysql_like_sql_file_bootstrap_analysis,
- read_sql_file_preview, sql_file_progress, SqlFileProgressEmitter,
+ execute_sql_file_paths, execute_sql_file_zip_package_paths, export_sql_file_failure_log as derive_failure_log_file,
+ mysql_like_sql_file_bootstrap_analysis, prune_sql_file_failure_logs, read_sql_file_preview,
+ sql_file_failure_log_text as derive_failure_log_text, sql_file_progress, start_sql_file_failure_log,
+ SqlFileFailureLogDeriveError, SqlFileFailureLogFileInfo, SqlFileFailureLogFormat, SqlFileFailureLogHandle,
+ SqlFileFailureLogInfo, SqlFileFailureLogKey, SqlFileFailureLogMeta, SqlFileFailureLogRegistry,
+ SqlFileFailureLogRunCounters, SqlFileProgressEmitter, SQL_FILE_FAILURE_LOG_CLIPBOARD_MAX_BYTES,
+ SQL_FILE_FAILURE_LOG_DIR,
};
pub use dbx_core::sql::{SqlFilePreview, SqlFileRequest, SqlFileStatus};
@@ -21,6 +27,17 @@ fn sql_file_executions() -> &'static RwLock<HashMap<String, CancellationToken>>
SQL_FILE_EXECUTIONS.get_or_init(|| RwLock::new(HashMap::new()))
}
+/// The desktop runtime has no session concept, so every log it registers shares
+/// one owner. The Web runtime binds the authenticated session instead, which is
+/// why the registry is owner-scoped at all.
+const DESKTOP_FAILURE_LOG_OWNER: &str = "desktop";
+
+static SQL_FILE_FAILURE_LOGS: OnceLock<SqlFileFailureLogRegistry> = OnceLock::new();
+
+pub(crate) fn sql_file_failure_log_registry() -> &'static SqlFileFailureLogRegistry {
+ SQL_FILE_FAILURE_LOGS.get_or_init(SqlFileFailureLogRegistry::new)
+}
+
#[cfg(test)]
#[derive(Debug, Clone, PartialEq, Eq)]
struct SqlFileSummary {
@@ -150,7 +167,35 @@ pub async fn execute_sql_files(
let started_at = Instant::now();
let is_zip_package = execution_targets_zip_package(&file_paths);
- let result = execute_sql_files_inner(&app, &state, &request, &file_paths, token, started_at, is_zip_package).await;
+
+ // Failure log (dbx#11619): created before the run so a mid-run export can
+ // already read what failed, registered under the execution id so the export
+ // and copy commands can find it, and finished on every exit path below.
+ let mut failure_log = start_execution_failure_log(&state, &request, &file_paths).await;
+ sql_file_failure_log_registry().register(
+ DESKTOP_FAILURE_LOG_OWNER,
+ &request.execution_id,
+ failure_log.path().map(Path::to_path_buf),
+ failure_log.status_probe(),
+ );
+ let mut counters = SqlFileFailureLogRunCounters::default();
+ let result = execute_sql_files_inner(
+ &app,
+ &state,
+ &request,
+ &file_paths,
+ token,
+ started_at,
+ is_zip_package,
+ &mut failure_log,
+ &mut counters,
+ )
+ .await;
+ failure_log.finish(counters.summary(started_at.elapsed().as_millis() as u64)).await;
+ // The log stays registered for later export, but stops shielding itself from
+ // retention now that nothing is writing to it.
+ sql_file_failure_log_registry().mark_finished(DESKTOP_FAILURE_LOG_OWNER, &request.execution_id);
+
cleanup_sql_zip_package_paths(&file_paths);
{
let mut executions = sql_file_executions().write().await;
@@ -159,6 +204,149 @@ pub async fn execute_sql_files(
result
}
+/// Status of the failure log of one execution, for the export/copy controls.
+/// Returns `None` when this process never logged that execution (unknown id, or
+/// the run predates a restart: logs are not re-discoverable across restarts).
+#[tauri::command]
+pub async fn sql_file_failure_log_info(execution_id: String) -> Result<Option<SqlFileFailureLogInfo>, String> {
+ Ok(sql_file_failure_log_registry().info(DESKTOP_FAILURE_LOG_OWNER, &execution_id))
+}
+
+#[derive(Debug, Clone, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct SqlFileFailureLogExportPayload {
+ pub records: u64,
+ pub bytes: u64,
+ pub format: String,
+}
+
+/// Clipboard payload. Being over the cap is a normal outcome the UI must handle
+/// by suggesting the file export, so it is a flag rather than an error.
+#[derive(Debug, Clone, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct SqlFileFailureLogTextPayload {
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub text: Option<String>,
+ pub too_large: bool,
+ pub limit_bytes: u64,
+}
+
+/// Streams the failure log into a path the frontend already picked with the
+/// native save dialog. Derivation is blocking (a log can be large), so it runs
+/// off the async runtime.
+#[tauri::command]
+pub async fn export_sql_file_failure_log(
+ execution_id: String,
+ format: String,
+ target_path: String,
+ keys: Option<Vec<SqlFileFailureLogKey>>,
+) -> Result<SqlFileFailureLogExportPayload, String> {
+ let format = SqlFileFailureLogFormat::parse(&format).ok_or_else(|| format!("Unsupported log format: {format}"))?;
+ let log_path = resolve_failure_log_path(&execution_id)?;
+ let selection = failure_log_selection(keys);
+ let format_name = format.extension().to_string();
+ let summary = tokio::task::spawn_blocking(move || {
+ derive_failure_log_file(&log_path, Path::new(&target_path), format, selection.as_ref())
+ })
+ .await
+ .map_err(|error| error.to_string())?
+ .map_err(|error| error.to_string())?;
+ Ok(SqlFileFailureLogExportPayload { records: summary.records, bytes: summary.bytes, format: format_name })
+}
+
+/// Derived TXT/CSV text for the clipboard, capped so a huge log cannot be
+/// pushed into the clipboard (and into the frontend) by accident.
+#[tauri::command]
+pub async fn read_sql_file_failure_log_text(
+ execution_id: String,
+ format: Option<String>,
+ keys: Option<Vec<SqlFileFailureLogKey>>,
+) -> Result<SqlFileFailureLogTextPayload, String> {
+ let format = match format {
+ Some(value) => {
+ SqlFileFailureLogFormat::parse(&value).ok_or_else(|| format!("Unsupported log format: {value}"))?
+ }
+ None => SqlFileFailureLogFormat::Txt,
+ };
+ let log_path = resolve_failure_log_path(&execution_id)?;
+ let selection = failure_log_selection(keys);
+ let limit = SQL_FILE_FAILURE_LOG_CLIPBOARD_MAX_BYTES;
+ let derived =
+ tokio::task::spawn_blocking(move || derive_failure_log_text(&log_path, format, selection.as_ref(), limit))
+ .await
+ .map_err(|error| error.to_string())?;
+ match derived {
+ Ok(text) => Ok(SqlFileFailureLogTextPayload { text: Some(text), too_large: false, limit_bytes: limit }),
+ Err(SqlFileFailureLogDeriveError::TooLarge { limit }) => {
+ Ok(SqlFileFailureLogTextPayload { text: None, too_large: true, limit_bytes: limit })
+ }
+ Err(error) => Err(error.to_string()),
+ }
+}
+
+fn resolve_failure_log_path(execution_id: &str) -> Result<PathBuf, String> {
+ sql_file_failure_log_registry().path(DESKTOP_FAILURE_LOG_OWNER, execution_id).ok_or_else(|| {
+ let reason =
+ sql_file_failure_log_registry().info(DESKTOP_FAILURE_LOG_OWNER, execution_id).and_then(|info| info.reason);
+ match reason {
+ Some(reason) => format!("No failure log to export for this execution: {reason}"),
+ None => "No failure log to export for this execution".to_string(),
+ }
+ })
+}
+
+fn failure_log_selection(keys: Option<Vec<SqlFileFailureLogKey>>) -> Option<HashSet<SqlFileFailureLogKey>> {
+ let keys = keys?;
+ if keys.is_empty() {
+ return None;
+ }
+ Some(keys.into_iter().collect())
+}
+
+/// Prunes the failure log directory down to its retention limits. Best effort:
+/// a failure to clean up must never block an import. Logs of executions that
+/// are still registered (i.e. possibly still running) are protected, so two
+/// parallel imports cannot delete each other's in-progress logs.
+pub(crate) fn prune_desktop_sql_file_failure_logs(data_dir: &Path) {
+ let dir = data_dir.join(SQL_FILE_FAILURE_LOG_DIR);
+ let protected = sql_file_failure_log_registry().active_paths();
+ if let Err(error) = prune_sql_file_failure_logs(&dir, &protected) {
+ log::warn!("Failed to prune SQL file failure logs: {error}");
+ }
+}
+
+async fn start_execution_failure_log(
+ state: &State<'_, Arc<AppState>>,
+ request: &SqlFileRequest,
+ file_paths: &[String],
+) -> SqlFileFailureLogHandle {
+ if !request.failure_log_enabled {
+ return SqlFileFailureLogHandle::unavailable("Failure log disabled for this execution".to_string());
+ }
+ let dir = state.storage.data_dir().join(SQL_FILE_FAILURE_LOG_DIR);
+ // Retention runs before the new file exists, so it can never delete the log
+ // this execution is about to write.
+ prune_desktop_sql_file_failure_logs(state.storage.data_dir());
+ let connection_name = state.configs.read().await.get(&request.connection_id).map(|config| config.name.clone());
+ let mut files = Vec::with_capacity(file_paths.len());
+ for (index, file_path) in file_paths.iter().enumerate() {
+ let path = PathBuf::from(file_path);
+ let size_bytes = tokio::fs::metadata(&path).await.ok().map(|metadata| metadata.len());
+ let name = path.file_name().map(|name| name.to_string_lossy().to_string()).unwrap_or_default();
+ files.push(SqlFileFailureLogFileInfo { index, name, path: file_path.clone(), size_bytes });
+ }
+ let meta = SqlFileFailureLogMeta {
+ execution_id: request.execution_id.clone(),
+ connection_id: request.connection_id.clone(),
+ connection_name,
+ database: request.database.clone(),
+ schema: request.schema.clone(),
+ files,
+ limits: Default::default(),
+ };
+ start_sql_file_failure_log(&dir, meta).await
+}
+
fn cleanup_sql_zip_package_paths(file_paths: &[String]) {
let mut directories = std::collections::HashSet::new();
for path in file_paths {
@@ -216,8 +404,14 @@ async fn execute_sql_files_inner(
token: CancellationToken,
started_at: Instant,
is_zip_package: bool,
+ failure_log: &mut SqlFileFailureLogHandle,
+ counters: &mut SqlFileFailureLogRunCounters,
) -> Result<(), String> {
let mut progress_emitter = SqlFileProgressEmitter::new(|progress| {
+ // The sink only serializes and hands off to its writer task, so a slow or
+ // failing disk stays out of the execution path.
+ counters.observe(&progress);
+ failure_log.record(&progress);
let _ = app.emit("sql-file-progress", progress);
});
progress_emitter.emit(sql_file_progress(
@@ -415,6 +609,49 @@ mod execution_tests {
let _ = std::fs::remove_dir_all(&user_dir);
}
+ fn terminal_progress(
+ status: SqlFileStatus,
+ success: usize,
+ failures: usize,
+ affected: u64,
+ ) -> dbx_core::sql::SqlFileProgress {
+ dbx_core::sql::SqlFileProgress {
+ execution_id: "exec-1".to_string(),
+ status,
+ statement_index: 0,
+ success_count: success,
+ failure_count: failures,
+ affected_rows: affected,
+ elapsed_ms: 0,
+ statement_summary: String::new(),
+ error: None,
+ bytes_read: None,
+ total_bytes: None,
+ phase: None,
+ file_index: None,
+ file_name: None,
+ failure_sql: None,
+ }
+ }
+
+ #[test]
+ fn terminal_counters_only_follow_terminal_events() {
+ let mut counters = SqlFileFailureLogRunCounters::default();
+ // Intermediate (and failure) progress must not be mistaken for the final
+ // figures, otherwise the log summary would contradict the UI counters.
+ counters.observe(&terminal_progress(SqlFileStatus::Running, 5, 1, 2));
+ counters.observe(&terminal_progress(SqlFileStatus::StatementFailed, 5, 1, 2));
+ assert_eq!(counters.success_count, 0);
+
+ counters.observe(&terminal_progress(SqlFileStatus::Done, 4, 1, 7));
+ assert_eq!((counters.success_count, counters.failure_count, counters.affected_rows), (4, 1, 7));
+ assert!(!counters.cancelled);
+
+ counters.observe(&terminal_progress(SqlFileStatus::Cancelled, 2, 1, 3));
+ assert!(counters.cancelled);
+ assert_eq!(counters.success_count, 2);
+ }
+
#[test]
fn duplicate_execution_id_is_rejected_without_replacing_token() {
let mut executions = HashMap::new();
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index a1c54d2314..fec6c555a1 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -1632,6 +1632,11 @@ pub fn run() {
}
let db_path = data_dir.join("dbx.db");
+ // Failure logs from previous runs are kept only within their retention
+ // limits; prune once at startup so a killed process cannot leave the
+ // directory growing forever.
+ commands::sql_file::prune_desktop_sql_file_failure_logs(&data_dir);
+
let t = Instant::now();
append_startup_probe(format!("opening storage file=dbx.db data_dir_mode={data_dir_mode}"));
let storage = tauri::async_runtime::block_on(async {
@@ -2188,6 +2193,9 @@ pub fn run() {
commands::sql_file::execute_sql_file,
commands::sql_file::execute_sql_files,
commands::sql_file::cancel_sql_file_execution,
+ commands::sql_file::sql_file_failure_log_info,
+ commands::sql_file::export_sql_file_failure_log,
+ commands::sql_file::read_sql_file_failure_log_text,
commands::external_sql::pending_open_sql_files,
commands::external_sql::read_external_sql_file,
commands::external_sql::inspect_external_sql_file,