2022-11-25 07:48:51 +13:00
|
|
|
import { newid } from "./utils"
|
2022-05-29 08:38:22 +12:00
|
|
|
import * as events from "./events"
|
2022-11-25 07:48:51 +13:00
|
|
|
import { StaticDatabases } from "./db"
|
2022-05-29 08:38:22 +12:00
|
|
|
import { doWithDB } from "./db"
|
2023-02-01 06:27:25 +13:00
|
|
|
import { Installation, IdentityType, Database } from "@budibase/types"
|
2022-05-29 08:38:22 +12:00
|
|
|
import * as context from "./context"
|
2022-05-27 10:57:14 +12:00
|
|
|
import semver from "semver"
|
2022-11-25 07:48:51 +13:00
|
|
|
import { bustCache, withCache, TTL, CacheKey } from "./cache/generic"
|
2022-05-27 10:57:14 +12:00
|
|
|
|
|
|
|
const pkg = require("../package.json")
|
|
|
|
|
|
|
|
export const getInstall = async (): Promise<Installation> => {
|
2022-11-25 07:48:51 +13:00
|
|
|
return withCache(CacheKey.INSTALLATION, TTL.ONE_DAY, getInstallFromDB, {
|
2022-05-31 08:46:08 +12:00
|
|
|
useTenancy: false,
|
|
|
|
})
|
|
|
|
}
|
2023-02-01 06:27:25 +13:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-31 08:46:08 +12:00
|
|
|
|
|
|
|
const getInstallFromDB = async (): Promise<Installation> => {
|
2022-05-29 08:38:22 +12:00
|
|
|
return doWithDB(
|
|
|
|
StaticDatabases.PLATFORM_INFO.name,
|
2022-05-27 10:57:14 +12:00
|
|
|
async (platformDb: any) => {
|
|
|
|
let install: Installation
|
|
|
|
try {
|
|
|
|
install = await platformDb.get(
|
2022-05-29 08:38:22 +12:00
|
|
|
StaticDatabases.PLATFORM_INFO.docs.install
|
2022-05-27 10:57:14 +12:00
|
|
|
)
|
|
|
|
} catch (e: any) {
|
|
|
|
if (e.status === 404) {
|
2023-02-01 06:27:25 +13:00
|
|
|
install = await createInstallDoc(platformDb)
|
2022-05-27 10:57:14 +12:00
|
|
|
} else {
|
|
|
|
throw e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return install
|
2022-06-09 23:33:10 +12:00
|
|
|
}
|
2022-05-27 10:57:14 +12:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateVersion = async (version: string): Promise<boolean> => {
|
|
|
|
try {
|
2022-05-29 08:38:22 +12:00
|
|
|
await doWithDB(
|
|
|
|
StaticDatabases.PLATFORM_INFO.name,
|
2022-05-27 10:57:14 +12:00
|
|
|
async (platformDb: any) => {
|
|
|
|
const install = await getInstall()
|
|
|
|
install.version = version
|
|
|
|
await platformDb.put(install)
|
2022-11-25 07:48:51 +13:00
|
|
|
await bustCache(CacheKey.INSTALLATION)
|
2022-06-09 23:33:10 +12:00
|
|
|
}
|
2022-05-27 10:57:14 +12:00
|
|
|
)
|
|
|
|
} catch (e: any) {
|
|
|
|
if (e.status === 409) {
|
|
|
|
// do nothing - version has already been updated
|
|
|
|
// likely in clustered environment
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
throw e
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
export const checkInstallVersion = async (): Promise<void> => {
|
|
|
|
const install = await getInstall()
|
|
|
|
|
|
|
|
const currentVersion = install.version
|
|
|
|
const newVersion = pkg.version
|
|
|
|
|
|
|
|
if (currentVersion !== newVersion) {
|
|
|
|
const isUpgrade = semver.gt(newVersion, currentVersion)
|
|
|
|
const isDowngrade = semver.lt(newVersion, currentVersion)
|
|
|
|
|
|
|
|
const success = await updateVersion(newVersion)
|
|
|
|
|
|
|
|
if (success) {
|
2022-05-29 08:38:22 +12:00
|
|
|
await context.doInIdentityContext(
|
2022-05-27 10:57:14 +12:00
|
|
|
{
|
|
|
|
_id: install.installId,
|
2022-05-29 08:38:22 +12:00
|
|
|
type: IdentityType.INSTALLATION,
|
2022-05-27 10:57:14 +12:00
|
|
|
},
|
|
|
|
async () => {
|
|
|
|
if (isUpgrade) {
|
2022-06-15 22:20:13 +12:00
|
|
|
await events.installation.upgraded(currentVersion, newVersion)
|
2022-05-27 10:57:14 +12:00
|
|
|
} else if (isDowngrade) {
|
2022-06-15 22:20:13 +12:00
|
|
|
await events.installation.downgraded(currentVersion, newVersion)
|
2022-05-27 10:57:14 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2022-05-29 08:38:22 +12:00
|
|
|
await events.identification.identifyInstallationGroup(install.installId)
|
2022-05-27 10:57:14 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|