From 4b25df60f9c0450dd71988a8a8969aad43f4d16c Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 3 May 2022 15:20:46 +0100 Subject: [PATCH] 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. --- .../server/src/utilities/fileSystem/index.js | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/server/src/utilities/fileSystem/index.js b/packages/server/src/utilities/fileSystem/index.js index 8a02afc5b3..e2a76c49a1 100644 --- a/packages/server/src/utilities/fileSystem/index.js +++ b/packages/server/src/utilities/fileSystem/index.js @@ -2,7 +2,11 @@ const { budibaseTempDir } = require("../budibaseDir") const fs = require("fs") const { join } = require("path") 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 { upload, @@ -151,14 +155,18 @@ exports.streamBackup = async appId => { * @return {*} either a readable stream or a string */ exports.exportDB = async (dbName, { stream, filter, exportName } = {}) => { - return doWithDB(dbName, async db => { - // Stream the dump if required - if (stream) { - const memStream = new MemoryStream() - db.dump(memStream, { filter }) - return memStream - } + // streaming a DB dump is a bit more complicated, can't close DB + if (stream) { + const db = dangerousGetDB(dbName) + const memStream = new MemoryStream() + memStream.on("end", async () => { + await closeDB(db) + }) + db.dump(memStream, { filter }) + return memStream + } + return doWithDB(dbName, async db => { // Write the dump to file if required if (exportName) { const path = join(budibaseTempDir(), exportName)