From 2900ef2fde1f459929472f66f5f91141f1bb70a7 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 27 Oct 2022 09:48:37 +0100 Subject: [PATCH] Adding a fix for checklist being incorrect after restoring from the CLI. --- packages/cli/src/backups/index.js | 11 ++++++++++- packages/cli/src/utils.js | 8 ++++++++ .../worker/src/api/controllers/system/restore.ts | 13 +++++++++++++ packages/worker/src/api/index.ts | 4 ++++ packages/worker/src/api/routes/index.ts | 2 ++ packages/worker/src/api/routes/system/restore.ts | 8 ++++++++ 6 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 packages/worker/src/api/controllers/system/restore.ts create mode 100644 packages/worker/src/api/routes/system/restore.ts diff --git a/packages/cli/src/backups/index.js b/packages/cli/src/backups/index.js index 697dafac6f..47b54fa9a0 100644 --- a/packages/cli/src/backups/index.js +++ b/packages/cli/src/backups/index.js @@ -4,7 +4,7 @@ const fs = require("fs") const { join } = require("path") const { getAllDbs } = require("../core/db") const tar = require("tar") -const { progressBar } = require("../utils") +const { progressBar, httpCall } = require("../utils") const { TEMP_DIR, COUCH_DIR, @@ -86,6 +86,15 @@ async function importBackup(opts) { bar.stop() console.log("MinIO Import") await importObjects() + // finish by letting the system know that a restore has occurred + try { + await httpCall( + `http://localhost:${config.MAIN_PORT}/api/system/restored`, + "POST" + ) + } catch (err) { + // ignore error - it will be an older system + } console.log("Import complete") fs.rmSync(TEMP_DIR, { recursive: true }) } diff --git a/packages/cli/src/utils.js b/packages/cli/src/utils.js index ba793420e7..d041fb498a 100644 --- a/packages/cli/src/utils.js +++ b/packages/cli/src/utils.js @@ -23,6 +23,14 @@ exports.downloadFile = async (url, filePath) => { }) } +exports.httpCall = async (url, method) => { + const response = await axios({ + url, + method, + }) + return response.data +} + exports.getHelpDescription = string => { return chalk.cyan(string) } diff --git a/packages/worker/src/api/controllers/system/restore.ts b/packages/worker/src/api/controllers/system/restore.ts new file mode 100644 index 0000000000..96a7c61cb4 --- /dev/null +++ b/packages/worker/src/api/controllers/system/restore.ts @@ -0,0 +1,13 @@ +import env from "../../../environment" +import { BBContext } from "@budibase/types" +import { cache } from "@budibase/backend-core" + +export async function systemRestored(ctx: BBContext) { + if (!env.SELF_HOSTED) { + ctx.throw(405, "This operation is not allowed in cloud.") + } + await cache.bustCache(cache.CacheKeys.CHECKLIST) + ctx.body = { + message: "System prepared after restore.", + } +} diff --git a/packages/worker/src/api/index.ts b/packages/worker/src/api/index.ts index 21fbf1d993..22ff159dff 100644 --- a/packages/worker/src/api/index.ts +++ b/packages/worker/src/api/index.ts @@ -55,6 +55,10 @@ const PUBLIC_ENDPOINTS = [ route: "/api/global/users/tenant/:id", method: "GET", }, + { + route: "/api/system/restored", + method: "POST", + }, ] const NO_TENANCY_ENDPOINTS = [ diff --git a/packages/worker/src/api/routes/index.ts b/packages/worker/src/api/routes/index.ts index 67edf5d51b..0c107aae26 100644 --- a/packages/worker/src/api/routes/index.ts +++ b/packages/worker/src/api/routes/index.ts @@ -13,6 +13,7 @@ import selfRoutes from "./global/self" import licenseRoutes from "./global/license" import migrationRoutes from "./system/migrations" import accountRoutes from "./system/accounts" +import restoreRoutes from "./system/restore" let userGroupRoutes = api.groups export const routes = [ @@ -31,4 +32,5 @@ export const routes = [ userGroupRoutes, migrationRoutes, accountRoutes, + restoreRoutes, ] diff --git a/packages/worker/src/api/routes/system/restore.ts b/packages/worker/src/api/routes/system/restore.ts new file mode 100644 index 0000000000..ee4bee091d --- /dev/null +++ b/packages/worker/src/api/routes/system/restore.ts @@ -0,0 +1,8 @@ +import * as controller from "../../controllers/system/restore" +import Router from "@koa/router" + +const router = new Router() + +router.post("/api/system/restored", controller.systemRestored) + +export = router