Fixing multi-app import from cloud.
This commit is contained in:
parent
ae0bfebae7
commit
488e0c44c0
|
@ -3,15 +3,9 @@ const { getAllApps, getGlobalDBName } = require("@budibase/backend-core/db")
|
||||||
const { getGlobalDB } = require("@budibase/backend-core/tenancy")
|
const { getGlobalDB } = require("@budibase/backend-core/tenancy")
|
||||||
const { streamFile } = require("../../utilities/fileSystem")
|
const { streamFile } = require("../../utilities/fileSystem")
|
||||||
const { stringToReadStream } = require("../../utilities")
|
const { stringToReadStream } = require("../../utilities")
|
||||||
const {
|
const { getDocParams, DocumentType, isDevAppID } = require("../../db/utils")
|
||||||
getDocParams,
|
|
||||||
DocumentType,
|
|
||||||
isDevAppID,
|
|
||||||
APP_PREFIX,
|
|
||||||
} = require("../../db/utils")
|
|
||||||
const { create } = require("./application")
|
const { create } = require("./application")
|
||||||
const { join } = require("path")
|
const { join } = require("path")
|
||||||
const fs = require("fs")
|
|
||||||
const sdk = require("../../sdk")
|
const sdk = require("../../sdk")
|
||||||
|
|
||||||
async function createApp(appName, appDirectory) {
|
async function createApp(appName, appDirectory) {
|
||||||
|
@ -41,9 +35,9 @@ async function getAllDocType(db, docType) {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.exportApps = async ctx => {
|
exports.exportApps = async ctx => {
|
||||||
if (env.SELF_HOSTED || !env.MULTI_TENANCY) {
|
// if (env.SELF_HOSTED || !env.MULTI_TENANCY) {
|
||||||
ctx.throw(400, "Exporting only allowed in multi-tenant cloud environments.")
|
// ctx.throw(400, "Exporting only allowed in multi-tenant cloud environments.")
|
||||||
}
|
// }
|
||||||
const apps = await getAllApps({ all: true })
|
const apps = await getAllApps({ all: true })
|
||||||
const globalDBString = await sdk.backups.exportDB(getGlobalDBName(), {
|
const globalDBString = await sdk.backups.exportDB(getGlobalDBName(), {
|
||||||
filter: doc => !doc._id.startsWith(DocumentType.USER),
|
filter: doc => !doc._id.startsWith(DocumentType.USER),
|
||||||
|
@ -92,20 +86,16 @@ exports.importApps = async ctx => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// initially get all the app databases out of the tarball
|
// initially get all the app databases out of the tarball
|
||||||
const tmpPath = sdk.backups.untarFile(ctx.request.file.importFile)
|
const tmpPath = sdk.backups.untarFile(ctx.request.files.importFile)
|
||||||
const globalDbImport = sdk.backups.getGlobalDBFile(tmpPath)
|
const globalDbImport = sdk.backups.getGlobalDBFile(tmpPath)
|
||||||
const appNames = fs
|
const appNames = sdk.backups.getListOfAppsInMulti(tmpPath)
|
||||||
.readdirSync(tmpPath)
|
|
||||||
.filter(dir => dir.startsWith(APP_PREFIX))
|
|
||||||
|
|
||||||
const globalDb = getGlobalDB()
|
const globalDb = getGlobalDB()
|
||||||
// load the global db first
|
// load the global db first
|
||||||
await globalDb.load(stringToReadStream(globalDbImport))
|
await globalDb.load(stringToReadStream(globalDbImport))
|
||||||
const appCreationPromises = []
|
|
||||||
for (let appName of appNames) {
|
for (let appName of appNames) {
|
||||||
appCreationPromises.push(createApp(appName, join(tmpPath, appName)))
|
await createApp(appName, join(tmpPath, appName))
|
||||||
}
|
}
|
||||||
await Promise.all(appCreationPromises)
|
|
||||||
|
|
||||||
// if there are any users make sure to remove them
|
// if there are any users make sure to remove them
|
||||||
let users = await getAllDocType(globalDb, DocumentType.USER)
|
let users = await getAllDocType(globalDb, DocumentType.USER)
|
||||||
|
|
|
@ -141,7 +141,7 @@ export async function exportMultipleApps(
|
||||||
// export each app to a directory, then move it into the complete export
|
// export each app to a directory, then move it into the complete export
|
||||||
const exportAndMove = async (appId: string, appName: string) => {
|
const exportAndMove = async (appId: string, appName: string) => {
|
||||||
const path = await exportApp(appId)
|
const path = await exportApp(appId)
|
||||||
await fs.promises.rename(path, join(tmpPath, appId))
|
await fs.promises.rename(path, join(tmpPath, appName))
|
||||||
}
|
}
|
||||||
for (let metadata of appMetadata) {
|
for (let metadata of appMetadata) {
|
||||||
exportPromises.push(exportAndMove(metadata.appId, metadata.name))
|
exportPromises.push(exportAndMove(metadata.appId, metadata.name))
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { db as dbCore } from "@budibase/backend-core"
|
import { db as dbCore } from "@budibase/backend-core"
|
||||||
import { TABLE_ROW_PREFIX } from "../../../db/utils"
|
import { APP_PREFIX, TABLE_ROW_PREFIX } from "../../../db/utils"
|
||||||
import { budibaseTempDir } from "../../../utilities/budibaseDir"
|
import { budibaseTempDir } from "../../../utilities/budibaseDir"
|
||||||
import {
|
import {
|
||||||
DB_EXPORT_FILE,
|
DB_EXPORT_FILE,
|
||||||
|
@ -111,6 +111,10 @@ export function getGlobalDBFile(tmpPath: string) {
|
||||||
return fs.readFileSync(join(tmpPath, GLOBAL_DB_EXPORT_FILE), "utf8")
|
return fs.readFileSync(join(tmpPath, GLOBAL_DB_EXPORT_FILE), "utf8")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getListOfAppsInMulti(tmpPath: string) {
|
||||||
|
return fs.readdirSync(tmpPath).filter(dir => dir !== GLOBAL_DB_EXPORT_FILE)
|
||||||
|
}
|
||||||
|
|
||||||
export async function importApp(
|
export async function importApp(
|
||||||
appId: string,
|
appId: string,
|
||||||
db: PouchDB.Database,
|
db: PouchDB.Database,
|
||||||
|
|
Loading…
Reference in New Issue