From 2ab368053807ab3f930ffdf1523842352d752537 Mon Sep 17 00:00:00 2001 From: adrinr Date: Tue, 31 Jan 2023 17:27:25 +0000 Subject: [PATCH] Fix installation race conditions --- packages/backend-core/src/installation.ts | 28 +++++++++++++------ .../src/tests/utilities/TestConfiguration.ts | 12 +------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/packages/backend-core/src/installation.ts b/packages/backend-core/src/installation.ts index 4e78a508a5..64be6f3f43 100644 --- a/packages/backend-core/src/installation.ts +++ b/packages/backend-core/src/installation.ts @@ -2,7 +2,7 @@ import { newid } from "./utils" import * as events from "./events" import { StaticDatabases } from "./db" import { doWithDB } from "./db" -import { Installation, IdentityType } from "@budibase/types" +import { Installation, IdentityType, Database } from "@budibase/types" import * as context from "./context" import semver from "semver" import { bustCache, withCache, TTL, CacheKey } from "./cache/generic" @@ -14,6 +14,24 @@ export const getInstall = async (): Promise => { useTenancy: false, }) } +async function createInstallDoc(platformDb: Database) { + const install: Installation = { + _id: StaticDatabases.PLATFORM_INFO.docs.install, + installId: newid(), + version: pkg.version, + } + try { + const resp = await platformDb.put(install) + install._rev = resp.rev + return install + } catch (err: any) { + if (err.status === 409) { + return getInstallFromDB() + } else { + throw err + } + } +} const getInstallFromDB = async (): Promise => { return doWithDB( @@ -26,13 +44,7 @@ const getInstallFromDB = async (): Promise => { ) } catch (e: any) { if (e.status === 404) { - install = { - _id: StaticDatabases.PLATFORM_INFO.docs.install, - installId: newid(), - version: pkg.version, - } - const resp = await platformDb.put(install) - install._rev = resp.rev + install = await createInstallDoc(platformDb) } else { throw e } diff --git a/packages/server/src/tests/utilities/TestConfiguration.ts b/packages/server/src/tests/utilities/TestConfiguration.ts index 6726de5bca..688b3cd698 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.ts +++ b/packages/server/src/tests/utilities/TestConfiguration.ts @@ -11,7 +11,7 @@ mocks.licenses.useUnlimited() import { init as dbInit } from "../../db" dbInit() import env from "../../environment" -import { db, env as coreEnv, StaticDatabases } from "@budibase/backend-core" +import { env as coreEnv } from "@budibase/backend-core" import { basicTable, basicRow, @@ -153,16 +153,6 @@ class TestConfiguration { this.tenantId = `tenant-${newid()}` } - try { - // Prepopulate dbs to avoid race conditions - await db.getDB(StaticDatabases.PLATFORM_INFO.name).checkSetup() - await db.getDB(StaticDatabases.PLATFORM_INFO.docs.install).checkSetup() - } catch (err: any) { - if (err.status !== 409) { - throw err - } - } - if (!this.started) { await startup() }