Fix installation race conditions

This commit is contained in:
adrinr 2023-01-31 17:27:25 +00:00
parent abdd658012
commit 2ab3680538
2 changed files with 21 additions and 19 deletions

View File

@ -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<Installation> => {
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<Installation> => {
return doWithDB(
@ -26,13 +44,7 @@ const getInstallFromDB = async (): Promise<Installation> => {
)
} 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
}

View File

@ -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()
}