diff --git a/services/web/scripts/helpers/batchedUpdate.js b/services/web/scripts/helpers/batchedUpdate.js index 129a28c327..8d0cc40f80 100644 --- a/services/web/scripts/helpers/batchedUpdate.js +++ b/services/web/scripts/helpers/batchedUpdate.js @@ -107,6 +107,7 @@ async function getIdEdgePast(collection) { const [first] = await collection .find({}) .project({ _id: 1 }) + .sort({ _id: 1 }) .limit(1) .toArray() if (!first) return null diff --git a/services/web/test/acceptance/src/BatchedUpdateTests.js b/services/web/test/acceptance/src/BatchedUpdateTests.js new file mode 100644 index 0000000000..504802ca57 --- /dev/null +++ b/services/web/test/acceptance/src/BatchedUpdateTests.js @@ -0,0 +1,38 @@ +const { spawnSync } = require('child_process') +const { expect } = require('chai') +const { db, ObjectId } = require('../../../app/src/infrastructure/mongodb') + +describe('BatchedUpdateTests', function () { + it('can handle non linear insert order', async function () { + await db.systemmessages.insertOne({ + content: '1', + _id: new ObjectId('500000000000000000000000'), + }) + await db.systemmessages.insertOne({ + content: '2', + _id: new ObjectId('400000000000000000000000'), + }) + await db.systemmessages.insertOne({ + content: '3', + _id: new ObjectId('600000000000000000000000'), + }) + await db.systemmessages.insertOne({ + content: '4', + _id: new ObjectId('300000000000000000000000'), + }) + + spawnSync(process.argv0, [ + '-e', + 'require("./scripts/helpers/batchedUpdate").batchedUpdateWithResultHandling("systemmessages", { content: { $ne: "42" }}, { $set: { content: "42" } })', + ]) + + await expect( + db.systemmessages.find({}).project({ content: 1, _id: 0 }).toArray() + ).to.eventually.deep.equal([ + { content: '42' }, + { content: '42' }, + { content: '42' }, + { content: '42' }, + ]) + }) +})