1
0
Fork 0
mirror of synced 2024-10-03 02:27:06 +13:00

Add appMigrationMetadata utils

This commit is contained in:
Adria Navarro 2023-11-27 11:06:49 +01:00
parent 661735a3fd
commit c6840960a4
2 changed files with 40 additions and 0 deletions

View file

@ -0,0 +1,39 @@
import { Duration, cache, db, env } from "@budibase/backend-core"
import { Database, App, DocumentType, Document } from "@budibase/types"
export interface AppMigrationDoc extends Document {
version: string
}
const EXPIRY_SECONDS = Duration.fromDays(1).toSeconds()
async function populateFromDB(appId: string) {
return db.doWithDB(
appId,
(db: Database) => {
return db.get<App>(DocumentType.APP_MIGRATION_METADATA)
},
{ skip_setup: true }
)
}
export async function getAppMigrationMetadata(appId: string): Promise<string> {
const cacheKey = `appmigrations_${env.VERSION}_${appId}`
let metadata: AppMigrationDoc | undefined = await cache.get(cacheKey)
if (!metadata) {
try {
metadata = await populateFromDB(appId)
} catch (err: any) {
if (err.status !== 404) {
throw err
}
metadata = { version: "" }
}
await cache.store(cacheKey, metadata, EXPIRY_SECONDS)
}
return metadata.version
}

View file

@ -37,6 +37,7 @@ export enum DocumentType {
USER_FLAG = "flag",
AUTOMATION_METADATA = "meta_au",
AUDIT_LOG = "al",
APP_MIGRATION_METADATA = "_design/migrations",
}
// these are the core documents that make up the data, design