From 479765c8089c4ac2754ba40e9bf0ce60685ffb9b Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 2 Dec 2026 09:42:34 -0500 Subject: [PATCH] fix: Do not store the attachment id map on every JSON import task (#14851) --- server/queues/tasks/JSONAPIImportTask.test.ts | 25 +++++++++++++++++++ server/queues/tasks/JSONAPIImportTask.ts | 22 ++++++++-------- shared/schema.ts | 2 -- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/server/queues/tasks/JSONAPIImportTask.test.ts b/server/queues/tasks/JSONAPIImportTask.test.ts index 6f1c42badf..35a673349c 100644 --- a/server/queues/tasks/JSONAPIImportTask.test.ts +++ b/server/queues/tasks/JSONAPIImportTask.test.ts @@ -347,6 +347,31 @@ describe("JSONAPIImportTask", () => { expect(attachments.length).toBeGreaterThanOrEqual(1); }); + it("rewrites document attachment references without storing the map on tasks", async () => { + const admin = await buildAdmin(); + const { importId } = await runImport({ + teamId: admin.teamId, + createdById: admin.id, + zipPath: zip.filePath, + }); + + const attachments = await Attachment.findAll({ + where: { teamId: admin.teamId }, + }); + const documents = await Document.findAll({ + where: { apiImportId: importId }, + }); + const tasks = await ImportTask.findAll({ where: { importId } }); + const content = JSON.stringify(documents.map((d) => d.content)); + + expect( + attachments.some((a) => content.includes(Attachment.getRedirectUrl(a.id))) + ).toBe(true); + expect(JSON.stringify(tasks.map((t) => t.input))).not.toContain( + "attachmentIdMap" + ); + }); + it("deletes the source archive after the import completes", async () => { const admin = await buildAdmin(); const sourceAttachment = await buildAttachment({ diff --git a/server/queues/tasks/JSONAPIImportTask.ts b/server/queues/tasks/JSONAPIImportTask.ts index 91cd03c02c..948855d64c 100644 --- a/server/queues/tasks/JSONAPIImportTask.ts +++ b/server/queues/tasks/JSONAPIImportTask.ts @@ -341,7 +341,6 @@ export default class JSONAPIImportTask extends APIImportTask { icon: c.export.icon, color: c.export.color, data: c.export.data ?? ProsemirrorDataHelper.getEmpty(), - attachmentIdMap, })), ]; @@ -363,7 +362,7 @@ export default class JSONAPIImportTask extends APIImportTask { // depth-ordered cascade of ImportTask rows so parent FKs are always // satisfied at child-doc creation time. const childTasksInput: ImportTaskInput = collections.flatMap( - (c) => c.children.map((d) => this.toPageInput(d, attachmentIdMap)) + (c) => c.children.map((d) => this.toPageInput(d)) ); return { taskOutput: collectionOutputs, childTasksInput }; @@ -378,11 +377,19 @@ export default class JSONAPIImportTask extends APIImportTask { const taskOutput: ImportTaskOutput = []; const childTasksInput: JSONPageImportTaskInputItem[] = []; + // Built from the manifest so the map is not serialized into every task input. + const attachmentIdMap: Record = Object.fromEntries( + (importTask.import.scratch?.manifest ?? []).map((m) => [ + m.externalId, + m.id, + ]) + ); + const items = importTask.input as JSONPageImportTaskInputItem[]; for (const item of items) { const transformed = rewriteAttachmentReferences( item.data, - item.attachmentIdMap + attachmentIdMap ) as ProsemirrorDoc; taskOutput.push({ @@ -529,13 +536,9 @@ export default class JSONAPIImportTask extends APIImportTask { * each tree-depth runs as its own task wave. * * @param doc The discovered document, including its descendants. - * @param attachmentIdMap External attachment id → new internal id map. * @returns A self-contained per-page task input. */ - private toPageInput( - doc: DiscoveredDocument, - attachmentIdMap: Record - ): JSONPageImportTaskInputItem { + private toPageInput(doc: DiscoveredDocument): JSONPageImportTaskInputItem { const exported = doc.export; return { externalId: doc.externalId, @@ -552,9 +555,8 @@ export default class JSONAPIImportTask extends APIImportTask { createdAt: exported.createdAt, updatedAt: exported.updatedAt, publishedAt: exported.publishedAt, - attachmentIdMap, children: doc.children.length - ? doc.children.map((c) => this.toPageInput(c, attachmentIdMap)) + ? doc.children.map((c) => this.toPageInput(c)) : undefined, }; } diff --git a/shared/schema.ts b/shared/schema.ts index 6ceb348f0e..dc7acf5e9a 100644 --- a/shared/schema.ts +++ b/shared/schema.ts @@ -190,8 +190,6 @@ export interface JSONPageImportTaskInputItem { createdAt?: string; updatedAt?: string; publishedAt?: string | null; - /** Map of external attachment id → manifest entry id, scoped to this doc. */ - attachmentIdMap: Record; children?: JSONPageImportTaskInputItem[]; }