Browse Source

feat: clear file references on schema delete

nc-feat/attachment-clean-up
mertmit 4 months ago
parent
commit
6f4f1e39b9
  1. 23
      packages/nocodb/src/meta/meta.service.ts
  2. 5
      packages/nocodb/src/models/Column.ts
  3. 20
      packages/nocodb/src/models/FileReference.ts

23
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<any> {
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;
}

5
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<T = any> 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,

20
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,

Loading…
Cancel
Save