From fbac698edc7fc148c94e2f5611e4ce85ff149c99 Mon Sep 17 00:00:00 2001 From: Ramesh Mane <101566080+rameshmane7218@users.noreply.github.com> Date: Fri, 5 Jul 2024 08:35:41 +0000 Subject: [PATCH] fix(nocodb): test fail issue --- packages/nocodb/src/db/BaseModelSqlv2.ts | 387 ++++++++++++++++------- 1 file changed, 265 insertions(+), 122 deletions(-) diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.ts index d6f3f4374f..fdd414b201 100644 --- a/packages/nocodb/src/db/BaseModelSqlv2.ts +++ b/packages/nocodb/src/db/BaseModelSqlv2.ts @@ -231,10 +231,12 @@ class BaseModelSqlv2 { ignoreView = false, getHiddenColumn = false, throwErrorIfInvalidParams = false, + extractOnlyPrimaries = false, }: { ignoreView?: boolean; getHiddenColumn?: boolean; throwErrorIfInvalidParams?: boolean; + extractOnlyPrimaries?: boolean; } = {}, ): Promise { const qb = this.dbDriver(this.tnPath); @@ -247,6 +249,7 @@ class BaseModelSqlv2 { : this.viewId && (await View.get(this.context, this.viewId)), getHiddenColumn, throwErrorIfInvalidParams, + extractOnlyPrimaries, }); await this.selectObject({ @@ -281,6 +284,32 @@ class BaseModelSqlv2 { return data ? await nocoExecute(ast, data, {}, parsedQuery) : null; } + public async readByPkFromModel( + model = this.model, + viewId?: string, + extractDisplayValueData?: boolean, + ...rest: Parameters + ): Promise { + let data; + if (this.model.id === model.id) { + data = await this.readByPk(...rest); + } else { + const baseModel = await Model.getBaseModelSQL(this.context, { + id: model.id, + viewId: viewId, + dbDriver: this.dbDriver, + }); + + data = await baseModel.readByPk(...rest); + } + + if (extractDisplayValueData) { + return data ? data[model.displayValue.title] ?? null : ''; + } + + return data; + } + public async exist(id?: any): Promise { const qb = this.dbDriver(this.tnPath); await this.model.getColumns(this.context); @@ -5542,10 +5571,13 @@ class BaseModelSqlv2 { break; case RelationTypes.HAS_MANY: { - const linkedHmRowObj = await this.dbDriver(childTn) - .select(childColumn.column_name) - .where(_wherePk(childTable.primaryKeys, childId)) - .first(); + const linkedHmRowObj = await this.execAndParse( + this.dbDriver(childTn) + .select(`${childTable.table_name}.${childColumn.column_name}`) + .where(_wherePk(childTable.primaryKeys, childId)), + null, + { raw: true, first: true }, + ); const oldRowId = linkedHmRowObj ? Object.values(linkedHmRowObj)?.[0] @@ -5554,18 +5586,32 @@ class BaseModelSqlv2 { if (oldRowId) { const [parentRelatedPkValue, childRelatedPkValue] = await Promise.all([ - await this.dbDriver(childTn) - .select( - `${childTable.table_name}.${childTable.displayValue.column_name}`, - ) - .where(_wherePk(childTable.primaryKeys, childId)) - .first(), - await this.dbDriver(parentTn) - .select( - `${parentTable.table_name}.${parentTable.displayValue.column_name}`, - ) - .where(_wherePk(parentTable.primaryKeys, oldRowId)) - .first(), + await this.readByPkFromModel( + childTable, + undefined, + true, + childId, + false, + {}, + { + ignoreView: true, + getHiddenColumn: true, + extractOnlyPrimaries: true, + }, + ), + await this.readByPkFromModel( + parentTable, + undefined, + true, + oldRowId, + false, + {}, + { + ignoreView: true, + getHiddenColumn: true, + extractOnlyPrimaries: true, + }, + ), ]); auditUpdateObj.push({ @@ -5635,19 +5681,24 @@ class BaseModelSqlv2 { childId: oldChildRowId as string, op_sub_type: AuditOperationSubTypes.UNLINK_RECORD, columnTitle: auditConfig.parentColTitle, - pkValue: { - [parentTable.displayValue.title]: - prevData[column.title]?.[parentTable.displayValue.title] ?? - null, - }, + pkValue: + prevData[column.title]?.[parentTable.displayValue.title] ?? + null, }); - const childRelatedPkValue = await this.dbDriver(childTn) - .select( - `${childTable.table_name}.${childTable.displayValue.column_name}`, - ) - .where(_wherePk(childTable.primaryKeys, rowId)) - .first(); + const childRelatedPkValue = await this.readByPkFromModel( + childTable, + undefined, + true, + rowId, + false, + {}, + { + ignoreView: true, + getHiddenColumn: true, + extractOnlyPrimaries: true, + }, + ); if (parentTable.id !== childTable.id) { auditUpdateObj.push({ @@ -5662,10 +5713,13 @@ class BaseModelSqlv2 { } } } else { - const linkedHmRowObj = await this.dbDriver(childTn) - .select(childColumn.column_name) - .where(_wherePk(childTable.primaryKeys, rowId)) - .first(); + const linkedHmRowObj = await this.execAndParse( + this.dbDriver(childTn) + .select(childColumn.column_name) + .where(_wherePk(childTable.primaryKeys, rowId)), + null, + { raw: true, first: true }, + ); const oldChildRowId = linkedHmRowObj ? Object.values(linkedHmRowObj)?.[0] @@ -5674,18 +5728,32 @@ class BaseModelSqlv2 { if (oldChildRowId) { const [parentRelatedPkValue, childRelatedPkValue] = await Promise.all([ - await this.dbDriver(parentTn) - .select( - `${parentTable.table_name}.${parentTable.displayValue.column_name}`, - ) - .where(_wherePk(parentTable.primaryKeys, oldChildRowId)) - .first(), - await this.dbDriver(childTn) - .select( - `${childTable.table_name}.${childTable.displayValue.column_name}`, - ) - .where(_wherePk(childTable.primaryKeys, rowId)) - .first(), + await this.readByPkFromModel( + parentTable, + undefined, + true, + oldChildRowId, + false, + {}, + { + ignoreView: true, + getHiddenColumn: true, + extractOnlyPrimaries: true, + }, + ), + await this.readByPkFromModel( + childTable, + undefined, + true, + rowId, + false, + {}, + { + ignoreView: true, + getHiddenColumn: true, + extractOnlyPrimaries: true, + }, + ), ]); auditUpdateObj.push({ @@ -5745,10 +5813,13 @@ class BaseModelSqlv2 { let linkedCurrentOoRowObj; if (isBt) { // 1. check current row is linked with another child - linkedCurrentOoRowObj = await this.dbDriver(childTn) - .select(childColumn.column_name) - .where(_wherePk(childTable.primaryKeys, rowId)) - .first(); + linkedCurrentOoRowObj = await this.execAndParse( + this.dbDriver(childTn) + .select(childColumn.column_name) + .where(_wherePk(childTable.primaryKeys, rowId)), + null, + { raw: true, first: true }, + ); const oldChildRowId = linkedCurrentOoRowObj ? Object.values(linkedCurrentOoRowObj)?.[0] @@ -5757,18 +5828,32 @@ class BaseModelSqlv2 { if (oldChildRowId) { const [parentRelatedPkValue, childRelatedPkValue] = await Promise.all([ - await this.dbDriver(childTn) - .select( - `${childTable.table_name}.${childTable.displayValue.column_name}`, - ) - .where(_wherePk(childTable.primaryKeys, rowId)) - .first(), - await this.dbDriver(parentTn) - .select( - `${parentTable.table_name}.${parentTable.displayValue.column_name}`, - ) - .where(_wherePk(parentTable.primaryKeys, oldChildRowId)) - .first(), + await this.readByPkFromModel( + childTable, + undefined, + true, + rowId, + false, + {}, + { + ignoreView: true, + getHiddenColumn: true, + extractOnlyPrimaries: true, + }, + ), + await this.readByPkFromModel( + parentTable, + undefined, + true, + oldChildRowId, + false, + {}, + { + ignoreView: true, + getHiddenColumn: true, + extractOnlyPrimaries: true, + }, + ), ]); auditUpdateObj.push({ @@ -5795,8 +5880,8 @@ class BaseModelSqlv2 { } // 2. check current child is linked with another row cell - linkedOoRowObj = await this.dbDriver(childTn) - .where({ + linkedOoRowObj = await this.execAndParse( + this.dbDriver(childTn).where({ [childColumn.column_name]: this.dbDriver.from( this.dbDriver(parentTn) .select(parentColumn.column_name) @@ -5806,8 +5891,10 @@ class BaseModelSqlv2 { .first() .as('___cn_alias'), ), - }) - .first(); + }), + null, + { raw: true, first: true }, + ); if (linkedOoRowObj) { const oldRowId = getCompositePkValue( @@ -5818,18 +5905,32 @@ class BaseModelSqlv2 { if (oldRowId) { const [parentRelatedPkValue, childRelatedPkValue] = await Promise.all([ - await this.dbDriver(parentTn) - .select( - `${parentTable.table_name}.${parentTable.displayValue.column_name}`, - ) - .where(_wherePk(parentTable.primaryKeys, childId)) - .first(), - await this.dbDriver(childTn) - .select( - `${childTable.table_name}.${childTable.displayValue.column_name}`, - ) - .where(_wherePk(childTable.primaryKeys, oldRowId)) - .first(), + await this.readByPkFromModel( + parentTable, + undefined, + true, + childId, + false, + {}, + { + ignoreView: true, + getHiddenColumn: true, + extractOnlyPrimaries: true, + }, + ), + await this.readByPkFromModel( + childTable, + undefined, + true, + oldRowId, + false, + {}, + { + ignoreView: true, + getHiddenColumn: true, + extractOnlyPrimaries: true, + }, + ), ]); auditUpdateObj.push({ @@ -5857,8 +5958,8 @@ class BaseModelSqlv2 { } } else { // 1. check current row is linked with another child - linkedCurrentOoRowObj = await this.dbDriver(childTn) - .where({ + linkedCurrentOoRowObj = await this.execAndParse( + this.dbDriver(childTn).where({ [childColumn.column_name]: this.dbDriver.from( this.dbDriver(parentTn) .select(parentColumn.column_name) @@ -5866,8 +5967,10 @@ class BaseModelSqlv2 { .first() .as('___cn_alias'), ), - }) - .first(); + }), + null, + { raw: true, first: true }, + ); if (linkedCurrentOoRowObj) { const oldChildRowId = getCompositePkValue( @@ -5878,18 +5981,32 @@ class BaseModelSqlv2 { if (oldChildRowId) { const [parentRelatedPkValue, childRelatedPkValue] = await Promise.all([ - await this.dbDriver(childTn) - .select( - `${childTable.table_name}.${childTable.displayValue.column_name}`, - ) - .where(_wherePk(childTable.primaryKeys, oldChildRowId)) - .first(), - await this.dbDriver(parentTn) - .select( - `${parentTable.table_name}.${parentTable.displayValue.column_name}`, - ) - .where(_wherePk(parentTable.primaryKeys, rowId)) - .first(), + await this.readByPkFromModel( + childTable, + undefined, + true, + oldChildRowId, + false, + {}, + { + ignoreView: true, + getHiddenColumn: true, + extractOnlyPrimaries: true, + }, + ), + await this.readByPkFromModel( + parentTable, + undefined, + true, + rowId, + false, + {}, + { + ignoreView: true, + getHiddenColumn: true, + extractOnlyPrimaries: true, + }, + ), ]); auditUpdateObj.push({ @@ -5917,28 +6034,46 @@ class BaseModelSqlv2 { } // 2. check current child is linked with another row cell - linkedOoRowObj = await this.dbDriver(childTn) - .select(childColumn.column_name) - .where(_wherePk(childTable.primaryKeys, childId)) - .first(); + linkedOoRowObj = await this.execAndParse( + this.dbDriver(childTn) + .select(childColumn.column_name) + .where(_wherePk(childTable.primaryKeys, childId)), + null, + { raw: true, first: true }, + ); + const oldRowId = linkedOoRowObj ? Object.values(linkedOoRowObj)?.[0] : null; if (oldRowId) { const [parentRelatedPkValue, childRelatedPkValue] = await Promise.all([ - await this.dbDriver(childTn) - .select( - `${childTable.table_name}.${childTable.displayValue.column_name}`, - ) - .where(_wherePk(childTable.primaryKeys, childId)) - .first(), - await this.dbDriver(parentTn) - .select( - `${parentTable.table_name}.${parentTable.displayValue.column_name}`, - ) - .where(_wherePk(parentTable.primaryKeys, oldRowId)) - .first(), + await this.readByPkFromModel( + childTable, + undefined, + true, + childId, + false, + {}, + { + ignoreView: true, + getHiddenColumn: true, + extractOnlyPrimaries: true, + }, + ), + await this.readByPkFromModel( + parentTable, + undefined, + true, + oldRowId, + false, + {}, + { + ignoreView: true, + getHiddenColumn: true, + extractOnlyPrimaries: true, + }, + ), ]); auditUpdateObj.push({ @@ -6075,12 +6210,15 @@ class BaseModelSqlv2 { pkValue = undefined, ): Promise { if (!pkValue) { - pkValue = await this.dbDriver(this.getTnPath(childModel)) - .select( - `${childModel.table_name}.${childModel.displayValue.column_name}`, - ) - .where(_wherePk(childModel.primaryKeys, childId)) - .first(); + pkValue = await this.readByPkFromModel( + childModel, + undefined, + true, + childId, + false, + {}, + { ignoreView: true, getHiddenColumn: true, extractOnlyPrimaries: true }, + ); } await Audit.insert({ @@ -6095,9 +6233,7 @@ class BaseModelSqlv2 { `Record [id:${childId}] has been linked with record [id:${rowId}] in ${model.title}`, ), details: DOMPurify.sanitize(`${columnTitle} - : ${ - Object.values(pkValue)[0] - }`), + : ${pkValue ?? null}`), ip: req?.clientIp, user: req?.user?.email, }); @@ -6229,6 +6365,9 @@ class BaseModelSqlv2 { break; case RelationTypes.BELONGS_TO: { + auditConfig.parentModel = childTable; + auditConfig.childModel = parentTable; + await this.execAndParse( this.dbDriver(childTn) // .where({ @@ -6248,14 +6387,15 @@ class BaseModelSqlv2 { rowIds: [childId], cookie, }); - - auditConfig.parentModel = childTable; - auditConfig.childModel = parentTable; } break; case RelationTypes.ONE_TO_ONE: { const isBt = column.meta?.bt; + + auditConfig.parentModel = isBt ? childTable : parentTable; + auditConfig.childModel = isBt ? parentTable : childTable; + await this.execAndParse( this.dbDriver(childTn) .where(_wherePk(childTable.primaryKeys, isBt ? rowId : childId)) @@ -6269,9 +6409,6 @@ class BaseModelSqlv2 { rowIds: [childId], cookie, }); - - auditConfig.parentModel = isBt ? childTable : parentTable; - auditConfig.childModel = isBt ? parentTable : childTable; } break; } @@ -7463,6 +7600,9 @@ class BaseModelSqlv2 { break; case RelationTypes.BELONGS_TO: { + auditConfig.parentModel = childTable; + auditConfig.childModel = parentTable; + // validate Ids { const childRowsQb = this.dbDriver(parentTn) @@ -7804,6 +7944,9 @@ class BaseModelSqlv2 { break; case RelationTypes.BELONGS_TO: { + auditConfig.parentModel = childTable; + auditConfig.childModel = parentTable; + // validate Ids { if (childIds.length > 1)