Handling deletion of related rows in formula updates.
This commit is contained in:
parent
10a2915bcb
commit
3bc51864b4
|
@ -36,53 +36,58 @@ 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
|
||||||
}
|
}
|
||||||
// the related rows by tableId
|
|
||||||
let relatedRows = {}
|
|
||||||
for (let [key, field] of Object.entries(enrichedRow)) {
|
|
||||||
const columnDefinition = table.schema[key]
|
|
||||||
if (columnDefinition && columnDefinition.type === FieldTypes.LINK) {
|
|
||||||
const relatedTableId = columnDefinition.tableId
|
|
||||||
if (!relatedRows[relatedTableId]) {
|
|
||||||
relatedRows[relatedTableId] = []
|
|
||||||
}
|
|
||||||
relatedRows[relatedTableId] = relatedRows[relatedTableId].concat(field)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let promises = []
|
let promises = []
|
||||||
for (let tableId of table.relatedFormula) {
|
for (let enrichedRow of Array.isArray(enrichedRows)
|
||||||
try {
|
? enrichedRows
|
||||||
// no rows to update, skip
|
: [enrichedRows]) {
|
||||||
if (!relatedRows[tableId] || relatedRows[tableId].length === 0) {
|
// the related rows by tableId
|
||||||
continue
|
let relatedRows = {}
|
||||||
}
|
for (let [key, field] of Object.entries(enrichedRow)) {
|
||||||
const relatedTable = await db.get(tableId)
|
const columnDefinition = table.schema[key]
|
||||||
for (let column of Object.values(relatedTable.schema)) {
|
if (columnDefinition && columnDefinition.type === FieldTypes.LINK) {
|
||||||
// needs updated in related rows
|
const relatedTableId = columnDefinition.tableId
|
||||||
if (
|
if (!relatedRows[relatedTableId]) {
|
||||||
column.type === FieldTypes.FORMULA &&
|
relatedRows[relatedTableId] = []
|
||||||
column.formulaType === FormulaTypes.STATIC
|
|
||||||
) {
|
|
||||||
// re-enrich rows for all the related, don't update the related formula for them
|
|
||||||
promises = promises.concat(
|
|
||||||
relatedRows[tableId].map(related =>
|
|
||||||
storeResponse(appId, db, relatedTable, related, {
|
|
||||||
updateFormula: false,
|
|
||||||
})
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
relatedRows[relatedTableId] = relatedRows[relatedTableId].concat(field)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (let tableId of table.relatedFormula) {
|
||||||
|
try {
|
||||||
|
// no rows to update, skip
|
||||||
|
if (!relatedRows[tableId] || relatedRows[tableId].length === 0) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
const relatedTable = await db.get(tableId)
|
||||||
|
for (let column of Object.values(relatedTable.schema)) {
|
||||||
|
// needs updated in related rows
|
||||||
|
if (
|
||||||
|
column.type === FieldTypes.FORMULA &&
|
||||||
|
column.formulaType === FormulaTypes.STATIC
|
||||||
|
) {
|
||||||
|
// re-enrich rows for all the related, don't update the related formula for them
|
||||||
|
promises = promises.concat(
|
||||||
|
relatedRows[tableId].map(related =>
|
||||||
|
storeResponse(appId, db, relatedTable, related, {
|
||||||
|
updateFormula: false,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
// no error scenario, table doesn't seem to exist anymore, ignore
|
||||||
}
|
}
|
||||||
} catch (err) {
|
|
||||||
// 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 }
|
||||||
}
|
}
|
||||||
|
|
|
@ -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(
|
||||||
|
|
Loading…
Reference in New Issue