Browse Source

feat: introduce transaction

Signed-off-by: Pranav C <pranavxc@gmail.com>
test/reset-fail
Pranav C 1 year ago
parent
commit
5c0524b7ef
  1. 38
      packages/nocodb/src/db/BaseModelSqlv2.ts

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

@ -1877,16 +1877,13 @@ class BaseModelSqlv2 {
} }
async delByPk(id, _trx?, cookie?) { async delByPk(id, _trx?, cookie?) {
const trx: Transaction = _trx; let trx: Transaction = _trx;
try { try {
// retrieve data for handling params in hook // retrieve data for handling params in hook
const data = await this.readByPk(id); const data = await this.readByPk(id);
await this.beforeDelete(id, trx, cookie); await this.beforeDelete(id, trx, cookie);
// todo: use transaction const execQueries: ((trx: Transaction) => Promise<any>)[] = [];
// if (!trx) {
// trx = await this.dbDriver.transaction();
// }
// start a transaction if not already in one // start a transaction if not already in one
for (const column of this.model.columns) { for (const column of this.model.columns) {
@ -1903,9 +1900,11 @@ class BaseModelSqlv2 {
colId: colOptions.fk_mm_child_column_id, colId: colOptions.fk_mm_child_column_id,
}); });
await this.dbDriver(mmTable.table_name) execQueries.push((trx) =>
.del() trx(mmTable.table_name)
.where(mmParentColumn.column_name, id); .del()
.where(mmParentColumn.column_name, id),
);
} }
break; break;
case 'hm': case 'hm':
@ -1917,11 +1916,13 @@ class BaseModelSqlv2 {
colId: colOptions.fk_child_column_id, colId: colOptions.fk_child_column_id,
}); });
await this.dbDriver(relatedTable.table_name) execQueries.push((trx) =>
.update({ trx(relatedTable.table_name)
[childColumn.column_name]: null, .update({
}) [childColumn.column_name]: null,
.where(childColumn.column_name, id); })
.where(childColumn.column_name, id),
);
} }
break; break;
case 'bt': case 'bt':
@ -1931,14 +1932,19 @@ class BaseModelSqlv2 {
break; break;
} }
await trx(this.model.table_name).where(column.column_name, id).del(); // await trx(this.model.table_name).where(column.column_name, id).del();
} }
// iterate over all columns and unlink all LTAR data if (!trx) {
trx = await this.dbDriver.transaction();
}
const response = await this.dbDriver(this.tnPath) await Promise.all(execQueries.map((q) => q(trx)));
const response = await trx(this.tnPath)
.del() .del()
.where(await this._wherePk(id)); .where(await this._wherePk(id));
await this.afterDelete(data, trx, cookie); await this.afterDelete(data, trx, cookie);
return response; return response;
} catch (e) { } catch (e) {

Loading…
Cancel
Save