From c261bdc36f0abf7754dff5c84c9545b8f9942dd2 Mon Sep 17 00:00:00 2001 From: Mert E Date: Tue, 21 May 2024 10:29:27 +0300 Subject: [PATCH] fix: self link duplicate issues (#8535) Signed-off-by: mertmit --- .../jobs/export-import/duplicate.processor.ts | 13 +--- .../jobs/jobs/export-import/import.service.ts | 78 ++++++++++--------- 2 files changed, 44 insertions(+), 47 deletions(-) diff --git a/packages/nocodb/src/modules/jobs/jobs/export-import/duplicate.processor.ts b/packages/nocodb/src/modules/jobs/jobs/export-import/duplicate.processor.ts index d2e73a834a..6e6b684715 100644 --- a/packages/nocodb/src/modules/jobs/jobs/export-import/duplicate.processor.ts +++ b/packages/nocodb/src/modules/jobs/jobs/export-import/duplicate.processor.ts @@ -205,12 +205,12 @@ export class DuplicateProcessor { (c.colOptions.type === RelationTypes.BELONGS_TO || (c.colOptions.type === RelationTypes.ONE_TO_ONE && c.meta?.bt)) && - c.colOptions.fk_related_model_id === modelId, + c.colOptions.fk_related_model_id === sourceModel.id, ) .map((c) => c.id); if (bts.length > 0) { - fields[md.id] = [md.primaryKey.id]; + fields[md.id] = fields[md.id] ? fields[md.id] : [md.primaryKey.id]; fields[md.id].push(...bts); } } @@ -294,14 +294,6 @@ export class DuplicateProcessor { throw new Error(`Export failed for model '${sourceModel.id}'`); } - exportedModel.model.columns = exportedModel.model.columns.filter((c) => - c.id.includes(columnId), - ); - - if (exportedModel.model.columns.length !== 1) { - throw new Error(`There was an error duplicating column!`); - } - const replacedColumn = exportedModel.model.columns.find((c) => c.id.includes(columnId), ); @@ -325,6 +317,7 @@ export class DuplicateProcessor { req, externalModels: relatedModels, existingModel: sourceModel, + importColumnIds: [columnId], }); elapsedTime(hrTime, 'import model schema', 'duplicateColumn'); diff --git a/packages/nocodb/src/modules/jobs/jobs/export-import/import.service.ts b/packages/nocodb/src/modules/jobs/jobs/export-import/import.service.ts index ea0ea99a49..f4db828cf6 100644 --- a/packages/nocodb/src/modules/jobs/jobs/export-import/import.service.ts +++ b/packages/nocodb/src/modules/jobs/jobs/export-import/import.service.ts @@ -81,6 +81,7 @@ export class ImportService { req: NcRequest; externalModels?: Model[]; existingModel?: Model; + importColumnIds?: string[]; }) { const hrTime = initTime(); @@ -143,7 +144,12 @@ export class ImportService { const modelData = data.model; const reducedColumnSet = modelData.columns.filter( - (a) => !isVirtualCol(a) && a.uidt !== UITypes.ForeignKey, + (a) => + !isVirtualCol(a) && + a.uidt !== UITypes.ForeignKey && + (param.importColumnIds + ? param.importColumnIds.includes(getEntityIdentifier(a.id)) + : true), ); // create table with static columns @@ -228,7 +234,14 @@ export class ImportService { const modelData = data.model; const table = tableReferences.get(modelData.id); - const linkedColumnSet = modelData.columns.filter((a) => isLinksOrLTAR(a)); + const linkedColumnSet = modelData.columns.filter( + (a) => + isLinksOrLTAR(a) && + !a.system && + (param.importColumnIds + ? param.importColumnIds.includes(getEntityIdentifier(a.id)) + : true), + ); for (const col of linkedColumnSet) { if (col.colOptions) { @@ -299,9 +312,8 @@ export class ImportService { colOptions.fk_mm_model_id && a.id !== col.id, ); - // referencing the same model if (colOptions.fk_related_model_id === modelData.id) { - continue; + childColumn.title = `${childColumn.title} copy`; } for (const nColumn of childModel.columns) { @@ -391,31 +403,8 @@ export class ImportService { a.id !== col.id, ); - // referencing the same model if (colOptions.fk_related_model_id === modelData.id) { - const counterRelationType = - colOptions.type === 'hm' ? 'bt' : 'oo'; - const oldCol = childModel.columns.find( - (oColumn) => - oColumn.colOptions?.fk_parent_column_id === - getEntityIdentifier(colOptions.fk_parent_column_id) && - oColumn.colOptions?.fk_child_column_id === - getEntityIdentifier(colOptions.fk_child_column_id) && - oColumn.colOptions?.type === counterRelationType, - ); - const col = childModel.columns.find( - (nColumn) => - nColumn.colOptions?.fk_parent_column_id === - getIdOrExternalId(colOptions.fk_parent_column_id) && - nColumn.colOptions?.fk_child_column_id === - getIdOrExternalId(colOptions.fk_child_column_id) && - nColumn.colOptions?.type === counterRelationType, - ); - idMap.set( - `${oldCol.base_id}::${oldCol.source_id}::${oldCol.fk_model_id}::${oldCol.id}`, - col.id, - ); - continue; + childColumn.title = `${childColumn.title} copy`; } for (const nColumn of childModel.columns) { @@ -520,6 +509,10 @@ export class ImportService { a.id !== getEntityIdentifier(col.id)), ); + if (colOptions.fk_related_model_id === modelData.id) { + childColumn.title = `${childColumn.title} copy`; + } + for (const nColumn of childModel.columns) { if ( nColumn?.colOptions?.fk_mm_model_id === @@ -643,6 +636,10 @@ export class ImportService { a.id !== getEntityIdentifier(col.id)), ); + if (colOptions.fk_related_model_id === modelData.id) { + childColumn.title = `${childColumn.title} copy`; + } + for (const nColumn of childModel.columns) { if ( nColumn.id !== getIdOrExternalId(col.id) && @@ -768,6 +765,10 @@ export class ImportService { a.id !== getEntityIdentifier(col.id)), ); + if (colOptions.fk_related_model_id === modelData.id) { + childColumn.title = `${childColumn.title} copy`; + } + for (const nColumn of childModel.columns) { if ( nColumn.id !== getIdOrExternalId(col.id) && @@ -815,15 +816,18 @@ export class ImportService { referencedColumnSet.push( ...modelData.columns.filter( (a) => - a.uidt === UITypes.Lookup || - a.uidt === UITypes.Rollup || - a.uidt === UITypes.Formula || - a.uidt === UITypes.QrCode || - a.uidt === UITypes.CreatedTime || - a.uidt === UITypes.LastModifiedTime || - a.uidt === UITypes.CreatedBy || - a.uidt === UITypes.LastModifiedBy || - a.uidt === UITypes.Barcode, + (a.uidt === UITypes.Lookup || + a.uidt === UITypes.Rollup || + a.uidt === UITypes.Formula || + a.uidt === UITypes.QrCode || + a.uidt === UITypes.CreatedTime || + a.uidt === UITypes.LastModifiedTime || + a.uidt === UITypes.CreatedBy || + a.uidt === UITypes.LastModifiedBy || + a.uidt === UITypes.Barcode) && + (param.importColumnIds + ? param.importColumnIds.includes(getEntityIdentifier(a.id)) + : true), ), ); }