feat(export): run data dictionary PDF exports in background

This commit is contained in:
ccq authored and GitHub committed 2026-09-26 00:04:52 +08:00
1 parent 4baec21fb7
commit 705ea7464d
18 files changed
+702 -104

No files matched your search

@@ -31,7 +31,9 @@ import {
type DictionaryTemplateId, type DictionaryTemplateId,
} from "@/lib/docs/dataDictionary"; } from "@/lib/docs/dataDictionary";
import { buildDataDictionaryPdf } from "@/lib/docs/dataDictionaryPdf"; import { buildDataDictionaryPdf } from "@/lib/docs/dataDictionaryPdf";
import { DictionaryFileExistsError, saveDataDictionaryFile } from "@/lib/docs/saveDataDictionaryFile"; import { DictionaryFileExistsError, chooseDataDictionaryPath } from "@/lib/docs/saveDataDictionaryFile";
import { hasActiveDictionaryExport, startDataDictionaryExport } from "@/lib/docs/dataDictionaryExportTask";
import { useToast } from "@/composables/useToast";
import { isTauriRuntime } from "@/lib/backend/tauriRuntime"; import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
import { useConnectionStore } from "@/stores/connectionStore"; import { useConnectionStore } from "@/stores/connectionStore";
@@ -46,14 +48,13 @@ const props = defineProps<{
const open = defineModel<boolean>("open", { default: false }); const open = defineModel<boolean>("open", { default: false });
const { t } = useI18n(); const { t } = useI18n();
const connectionStore = useConnectionStore(); const connectionStore = useConnectionStore();
const { toast } = useToast();
const step = ref(0); const step = ref(0);
const loading = ref(false); const loading = ref(false);
const exporting = ref(false); const exporting = ref(false);
const loadError = ref<string | null>(null); const loadError = ref<string | null>(null);
const exportError = ref<string | null>(null); const exportError = ref<string | null>(null);
const generatedPath = ref("");
const elapsed = ref(0);
const databases = ref<Array<{ name: string; checked: boolean }>>([]); const databases = ref<Array<{ name: string; checked: boolean }>>([]);
const schemas = ref<Array<{ database: string; name: string; checked: boolean }>>([]); const schemas = ref<Array<{ database: string; name: string; checked: boolean }>>([]);
const objects = ref<DictionaryObjectRef[]>([]); const objects = ref<DictionaryObjectRef[]>([]);
@@ -67,7 +68,6 @@ const continueOnError = ref(true);
const outputPath = ref(""); const outputPath = ref("");
const previewUrl = ref(""); const previewUrl = ref("");
let generation = 0; let generation = 0;
let elapsedTimer = 0;
const steps = computed(() => [t("dataDictionary.stepDatabases"), t("dataDictionary.stepObjects"), t("dataDictionary.stepTemplate"), t("dataDictionary.stepLayout"), t("dataDictionary.stepFile")]); const steps = computed(() => [t("dataDictionary.stepDatabases"), t("dataDictionary.stepObjects"), t("dataDictionary.stepTemplate"), t("dataDictionary.stepLayout"), t("dataDictionary.stepFile")]);
const dbType = computed(() => connectionStore.getConfig(props.prefillConnectionId || "")?.db_type); const dbType = computed(() => connectionStore.getConfig(props.prefillConnectionId || "")?.db_type);
@@ -312,22 +312,42 @@ async function browse(): Promise<void> {
} }
async function exportDictionary(): Promise<void> { async function exportDictionary(): Promise<void> {
const connectionId = props.prefillConnectionId;
if (!connectionId) return;
if (hasActiveDictionaryExport(connectionId)) {
exportError.value = t("dataDictionary.alreadyRunning");
return;
}
exporting.value = true; exporting.value = true;
exportError.value = null; exportError.value = null;
generatedPath.value = "";
elapsed.value = 0;
elapsedTimer = window.setInterval(() => (elapsed.value += 1), 1000);
try { try {
const fallback = dataDictionaryFileName(checkedDatabases.value[0] || "DataDictionary", false); const fallback = dataDictionaryFileName(checkedDatabases.value[0] || "DataDictionary", false);
const suggested = resolveDictionaryExportPath(outputPath.value, fallback, appendTimestamp.value); const suggested = resolveDictionaryExportPath(outputPath.value, fallback, appendTimestamp.value);
const document = await collectSelection(); const path = await chooseDataDictionaryPath(suggested);
const bytes = buildDataDictionaryPdf(document.tables, labels(), layout.value, continueOnError.value ? document.warnings : []); if (!path) return;
const saved = await saveDataDictionaryFile(suggested, bytes, { overwrite: overwrite.value }); const started = startDataDictionaryExport({
if (saved) generatedPath.value = saved; connectionId,
databases: [...checkedDatabases.value],
schemas: checkedSchemas.value.map((item) => ({ database: item.database, name: item.name })),
objects: orderedObjects.value.map((item) => ({ ...item })),
snapshot: props.snapshot,
layout: { ...layout.value },
labels: labels(),
outputPath: path,
overwrite: overwrite.value,
continueOnError: continueOnError.value,
warningText: (warning) => warningText(warning),
skippedDatabase: (database, reason) => t("dataDictionary.warningTableSkipped", { table: database, reason }),
missingObject: (object) => t("dataDictionary.warningTableSkipped", { table: objectLabel(object), reason: t("dataDictionary.objectUnavailable") }),
onSuccess: (saved, warnings) => toast(t("dataDictionary.backgroundDone", { path: saved }) + (warnings ? ` · ${t("dataDictionary.warningCount", { count: warnings })}` : ""), 8000),
onFailure: (error) => toast(t("dataDictionary.backgroundFailed", { error: error instanceof DictionaryFileExistsError ? t("dataDictionary.fileExists") : error instanceof Error ? error.message : String(error) }), 8000),
});
if (!started) throw new Error(t("dataDictionary.alreadyRunning"));
toast(t("dataDictionary.backgroundStarted"));
open.value = false;
} catch (error) { } catch (error) {
exportError.value = error instanceof DictionaryFileExistsError ? t("dataDictionary.fileExists") : t("dataDictionary.exportFailed", { error: error instanceof Error ? error.message : String(error) }); exportError.value = error instanceof DictionaryFileExistsError ? t("dataDictionary.fileExists") : t("dataDictionary.exportFailed", { error: error instanceof Error ? error.message : String(error) });
} finally { } finally {
window.clearInterval(elapsedTimer);
exporting.value = false; exporting.value = false;
} }
} }
@@ -420,7 +440,6 @@ watch(
return; return;
} }
step.value = 0; step.value = 0;
generatedPath.value = "";
exportError.value = null; exportError.value = null;
outputPath.value = ""; outputPath.value = "";
appendTimestamp.value = false; appendTimestamp.value = false;
@@ -434,7 +453,6 @@ watch(
onBeforeUnmount(() => { onBeforeUnmount(() => {
generation += 1; generation += 1;
window.clearInterval(elapsedTimer);
if (previewUrl.value) URL.revokeObjectURL(previewUrl.value); if (previewUrl.value) URL.revokeObjectURL(previewUrl.value);
}); });
</script> </script>
@@ -597,8 +615,7 @@ onBeforeUnmount(() => {
><span>{{ t("dataDictionary.continueOnError") }}</span ><span>{{ t("dataDictionary.continueOnError") }}</span
><input v-model="continueOnError" type="checkbox" ><input v-model="continueOnError" type="checkbox"
/></label> /></label>
<p v-if="exporting" class="text-sm text-muted-foreground">{{ t("dataDictionary.generating") }} {{ elapsed }}s</p> <p v-if="exporting" class="text-sm text-muted-foreground">{{ t("dataDictionary.exporting") }}</p>
<p v-if="generatedPath" class="text-sm">{{ t("dataDictionary.generated") }} {{ generatedPath }}</p>
<p v-if="exportError" class="text-sm text-destructive">{{ exportError }}</p> <p v-if="exportError" class="text-sm text-destructive">{{ exportError }}</p>
</div> </div>
</div> </div>
@@ -61,6 +61,9 @@ const progressPercent = (totalRows: number | null, rowsExported: number) => {
}; };
const progressValue = (task: ExportTask) => { const progressValue = (task: ExportTask) => {
if (task.kind === "data-dictionary") {
return task.dictionaryTotal ? Math.min(95, Math.round(((task.dictionaryCompleted ?? 0) / task.dictionaryTotal) * 95)) : 0;
}
if (task.kind === "database-export") { if (task.kind === "database-export") {
if (task.overallPercent !== undefined) return task.overallPercent; if (task.overallPercent !== undefined) return task.overallPercent;
if (!task.totalObjects || task.totalObjects <= 0) return 0; if (!task.totalObjects || task.totalObjects <= 0) return 0;
@@ -94,6 +97,7 @@ const taskFileName = (task: ExportTask) => {
}; };
const taskTitle = (task: ExportTask) => { const taskTitle = (task: ExportTask) => {
if (task.kind === "data-dictionary") return `${t("dataDictionary.title")}: ${task.tableName}`;
// database-export and sql-file keep their task labels on purpose: the former // database-export and sql-file keep their task labels on purpose: the former
// names the database (filePath may be a directory), the latter already names // names the database (filePath may be a directory), the latter already names
// the executed script. Only table-export synthesizes a misleading // the executed script. Only table-export synthesizes a misleading
@@ -125,6 +129,14 @@ const comparePhaseText = (phase?: string) => {
}; };
const rowsText = (task: ExportTask) => { const rowsText = (task: ExportTask) => {
if (task.kind === "data-dictionary") {
if (task.status === "Done") return [t("exportProgress.done"), task.dictionaryWarnings ? t("dataDictionary.warningCount", { count: task.dictionaryWarnings }) : ""].filter(Boolean).join(" · ");
if (task.status === "Error") return t("exportProgress.error");
const phase = t(`dataDictionary.phase${(task.dictionaryPhase ?? "preparing").replace(/^./, (letter) => letter.toUpperCase())}`);
const count = task.dictionaryPhase === "collecting" && task.dictionaryTotal ? t("exportProgress.objectsCount", { current: (task.dictionaryCompleted ?? 0).toLocaleString(), total: task.dictionaryTotal.toLocaleString() }) : "";
const warnings = task.dictionaryWarnings ? t("dataDictionary.warningCount", { count: task.dictionaryWarnings }) : "";
return [phase, count, task.dictionaryCurrent, warnings].filter(Boolean).join(" · ");
}
if (task.kind === "schema-diff") { if (task.kind === "schema-diff") {
if (isActive(task.status)) { if (isActive(task.status)) {
const progress = task.compareTotal ? `${task.compareCurrent ?? 0}/${task.compareTotal}` : ""; const progress = task.compareTotal ? `${task.compareCurrent ?? 0}/${task.compareTotal}` : "";
@@ -217,6 +229,7 @@ const databaseObjectText = (task: ExportTask) => {
const statusIcon = (task: ExportTask) => { const statusIcon = (task: ExportTask) => {
if (isActive(task.status)) { if (isActive(task.status)) {
if (task.kind === "database-export") return DatabaseBackup; if (task.kind === "database-export") return DatabaseBackup;
if (task.kind === "data-dictionary") return FileDown;
if (task.kind === "sql-file") return FileCode2; if (task.kind === "sql-file") return FileCode2;
if (task.kind === "data-transfer") return ArrowRightLeft; if (task.kind === "data-transfer") return ArrowRightLeft;
if (task.kind === "multi-db-execution") return Layers3; if (task.kind === "multi-db-execution") return Layers3;
@@ -262,7 +275,7 @@ function toggleShowAll() {
// Reveal is offered only for tasks that produce one local output file. // Reveal is offered only for tasks that produce one local output file.
// sql-file filePath can be a "; "-joined list of input scripts (not an // sql-file filePath can be a "; "-joined list of input scripts (not an
// output), and data-transfer has no local file at all. // output), and data-transfer has no local file at all.
const canRevealTaskFile = (task: ExportTask) => (task.kind === "table-export" || task.kind === "database-export") && task.status === "Done" && !!task.filePath && isTauriRuntime(); const canRevealTaskFile = (task: ExportTask) => (task.kind === "table-export" || task.kind === "database-export" || task.kind === "data-dictionary") && task.status === "Done" && !!task.filePath && isTauriRuntime();
async function revealTaskFile(task: ExportTask) { async function revealTaskFile(task: ExportTask) {
if (!canRevealTaskFile(task) || revealingTaskIds.value.includes(task.exportId)) return; if (!canRevealTaskFile(task) || revealingTaskIds.value.includes(task.exportId)) return;
@@ -359,6 +372,7 @@ function openTask(task: ExportTask): void {
v-if=" v-if="
task.totalRows || task.totalRows ||
(task.kind === 'database-export' && (task.totalObjects || task.overallPercent !== undefined)) || (task.kind === 'database-export' && (task.totalObjects || task.overallPercent !== undefined)) ||
(task.kind === 'data-dictionary' && task.dictionaryPhase === 'collecting' && task.dictionaryProgressKnown && task.dictionaryTotal) ||
(task.kind === 'data-transfer' && task.totalTables) || (task.kind === 'data-transfer' && task.totalTables) ||
(task.kind === 'multi-db-execution' && task.multiDbTotal) || (task.kind === 'multi-db-execution' && task.multiDbTotal) ||
((task.kind === 'schema-diff' || task.kind === 'data-compare') && task.compareTotal) ((task.kind === 'schema-diff' || task.kind === 'data-compare') && task.compareTotal)
@@ -94,6 +94,24 @@ describe("failure detail copy text", () => {
}); });
}); });
describe("data dictionary background task", () => {
it("shows actual collected-object progress and retains the finished output after the wizard closes", async () => {
const tracker = useExportTracker();
tracker.addDataDictionaryTask("dictionary-task", "shop", 2);
tracker.updateDataDictionaryTask("dictionary-task", { dictionaryPhase: "collecting", dictionaryProgressKnown: true, dictionaryCompleted: 1, dictionaryTotal: 2 });
await mountPopover();
expect(document.body.textContent).toContain("Export Data Dictionary: shop");
expect(document.body.textContent).toContain("1 / 2 objects");
tracker.updateDataDictionaryTask("dictionary-task", { dictionaryPhase: "saving", status: "Writing" });
await nextTick();
expect(document.body.textContent).toContain("Saving file");
tracker.updateDataDictionaryTask("dictionary-task", { status: "Done", filePath: "/tmp/shop.pdf" });
await nextTick();
expect(document.body.textContent).toContain("Export completed!");
expect(document.querySelector('[title="Open containing folder"]')).not.toBeNull();
});
});
describe("SQL file byte progress", () => { describe("SQL file byte progress", () => {
it("keeps legacy progress indeterminate instead of using successful statements as the total", async () => { it("keeps legacy progress indeterminate instead of using successful statements as the total", async () => {
const tracker = useExportTracker(); const tracker = useExportTracker();
@@ -4,7 +4,7 @@ import { isTerminalTransferProgress } from "@/lib/backend/transferProgress";
import { uuid } from "@/lib/common/utils"; import { uuid } from "@/lib/common/utils";
import { formatQueryDuration } from "@/lib/format/duration"; import { formatQueryDuration } from "@/lib/format/duration";
export type BackgroundTaskKind = "table-export" | "database-export" | "sql-file" | "data-transfer" | "multi-db-execution" | "schema-diff" | "data-compare"; export type BackgroundTaskKind = "table-export" | "database-export" | "data-dictionary" | "sql-file" | "data-transfer" | "multi-db-execution" | "schema-diff" | "data-compare";
export type BackgroundTaskStatus = "Running" | "Writing" | "Cancelling" | "Done" | "Error" | "Cancelled"; export type BackgroundTaskStatus = "Running" | "Writing" | "Cancelling" | "Done" | "Error" | "Cancelled";
export type DatabaseExportSource = "manual" | "scheduled"; export type DatabaseExportSource = "manual" | "scheduled";
@@ -86,6 +86,12 @@ export interface ExportTask {
compareAddedCount?: number; compareAddedCount?: number;
compareRemovedCount?: number; compareRemovedCount?: number;
compareModifiedCount?: number; compareModifiedCount?: number;
dictionaryPhase?: "preparing" | "collecting" | "generating" | "saving";
dictionaryCompleted?: number;
dictionaryTotal?: number;
dictionaryCurrent?: string;
dictionaryWarnings?: number;
dictionaryProgressKnown?: boolean;
canCancel?: boolean; canCancel?: boolean;
onOpen?: () => void; onOpen?: () => void;
onRemove?: () => void; onRemove?: () => void;
@@ -422,6 +428,35 @@ export function useExportTracker() {
return task; return task;
} }
function addDataDictionaryTask(exportId: string, label: string, total: number): ExportTask {
const task = reactive<ExportTask>({
exportId,
kind: "data-dictionary",
tableName: label,
format: "pdf",
filePath: "",
rowsExported: 0,
totalRows: null,
status: "Running",
errorMessage: null,
dictionaryPhase: "preparing",
dictionaryCompleted: 0,
dictionaryTotal: total,
dictionaryProgressKnown: false,
canCancel: false,
startedAt: Date.now(),
});
taskMap.set(exportId, task);
return task;
}
function updateDataDictionaryTask(exportId: string, changes: Partial<ExportTask>): void {
const task = taskMap.get(exportId);
if (!task || task.kind !== "data-dictionary") return;
Object.assign(task, changes);
if (task.status === "Done" || task.status === "Error") finishExportTask(task);
}
function addSqlFileTask(executionId: string, fileName: string, filePath: string): ExportTask { function addSqlFileTask(executionId: string, fileName: string, filePath: string): ExportTask {
sqlFileFailureStates.delete(executionId); sqlFileFailureStates.delete(executionId);
const task = reactive<ExportTask>({ const task = reactive<ExportTask>({
@@ -771,6 +806,8 @@ export function useExportTracker() {
hasActive, hasActive,
addTask, addTask,
addDatabaseExportTask, addDatabaseExportTask,
addDataDictionaryTask,
updateDataDictionaryTask,
addSqlFileTask, addSqlFileTask,
addDataTransferTask, addDataTransferTask,
addSchemaDiffTask, addSchemaDiffTask,
@@ -12,6 +12,7 @@ export const dataDictionaryEn = {
loadFailed: "Could not read structure: {error}", loadFailed: "Could not read structure: {error}",
warningsTitle: "This dictionary is incomplete", warningsTitle: "This dictionary is incomplete",
warningTableSkipped: "{table} was skipped: {reason}", warningTableSkipped: "{table} was skipped: {reason}",
objectUnavailable: "The object was not found during export.",
warningNoForeignKeys: "{engine} does not report foreign keys.", warningNoForeignKeys: "{engine} does not report foreign keys.",
warningCommentsUnsupported: "{engine} does not support table or column comments.", warningCommentsUnsupported: "{engine} does not support table or column comments.",
empty: "No tables to export.", empty: "No tables to export.",
@@ -97,6 +98,15 @@ export const dataDictionaryEn = {
continueOnError: "Continue on error", continueOnError: "Continue on error",
generating: "Generating", generating: "Generating",
generated: "Data dictionary generated:", generated: "Data dictionary generated:",
phasePreparing: "Preparing objects",
phaseCollecting: "Reading metadata",
phaseGenerating: "Generating PDF",
phaseSaving: "Saving file",
warningCount: "{count} warning(s)",
alreadyRunning: "An export is already running for this connection.",
backgroundStarted: "Export started in background tasks.",
backgroundDone: "Data dictionary exported: {path}",
backgroundFailed: "Data dictionary export failed: {error}",
fileExists: "The file already exists.", fileExists: "The file already exists.",
contentsHeading: "Contents", contentsHeading: "Contents",
kindTable: "Table", kindTable: "Table",
@@ -123,6 +133,7 @@ export const dataDictionaryZhCN = {
loadFailed: "无法读取结构:{error}", loadFailed: "无法读取结构:{error}",
warningsTitle: "这份字典不完整", warningsTitle: "这份字典不完整",
warningTableSkipped: "{table} 被跳过:{reason}", warningTableSkipped: "{table} 被跳过:{reason}",
objectUnavailable: "导出时未找到该对象。",
warningNoForeignKeys: "{engine} 不报告外键。", warningNoForeignKeys: "{engine} 不报告外键。",
warningCommentsUnsupported: "{engine} 不支持表或列注释。", warningCommentsUnsupported: "{engine} 不支持表或列注释。",
empty: "没有可导出的表。", empty: "没有可导出的表。",
@@ -208,6 +219,15 @@ export const dataDictionaryZhCN = {
continueOnError: "遇到错误时继续", continueOnError: "遇到错误时继续",
generating: "正在生成", generating: "正在生成",
generated: "已生成数据字典:", generated: "已生成数据字典:",
phasePreparing: "正在准备对象",
phaseCollecting: "正在读取元数据",
phaseGenerating: "正在生成 PDF",
phaseSaving: "正在保存文件",
warningCount: "{count} 条警告",
alreadyRunning: "该连接已有数据字典导出任务在运行。",
backgroundStarted: "导出已转入后台任务。",
backgroundDone: "数据字典已导出:{path}",
backgroundFailed: "数据字典导出失败:{error}",
fileExists: "文件已存在。", fileExists: "文件已存在。",
contentsHeading: "目录", contentsHeading: "目录",
kindTable: "表", kindTable: "表",
@@ -234,6 +254,7 @@ export const dataDictionaryZhTW = {
loadFailed: "無法讀取結構:{error}", loadFailed: "無法讀取結構:{error}",
warningsTitle: "這份字典不完整", warningsTitle: "這份字典不完整",
warningTableSkipped: "{table} 已略過:{reason}", warningTableSkipped: "{table} 已略過:{reason}",
objectUnavailable: "匯出時找不到該物件。",
warningNoForeignKeys: "{engine} 不回報外鍵。", warningNoForeignKeys: "{engine} 不回報外鍵。",
warningCommentsUnsupported: "{engine} 不支援資料表或欄位註解。", warningCommentsUnsupported: "{engine} 不支援資料表或欄位註解。",
empty: "沒有可匯出的資料表。", empty: "沒有可匯出的資料表。",
@@ -319,6 +340,15 @@ export const dataDictionaryZhTW = {
continueOnError: "遇到錯誤時繼續", continueOnError: "遇到錯誤時繼續",
generating: "正在產生", generating: "正在產生",
generated: "已產生資料字典:", generated: "已產生資料字典:",
phasePreparing: "正在準備物件",
phaseCollecting: "正在讀取中繼資料",
phaseGenerating: "正在產生 PDF",
phaseSaving: "正在儲存檔案",
warningCount: "{count} 條警告",
alreadyRunning: "此連線已有資料字典匯出任務在執行。",
backgroundStarted: "匯出已移至背景任務。",
backgroundDone: "資料字典已匯出:{path}",
backgroundFailed: "資料字典匯出失敗:{error}",
fileExists: "檔案已存在。", fileExists: "檔案已存在。",
contentsHeading: "目錄", contentsHeading: "目錄",
kindTable: "資料表", kindTable: "資料表",
@@ -345,6 +375,7 @@ export const dataDictionaryJa = {
loadFailed: "構造を読み込めません: {error}", loadFailed: "構造を読み込めません: {error}",
warningsTitle: "この辞書は不完全です", warningsTitle: "この辞書は不完全です",
warningTableSkipped: "{table} はスキップされました: {reason}", warningTableSkipped: "{table} はスキップされました: {reason}",
objectUnavailable: "The object was not found during export.",
warningNoForeignKeys: "{engine} は外部キーを報告しません。", warningNoForeignKeys: "{engine} は外部キーを報告しません。",
warningCommentsUnsupported: "{engine} はテーブルまたは列のコメントに対応していません。", warningCommentsUnsupported: "{engine} はテーブルまたは列のコメントに対応していません。",
empty: "エクスポートするテーブルがありません。", empty: "エクスポートするテーブルがありません。",
@@ -430,6 +461,15 @@ export const dataDictionaryJa = {
continueOnError: "Continue on error", continueOnError: "Continue on error",
generating: "Generating", generating: "Generating",
generated: "Data dictionary generated:", generated: "Data dictionary generated:",
phasePreparing: "Preparing objects",
phaseCollecting: "Reading metadata",
phaseGenerating: "Generating PDF",
phaseSaving: "Saving file",
warningCount: "{count} warning(s)",
alreadyRunning: "An export is already running for this connection.",
backgroundStarted: "Export started in background tasks.",
backgroundDone: "Data dictionary exported: {path}",
backgroundFailed: "Data dictionary export failed: {error}",
fileExists: "The file already exists.", fileExists: "The file already exists.",
contentsHeading: "Contents", contentsHeading: "Contents",
kindTable: "Table", kindTable: "Table",
@@ -456,6 +496,7 @@ export const dataDictionaryKo = {
loadFailed: "구조를 읽을 수 없습니다: {error}", loadFailed: "구조를 읽을 수 없습니다: {error}",
warningsTitle: "이 사전은 불완전합니다", warningsTitle: "이 사전은 불완전합니다",
warningTableSkipped: "{table}을(를) 건너뛰었습니다: {reason}", warningTableSkipped: "{table}을(를) 건너뛰었습니다: {reason}",
objectUnavailable: "The object was not found during export.",
warningNoForeignKeys: "{engine}은(는) 외래 키를 보고하지 않습니다.", warningNoForeignKeys: "{engine}은(는) 외래 키를 보고하지 않습니다.",
warningCommentsUnsupported: "{engine}은(는) 테이블 또는 열 주석을 지원하지 않습니다.", warningCommentsUnsupported: "{engine}은(는) 테이블 또는 열 주석을 지원하지 않습니다.",
empty: "내보낼 테이블이 없습니다.", empty: "내보낼 테이블이 없습니다.",
@@ -541,6 +582,15 @@ export const dataDictionaryKo = {
continueOnError: "Continue on error", continueOnError: "Continue on error",
generating: "Generating", generating: "Generating",
generated: "Data dictionary generated:", generated: "Data dictionary generated:",
phasePreparing: "Preparing objects",
phaseCollecting: "Reading metadata",
phaseGenerating: "Generating PDF",
phaseSaving: "Saving file",
warningCount: "{count} warning(s)",
alreadyRunning: "An export is already running for this connection.",
backgroundStarted: "Export started in background tasks.",
backgroundDone: "Data dictionary exported: {path}",
backgroundFailed: "Data dictionary export failed: {error}",
fileExists: "The file already exists.", fileExists: "The file already exists.",
contentsHeading: "Contents", contentsHeading: "Contents",
kindTable: "Table", kindTable: "Table",
@@ -567,6 +617,7 @@ export const dataDictionaryEs = {
loadFailed: "No se pudo leer la estructura: {error}", loadFailed: "No se pudo leer la estructura: {error}",
warningsTitle: "Este diccionario está incompleto", warningsTitle: "Este diccionario está incompleto",
warningTableSkipped: "{table} se omitió: {reason}", warningTableSkipped: "{table} se omitió: {reason}",
objectUnavailable: "The object was not found during export.",
warningNoForeignKeys: "{engine} no informa claves foráneas.", warningNoForeignKeys: "{engine} no informa claves foráneas.",
warningCommentsUnsupported: "{engine} no admite comentarios de tabla o columna.", warningCommentsUnsupported: "{engine} no admite comentarios de tabla o columna.",
empty: "No hay tablas para exportar.", empty: "No hay tablas para exportar.",
@@ -652,6 +703,15 @@ export const dataDictionaryEs = {
continueOnError: "Continue on error", continueOnError: "Continue on error",
generating: "Generating", generating: "Generating",
generated: "Data dictionary generated:", generated: "Data dictionary generated:",
phasePreparing: "Preparing objects",
phaseCollecting: "Reading metadata",
phaseGenerating: "Generating PDF",
phaseSaving: "Saving file",
warningCount: "{count} warning(s)",
alreadyRunning: "An export is already running for this connection.",
backgroundStarted: "Export started in background tasks.",
backgroundDone: "Data dictionary exported: {path}",
backgroundFailed: "Data dictionary export failed: {error}",
fileExists: "The file already exists.", fileExists: "The file already exists.",
contentsHeading: "Contents", contentsHeading: "Contents",
kindTable: "Table", kindTable: "Table",
@@ -678,6 +738,7 @@ export const dataDictionaryIt = {
loadFailed: "Impossibile leggere la struttura: {error}", loadFailed: "Impossibile leggere la struttura: {error}",
warningsTitle: "Questo dizionario è incompleto", warningsTitle: "Questo dizionario è incompleto",
warningTableSkipped: "{table} è stata ignorata: {reason}", warningTableSkipped: "{table} è stata ignorata: {reason}",
objectUnavailable: "The object was not found during export.",
warningNoForeignKeys: "{engine} non riporta le chiavi esterne.", warningNoForeignKeys: "{engine} non riporta le chiavi esterne.",
warningCommentsUnsupported: "{engine} non supporta i commenti di tabella o colonna.", warningCommentsUnsupported: "{engine} non supporta i commenti di tabella o colonna.",
empty: "Nessuna tabella da esportare.", empty: "Nessuna tabella da esportare.",
@@ -763,6 +824,15 @@ export const dataDictionaryIt = {
continueOnError: "Continue on error", continueOnError: "Continue on error",
generating: "Generating", generating: "Generating",
generated: "Data dictionary generated:", generated: "Data dictionary generated:",
phasePreparing: "Preparing objects",
phaseCollecting: "Reading metadata",
phaseGenerating: "Generating PDF",
phaseSaving: "Saving file",
warningCount: "{count} warning(s)",
alreadyRunning: "An export is already running for this connection.",
backgroundStarted: "Export started in background tasks.",
backgroundDone: "Data dictionary exported: {path}",
backgroundFailed: "Data dictionary export failed: {error}",
fileExists: "The file already exists.", fileExists: "The file already exists.",
contentsHeading: "Contents", contentsHeading: "Contents",
kindTable: "Table", kindTable: "Table",
@@ -789,6 +859,7 @@ export const dataDictionaryPtBR = {
loadFailed: "Não foi possível ler a estrutura: {error}", loadFailed: "Não foi possível ler a estrutura: {error}",
warningsTitle: "Este dicionário está incompleto", warningsTitle: "Este dicionário está incompleto",
warningTableSkipped: "{table} foi ignorada: {reason}", warningTableSkipped: "{table} foi ignorada: {reason}",
objectUnavailable: "The object was not found during export.",
warningNoForeignKeys: "{engine} não informa chaves estrangeiras.", warningNoForeignKeys: "{engine} não informa chaves estrangeiras.",
warningCommentsUnsupported: "{engine} não oferece comentários de tabela ou coluna.", warningCommentsUnsupported: "{engine} não oferece comentários de tabela ou coluna.",
empty: "Não há tabelas para exportar.", empty: "Não há tabelas para exportar.",
@@ -874,6 +945,15 @@ export const dataDictionaryPtBR = {
continueOnError: "Continue on error", continueOnError: "Continue on error",
generating: "Generating", generating: "Generating",
generated: "Data dictionary generated:", generated: "Data dictionary generated:",
phasePreparing: "Preparing objects",
phaseCollecting: "Reading metadata",
phaseGenerating: "Generating PDF",
phaseSaving: "Saving file",
warningCount: "{count} warning(s)",
alreadyRunning: "An export is already running for this connection.",
backgroundStarted: "Export started in background tasks.",
backgroundDone: "Data dictionary exported: {path}",
backgroundFailed: "Data dictionary export failed: {error}",
fileExists: "The file already exists.", fileExists: "The file already exists.",
contentsHeading: "Contents", contentsHeading: "Contents",
kindTable: "Table", kindTable: "Table",
@@ -900,6 +980,7 @@ export const dataDictionaryTr = {
loadFailed: "Yapı okunamadı: {error}", loadFailed: "Yapı okunamadı: {error}",
warningsTitle: "Bu sözlük eksik", warningsTitle: "Bu sözlük eksik",
warningTableSkipped: "{table} atlandı: {reason}", warningTableSkipped: "{table} atlandı: {reason}",
objectUnavailable: "The object was not found during export.",
warningNoForeignKeys: "{engine} yabancı anahtar bildirmiyor.", warningNoForeignKeys: "{engine} yabancı anahtar bildirmiyor.",
warningCommentsUnsupported: "{engine} tablo veya sütun açıklamalarını desteklemiyor.", warningCommentsUnsupported: "{engine} tablo veya sütun açıklamalarını desteklemiyor.",
empty: "Dışa aktarılacak tablo yok.", empty: "Dışa aktarılacak tablo yok.",
@@ -985,6 +1066,15 @@ export const dataDictionaryTr = {
continueOnError: "Continue on error", continueOnError: "Continue on error",
generating: "Generating", generating: "Generating",
generated: "Data dictionary generated:", generated: "Data dictionary generated:",
phasePreparing: "Preparing objects",
phaseCollecting: "Reading metadata",
phaseGenerating: "Generating PDF",
phaseSaving: "Saving file",
warningCount: "{count} warning(s)",
alreadyRunning: "An export is already running for this connection.",
backgroundStarted: "Export started in background tasks.",
backgroundDone: "Data dictionary exported: {path}",
backgroundFailed: "Data dictionary export failed: {error}",
fileExists: "The file already exists.", fileExists: "The file already exists.",
contentsHeading: "Contents", contentsHeading: "Contents",
kindTable: "Table", kindTable: "Table",
@@ -1011,6 +1101,7 @@ export const dataDictionaryRu = {
loadFailed: "Не удалось прочитать структуру: {error}", loadFailed: "Не удалось прочитать структуру: {error}",
warningsTitle: "Этот словарь неполный", warningsTitle: "Этот словарь неполный",
warningTableSkipped: "{table} пропущена: {reason}", warningTableSkipped: "{table} пропущена: {reason}",
objectUnavailable: "The object was not found during export.",
warningNoForeignKeys: "{engine} не сообщает внешние ключи.", warningNoForeignKeys: "{engine} не сообщает внешние ключи.",
warningCommentsUnsupported: "{engine} не поддерживает комментарии таблиц или столбцов.", warningCommentsUnsupported: "{engine} не поддерживает комментарии таблиц или столбцов.",
empty: "Нет таблиц для экспорта.", empty: "Нет таблиц для экспорта.",
@@ -1096,6 +1187,15 @@ export const dataDictionaryRu = {
continueOnError: "Continue on error", continueOnError: "Continue on error",
generating: "Generating", generating: "Generating",
generated: "Data dictionary generated:", generated: "Data dictionary generated:",
phasePreparing: "Preparing objects",
phaseCollecting: "Reading metadata",
phaseGenerating: "Generating PDF",
phaseSaving: "Saving file",
warningCount: "{count} warning(s)",
alreadyRunning: "An export is already running for this connection.",
backgroundStarted: "Export started in background tasks.",
backgroundDone: "Data dictionary exported: {path}",
backgroundFailed: "Data dictionary export failed: {error}",
fileExists: "The file already exists.", fileExists: "The file already exists.",
contentsHeading: "Contents", contentsHeading: "Contents",
kindTable: "Table", kindTable: "Table",
@@ -1122,6 +1222,7 @@ export const dataDictionaryAz = {
loadFailed: "Strukturu oxumaq mümkün olmadı: {error}", loadFailed: "Strukturu oxumaq mümkün olmadı: {error}",
warningsTitle: "Bu lüğət tam deyil", warningsTitle: "Bu lüğət tam deyil",
warningTableSkipped: "{table} ötürülüb: {reason}", warningTableSkipped: "{table} ötürülüb: {reason}",
objectUnavailable: "The object was not found during export.",
warningNoForeignKeys: "{engine} xarici açarları bildirmir.", warningNoForeignKeys: "{engine} xarici açarları bildirmir.",
warningCommentsUnsupported: "{engine} cədvəl və ya sütun şərhlərini dəstəkləmir.", warningCommentsUnsupported: "{engine} cədvəl və ya sütun şərhlərini dəstəkləmir.",
empty: "İxrac ediləcək cədvəl yoxdur.", empty: "İxrac ediləcək cədvəl yoxdur.",
@@ -1207,6 +1308,15 @@ export const dataDictionaryAz = {
continueOnError: "Continue on error", continueOnError: "Continue on error",
generating: "Generating", generating: "Generating",
generated: "Data dictionary generated:", generated: "Data dictionary generated:",
phasePreparing: "Preparing objects",
phaseCollecting: "Reading metadata",
phaseGenerating: "Generating PDF",
phaseSaving: "Saving file",
warningCount: "{count} warning(s)",
alreadyRunning: "An export is already running for this connection.",
backgroundStarted: "Export started in background tasks.",
backgroundDone: "Data dictionary exported: {path}",
backgroundFailed: "Data dictionary export failed: {error}",
fileExists: "The file already exists.", fileExists: "The file already exists.",
contentsHeading: "Contents", contentsHeading: "Contents",
kindTable: "Table", kindTable: "Table",
+1
View File
@@ -255,6 +255,7 @@ export const listDialectDataTypes = forward("listDialectDataTypes");
// Docs // Docs
export const collectDocsSnapshot = forward("collectDocsSnapshot"); export const collectDocsSnapshot = forward("collectDocsSnapshot");
export const collectDocsSnapshotForExport = forward("collectDocsSnapshotForExport");
export const loadDocsAnnotations = forward("loadDocsAnnotations"); export const loadDocsAnnotations = forward("loadDocsAnnotations");
export const applyDocsAnnotations = forward("applyDocsAnnotations"); export const applyDocsAnnotations = forward("applyDocsAnnotations");
export const saveDocsAnnotations = forward("saveDocsAnnotations"); export const saveDocsAnnotations = forward("saveDocsAnnotations");
+5
View File
@@ -1352,6 +1352,11 @@ export async function collectDocsSnapshot(connectionId: string, database: string
return post("/api/docs/snapshot", { connectionId, database, schemas, tables, projectName }); return post("/api/docs/snapshot", { connectionId, database, schemas, tables, projectName });
} }
// HTTP snapshot transport is request/response; its progress remains indeterminate.
export async function collectDocsSnapshotForExport(connectionId: string, database: string, schemas: string[], tables: string[], _onProgress: (progress: import("./tauri").DocsCollectProgress) => void): Promise<SchemaSnapshot> {
return post("/api/docs/snapshot", { connectionId, database, schemas, tables, projectName: database, maxConcurrentTables: 2 });
}
export async function loadDocsAnnotations(connectionId: string): Promise<AnnotationFile | null> { export async function loadDocsAnnotations(connectionId: string): Promise<AnnotationFile | null> {
return post("/api/docs/annotations/load", { connectionId }); return post("/api/docs/annotations/load", { connectionId });
} }
+13
View File
@@ -1,4 +1,5 @@
import { invoke as tauriInvoke } from "@tauri-apps/api/core"; import { invoke as tauriInvoke } from "@tauri-apps/api/core";
import { Channel } from "@tauri-apps/api/core";
import type { MongoDumpFormat, MongoDumpSourceInput, MongoDumpCatalog, MongoRestoreSourcePreview, MongoDatabaseDumpRequest, MongoDatabaseRestoreRequest, MongoDatabaseDumpProgress } from "./mongodbDumpTypes"; import type { MongoDumpFormat, MongoDumpSourceInput, MongoDumpCatalog, MongoRestoreSourcePreview, MongoDatabaseDumpRequest, MongoDatabaseRestoreRequest, MongoDatabaseDumpProgress } from "./mongodbDumpTypes";
import type { MongoRestoreUpload, MongoSourceReadOptions } from "./mongodbDumpTypes"; import type { MongoRestoreUpload, MongoSourceReadOptions } from "./mongodbDumpTypes";
import type { UserSkillRootSettings, UserSkillsListResult, UserSkillsReadResult } from "@/types/userSkills"; import type { UserSkillRootSettings, UserSkillsListResult, UserSkillsReadResult } from "@/types/userSkills";
@@ -2465,6 +2466,18 @@ export async function collectDocsSnapshot(connectionId: string, database: string
return invoke("docs_collect_snapshot", { connectionId, database, schemas, tables, projectName }); return invoke("docs_collect_snapshot", { connectionId, database, schemas, tables, projectName });
} }
export interface DocsCollectProgress {
completed: number;
total: number;
current: string;
}
export async function collectDocsSnapshotForExport(connectionId: string, database: string, schemas: string[], tables: string[], onProgress: (progress: DocsCollectProgress) => void): Promise<SchemaSnapshot> {
const channel = new Channel<DocsCollectProgress>();
channel.onmessage = onProgress;
return invoke("docs_collect_snapshot_for_export", { connectionId, database, schemas, tables, projectName: database, onProgress: channel });
}
export async function loadDocsAnnotations(connectionId: string): Promise<AnnotationFile | null> { export async function loadDocsAnnotations(connectionId: string): Promise<AnnotationFile | null> {
return invoke("docs_load_annotations", { connectionId }); return invoke("docs_load_annotations", { connectionId });
} }
@@ -0,0 +1,159 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { useExportTracker } from "@/composables/useExportTracker";
import { hasActiveDictionaryExport, startDataDictionaryExport, type DictionaryExportRequest } from "@/lib/docs/dataDictionaryExportTask";
import * as api from "@/lib/backend/api";
import { renderDataDictionaryPdf } from "@/lib/docs/renderDataDictionaryPdf";
import { saveDataDictionaryFile } from "@/lib/docs/saveDataDictionaryFile";
vi.mock("@/lib/backend/api", () => ({
collectDocsSnapshotForExport: vi.fn(),
loadDocsAnnotations: vi.fn(),
applyDocsAnnotations: vi.fn(),
}));
vi.mock("@/lib/docs/renderDataDictionaryPdf", () => ({ renderDataDictionaryPdf: vi.fn() }));
vi.mock("@/lib/docs/saveDataDictionaryFile", () => ({ saveDataDictionaryFile: vi.fn() }));
const tracker = useExportTracker();
const snapshot = (database: string, names: string[], warnings: unknown[] = []) => ({
project: { database },
tables: names.map((name) => ({ name, schema: null, kind: "TABLE" })),
warnings,
});
function request(databases = ["shop"], names = ["a"]): DictionaryExportRequest {
return {
connectionId: "connection",
databases,
schemas: [],
objects: databases.flatMap((database) => names.map((name) => ({ database, schema: "", name, kind: "TABLE" as const }))),
layout: {} as DictionaryExportRequest["layout"],
labels: {} as DictionaryExportRequest["labels"],
outputPath: "/tmp/dictionary.pdf",
overwrite: true,
continueOnError: true,
warningText: (warning) => (warning.kind === "tableSkipped" ? (warning.reason ?? "skipped") : "warning"),
skippedDatabase: (database, reason) => `${database}: ${reason}`,
missingObject: (object) => `${object.database}.${object.name}: unavailable`,
onSuccess: vi.fn(),
onFailure: vi.fn(),
};
}
beforeEach(() => {
vi.mocked(api.collectDocsSnapshotForExport).mockReset();
vi.mocked(api.loadDocsAnnotations).mockResolvedValue(null);
vi.mocked(api.applyDocsAnnotations).mockImplementation(async (_id, raw) => raw);
vi.mocked(renderDataDictionaryPdf).mockReset();
vi.mocked(renderDataDictionaryPdf).mockResolvedValue(new Uint8Array([1, 2]));
vi.mocked(saveDataDictionaryFile).mockReset();
vi.mocked(saveDataDictionaryFile).mockResolvedValue("/tmp/dictionary.pdf");
});
afterEach(async () => {
await vi.waitFor(() => expect(hasActiveDictionaryExport("connection")).toBe(false));
for (const task of tracker.tasks.value) tracker.removeTask(task.exportId);
});
describe("background dictionary export", () => {
it("survives its caller, tracks completed objects over multiple databases and finishes only after the file is written", async () => {
let resolveSave!: (path: string) => void;
vi.mocked(saveDataDictionaryFile).mockImplementation(
() =>
new Promise((resolve) => {
resolveSave = resolve;
}),
);
vi.mocked(api.collectDocsSnapshotForExport).mockImplementation(async (_id, database, _schemas, _names, progress) => {
progress({ completed: 0, total: 2, current: "" });
progress({ completed: 1, total: 2, current: "a" });
progress({ completed: 2, total: 2, current: "b" });
return snapshot(database, ["a", "b"]) as never;
});
const input = request(["shop", "archive"], ["a", "b"]);
expect(startDataDictionaryExport(input)).toBe(true);
expect(startDataDictionaryExport(input)).toBe(false);
await vi.waitFor(() => expect(saveDataDictionaryFile).toHaveBeenCalledOnce());
const task = tracker.tasks.value[0]!;
expect(task.dictionaryCompleted).toBe(4);
expect(task.dictionaryTotal).toBe(4);
expect(task.status).toBe("Writing");
expect(task.filePath).toBe("");
expect(input.onSuccess).not.toHaveBeenCalled();
resolveSave("/tmp/dictionary.pdf");
await vi.waitFor(() => expect(task.status).toBe("Done"));
expect(task.filePath).toBe("/tmp/dictionary.pdf");
expect(input.onSuccess).toHaveBeenCalledWith("/tmp/dictionary.pdf", 0);
expect(hasActiveDictionaryExport("connection")).toBe(false);
});
it("reports save failures, frees the connection slot and permits a retry", async () => {
vi.mocked(api.collectDocsSnapshotForExport).mockResolvedValue(snapshot("shop", ["a"]) as never);
vi.mocked(saveDataDictionaryFile).mockRejectedValueOnce(new Error("disk full"));
const input = request();
startDataDictionaryExport(input);
await vi.waitFor(() => expect(tracker.tasks.value[0]?.status).toBe("Error"));
expect(tracker.tasks.value[0]?.errorMessage).toBe("disk full");
expect(input.onSuccess).not.toHaveBeenCalled();
expect(input.onFailure).toHaveBeenCalledOnce();
expect(startDataDictionaryExport(request())).toBe(true);
await vi.waitFor(() => expect(tracker.tasks.value[1]?.status).toBe("Done"));
});
it("preserves skipped-object warnings only when continue-on-error is enabled", async () => {
vi.mocked(api.collectDocsSnapshotForExport).mockResolvedValue(snapshot("shop", ["a"], [{ kind: "tableSkipped", table: ".b", reason: "denied" }]) as never);
const input = request(["shop"], ["a", "b"]);
startDataDictionaryExport(input);
await vi.waitFor(() => expect(tracker.tasks.value[0]?.status).toBe("Done"));
expect(tracker.tasks.value[0]?.dictionaryWarnings).toBe(1);
expect(renderDataDictionaryPdf).toHaveBeenCalledWith(expect.any(Array), expect.anything(), expect.anything(), ["denied"]);
const strict = request(["shop"], ["a", "b"]);
strict.continueOnError = false;
startDataDictionaryExport(strict);
await vi.waitFor(() => expect(tracker.tasks.value[1]?.status).toBe("Error"));
expect(saveDataDictionaryFile).toHaveBeenCalledTimes(1);
});
it("does not silently export a partial PDF when a selected object disappears", async () => {
vi.mocked(api.collectDocsSnapshotForExport).mockResolvedValue(snapshot("shop", ["a"]) as never);
const input = request(["shop"], ["a", "b"]);
startDataDictionaryExport(input);
await vi.waitFor(() => expect(tracker.tasks.value[0]?.status).toBe("Done"));
expect(renderDataDictionaryPdf).toHaveBeenCalledWith(expect.any(Array), expect.anything(), expect.anything(), ["shop.b: unavailable"]);
expect(tracker.tasks.value[0]?.dictionaryWarnings).toBe(1);
const strict = request(["shop"], ["a", "b"]);
strict.continueOnError = false;
startDataDictionaryExport(strict);
await vi.waitFor(() => expect(tracker.tasks.value[1]?.status).toBe("Error"));
expect(tracker.tasks.value[1]?.errorMessage).toContain("shop.b: unavailable");
expect(saveDataDictionaryFile).toHaveBeenCalledTimes(1);
});
it("ignores stale and out-of-order progress callbacks", async () => {
let firstProgress!: Parameters<typeof api.collectDocsSnapshotForExport>[4];
vi.mocked(api.collectDocsSnapshotForExport).mockImplementation(async (_id, database, _schemas, _names, progress) => {
if (database === "shop") {
firstProgress = progress;
progress({ completed: 2, total: 2, current: "b" });
progress({ completed: 1, total: 2, current: "a" });
} else {
firstProgress({ completed: 0, total: 2, current: "old" });
progress({ completed: 1, total: 2, current: "new" });
}
return snapshot(database, ["a", "b"]) as never;
});
let resolveRender!: (bytes: Uint8Array) => void;
vi.mocked(renderDataDictionaryPdf).mockImplementation(
() =>
new Promise((resolve) => {
resolveRender = resolve;
}),
);
startDataDictionaryExport(request(["shop", "archive"], ["a", "b"]));
await vi.waitFor(() => expect(renderDataDictionaryPdf).toHaveBeenCalled());
expect(tracker.tasks.value[0]?.dictionaryCompleted).toBe(4);
expect(tracker.tasks.value[0]?.dictionaryCurrent).toBe("");
resolveRender(new Uint8Array([1]));
await vi.waitFor(() => expect(tracker.tasks.value[0]?.status).toBe("Done"));
});
});
@@ -0,0 +1,123 @@
import * as api from "@/lib/backend/api";
import { useExportTracker } from "@/composables/useExportTracker";
import { emptyAnnotations } from "@/docs/annotationEdits";
import type { SchemaSnapshot, SnapshotWarning } from "@/docs/types";
import { uuid } from "@/lib/common/utils";
import { exportBlockedBySkippedTables, objectKey, orderDictionaryTables, warningsForSelection, type DataDictionaryLabels, type DictionaryLayout, type DictionaryObjectRef, type DictionaryTable } from "./dataDictionary";
import { renderDataDictionaryPdf } from "./renderDataDictionaryPdf";
import { saveDataDictionaryFile } from "./saveDataDictionaryFile";
export interface DictionaryExportRequest {
connectionId: string;
databases: string[];
schemas: Array<{ database: string; name: string }>;
objects: DictionaryObjectRef[];
snapshot?: SchemaSnapshot | null;
layout: DictionaryLayout;
labels: DataDictionaryLabels;
outputPath: string;
overwrite: boolean;
continueOnError: boolean;
warningText: (warning: SnapshotWarning) => string;
skippedDatabase: (database: string, reason: string) => string;
missingObject: (object: DictionaryObjectRef) => string;
onSuccess: (path: string, warnings: number) => void;
onFailure: (error: unknown) => void;
}
// Export lifetimes belong to the app, not the dialog instance. One job per
// connection also bounds the aggregate metadata pressure on its pool.
const activeConnections = new Set<string>();
export function hasActiveDictionaryExport(connectionId: string): boolean {
return activeConnections.has(connectionId);
}
export function startDataDictionaryExport(request: DictionaryExportRequest): boolean {
if (activeConnections.has(request.connectionId)) return false;
activeConnections.add(request.connectionId);
const tracker = useExportTracker();
const id = uuid();
tracker.addDataDictionaryTask(id, request.databases.join(", "), request.objects.length);
void (async () => {
const warnings: string[] = [];
const tables: DictionaryTable[] = [];
const order = request.objects.map(objectKey);
let completed = 0;
try {
for (const database of request.databases) {
const selected = request.objects.filter((item) => item.database === database);
if (!selected.length) continue;
tracker.updateDataDictionaryTask(id, { dictionaryPhase: "preparing", dictionaryCompleted: completed, dictionaryCurrent: database });
try {
let merged: SchemaSnapshot;
const fromSnapshot =
request.snapshot?.project.database === database
? orderDictionaryTables(
request.snapshot.tables.map((table) => ({ ...table, database })),
selected.map(objectKey),
)
: [];
if (request.snapshot && fromSnapshot.length === selected.length) {
merged = request.snapshot;
tracker.updateDataDictionaryTask(id, { dictionaryPhase: "collecting", dictionaryProgressKnown: true });
} else {
const schemas = request.schemas.filter((item) => item.database === database).map((item) => item.name);
const names = selected.map((item) => (item.schema ? `${item.schema}.${item.name}` : item.name));
const baseCompleted = completed;
let acceptingProgress = true;
let raw: SchemaSnapshot;
try {
raw = await api.collectDocsSnapshotForExport(request.connectionId, database, schemas, names, (progress) => {
if (!acceptingProgress) return;
const task = tracker.tasks.value.find((item) => item.exportId === id);
tracker.updateDataDictionaryTask(id, {
dictionaryPhase: "collecting",
dictionaryProgressKnown: progress.total > 0,
dictionaryCompleted: Math.max(task?.dictionaryCompleted ?? 0, baseCompleted + Math.min(progress.completed, selected.length)),
dictionaryCurrent: progress.current || database,
});
});
} finally {
acceptingProgress = false;
}
const file = (await api.loadDocsAnnotations(request.connectionId)) ?? emptyAnnotations();
merged = await api.applyDocsAnnotations(request.connectionId, raw, file);
}
const relevant = warningsForSelection(merged.warnings, selected);
const collected = new Set(merged.tables.map((table) => objectKey({ database, schema: table.schema ?? "", name: table.name })));
const missing = selected.filter((item) => !collected.has(objectKey(item)));
const missingWarnings = missing.filter((item) => !relevant.some((warning) => warning.kind === "tableSkipped" && (warning.table === "*" || warning.table === `${item.schema}.*` || warning.table === item.name || warning.table === `${item.schema}.${item.name}`))).map(request.missingObject);
if (exportBlockedBySkippedTables(relevant, request.continueOnError) || (!request.continueOnError && missingWarnings.length > 0)) {
throw new Error(relevant.map(request.warningText).find(Boolean) || missingWarnings[0] || database);
}
warnings.push(...relevant.map(request.warningText).filter(Boolean));
warnings.push(...missingWarnings);
tables.push(...merged.tables.filter((table) => selected.some((item) => objectKey(item) === objectKey({ database, schema: table.schema ?? "", name: table.name }))).map((table) => ({ ...table, database })));
} catch (error) {
if (!request.continueOnError) throw error;
warnings.push(request.skippedDatabase(database, error instanceof Error ? error.message : String(error)));
}
completed += selected.length;
tracker.updateDataDictionaryTask(id, { dictionaryCompleted: completed, dictionaryCurrent: "", dictionaryWarnings: warnings.length });
}
if (!tables.length) throw new Error("No selected objects could be collected");
tracker.updateDataDictionaryTask(id, { dictionaryPhase: "generating", dictionaryCurrent: "" });
// Worker messages cannot clone Vue proxies; capture only serializable metadata.
const ordered = JSON.parse(JSON.stringify(orderDictionaryTables(tables, order))) as DictionaryTable[];
const bytes = await renderDataDictionaryPdf(ordered, request.labels, request.layout, request.continueOnError ? warnings : []);
tracker.updateDataDictionaryTask(id, { dictionaryPhase: "saving", status: "Writing" });
const path = await saveDataDictionaryFile(request.outputPath, bytes, { overwrite: request.overwrite, grantedPath: request.outputPath });
if (!path) throw new Error("File was not saved");
tracker.updateDataDictionaryTask(id, { status: "Done", filePath: path, dictionaryWarnings: warnings.length });
request.onSuccess(path, warnings.length);
} catch (error) {
tracker.updateDataDictionaryTask(id, { status: "Error", errorMessage: error instanceof Error ? error.message : String(error) });
request.onFailure(error);
} finally {
activeConnections.delete(request.connectionId);
}
})();
return true;
}
@@ -0,0 +1,12 @@
import { buildDataDictionaryPdf } from "./dataDictionaryPdf";
import type { DataDictionaryLabels, DictionaryLayout, DictionaryTable } from "./dataDictionary";
self.onmessage = (event: MessageEvent<{ tables: DictionaryTable[]; labels: DataDictionaryLabels; layout: DictionaryLayout; warnings: string[] }>) => {
try {
const { tables, labels, layout, warnings } = event.data;
const bytes = buildDataDictionaryPdf(tables, labels, layout, warnings);
self.postMessage({ bytes }, { transfer: [bytes.buffer] });
} catch (error) {
self.postMessage({ error: error instanceof Error ? error.message : String(error) });
}
};
@@ -0,0 +1,18 @@
import type { DataDictionaryLabels, DictionaryLayout, DictionaryTable } from "./dataDictionary";
export function renderDataDictionaryPdf(tables: DictionaryTable[], labels: DataDictionaryLabels, layout: DictionaryLayout, warnings: string[]): Promise<Uint8Array> {
return new Promise((resolve, reject) => {
const worker = new Worker(new URL("./dataDictionaryPdf.worker.ts", import.meta.url), { type: "module" });
worker.onmessage = (event: MessageEvent<{ bytes?: Uint8Array; error?: string }>) => {
worker.terminate();
if (event.data.error) reject(new Error(event.data.error));
else if (event.data.bytes) resolve(event.data.bytes);
else reject(new Error("PDF worker returned no data"));
};
worker.onerror = (event) => {
worker.terminate();
reject(new Error(event.message));
};
worker.postMessage({ tables, labels, layout, warnings });
});
}
@@ -7,18 +7,21 @@ export class DictionaryFileExistsError extends Error {
} }
} }
export async function chooseDataDictionaryPath(suggestedName: string): Promise<string | null> {
if (!isTauriRuntime()) return suggestedName;
const { save } = await import("@tauri-apps/plugin-dialog");
return save({ defaultPath: suggestedName, filters: [{ name: "PDF", extensions: ["pdf"] }] });
}
/** /**
* Suggested name is only the save dialog's defaultPath. The write uses the path * Suggested name is only the save dialog's defaultPath. The write uses the path
* `save()` just returned. Tauri's dialog grant covers that exact path, not a * `save()` just returned. Tauri's dialog grant covers that exact path, not a
* typed path or a timestamp appended after the dialog closes. * typed path or a timestamp appended after the dialog closes.
*/ */
export async function saveDataDictionaryFile(suggestedName: string, content: Uint8Array, options: { overwrite: boolean }): Promise<string | null> { export async function saveDataDictionaryFile(suggestedName: string, content: Uint8Array, options: { overwrite: boolean; grantedPath?: string }): Promise<string | null> {
if (isTauriRuntime()) { if (isTauriRuntime()) {
const [{ save }, fs] = await Promise.all([import("@tauri-apps/plugin-dialog"), import("@tauri-apps/plugin-fs")]); const fs = await import("@tauri-apps/plugin-fs");
const granted = await save({ const granted = options.grantedPath ?? (await chooseDataDictionaryPath(suggestedName));
defaultPath: suggestedName,
filters: [{ name: "PDF", extensions: ["pdf"] }],
});
if (!granted) return null; if (!granted) return null;
if (!options.overwrite && (await fs.exists(granted))) throw new DictionaryFileExistsError(); if (!options.overwrite && (await fs.exists(granted))) throw new DictionaryFileExistsError();
await fs.writeFile(granted, content); await fs.writeFile(granted, content);
+91 -74
View File
@@ -1,5 +1,5 @@
use std::collections::{BTreeMap, HashSet}; use std::collections::{BTreeMap, HashSet};
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use futures::stream::{self, StreamExt}; use futures::stream::{self, StreamExt};
@@ -144,6 +144,18 @@ pub async fn collect_snapshot(
options: &CollectOptions, options: &CollectOptions,
progress: &(dyn Fn(CollectProgress) + Send + Sync), progress: &(dyn Fn(CollectProgress) + Send + Sync),
cancel: &AtomicBool, cancel: &AtomicBool,
) -> Result<SchemaSnapshot, String> {
collect_snapshot_with_concurrency(state, connection, options, progress, cancel, MAX_CONCURRENT_TABLES).await
}
/// Dictionary exports use a smaller limit to leave pool capacity for interactive queries.
pub async fn collect_snapshot_with_concurrency(
state: &AppState,
connection: &ConnectionConfig,
options: &CollectOptions,
progress: &(dyn Fn(CollectProgress) + Send + Sync),
cancel: &AtomicBool,
max_concurrent_tables: usize,
) -> Result<SchemaSnapshot, String> { ) -> Result<SchemaSnapshot, String> {
let mut warnings: Vec<SnapshotWarning> = Vec::new(); let mut warnings: Vec<SnapshotWarning> = Vec::new();
let engine = database_type_label(connection.db_type); let engine = database_type_label(connection.db_type);
@@ -199,64 +211,64 @@ pub async fn collect_snapshot(
let total = targets.len(); let total = targets.len();
progress(CollectProgress { completed: 0, total, current: String::new() });
let completed = AtomicUsize::new(0);
let collected: Vec<Result<(DocTable, Vec<SnapshotWarning>), SnapshotWarning>> = let collected: Vec<Result<(DocTable, Vec<SnapshotWarning>), SnapshotWarning>> =
stream::iter(targets.into_iter().enumerate()) stream::iter(targets.into_iter().enumerate())
.map(|(index, (schema_name, info))| { .map(|(_index, (schema_name, info))| {
let database = options.database.clone(); let database = options.database.clone();
let completed = &completed;
async move { async move {
if cancelled(cancel) { let current = format!("{schema_name}.{}", info.name);
return Err(SnapshotWarning::TableSkipped { let outcome = async {
table: info.name.clone(), if cancelled(cancel) {
reason: "cancelled".to_string(), return Err(SnapshotWarning::TableSkipped {
}); table: info.name.clone(),
} reason: "cancelled".to_string(),
progress(CollectProgress {
completed: index,
total,
current: format!("{schema_name}.{}", info.name),
});
let columns = schema::get_columns_core(state, connection_id, &database, &schema_name, &info.name)
.await
.map_err(|error| SnapshotWarning::TableSkipped {
table: format!("{schema_name}.{}", info.name),
reason: error,
})?;
let column_notes = database_column_notes(&columns);
let mut table_warnings = Vec::new();
// Indexes feed `relations.rs`'s uniqueness check, so a
// failure here must not look identical to "this table
// genuinely has no indexes" — that would silently
// downgrade a OneToOne relationship to ManyToOne.
let indexes = match schema::list_indexes_core(
state,
connection_id,
&database,
&schema_name,
&info.name,
)
.await
{
Ok(indexes) => indexes,
Err(error) => {
table_warnings.push(SnapshotWarning::TableSkipped {
table: format!("{schema_name}.{}", info.name),
reason: format!("indexes unavailable: {error}"),
}); });
Vec::new()
} }
};
// Foreign keys also degrade to empty rather than failing the let columns =
// table, but a real query failure must not look identical to schema::get_columns_core(state, connection_id, &database, &schema_name, &info.name)
// "this table genuinely has no foreign keys" — it is reported .await
// as its own warning instead of being silently discarded. .map_err(|error| SnapshotWarning::TableSkipped {
let foreign_keys = table: format!("{schema_name}.{}", info.name),
match schema::list_foreign_keys_core(state, connection_id, &database, &schema_name, &info.name) reason: error,
.await })?;
let column_notes = database_column_notes(&columns);
let mut table_warnings = Vec::new();
// Indexes feed `relations.rs`'s uniqueness check, so a
// failure here must not look identical to "this table
// genuinely has no indexes" — that would silently
// downgrade a OneToOne relationship to ManyToOne.
let indexes =
match schema::list_indexes_core(state, connection_id, &database, &schema_name, &info.name)
.await
{
Ok(indexes) => indexes,
Err(error) => {
table_warnings.push(SnapshotWarning::TableSkipped {
table: format!("{schema_name}.{}", info.name),
reason: format!("indexes unavailable: {error}"),
});
Vec::new()
}
};
// Foreign keys also degrade to empty rather than failing the
// table, but a real query failure must not look identical to
// "this table genuinely has no foreign keys" — it is reported
// as its own warning instead of being silently discarded.
let foreign_keys = match schema::list_foreign_keys_core(
state,
connection_id,
&database,
&schema_name,
&info.name,
)
.await
{ {
Ok(keys) => keys, Ok(keys) => keys,
Err(error) => { Err(error) => {
@@ -268,31 +280,36 @@ pub async fn collect_snapshot(
} }
}; };
Ok(( Ok((
DocTable { DocTable {
schema: (!schema_name.is_empty()).then(|| schema_name.clone()), schema: (!schema_name.is_empty()).then(|| schema_name.clone()),
name: info.name.clone(), name: info.name.clone(),
kind: table_kind_from(&info.table_type), kind: table_kind_from(&info.table_type),
columns, columns,
indexes, indexes,
foreign_keys, foreign_keys,
group_id: None, group_id: None,
note: info.comment.clone().filter(|value| !value.trim().is_empty()), note: info.comment.clone().filter(|value| !value.trim().is_empty()),
note_source: if info.comment.as_deref().is_some_and(|v| !v.trim().is_empty()) { note_source: if info.comment.as_deref().is_some_and(|v| !v.trim().is_empty()) {
NoteSource::Database NoteSource::Database
} else { } else {
NoteSource::None NoteSource::None
},
shadowed_note: None,
column_notes,
estimated_rows: None,
view_definition: None,
}, },
shadowed_note: None, table_warnings,
column_notes, ))
estimated_rows: None, }
view_definition: None, .await;
}, let done = completed.fetch_add(1, Ordering::SeqCst) + 1;
table_warnings, progress(CollectProgress { completed: done, total, current });
)) outcome
} }
}) })
.buffer_unordered(MAX_CONCURRENT_TABLES) .buffer_unordered(max_concurrent_tables.max(1))
.collect() .collect()
.await; .await;
+1 -1
View File
@@ -7,7 +7,7 @@ pub mod keys;
pub mod relations; pub mod relations;
pub mod snapshot; pub mod snapshot;
pub use collector::{collect_snapshot, CollectOptions, CollectProgress}; pub use collector::{collect_snapshot, collect_snapshot_with_concurrency, CollectOptions, CollectProgress};
pub use color::hue_to_hex; pub use color::hue_to_hex;
pub use dbml::{to_dbml, DbmlOutput}; pub use dbml::{to_dbml, DbmlOutput};
pub use export::{to_standalone_html, EXPORT_LANGUAGES}; pub use export::{to_standalone_html, EXPORT_LANGUAGES};
+12 -4
View File
@@ -21,6 +21,8 @@ pub struct DocsSnapshotRequest {
pub tables: Vec<String>, pub tables: Vec<String>,
#[serde(default)] #[serde(default)]
pub project_name: Option<String>, pub project_name: Option<String>,
#[serde(default)]
pub max_concurrent_tables: Option<usize>,
} }
async fn load_connection(state: &Arc<WebState>, connection_id: &str) -> Result<ConnectionConfig, AppError> { async fn load_connection(state: &Arc<WebState>, connection_id: &str) -> Result<ConnectionConfig, AppError> {
@@ -48,10 +50,16 @@ pub async fn collect_snapshot(
project_name: request.project_name.clone().unwrap_or_else(|| connection.name.clone()), project_name: request.project_name.clone().unwrap_or_else(|| connection.name.clone()),
}; };
let snapshot = let snapshot = dbx_core::docs::collect_snapshot_with_concurrency(
dbx_core::docs::collect_snapshot(&state.app, &connection, &options, &|_progress| {}, &AtomicBool::new(false)) &state.app,
.await &connection,
.map_err(AppError::from)?; &options,
&|_progress| {},
&AtomicBool::new(false),
request.max_concurrent_tables.unwrap_or(8).clamp(1, 8),
)
.await
.map_err(AppError::from)?;
Ok(Json(snapshot)) Ok(Json(snapshot))
} }
+43 -1
View File
@@ -8,10 +8,22 @@ use dbx_core::connection::AppState;
use dbx_core::docs::annotations::{ use dbx_core::docs::annotations::{
apply_annotations, load_annotations, resolve_notes_path, save_annotations, AnnotationFile, apply_annotations, load_annotations, resolve_notes_path, save_annotations, AnnotationFile,
}; };
use dbx_core::docs::{collect_snapshot, to_standalone_html, CollectOptions, SchemaSnapshot}; use dbx_core::docs::{
collect_snapshot, collect_snapshot_with_concurrency, to_standalone_html, CollectOptions, SchemaSnapshot,
};
use dbx_core::models::connection::ConnectionConfig; use dbx_core::models::connection::ConnectionConfig;
use serde::Serialize;
use tauri::ipc::Channel;
use tauri::State; use tauri::State;
#[derive(Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DocsCollectProgress {
completed: usize,
total: usize,
current: String,
}
async fn connection_of(state: &Arc<AppState>, connection_id: &str) -> Result<ConnectionConfig, String> { async fn connection_of(state: &Arc<AppState>, connection_id: &str) -> Result<ConnectionConfig, String> {
let configs = state.configs.read().await; let configs = state.configs.read().await;
configs.get(connection_id).cloned().ok_or_else(|| format!("Connection {connection_id} not found.")) configs.get(connection_id).cloned().ok_or_else(|| format!("Connection {connection_id} not found."))
@@ -45,6 +57,36 @@ pub async fn docs_collect_snapshot(
collect_snapshot(&state, &config, &options, &|_progress| {}, &AtomicBool::new(false)).await collect_snapshot(&state, &config, &options, &|_progress| {}, &AtomicBool::new(false)).await
} }
#[tauri::command]
pub async fn docs_collect_snapshot_for_export(
state: State<'_, Arc<AppState>>,
connection_id: String,
database: String,
schemas: Vec<String>,
tables: Vec<String>,
project_name: Option<String>,
on_progress: Channel<DocsCollectProgress>,
) -> Result<SchemaSnapshot, String> {
let config = connection_of(&state, &connection_id).await?;
let options =
CollectOptions { database, schemas, tables, project_name: project_name.unwrap_or_else(|| config.name.clone()) };
collect_snapshot_with_concurrency(
&state,
&config,
&options,
&|event| {
let _ = on_progress.send(DocsCollectProgress {
completed: event.completed,
total: event.total,
current: event.current,
});
},
&AtomicBool::new(false),
2,
)
.await
}
#[tauri::command] #[tauri::command]
pub async fn docs_load_annotations( pub async fn docs_load_annotations(
state: State<'_, Arc<AppState>>, state: State<'_, Arc<AppState>>,
+1
View File
@@ -2412,6 +2412,7 @@ pub fn run() {
commands::mongo_cmd::mongo_rename_collection, commands::mongo_cmd::mongo_rename_collection,
commands::mongo_cmd::mongo_clone_collection, commands::mongo_cmd::mongo_clone_collection,
commands::docs::docs_collect_snapshot, commands::docs::docs_collect_snapshot,
commands::docs::docs_collect_snapshot_for_export,
commands::docs::docs_load_annotations, commands::docs::docs_load_annotations,
commands::docs::docs_apply_annotations, commands::docs::docs_apply_annotations,
commands::docs::docs_save_annotations, commands::docs::docs_save_annotations,