Browse Source

feat: keep source_id & is_external for attachment references

nc-feat/attachment-clean-up
mertmit 4 months ago
parent
commit
d2b23834ea
  1. 17
      packages/nocodb/src/db/BaseModelSqlv2.ts
  2. 2
      packages/nocodb/src/meta/migrations/v2/nc_057_file_references.ts
  3. 4
      packages/nocodb/src/models/FileReference.ts
  4. 4
      packages/nocodb/src/modules/jobs/migration-jobs/nc_job_001_attachment.ts

17
packages/nocodb/src/db/BaseModelSqlv2.ts

@ -199,6 +199,7 @@ class BaseModelSqlv2 {
protected viewId: string;
protected _proto: any;
protected _columns = {};
protected source: Source;
public model: Model;
public context: NcContext;
@ -4440,7 +4441,7 @@ class BaseModelSqlv2 {
async delByPk(id, _trx?, cookie?) {
let trx: Knex.Transaction = _trx;
try {
const source = await Source.get(this.context, this.model.source_id);
const source = await this.getSource();
// retrieve data for handling params in hook
const data = await this.readRecord({
idOrRecord: id,
@ -4761,7 +4762,7 @@ class BaseModelSqlv2 {
async nestedInsert(data, _trx = null, cookie?) {
// const driver = trx ? trx : await this.dbDriver.transaction();
try {
const source = await Source.get(this.context, this.model.source_id);
const source = await this.getSource();
await populatePk(this.context, this.model, data);
const columns = await this.model.getColumns(this.context);
@ -5753,7 +5754,7 @@ class BaseModelSqlv2 {
ids: any[],
) => Promise<any>)[] = [];
const base = await Source.get(this.context, this.model.source_id);
const base = await this.getSource();
for (const column of this.model.columns) {
if (column.uidt !== UITypes.LinkToAnotherRecord) continue;
@ -5953,7 +5954,7 @@ class BaseModelSqlv2 {
}
}
const source = await Source.get(this.context, this.model.source_id);
const source = await this.getSource();
// remove FileReferences for attachments
const attachmentColumns = columns.filter(
@ -9371,6 +9372,7 @@ class BaseModelSqlv2 {
!sanitizedAttachment.id ||
regenerateIds.includes(sanitizedAttachment.id)
) {
const source = await this.getSource();
sanitizedAttachment.id = await FileReference.insert(
this.context,
{
@ -9378,8 +9380,10 @@ class BaseModelSqlv2 {
sanitizedAttachment.url ?? sanitizedAttachment.path,
file_size: sanitizedAttachment.size,
fk_user_id: cookie?.user?.id ?? 'anonymous',
source_id: source.id,
fk_model_id: this.model.id,
fk_column_id: column.id,
is_external: !!source.isMeta(),
},
);
}
@ -9566,6 +9570,11 @@ class BaseModelSqlv2 {
);
}
async getSource() {
// return this.source if defined or fetch and return
return this.source || (this.source = await Source.get(this.context, this.model.source_id));
}
protected async clearFileReferences(args: {
oldData?: Record<string, any>[];
columns?: Column[];

2
packages/nocodb/src/meta/migrations/v2/nc_057_file_references.ts

@ -10,8 +10,10 @@ const up = async (knex: Knex) => {
table.string('fk_user_id', 20);
table.string('fk_workspace_id', 20);
table.string('base_id', 20);
table.string('source_id', 20);
table.string('fk_model_id', 20);
table.string('fk_column_id', 20);
table.boolean('is_external').defaultTo(false);
table.boolean('deleted').defaultTo(false);
table.timestamps(true, true);

4
packages/nocodb/src/models/FileReference.ts

@ -11,8 +11,10 @@ export default class FileReference {
fk_user_id: string;
fk_workspace_id: string;
base_id: string;
source_id: string;
fk_model_id: string;
fk_column_id: string;
is_external: boolean;
deleted: boolean;
created_at: Date;
updated_at: Date;
@ -31,8 +33,10 @@ export default class FileReference {
'file_url',
'file_size',
'fk_user_id',
'source_id',
'fk_model_id',
'fk_column_id',
'is_external',
'deleted',
]);

4
packages/nocodb/src/modules/jobs/migration-jobs/nc_job_001_attachment.ts

@ -354,17 +354,17 @@ export class AttachmentMigrationProcessor {
deleted: true,
},
);
updateRequired = true;
}
}
if (!('id' in attachment)) {
attachment.id = await FileReference.insert(context, {
source_id: source.id,
fk_model_id,
fk_column_id: column.id,
file_url: attachment.path || attachment.url,
file_size: attachment.size,
is_external: !!source.isMeta(),
deleted: false,
});

Loading…
Cancel
Save