Handling deletion of related rows in formula updates.

This commit is contained in:
mike12345567 2022-01-24 16:31:18 +00:00
parent faa82d8e0f
commit 597faa6081
2 changed files with 46 additions and 38 deletions

View File

@ -36,15 +36,20 @@ const CALCULATION_TYPES = {
} }
/** /**
* This function runs through the enriched row, looks at the rows which * This function runs through a list of enriched rows, looks at the rows which
* are related and then checks if they need the state of their formulas * are related and then checks if they need the state of their formulas
* updated. * updated.
* NOTE: this will only for affect static formulas.
*/ */
async function updateRelatedFormula(appId, db, table, enrichedRow) { async function updateRelatedFormula(appId, db, table, enrichedRows) {
// no formula to update, we're done // no formula to update, we're done
if (!table.relatedFormula) { if (!table.relatedFormula) {
return return
} }
let promises = []
for (let enrichedRow of Array.isArray(enrichedRows)
? enrichedRows
: [enrichedRows]) {
// the related rows by tableId // the related rows by tableId
let relatedRows = {} let relatedRows = {}
for (let [key, field] of Object.entries(enrichedRow)) { for (let [key, field] of Object.entries(enrichedRow)) {
@ -57,7 +62,6 @@ async function updateRelatedFormula(appId, db, table, enrichedRow) {
relatedRows[relatedTableId] = relatedRows[relatedTableId].concat(field) relatedRows[relatedTableId] = relatedRows[relatedTableId].concat(field)
} }
} }
let promises = []
for (let tableId of table.relatedFormula) { for (let tableId of table.relatedFormula) {
try { try {
// no rows to update, skip // no rows to update, skip
@ -85,6 +89,7 @@ async function updateRelatedFormula(appId, db, table, enrichedRow) {
// no error scenario, table doesn't seem to exist anymore, ignore // no error scenario, table doesn't seem to exist anymore, ignore
} }
} }
}
await Promise.all(promises) await Promise.all(promises)
} }
@ -388,6 +393,8 @@ exports.destroy = async function (ctx) {
}) })
// remove any attachments that were on the row from object storage // remove any attachments that were on the row from object storage
await cleanupAttachments(appId, table, { row }) await cleanupAttachments(appId, table, { row })
// remove any static formula
await updateRelatedFormula(appId, db, table, row)
let response let response
if (ctx.params.tableId === InternalTables.USER_METADATA) { if (ctx.params.tableId === InternalTables.USER_METADATA) {
@ -436,6 +443,7 @@ exports.bulkDestroy = async ctx => {
} }
// remove any attachments that were on the rows from object storage // remove any attachments that were on the rows from object storage
await cleanupAttachments(appId, table, { rows }) await cleanupAttachments(appId, table, { rows })
await updateRelatedFormula(appId, db, table, rows)
await Promise.all(updates) await Promise.all(updates)
return { response: { ok: true }, rows } return { response: { ok: true }, rows }
} }

View File

@ -29,7 +29,7 @@ exports.checkForColumnUpdates = async (db, oldTable, updatedTable) => {
colName => updatedTable.schema[colName] == null colName => updatedTable.schema[colName] == null
) )
} }
// check for renaming of columns or deleted columns // check for renaming of columns, deleted columns or static formula update
if (rename || deletedColumns.length !== 0) { if (rename || deletedColumns.length !== 0) {
// Update all rows // Update all rows
const rows = await db.allDocs( const rows = await db.allDocs(