Merge pull request #17926 from overleaf/jpa-batched-update-sorting

[web] batchedUpdate: use explicit sorting to find first record to update

GitOrigin-RevId: 6f57b92a4e5907f307618bd98642b4874018e9fa
This commit is contained in:
Jakob Ackermann 2024-04-16 09:37:14 +01:00 committed by Copybot
parent ac2ea9f34d
commit 3df0fe82ce
2 changed files with 39 additions and 0 deletions

View file

@ -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

View file

@ -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' },
])
})
})