From 0420734d9793403a6a119e9d48bdd32b0e9270ed Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 13 Mar 2024 16:48:32 +0100 Subject: [PATCH 1/3] Add failing test --- .../rowProcessor/tests/attachments.spec.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/server/src/utilities/rowProcessor/tests/attachments.spec.ts b/packages/server/src/utilities/rowProcessor/tests/attachments.spec.ts index 43af79d82c..beea04829b 100644 --- a/packages/server/src/utilities/rowProcessor/tests/attachments.spec.ts +++ b/packages/server/src/utilities/rowProcessor/tests/attachments.spec.ts @@ -115,4 +115,17 @@ describe("attachment cleanup", () => { await AttachmentCleanup.rowUpdate(table(), { row: row(), oldRow: row() }) expect(mockedDeleteFiles).not.toBeCalled() }) + + it("should be able to cleanup a column and not throw when attachments are undefined", async () => { + const originalTable = table() + delete originalTable.schema["attach"] + await AttachmentCleanup.tableUpdate( + originalTable, + [{ attach: undefined }], + { + oldTable: table(), + } + ) + expect(mockedDeleteFiles).not.toBeCalled() + }) }) From 940ff5acd24b00ff489cc7a3e41c334cde13becb Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 13 Mar 2024 16:48:58 +0100 Subject: [PATCH 2/3] Fix table changes with empty attachments --- packages/server/src/utilities/rowProcessor/attachments.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server/src/utilities/rowProcessor/attachments.ts b/packages/server/src/utilities/rowProcessor/attachments.ts index c289680eb3..e1c83352d4 100644 --- a/packages/server/src/utilities/rowProcessor/attachments.ts +++ b/packages/server/src/utilities/rowProcessor/attachments.ts @@ -43,7 +43,7 @@ export class AttachmentCleanup { if ((columnRemoved && !renaming) || opts.deleting) { rows.forEach(row => { files = files.concat( - row[key].map((attachment: any) => attachment.key) + (row[key] || []).map((attachment: any) => attachment.key) ) }) } From 625c1dda9fd49444e20bdc044c7da865495e8b05 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 13 Mar 2024 16:54:39 +0100 Subject: [PATCH 3/3] Improve tests --- .../rowProcessor/tests/attachments.spec.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/server/src/utilities/rowProcessor/tests/attachments.spec.ts b/packages/server/src/utilities/rowProcessor/tests/attachments.spec.ts index beea04829b..3c58d2c056 100644 --- a/packages/server/src/utilities/rowProcessor/tests/attachments.spec.ts +++ b/packages/server/src/utilities/rowProcessor/tests/attachments.spec.ts @@ -121,7 +121,21 @@ describe("attachment cleanup", () => { delete originalTable.schema["attach"] await AttachmentCleanup.tableUpdate( originalTable, - [{ attach: undefined }], + [row("file 1"), { attach: undefined }, row("file 2")], + { + oldTable: table(), + } + ) + expect(mockedDeleteFiles).toBeCalledTimes(1) + expect(mockedDeleteFiles).toBeCalledWith(BUCKET, ["file 1", "file 2"]) + }) + + it("should be able to cleanup a column and not throw when ALL attachments are undefined", async () => { + const originalTable = table() + delete originalTable.schema["attach"] + await AttachmentCleanup.tableUpdate( + originalTable, + [{}, { attach: undefined }], { oldTable: table(), }