From 758c46ce838724d3a27f231555cd43064238f83b Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 14 Oct 2022 20:10:44 +0100 Subject: [PATCH] Minor fixes after testing manual backup system. --- packages/backend-core/src/context/index.ts | 3 ++ packages/server/src/api/routes/index.ts | 10 ++-- packages/server/src/sdk/app/backups/backup.ts | 50 ++++++++++--------- packages/types/src/sdk/koa.ts | 9 +++- 4 files changed, 41 insertions(+), 31 deletions(-) diff --git a/packages/backend-core/src/context/index.ts b/packages/backend-core/src/context/index.ts index 35eeee608b..7efe0e23f7 100644 --- a/packages/backend-core/src/context/index.ts +++ b/packages/backend-core/src/context/index.ts @@ -53,6 +53,9 @@ export const getTenantIDFromAppID = (appId: string) => { if (!appId) { return null } + if (!isMultiTenant()) { + return DEFAULT_TENANT_ID + } const split = appId.split(SEPARATOR) const hasDev = split[1] === DocumentType.DEV if ((hasDev && split.length === 3) || (!hasDev && split.length === 2)) { diff --git a/packages/server/src/api/routes/index.ts b/packages/server/src/api/routes/index.ts index 1cf34d4a68..02a4900077 100644 --- a/packages/server/src/api/routes/index.ts +++ b/packages/server/src/api/routes/index.ts @@ -34,6 +34,8 @@ export { default as publicRoutes } from "./public" const appBackupRoutes = api.appBackups const scheduleRoutes = api.schedules export const mainRoutes: Router[] = [ + appBackupRoutes, + backupRoutes, authRoutes, deployRoutes, layoutRoutes, @@ -53,16 +55,14 @@ export const mainRoutes: Router[] = [ permissionRoutes, datasourceRoutes, queryRoutes, - backupRoutes, metadataRoutes, devRoutes, cloudRoutes, - // these need to be handled last as they still use /api/:tableId - // this could be breaking as koa may recognise other routes as this - tableRoutes, rowRoutes, migrationRoutes, pluginRoutes, - appBackupRoutes, scheduleRoutes, + // these need to be handled last as they still use /api/:tableId + // this could be breaking as koa may recognise other routes as this + tableRoutes, ] diff --git a/packages/server/src/sdk/app/backups/backup.ts b/packages/server/src/sdk/app/backups/backup.ts index 6a0d370653..fb4bcc6022 100644 --- a/packages/server/src/sdk/app/backups/backup.ts +++ b/packages/server/src/sdk/app/backups/backup.ts @@ -10,30 +10,32 @@ export async function init() { const appId = job.data.appId, trigger = job.data.trigger, name = job.data.name - const createdAt = new Date().toISOString() - const tarPath = await exportApp(appId, { tar: true }) - let filename = `${appId}/backup-${createdAt}.tar.gz` - // add the tenant to the bucket path if backing up within a multi-tenant environment - if (env.MULTI_TENANCY) { - const tenantId = tenancy.getTenantIDFromAppID(appId) - filename = `${tenantId}/${filename}` - } - const bucket = objectStore.ObjectStoreBuckets.BACKUPS - const metadata = { - appId, - createdAt, - trigger, - name, - } - await objectStore.upload({ - path: tarPath, - type: "application/gzip", - bucket, - filename, - metadata, + const tenantId = tenancy.getTenantIDFromAppID(appId) + await tenancy.doInTenant(tenantId, async () => { + const createdAt = new Date().toISOString() + const tarPath = await exportApp(appId, { tar: true }) + let filename = `${appId}/backup-${createdAt}.tar.gz` + // add the tenant to the bucket path if backing up within a multi-tenant environment + if (env.MULTI_TENANCY) { + filename = `${tenantId}/${filename}` + } + const bucket = objectStore.ObjectStoreBuckets.BACKUPS + const metadata = { + appId, + createdAt, + trigger, + name, + } + await objectStore.upload({ + path: tarPath, + type: "application/gzip", + bucket, + filename, + metadata, + }) + await backups.storeAppBackupMetadata(filename, metadata) + // clear up the tarball after uploading it + fs.rmSync(tarPath) }) - await backups.storeAppBackupMetadata(filename, metadata) - // clear up the tarball after uploading it - fs.rmSync(tarPath) }) } diff --git a/packages/types/src/sdk/koa.ts b/packages/types/src/sdk/koa.ts index 8d419d5cf1..250b176d79 100644 --- a/packages/types/src/sdk/koa.ts +++ b/packages/types/src/sdk/koa.ts @@ -7,7 +7,12 @@ export interface ContextUser extends User { license: License } -export interface BBContext extends Context { +export interface BBContext { user?: ContextUser - body: any + request: { + body: any + } + params: any + body?: any + redirect?: any }