2021-09-28 07:12:41 +13:00
|
|
|
const env = require("../../environment")
|
|
|
|
const { getAllApps } = require("@budibase/auth/db")
|
|
|
|
const CouchDB = require("../../db")
|
|
|
|
const {
|
|
|
|
exportDB,
|
|
|
|
sendTempFile,
|
|
|
|
readFileSync,
|
|
|
|
} = require("../../utilities/fileSystem")
|
2021-09-29 06:05:52 +13:00
|
|
|
const { stringToReadStream } = require("../../utilities")
|
2021-09-28 07:12:41 +13:00
|
|
|
const { getGlobalDBName, getGlobalDB } = require("@budibase/auth/tenancy")
|
|
|
|
const { create } = require("./application")
|
2021-09-30 05:43:16 +13:00
|
|
|
const { getDocParams, DocumentTypes } = require("../../db/utils")
|
2021-09-28 07:12:41 +13:00
|
|
|
|
|
|
|
async function createApp(appName, appImport) {
|
|
|
|
const ctx = {
|
|
|
|
request: {
|
|
|
|
body: {
|
|
|
|
templateString: appImport,
|
|
|
|
name: appName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return create(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.exportApps = async ctx => {
|
|
|
|
if (env.SELF_HOSTED || !env.MULTI_TENANCY) {
|
|
|
|
ctx.throw(400, "Exporting only allowed in multi-tenant cloud environments.")
|
|
|
|
}
|
|
|
|
const apps = await getAllApps(CouchDB, { all: true })
|
|
|
|
const globalDBString = await exportDB(getGlobalDBName())
|
|
|
|
let allDBs = {
|
|
|
|
global: globalDBString,
|
|
|
|
}
|
|
|
|
for (let app of apps) {
|
|
|
|
allDBs[app.name] = await exportDB(app._id)
|
|
|
|
}
|
|
|
|
const filename = `cloud-export-${new Date().getTime()}.txt`
|
|
|
|
ctx.attachment(filename)
|
|
|
|
ctx.body = sendTempFile(JSON.stringify(allDBs))
|
|
|
|
}
|
|
|
|
|
2021-09-30 05:43:16 +13:00
|
|
|
async function getAllDocType(db, docType) {
|
|
|
|
const response = await db.allDocs(
|
|
|
|
getDocParams(docType, null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
return response.rows.map(row => row.doc)
|
|
|
|
}
|
|
|
|
|
2021-09-28 07:12:41 +13:00
|
|
|
exports.importApps = async ctx => {
|
|
|
|
if (!env.SELF_HOSTED || env.MULTI_TENANCY) {
|
|
|
|
ctx.throw(400, "Importing only allowed in self hosted environments.")
|
|
|
|
}
|
|
|
|
const apps = await getAllApps(CouchDB, { all: true })
|
|
|
|
if (
|
|
|
|
apps.length !== 0 ||
|
|
|
|
!ctx.request.files ||
|
|
|
|
!ctx.request.files.importFile
|
|
|
|
) {
|
|
|
|
ctx.throw(
|
|
|
|
400,
|
|
|
|
"Import file is required and environment must be fresh to import apps."
|
|
|
|
)
|
|
|
|
}
|
|
|
|
const importFile = ctx.request.files.importFile
|
2021-09-29 06:05:52 +13:00
|
|
|
const importString = readFileSync(importFile.path)
|
2021-09-28 07:12:41 +13:00
|
|
|
const dbs = JSON.parse(importString)
|
2021-09-30 05:43:16 +13:00
|
|
|
const globalDbImport = dbs.global
|
2021-09-29 06:05:52 +13:00
|
|
|
// remove from the list of apps
|
|
|
|
delete dbs.global
|
2021-09-30 05:43:16 +13:00
|
|
|
const globalDb = getGlobalDB()
|
2021-09-28 07:12:41 +13:00
|
|
|
// load the global db first
|
2021-09-30 05:43:16 +13:00
|
|
|
await globalDb.load(stringToReadStream(globalDbImport))
|
2021-09-29 06:05:52 +13:00
|
|
|
for (let [appName, appImport] of Object.entries(dbs)) {
|
2021-09-28 07:12:41 +13:00
|
|
|
await createApp(appName, appImport)
|
|
|
|
}
|
2021-09-30 05:43:16 +13:00
|
|
|
// once apps are created clean up the global db
|
|
|
|
let users = await getAllDocType(globalDb, DocumentTypes.USER)
|
|
|
|
for (let user of users) {
|
|
|
|
delete user.tenantId
|
|
|
|
}
|
|
|
|
await globalDb.bulkDocs(users)
|
2021-09-28 07:12:41 +13:00
|
|
|
ctx.body = {
|
|
|
|
message: "Apps successfully imported.",
|
|
|
|
}
|
|
|
|
}
|