From 8ac9420e5be93b69c8d7990bbe49c9b81a017ae7 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 27 Nov 2023 15:40:37 +0100 Subject: [PATCH] Add middleware to queue --- packages/backend-core/src/queue/constants.ts | 1 + packages/backend-core/src/queue/listeners.ts | 2 ++ packages/server/src/api/index.ts | 3 +++ packages/server/src/appMigrations/index.ts | 18 ++++++++++++++++++ packages/server/src/appMigrations/queue.ts | 13 +++++++++++++ .../server/src/middleware/appMigrations.ts | 14 ++++++++++++++ packages/types/src/sdk/locks.ts | 1 + 7 files changed, 52 insertions(+) create mode 100644 packages/server/src/appMigrations/index.ts create mode 100644 packages/server/src/appMigrations/queue.ts create mode 100644 packages/server/src/middleware/appMigrations.ts diff --git a/packages/backend-core/src/queue/constants.ts b/packages/backend-core/src/queue/constants.ts index e1ffcfee36..eb4f21aced 100644 --- a/packages/backend-core/src/queue/constants.ts +++ b/packages/backend-core/src/queue/constants.ts @@ -3,4 +3,5 @@ export enum JobQueue { APP_BACKUP = "appBackupQueue", AUDIT_LOG = "auditLogQueue", SYSTEM_EVENT_QUEUE = "systemEventQueue", + APP_MIGRATION = "appMigration", } diff --git a/packages/backend-core/src/queue/listeners.ts b/packages/backend-core/src/queue/listeners.ts index 42e3172364..063a01bd2f 100644 --- a/packages/backend-core/src/queue/listeners.ts +++ b/packages/backend-core/src/queue/listeners.ts @@ -87,6 +87,7 @@ enum QueueEventType { APP_BACKUP_EVENT = "app-backup-event", AUDIT_LOG_EVENT = "audit-log-event", SYSTEM_EVENT = "system-event", + APP_MIGRATION = "app-migration", } const EventTypeMap: { [key in JobQueue]: QueueEventType } = { @@ -94,6 +95,7 @@ const EventTypeMap: { [key in JobQueue]: QueueEventType } = { [JobQueue.APP_BACKUP]: QueueEventType.APP_BACKUP_EVENT, [JobQueue.AUDIT_LOG]: QueueEventType.AUDIT_LOG_EVENT, [JobQueue.SYSTEM_EVENT_QUEUE]: QueueEventType.SYSTEM_EVENT, + [JobQueue.APP_MIGRATION]: QueueEventType.APP_MIGRATION, } function logging(queue: Queue, jobQueue: JobQueue) { diff --git a/packages/server/src/api/index.ts b/packages/server/src/api/index.ts index 6221909347..a01e3764f0 100644 --- a/packages/server/src/api/index.ts +++ b/packages/server/src/api/index.ts @@ -4,6 +4,7 @@ import currentApp from "../middleware/currentapp" import zlib from "zlib" import { mainRoutes, staticRoutes, publicRoutes } from "./routes" import { middleware as pro } from "@budibase/pro" +import migrations from "../middleware/appMigrations" export { shutdown } from "./routes/public" const compress = require("koa-compress") @@ -47,6 +48,8 @@ router // @ts-ignore .use(currentApp) .use(auth.auditLog) + // @ts-ignore + .use(migrations) // authenticated routes for (let route of mainRoutes) { diff --git a/packages/server/src/appMigrations/index.ts b/packages/server/src/appMigrations/index.ts new file mode 100644 index 0000000000..079e3e8c44 --- /dev/null +++ b/packages/server/src/appMigrations/index.ts @@ -0,0 +1,18 @@ +import queue from "./queue" +import { getAppMigrationMetadata } from "./appMigrationMetadata" +import { MIGRATIONS } from "./migrations" + +const latestMigration = Object.keys(MIGRATIONS).sort().reverse()[0] + +export async function checkMissingMigrations(appId: string) { + const currentVersion = await getAppMigrationMetadata(appId) + + if (currentVersion < latestMigration) { + await queue.add( + { + appId, + } + // TODO: idempotency + ) + } +} diff --git a/packages/server/src/appMigrations/queue.ts b/packages/server/src/appMigrations/queue.ts new file mode 100644 index 0000000000..d842b97e49 --- /dev/null +++ b/packages/server/src/appMigrations/queue.ts @@ -0,0 +1,13 @@ +import { queue } from "@budibase/backend-core" +import { Job } from "bull" + +const appMigrationQueue = queue.createQueue(queue.JobQueue.APP_MIGRATION) +appMigrationQueue.process(processMessage) + +async function processMessage(job: Job) { + const { appId } = job.data + + console.log(appId) +} + +export default appMigrationQueue diff --git a/packages/server/src/middleware/appMigrations.ts b/packages/server/src/middleware/appMigrations.ts new file mode 100644 index 0000000000..a94b8823e8 --- /dev/null +++ b/packages/server/src/middleware/appMigrations.ts @@ -0,0 +1,14 @@ +import { UserCtx } from "@budibase/types" +import { checkMissingMigrations } from "../appMigrations" + +export default async (ctx: UserCtx, next: any) => { + const { appId } = ctx + + if (!appId) { + return next() + } + + await checkMissingMigrations(appId) + + return next() +} diff --git a/packages/types/src/sdk/locks.ts b/packages/types/src/sdk/locks.ts index 6ff91d4315..82a7089b3f 100644 --- a/packages/types/src/sdk/locks.ts +++ b/packages/types/src/sdk/locks.ts @@ -20,6 +20,7 @@ export enum LockName { UPDATE_TENANTS_DOC = "update_tenants_doc", PERSIST_WRITETHROUGH = "persist_writethrough", QUOTA_USAGE_EVENT = "quota_usage_event", + APP_MIGRATION = "app_migrations", } export type LockOptions = {