From 6f4f1e39b9e94d7da7949ff666a988c8f6483039 Mon Sep 17 00:00:00 2001 From: mertmit Date: Sat, 10 Aug 2024 07:31:54 +0000 Subject: [PATCH] feat: clear file references on schema delete --- packages/nocodb/src/meta/meta.service.ts | 23 ++++++++++++++++++--- packages/nocodb/src/models/Column.ts | 5 ++++- packages/nocodb/src/models/FileReference.ts | 20 ++++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/packages/nocodb/src/meta/meta.service.ts b/packages/nocodb/src/meta/meta.service.ts index a8b75d389d..e5120d22d9 100644 --- a/packages/nocodb/src/meta/meta.service.ts +++ b/packages/nocodb/src/meta/meta.service.ts @@ -223,6 +223,7 @@ export class MetaService { target: string, data: any | any[], ids: string[], + condition?: { [key: string]: any }, ): Promise { if (Array.isArray(data) ? !data.length : !data) { return []; @@ -253,8 +254,6 @@ export class MetaService { sql: '', }); } - - this.contextCondition(query, workspace_id, base_id, target); } const updateObj = { @@ -262,7 +261,25 @@ export class MetaService { updated_at: at, }; - query.whereIn('id', ids).update(updateObj); + if (!condition) { + query.whereIn('id', ids).update(updateObj); + } else { + if (![MetaTable.FILE_REFERENCES].includes(target as MetaTable)) { + NcError.metaError({ + message: 'This table does not support conditional bulk update', + sql: '', + }); + } + + query.where(condition); + + // Check if a condition is present in the query builder and throw an error if not. + this.checkConditionPresent(query, 'update'); + + query.update(updateObj); + } + + this.contextCondition(query, workspace_id, base_id, target); return query; } diff --git a/packages/nocodb/src/models/Column.ts b/packages/nocodb/src/models/Column.ts index c0d9053e4e..e2a87f4165 100644 --- a/packages/nocodb/src/models/Column.ts +++ b/packages/nocodb/src/models/Column.ts @@ -17,7 +17,7 @@ import Sort from '~/models/Sort'; import Filter from '~/models/Filter'; import QrCodeColumn from '~/models/QrCodeColumn'; import BarcodeColumn from '~/models/BarcodeColumn'; -import { GalleryView, KanbanView, LinksColumn } from '~/models'; +import { FileReference, GalleryView, KanbanView, LinksColumn } from '~/models'; import { extractProps } from '~/helpers/extractProps'; import { NcError } from '~/helpers/catchError'; import addFormulaErrorIfMissingColumn from '~/helpers/addFormulaErrorIfMissingColumn'; @@ -984,6 +984,9 @@ export default class Column implements ColumnType { await Column.delete(context, ltarColumn.fk_column_id, ncMeta); } + // Delete FileReference + await FileReference.bulkDelete(context, { fk_column_id: col.id }, ncMeta); + // Columns await ncMeta.metaDelete( context.workspace_id, diff --git a/packages/nocodb/src/models/FileReference.ts b/packages/nocodb/src/models/FileReference.ts index 5e79e506f8..1a59192438 100644 --- a/packages/nocodb/src/models/FileReference.ts +++ b/packages/nocodb/src/models/FileReference.ts @@ -121,6 +121,26 @@ export default class FileReference { ); } + public static async bulkDelete( + context: NcContext, + condition: { + workspace_id?: string; + base_id?: string; + fk_model_id?: string; + fk_column_id?: string; + }, + ncMeta = Noco.ncMeta, + ) { + await ncMeta.bulkMetaUpdate( + context.workspace_id, + context.base_id, + MetaTable.FILE_REFERENCES, + { deleted: true }, + null, + condition, + ); + } + public static async hardDelete( context: NcContext, fileReferenceId: string,