Fix installation race conditions
This commit is contained in:
parent
abdd658012
commit
2ab3680538
2 changed files with 21 additions and 19 deletions
|
@ -2,7 +2,7 @@ import { newid } from "./utils"
|
||||||
import * as events from "./events"
|
import * as events from "./events"
|
||||||
import { StaticDatabases } from "./db"
|
import { StaticDatabases } from "./db"
|
||||||
import { doWithDB } 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 * as context from "./context"
|
||||||
import semver from "semver"
|
import semver from "semver"
|
||||||
import { bustCache, withCache, TTL, CacheKey } from "./cache/generic"
|
import { bustCache, withCache, TTL, CacheKey } from "./cache/generic"
|
||||||
|
@ -14,6 +14,24 @@ export const getInstall = async (): Promise<Installation> => {
|
||||||
useTenancy: false,
|
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> => {
|
const getInstallFromDB = async (): Promise<Installation> => {
|
||||||
return doWithDB(
|
return doWithDB(
|
||||||
|
@ -26,13 +44,7 @@ const getInstallFromDB = async (): Promise<Installation> => {
|
||||||
)
|
)
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
if (e.status === 404) {
|
if (e.status === 404) {
|
||||||
install = {
|
install = await createInstallDoc(platformDb)
|
||||||
_id: StaticDatabases.PLATFORM_INFO.docs.install,
|
|
||||||
installId: newid(),
|
|
||||||
version: pkg.version,
|
|
||||||
}
|
|
||||||
const resp = await platformDb.put(install)
|
|
||||||
install._rev = resp.rev
|
|
||||||
} else {
|
} else {
|
||||||
throw e
|
throw e
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ mocks.licenses.useUnlimited()
|
||||||
import { init as dbInit } from "../../db"
|
import { init as dbInit } from "../../db"
|
||||||
dbInit()
|
dbInit()
|
||||||
import env from "../../environment"
|
import env from "../../environment"
|
||||||
import { db, env as coreEnv, StaticDatabases } from "@budibase/backend-core"
|
import { env as coreEnv } from "@budibase/backend-core"
|
||||||
import {
|
import {
|
||||||
basicTable,
|
basicTable,
|
||||||
basicRow,
|
basicRow,
|
||||||
|
@ -153,16 +153,6 @@ class TestConfiguration {
|
||||||
this.tenantId = `tenant-${newid()}`
|
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) {
|
if (!this.started) {
|
||||||
await startup()
|
await startup()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue