Fix for export issue reported in #5669 - streaming + closing the database didn't work well together, have to support an edge case for our code stopping execution, but DB access continuing.

This commit is contained in:
mike12345567 2022-05-03 15:20:46 +01:00
parent 107c9e2dfc
commit 4b25df60f9
1 changed files with 16 additions and 8 deletions

View File

@ -2,7 +2,11 @@ const { budibaseTempDir } = require("../budibaseDir")
const fs = require("fs") const fs = require("fs")
const { join } = require("path") const { join } = require("path")
const uuid = require("uuid/v4") const uuid = require("uuid/v4")
const { doWithDB } = require("@budibase/backend-core/db") const {
doWithDB,
dangerousGetDB,
closeDB,
} = require("@budibase/backend-core/db")
const { ObjectStoreBuckets } = require("../../constants") const { ObjectStoreBuckets } = require("../../constants")
const { const {
upload, upload,
@ -151,14 +155,18 @@ exports.streamBackup = async appId => {
* @return {*} either a readable stream or a string * @return {*} either a readable stream or a string
*/ */
exports.exportDB = async (dbName, { stream, filter, exportName } = {}) => { exports.exportDB = async (dbName, { stream, filter, exportName } = {}) => {
return doWithDB(dbName, async db => { // streaming a DB dump is a bit more complicated, can't close DB
// Stream the dump if required if (stream) {
if (stream) { const db = dangerousGetDB(dbName)
const memStream = new MemoryStream() const memStream = new MemoryStream()
db.dump(memStream, { filter }) memStream.on("end", async () => {
return memStream await closeDB(db)
} })
db.dump(memStream, { filter })
return memStream
}
return doWithDB(dbName, async db => {
// Write the dump to file if required // Write the dump to file if required
if (exportName) { if (exportName) {
const path = join(budibaseTempDir(), exportName) const path = join(budibaseTempDir(), exportName)