feat(export): run data dictionary PDF exports in background
This commit is contained in:
18 files changed
+702
-104
No files matched your search
@@ -31,7 +31,9 @@ import {
|
||||
type DictionaryTemplateId,
|
||||
} from "@/lib/docs/dataDictionary";
|
||||
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 { useConnectionStore } from "@/stores/connectionStore";
|
||||
|
||||
@@ -46,14 +48,13 @@ const props = defineProps<{
|
||||
const open = defineModel<boolean>("open", { default: false });
|
||||
const { t } = useI18n();
|
||||
const connectionStore = useConnectionStore();
|
||||
const { toast } = useToast();
|
||||
|
||||
const step = ref(0);
|
||||
const loading = ref(false);
|
||||
const exporting = ref(false);
|
||||
const loadError = 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 schemas = ref<Array<{ database: string; name: string; checked: boolean }>>([]);
|
||||
const objects = ref<DictionaryObjectRef[]>([]);
|
||||
@@ -67,7 +68,6 @@ const continueOnError = ref(true);
|
||||
const outputPath = ref("");
|
||||
const previewUrl = ref("");
|
||||
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 dbType = computed(() => connectionStore.getConfig(props.prefillConnectionId || "")?.db_type);
|
||||
@@ -312,22 +312,42 @@ async function browse(): 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;
|
||||
exportError.value = null;
|
||||
generatedPath.value = "";
|
||||
elapsed.value = 0;
|
||||
elapsedTimer = window.setInterval(() => (elapsed.value += 1), 1000);
|
||||
try {
|
||||
const fallback = dataDictionaryFileName(checkedDatabases.value[0] || "DataDictionary", false);
|
||||
const suggested = resolveDictionaryExportPath(outputPath.value, fallback, appendTimestamp.value);
|
||||
const document = await collectSelection();
|
||||
const bytes = buildDataDictionaryPdf(document.tables, labels(), layout.value, continueOnError.value ? document.warnings : []);
|
||||
const saved = await saveDataDictionaryFile(suggested, bytes, { overwrite: overwrite.value });
|
||||
if (saved) generatedPath.value = saved;
|
||||
const path = await chooseDataDictionaryPath(suggested);
|
||||
if (!path) return;
|
||||
const started = startDataDictionaryExport({
|
||||
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) {
|
||||
exportError.value = error instanceof DictionaryFileExistsError ? t("dataDictionary.fileExists") : t("dataDictionary.exportFailed", { error: error instanceof Error ? error.message : String(error) });
|
||||
} finally {
|
||||
window.clearInterval(elapsedTimer);
|
||||
exporting.value = false;
|
||||
}
|
||||
}
|
||||
@@ -420,7 +440,6 @@ watch(
|
||||
return;
|
||||
}
|
||||
step.value = 0;
|
||||
generatedPath.value = "";
|
||||
exportError.value = null;
|
||||
outputPath.value = "";
|
||||
appendTimestamp.value = false;
|
||||
@@ -434,7 +453,6 @@ watch(
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
generation += 1;
|
||||
window.clearInterval(elapsedTimer);
|
||||
if (previewUrl.value) URL.revokeObjectURL(previewUrl.value);
|
||||
});
|
||||
</script>
|
||||
@@ -597,8 +615,7 @@ onBeforeUnmount(() => {
|
||||
><span>{{ t("dataDictionary.continueOnError") }}</span
|
||||
><input v-model="continueOnError" type="checkbox"
|
||||
/></label>
|
||||
<p v-if="exporting" class="text-sm text-muted-foreground">{{ t("dataDictionary.generating") }} {{ elapsed }}s</p>
|
||||
<p v-if="generatedPath" class="text-sm">{{ t("dataDictionary.generated") }} {{ generatedPath }}</p>
|
||||
<p v-if="exporting" class="text-sm text-muted-foreground">{{ t("dataDictionary.exporting") }}</p>
|
||||
<p v-if="exportError" class="text-sm text-destructive">{{ exportError }}</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -61,6 +61,9 @@ const progressPercent = (totalRows: number | null, rowsExported: number) => {
|
||||
};
|
||||
|
||||
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.overallPercent !== undefined) return task.overallPercent;
|
||||
if (!task.totalObjects || task.totalObjects <= 0) return 0;
|
||||
@@ -94,6 +97,7 @@ const taskFileName = (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
|
||||
// names the database (filePath may be a directory), the latter already names
|
||||
// the executed script. Only table-export synthesizes a misleading
|
||||
@@ -125,6 +129,14 @@ const comparePhaseText = (phase?: string) => {
|
||||
};
|
||||
|
||||
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 (isActive(task.status)) {
|
||||
const progress = task.compareTotal ? `${task.compareCurrent ?? 0}/${task.compareTotal}` : "";
|
||||
@@ -217,6 +229,7 @@ const databaseObjectText = (task: ExportTask) => {
|
||||
const statusIcon = (task: ExportTask) => {
|
||||
if (isActive(task.status)) {
|
||||
if (task.kind === "database-export") return DatabaseBackup;
|
||||
if (task.kind === "data-dictionary") return FileDown;
|
||||
if (task.kind === "sql-file") return FileCode2;
|
||||
if (task.kind === "data-transfer") return ArrowRightLeft;
|
||||
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.
|
||||
// sql-file filePath can be a "; "-joined list of input scripts (not an
|
||||
// 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) {
|
||||
if (!canRevealTaskFile(task) || revealingTaskIds.value.includes(task.exportId)) return;
|
||||
@@ -359,6 +372,7 @@ function openTask(task: ExportTask): void {
|
||||
v-if="
|
||||
task.totalRows ||
|
||||
(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 === 'multi-db-execution' && task.multiDbTotal) ||
|
||||
((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", () => {
|
||||
it("keeps legacy progress indeterminate instead of using successful statements as the total", async () => {
|
||||
const tracker = useExportTracker();
|
||||
|
||||
Reference in new issue
Block a user