From 12bef53645dc21a3778b175c451cacead72df5d7 Mon Sep 17 00:00:00 2001 From: Jakob Ackermann Date: Mon, 4 Jan 2021 17:04:33 +0000 Subject: [PATCH] [DocArchiveManager] optionally do not un-archive soft deleted docs --- services/docstore/app/js/DocArchiveManager.js | 7 ++- services/docstore/app/js/MongoManager.js | 9 ++++ services/docstore/config/settings.defaults.js | 2 + .../test/acceptance/js/ArchiveDocsTests.js | 51 +++++++++++++++++-- 4 files changed, 65 insertions(+), 4 deletions(-) diff --git a/services/docstore/app/js/DocArchiveManager.js b/services/docstore/app/js/DocArchiveManager.js index a0868d78f6..a123ccdfb6 100644 --- a/services/docstore/app/js/DocArchiveManager.js +++ b/services/docstore/app/js/DocArchiveManager.js @@ -100,7 +100,12 @@ async function archiveDoc(projectId, doc) { } async function unArchiveAllDocs(projectId) { - const docs = await MongoManager.getArchivedProjectDocs(projectId) + let docs + if (settings.docstore.keepSoftDeletedDocsArchived) { + docs = await MongoManager.getNonDeletedArchivedProjectDocs(projectId) + } else { + docs = await MongoManager.getArchivedProjectDocs(projectId) + } if (!docs) { throw new Errors.NotFoundError(`No docs for project ${projectId}`) } diff --git a/services/docstore/app/js/MongoManager.js b/services/docstore/app/js/MongoManager.js index 313aec033a..c6bd24c639 100644 --- a/services/docstore/app/js/MongoManager.js +++ b/services/docstore/app/js/MongoManager.js @@ -53,6 +53,15 @@ module.exports = MongoManager = { db.docs.find(query).toArray(callback) }, + getNonDeletedArchivedProjectDocs(project_id, callback) { + const query = { + project_id: ObjectId(project_id.toString()), + deleted: { $ne: true }, + inS3: true + } + db.docs.find(query).toArray(callback) + }, + upsertIntoDocCollection(project_id, doc_id, updates, callback) { const update = { $set: updates, diff --git a/services/docstore/config/settings.defaults.js b/services/docstore/config/settings.defaults.js index 0bbf99424e..1ab77e784b 100644 --- a/services/docstore/config/settings.defaults.js +++ b/services/docstore/config/settings.defaults.js @@ -23,6 +23,8 @@ const Settings = { docstore: { archiveOnSoftDelete: process.env.ARCHIVE_ON_SOFT_DELETE === 'true', + keepSoftDeletedDocsArchived: + process.env.KEEP_SOFT_DELETED_DOCS_ARCHIVED === 'true', backend: process.env.BACKEND || 's3', healthCheck: { diff --git a/services/docstore/test/acceptance/js/ArchiveDocsTests.js b/services/docstore/test/acceptance/js/ArchiveDocsTests.js index 234ded5a72..f8531f23a2 100644 --- a/services/docstore/test/acceptance/js/ArchiveDocsTests.js +++ b/services/docstore/test/acceptance/js/ArchiveDocsTests.js @@ -167,7 +167,7 @@ describe('Archiving', function () { }) describe('a deleted doc', function () { - before(function (done) { + beforeEach(function (done) { this.project_id = ObjectId() this.doc = { _id: ObjectId(), @@ -241,8 +241,8 @@ describe('Archiving', function () { ) }) - return describe('after unarchiving from a request for the project', function () { - before(function (done) { + describe('after unarchiving from a request for the project', function () { + beforeEach(function (done) { return DocstoreClient.getAllDocs( this.project_id, (error, res, fetched_docs) => { @@ -273,6 +273,51 @@ describe('Archiving', function () { }) }) }) + + describe('when keepSoftDeletedDocsArchived is enabled', function () { + let keepSoftDeletedDocsArchived + beforeEach(function overwriteSetting() { + keepSoftDeletedDocsArchived = + Settings.docstore.keepSoftDeletedDocsArchived + Settings.docstore.keepSoftDeletedDocsArchived = true + }) + afterEach(function restoreSetting() { + Settings.docstore.keepSoftDeletedDocsArchived = keepSoftDeletedDocsArchived + }) + + describe('after unarchiving from a request for the project', function () { + beforeEach(function (done) { + DocstoreClient.getAllDocs( + this.project_id, + (error, res, fetched_docs) => { + this.fetched_docs = fetched_docs + if (error) { + return done(error) + } + done() + } + ) + }) + + it('should not included the deleted', function (done) { + this.fetched_docs.length.should.equal(0) + done() + }) + + it('should not have restored the deleted doc to mongo', function (done) { + db.docs.findOne({ _id: this.doc._id }, (error, doc) => { + if (error) { + return done(error) + } + expect(doc.lines).to.not.exist + expect(doc.ranges).to.not.exist + expect(doc.inS3).to.equal(true) + expect(doc.deleted).to.equal(true) + done() + }) + }) + }) + }) }) describe('archiving a single doc', function () {