From 0c77cf2b40d6fcec40de872934e7a90e79278ea8 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 21 Dec 2023 17:14:34 +0100 Subject: [PATCH] Fix concurrent saves --- .../src/api/controllers/row/staticFormula.ts | 26 +++++++++++++------ .../src/sdk/app/rows/tests/internal.spec.ts | 17 ++++++++---- packages/types/src/sdk/locks.ts | 1 + 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/packages/server/src/api/controllers/row/staticFormula.ts b/packages/server/src/api/controllers/row/staticFormula.ts index 8d52b6a05c..3cb7348ee3 100644 --- a/packages/server/src/api/controllers/row/staticFormula.ts +++ b/packages/server/src/api/controllers/row/staticFormula.ts @@ -5,8 +5,8 @@ import { processFormulas, } from "../../../utilities/rowProcessor" import { FieldTypes, FormulaTypes } from "../../../constants" -import { context } from "@budibase/backend-core" -import { Table, Row } from "@budibase/types" +import { context, locks } from "@budibase/backend-core" +import { Table, Row, LockType, LockName } from "@budibase/types" import * as linkRows from "../../../db/linkedRows" import sdk from "../../../sdk" import isEqual from "lodash/isEqual" @@ -149,12 +149,22 @@ export async function finaliseRow( await db.put(table) } catch (err: any) { if (err.status === 409) { - const updatedTable = await sdk.tables.getTable(table._id!) - let response = processAutoColumn(null, updatedTable, row, { - reprocessing: true, - }) - await db.put(response.table) - row = response.row + // Some conflicts with the autocolumns occurred, we need to refetch the table and recalculate + await locks.doWithLock( + { + type: LockType.AUTO_EXTEND, + name: LockName.PROCESS_AUTO_COLUMNS, + resource: table._id, + }, + async () => { + const latestTable = await sdk.tables.getTable(table._id!) + let response = processAutoColumn(null, latestTable, row, { + reprocessing: true, + }) + await db.put(response.table) + row = response.row + } + ) } else { throw err } diff --git a/packages/server/src/sdk/app/rows/tests/internal.spec.ts b/packages/server/src/sdk/app/rows/tests/internal.spec.ts index 62a056bef3..b60d31b226 100644 --- a/packages/server/src/sdk/app/rows/tests/internal.spec.ts +++ b/packages/server/src/sdk/app/rows/tests/internal.spec.ts @@ -189,25 +189,32 @@ describe("sdk >> rows >> internal", () => { }) await config.doInContext(config.appId, async () => { - for (const row of makeRows(30)) { + for (const row of makeRows(5)) { await internalSdk.save(table._id!, row, config.user._id) } await Promise.all( - makeRows(200).map(row => + makeRows(10).map(row => internalSdk.save(table._id!, row, config.user._id) ) ) - for (const row of makeRows(20)) { + for (const row of makeRows(5)) { await internalSdk.save(table._id!, row, config.user._id) } }) const persistedRows = await config.getRows(table._id!) - expect(persistedRows).toHaveLength(250) + expect(persistedRows).toHaveLength(20) + expect(persistedRows).toEqual( + expect.arrayContaining( + Array.from({ length: 20 }).map((_, i) => + expect.objectContaining({ id: i + 1 }) + ) + ) + ) const persistedTable = await config.getTable(table._id) expect((table as any).schema.id.lastID).toBe(0) - expect(persistedTable.schema.id.lastID).toBe(250) + expect(persistedTable.schema.id.lastID).toBe(20) }) }) }) diff --git a/packages/types/src/sdk/locks.ts b/packages/types/src/sdk/locks.ts index 82a7089b3f..0e6053a4db 100644 --- a/packages/types/src/sdk/locks.ts +++ b/packages/types/src/sdk/locks.ts @@ -21,6 +21,7 @@ export enum LockName { PERSIST_WRITETHROUGH = "persist_writethrough", QUOTA_USAGE_EVENT = "quota_usage_event", APP_MIGRATION = "app_migrations", + PROCESS_AUTO_COLUMNS = "process_auto_columns", } export type LockOptions = {