From 8e0cf1d087c473a60e162f08edcb3b6a8bc8796c Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 19 Sep 2023 18:38:44 +0100 Subject: [PATCH] Moving things around ready for implementation of temp db handle. --- .../server/src/api/controllers/application.ts | 12 ++++---- .../server/src/sdk/app/applications/import.ts | 29 +++++++++++++++++++ .../server/src/sdk/app/applications/index.ts | 2 ++ 3 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 packages/server/src/sdk/app/applications/import.ts diff --git a/packages/server/src/api/controllers/application.ts b/packages/server/src/api/controllers/application.ts index a7d3634a89..8d4d1c673a 100644 --- a/packages/server/src/api/controllers/application.ts +++ b/packages/server/src/api/controllers/application.ts @@ -39,9 +39,8 @@ import { } from "../../db/defaultData/datasource_bb_default" import { removeAppFromUserRoles } from "../../utilities/workerRequests" import { stringToReadStream } from "../../utilities" -import { doesUserHaveLock, getLocksById } from "../../utilities/redis" +import { doesUserHaveLock } from "../../utilities/redis" import { cleanupAutomations } from "../../automations/utils" -import { checkAppMetadata } from "../../automations/logging" import { getUniqueRows } from "../../utilities/usageQuota/rows" import { groups, licensing, quotas } from "@budibase/pro" import { @@ -51,7 +50,6 @@ import { PlanType, Screen, UserCtx, - ContextUser, } from "@budibase/types" import { BASE_LAYOUT_PROP_IDS } from "../../constants/layouts" import sdk from "../../sdk" @@ -578,11 +576,15 @@ export async function sync(ctx: UserCtx) { export async function importToApp(ctx: UserCtx) { const { appId } = ctx.params const appExport = ctx.request.files?.appExport - const password = ctx.request.body.encryptionPassword + const password = ctx.request.body.encryptionPassword as string if (!appExport) { ctx.throw(400, "Must supply app export to import") } - console.log(appExport) + if (Array.isArray(appExport)) { + ctx.throw(400, "Must only supply one app export") + } + const fileAttributes = { type: appExport.type!, path: appExport.path! } + await sdk.applications.updateWithExport(appId, fileAttributes, password) ctx.body = { message: "app updated" } } diff --git a/packages/server/src/sdk/app/applications/import.ts b/packages/server/src/sdk/app/applications/import.ts new file mode 100644 index 0000000000..e72300fa4e --- /dev/null +++ b/packages/server/src/sdk/app/applications/import.ts @@ -0,0 +1,29 @@ +import { db as dbCore } from "@budibase/backend-core" +import backups from "../backups" + +export type FileAttributes = { + type: string + path: string +} + +export async function updateWithExport( + appId: string, + file: FileAttributes, + password?: string +) { + const devId = dbCore.getDevAppID(appId) + // TEMPORARY BEGIN + const appDb = dbCore.getDB(devId) + await appDb.destroy() + // TEMPORARY END + // const tempAppName = `temp_${devId}` + // const tempDb = dbCore.getDB(tempAppName) + const template = { + file: { + type: file.type!, + path: file.path!, + password, + }, + } + await backups.importApp(devId, appDb, template) +} diff --git a/packages/server/src/sdk/app/applications/index.ts b/packages/server/src/sdk/app/applications/index.ts index 963d065ce2..04ed3b2919 100644 --- a/packages/server/src/sdk/app/applications/index.ts +++ b/packages/server/src/sdk/app/applications/index.ts @@ -1,9 +1,11 @@ import * as sync from "./sync" import * as utils from "./utils" import * as applications from "./applications" +import * as imports from "./import" export default { ...sync, ...utils, ...applications, + ...imports, }