Browse Source

fix(nocodb): test fail issue

pull/8901/head
Ramesh Mane 5 months ago
parent
commit
fbac698edc
  1. 377
      packages/nocodb/src/db/BaseModelSqlv2.ts

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

@ -231,10 +231,12 @@ class BaseModelSqlv2 {
ignoreView = false, ignoreView = false,
getHiddenColumn = false, getHiddenColumn = false,
throwErrorIfInvalidParams = false, throwErrorIfInvalidParams = false,
extractOnlyPrimaries = false,
}: { }: {
ignoreView?: boolean; ignoreView?: boolean;
getHiddenColumn?: boolean; getHiddenColumn?: boolean;
throwErrorIfInvalidParams?: boolean; throwErrorIfInvalidParams?: boolean;
extractOnlyPrimaries?: boolean;
} = {}, } = {},
): Promise<any> { ): Promise<any> {
const qb = this.dbDriver(this.tnPath); const qb = this.dbDriver(this.tnPath);
@ -247,6 +249,7 @@ class BaseModelSqlv2 {
: this.viewId && (await View.get(this.context, this.viewId)), : this.viewId && (await View.get(this.context, this.viewId)),
getHiddenColumn, getHiddenColumn,
throwErrorIfInvalidParams, throwErrorIfInvalidParams,
extractOnlyPrimaries,
}); });
await this.selectObject({ await this.selectObject({
@ -281,6 +284,32 @@ class BaseModelSqlv2 {
return data ? await nocoExecute(ast, data, {}, parsedQuery) : null; return data ? await nocoExecute(ast, data, {}, parsedQuery) : null;
} }
public async readByPkFromModel(
model = this.model,
viewId?: string,
extractDisplayValueData?: boolean,
...rest: Parameters<BaseModelSqlv2['readByPk']>
): Promise<any> {
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<any> { public async exist(id?: any): Promise<any> {
const qb = this.dbDriver(this.tnPath); const qb = this.dbDriver(this.tnPath);
await this.model.getColumns(this.context); await this.model.getColumns(this.context);
@ -5542,10 +5571,13 @@ class BaseModelSqlv2 {
break; break;
case RelationTypes.HAS_MANY: case RelationTypes.HAS_MANY:
{ {
const linkedHmRowObj = await this.dbDriver(childTn) const linkedHmRowObj = await this.execAndParse(
.select(childColumn.column_name) this.dbDriver(childTn)
.where(_wherePk(childTable.primaryKeys, childId)) .select(`${childTable.table_name}.${childColumn.column_name}`)
.first(); .where(_wherePk(childTable.primaryKeys, childId)),
null,
{ raw: true, first: true },
);
const oldRowId = linkedHmRowObj const oldRowId = linkedHmRowObj
? Object.values(linkedHmRowObj)?.[0] ? Object.values(linkedHmRowObj)?.[0]
@ -5554,18 +5586,32 @@ class BaseModelSqlv2 {
if (oldRowId) { if (oldRowId) {
const [parentRelatedPkValue, childRelatedPkValue] = const [parentRelatedPkValue, childRelatedPkValue] =
await Promise.all([ await Promise.all([
await this.dbDriver(childTn) await this.readByPkFromModel(
.select( childTable,
`${childTable.table_name}.${childTable.displayValue.column_name}`, undefined,
) true,
.where(_wherePk(childTable.primaryKeys, childId)) childId,
.first(), false,
await this.dbDriver(parentTn) {},
.select( {
`${parentTable.table_name}.${parentTable.displayValue.column_name}`, ignoreView: true,
) getHiddenColumn: true,
.where(_wherePk(parentTable.primaryKeys, oldRowId)) extractOnlyPrimaries: true,
.first(), },
),
await this.readByPkFromModel(
parentTable,
undefined,
true,
oldRowId,
false,
{},
{
ignoreView: true,
getHiddenColumn: true,
extractOnlyPrimaries: true,
},
),
]); ]);
auditUpdateObj.push({ auditUpdateObj.push({
@ -5635,19 +5681,24 @@ class BaseModelSqlv2 {
childId: oldChildRowId as string, childId: oldChildRowId as string,
op_sub_type: AuditOperationSubTypes.UNLINK_RECORD, op_sub_type: AuditOperationSubTypes.UNLINK_RECORD,
columnTitle: auditConfig.parentColTitle, columnTitle: auditConfig.parentColTitle,
pkValue: { pkValue:
[parentTable.displayValue.title]:
prevData[column.title]?.[parentTable.displayValue.title] ?? prevData[column.title]?.[parentTable.displayValue.title] ??
null, null,
},
}); });
const childRelatedPkValue = await this.dbDriver(childTn) const childRelatedPkValue = await this.readByPkFromModel(
.select( childTable,
`${childTable.table_name}.${childTable.displayValue.column_name}`, undefined,
) true,
.where(_wherePk(childTable.primaryKeys, rowId)) rowId,
.first(); false,
{},
{
ignoreView: true,
getHiddenColumn: true,
extractOnlyPrimaries: true,
},
);
if (parentTable.id !== childTable.id) { if (parentTable.id !== childTable.id) {
auditUpdateObj.push({ auditUpdateObj.push({
@ -5662,10 +5713,13 @@ class BaseModelSqlv2 {
} }
} }
} else { } else {
const linkedHmRowObj = await this.dbDriver(childTn) const linkedHmRowObj = await this.execAndParse(
this.dbDriver(childTn)
.select(childColumn.column_name) .select(childColumn.column_name)
.where(_wherePk(childTable.primaryKeys, rowId)) .where(_wherePk(childTable.primaryKeys, rowId)),
.first(); null,
{ raw: true, first: true },
);
const oldChildRowId = linkedHmRowObj const oldChildRowId = linkedHmRowObj
? Object.values(linkedHmRowObj)?.[0] ? Object.values(linkedHmRowObj)?.[0]
@ -5674,18 +5728,32 @@ class BaseModelSqlv2 {
if (oldChildRowId) { if (oldChildRowId) {
const [parentRelatedPkValue, childRelatedPkValue] = const [parentRelatedPkValue, childRelatedPkValue] =
await Promise.all([ await Promise.all([
await this.dbDriver(parentTn) await this.readByPkFromModel(
.select( parentTable,
`${parentTable.table_name}.${parentTable.displayValue.column_name}`, undefined,
) true,
.where(_wherePk(parentTable.primaryKeys, oldChildRowId)) oldChildRowId,
.first(), false,
await this.dbDriver(childTn) {},
.select( {
`${childTable.table_name}.${childTable.displayValue.column_name}`, ignoreView: true,
) getHiddenColumn: true,
.where(_wherePk(childTable.primaryKeys, rowId)) extractOnlyPrimaries: true,
.first(), },
),
await this.readByPkFromModel(
childTable,
undefined,
true,
rowId,
false,
{},
{
ignoreView: true,
getHiddenColumn: true,
extractOnlyPrimaries: true,
},
),
]); ]);
auditUpdateObj.push({ auditUpdateObj.push({
@ -5745,10 +5813,13 @@ class BaseModelSqlv2 {
let linkedCurrentOoRowObj; let linkedCurrentOoRowObj;
if (isBt) { if (isBt) {
// 1. check current row is linked with another child // 1. check current row is linked with another child
linkedCurrentOoRowObj = await this.dbDriver(childTn) linkedCurrentOoRowObj = await this.execAndParse(
this.dbDriver(childTn)
.select(childColumn.column_name) .select(childColumn.column_name)
.where(_wherePk(childTable.primaryKeys, rowId)) .where(_wherePk(childTable.primaryKeys, rowId)),
.first(); null,
{ raw: true, first: true },
);
const oldChildRowId = linkedCurrentOoRowObj const oldChildRowId = linkedCurrentOoRowObj
? Object.values(linkedCurrentOoRowObj)?.[0] ? Object.values(linkedCurrentOoRowObj)?.[0]
@ -5757,18 +5828,32 @@ class BaseModelSqlv2 {
if (oldChildRowId) { if (oldChildRowId) {
const [parentRelatedPkValue, childRelatedPkValue] = const [parentRelatedPkValue, childRelatedPkValue] =
await Promise.all([ await Promise.all([
await this.dbDriver(childTn) await this.readByPkFromModel(
.select( childTable,
`${childTable.table_name}.${childTable.displayValue.column_name}`, undefined,
) true,
.where(_wherePk(childTable.primaryKeys, rowId)) rowId,
.first(), false,
await this.dbDriver(parentTn) {},
.select( {
`${parentTable.table_name}.${parentTable.displayValue.column_name}`, ignoreView: true,
) getHiddenColumn: true,
.where(_wherePk(parentTable.primaryKeys, oldChildRowId)) extractOnlyPrimaries: true,
.first(), },
),
await this.readByPkFromModel(
parentTable,
undefined,
true,
oldChildRowId,
false,
{},
{
ignoreView: true,
getHiddenColumn: true,
extractOnlyPrimaries: true,
},
),
]); ]);
auditUpdateObj.push({ auditUpdateObj.push({
@ -5795,8 +5880,8 @@ class BaseModelSqlv2 {
} }
// 2. check current child is linked with another row cell // 2. check current child is linked with another row cell
linkedOoRowObj = await this.dbDriver(childTn) linkedOoRowObj = await this.execAndParse(
.where({ this.dbDriver(childTn).where({
[childColumn.column_name]: this.dbDriver.from( [childColumn.column_name]: this.dbDriver.from(
this.dbDriver(parentTn) this.dbDriver(parentTn)
.select(parentColumn.column_name) .select(parentColumn.column_name)
@ -5806,8 +5891,10 @@ class BaseModelSqlv2 {
.first() .first()
.as('___cn_alias'), .as('___cn_alias'),
), ),
}) }),
.first(); null,
{ raw: true, first: true },
);
if (linkedOoRowObj) { if (linkedOoRowObj) {
const oldRowId = getCompositePkValue( const oldRowId = getCompositePkValue(
@ -5818,18 +5905,32 @@ class BaseModelSqlv2 {
if (oldRowId) { if (oldRowId) {
const [parentRelatedPkValue, childRelatedPkValue] = const [parentRelatedPkValue, childRelatedPkValue] =
await Promise.all([ await Promise.all([
await this.dbDriver(parentTn) await this.readByPkFromModel(
.select( parentTable,
`${parentTable.table_name}.${parentTable.displayValue.column_name}`, undefined,
) true,
.where(_wherePk(parentTable.primaryKeys, childId)) childId,
.first(), false,
await this.dbDriver(childTn) {},
.select( {
`${childTable.table_name}.${childTable.displayValue.column_name}`, ignoreView: true,
) getHiddenColumn: true,
.where(_wherePk(childTable.primaryKeys, oldRowId)) extractOnlyPrimaries: true,
.first(), },
),
await this.readByPkFromModel(
childTable,
undefined,
true,
oldRowId,
false,
{},
{
ignoreView: true,
getHiddenColumn: true,
extractOnlyPrimaries: true,
},
),
]); ]);
auditUpdateObj.push({ auditUpdateObj.push({
@ -5857,8 +5958,8 @@ class BaseModelSqlv2 {
} }
} else { } else {
// 1. check current row is linked with another child // 1. check current row is linked with another child
linkedCurrentOoRowObj = await this.dbDriver(childTn) linkedCurrentOoRowObj = await this.execAndParse(
.where({ this.dbDriver(childTn).where({
[childColumn.column_name]: this.dbDriver.from( [childColumn.column_name]: this.dbDriver.from(
this.dbDriver(parentTn) this.dbDriver(parentTn)
.select(parentColumn.column_name) .select(parentColumn.column_name)
@ -5866,8 +5967,10 @@ class BaseModelSqlv2 {
.first() .first()
.as('___cn_alias'), .as('___cn_alias'),
), ),
}) }),
.first(); null,
{ raw: true, first: true },
);
if (linkedCurrentOoRowObj) { if (linkedCurrentOoRowObj) {
const oldChildRowId = getCompositePkValue( const oldChildRowId = getCompositePkValue(
@ -5878,18 +5981,32 @@ class BaseModelSqlv2 {
if (oldChildRowId) { if (oldChildRowId) {
const [parentRelatedPkValue, childRelatedPkValue] = const [parentRelatedPkValue, childRelatedPkValue] =
await Promise.all([ await Promise.all([
await this.dbDriver(childTn) await this.readByPkFromModel(
.select( childTable,
`${childTable.table_name}.${childTable.displayValue.column_name}`, undefined,
) true,
.where(_wherePk(childTable.primaryKeys, oldChildRowId)) oldChildRowId,
.first(), false,
await this.dbDriver(parentTn) {},
.select( {
`${parentTable.table_name}.${parentTable.displayValue.column_name}`, ignoreView: true,
) getHiddenColumn: true,
.where(_wherePk(parentTable.primaryKeys, rowId)) extractOnlyPrimaries: true,
.first(), },
),
await this.readByPkFromModel(
parentTable,
undefined,
true,
rowId,
false,
{},
{
ignoreView: true,
getHiddenColumn: true,
extractOnlyPrimaries: true,
},
),
]); ]);
auditUpdateObj.push({ auditUpdateObj.push({
@ -5917,28 +6034,46 @@ class BaseModelSqlv2 {
} }
// 2. check current child is linked with another row cell // 2. check current child is linked with another row cell
linkedOoRowObj = await this.dbDriver(childTn) linkedOoRowObj = await this.execAndParse(
this.dbDriver(childTn)
.select(childColumn.column_name) .select(childColumn.column_name)
.where(_wherePk(childTable.primaryKeys, childId)) .where(_wherePk(childTable.primaryKeys, childId)),
.first(); null,
{ raw: true, first: true },
);
const oldRowId = linkedOoRowObj const oldRowId = linkedOoRowObj
? Object.values(linkedOoRowObj)?.[0] ? Object.values(linkedOoRowObj)?.[0]
: null; : null;
if (oldRowId) { if (oldRowId) {
const [parentRelatedPkValue, childRelatedPkValue] = const [parentRelatedPkValue, childRelatedPkValue] =
await Promise.all([ await Promise.all([
await this.dbDriver(childTn) await this.readByPkFromModel(
.select( childTable,
`${childTable.table_name}.${childTable.displayValue.column_name}`, undefined,
) true,
.where(_wherePk(childTable.primaryKeys, childId)) childId,
.first(), false,
await this.dbDriver(parentTn) {},
.select( {
`${parentTable.table_name}.${parentTable.displayValue.column_name}`, ignoreView: true,
) getHiddenColumn: true,
.where(_wherePk(parentTable.primaryKeys, oldRowId)) extractOnlyPrimaries: true,
.first(), },
),
await this.readByPkFromModel(
parentTable,
undefined,
true,
oldRowId,
false,
{},
{
ignoreView: true,
getHiddenColumn: true,
extractOnlyPrimaries: true,
},
),
]); ]);
auditUpdateObj.push({ auditUpdateObj.push({
@ -6075,12 +6210,15 @@ class BaseModelSqlv2 {
pkValue = undefined, pkValue = undefined,
): Promise<void> { ): Promise<void> {
if (!pkValue) { if (!pkValue) {
pkValue = await this.dbDriver(this.getTnPath(childModel)) pkValue = await this.readByPkFromModel(
.select( childModel,
`${childModel.table_name}.${childModel.displayValue.column_name}`, undefined,
) true,
.where(_wherePk(childModel.primaryKeys, childId)) childId,
.first(); false,
{},
{ ignoreView: true, getHiddenColumn: true, extractOnlyPrimaries: true },
);
} }
await Audit.insert({ await Audit.insert({
@ -6095,9 +6233,7 @@ class BaseModelSqlv2 {
`Record [id:${childId}] has been linked with record [id:${rowId}] in ${model.title}`, `Record [id:${childId}] has been linked with record [id:${rowId}] in ${model.title}`,
), ),
details: DOMPurify.sanitize(`<span class="">${columnTitle}</span> details: DOMPurify.sanitize(`<span class="">${columnTitle}</span>
: <span class="black--text green lighten-4 px-2">${ : <span class="black--text green lighten-4 px-2">${pkValue ?? null}</span>`),
Object.values(pkValue)[0]
}</span>`),
ip: req?.clientIp, ip: req?.clientIp,
user: req?.user?.email, user: req?.user?.email,
}); });
@ -6229,6 +6365,9 @@ class BaseModelSqlv2 {
break; break;
case RelationTypes.BELONGS_TO: case RelationTypes.BELONGS_TO:
{ {
auditConfig.parentModel = childTable;
auditConfig.childModel = parentTable;
await this.execAndParse( await this.execAndParse(
this.dbDriver(childTn) this.dbDriver(childTn)
// .where({ // .where({
@ -6248,14 +6387,15 @@ class BaseModelSqlv2 {
rowIds: [childId], rowIds: [childId],
cookie, cookie,
}); });
auditConfig.parentModel = childTable;
auditConfig.childModel = parentTable;
} }
break; break;
case RelationTypes.ONE_TO_ONE: case RelationTypes.ONE_TO_ONE:
{ {
const isBt = column.meta?.bt; const isBt = column.meta?.bt;
auditConfig.parentModel = isBt ? childTable : parentTable;
auditConfig.childModel = isBt ? parentTable : childTable;
await this.execAndParse( await this.execAndParse(
this.dbDriver(childTn) this.dbDriver(childTn)
.where(_wherePk(childTable.primaryKeys, isBt ? rowId : childId)) .where(_wherePk(childTable.primaryKeys, isBt ? rowId : childId))
@ -6269,9 +6409,6 @@ class BaseModelSqlv2 {
rowIds: [childId], rowIds: [childId],
cookie, cookie,
}); });
auditConfig.parentModel = isBt ? childTable : parentTable;
auditConfig.childModel = isBt ? parentTable : childTable;
} }
break; break;
} }
@ -7463,6 +7600,9 @@ class BaseModelSqlv2 {
break; break;
case RelationTypes.BELONGS_TO: case RelationTypes.BELONGS_TO:
{ {
auditConfig.parentModel = childTable;
auditConfig.childModel = parentTable;
// validate Ids // validate Ids
{ {
const childRowsQb = this.dbDriver(parentTn) const childRowsQb = this.dbDriver(parentTn)
@ -7804,6 +7944,9 @@ class BaseModelSqlv2 {
break; break;
case RelationTypes.BELONGS_TO: case RelationTypes.BELONGS_TO:
{ {
auditConfig.parentModel = childTable;
auditConfig.childModel = parentTable;
// validate Ids // validate Ids
{ {
if (childIds.length > 1) if (childIds.length > 1)

Loading…
Cancel
Save