Repository navigation
fix: skip operation transaction in bulk update when bulkOperationsSingleTransaction is enabled (#18494) - #18496
Conversation
…gleTransaction is enabled (payloadcms#18494)
|
Thanks for picking this up! I ran the reproduction from #18494 against While this fixes the problem, nothing stops it from being reverted later without anyone noticing: the existing test('should keep earlier updates when a later document fails with bulkOperationsSingleTransaction: true', async ({
payload,
}) => {
const originalValue = payload.db.bulkOperationsSingleTransaction
const hooks = payload.collections[collection].config.hooks
const originalBeforeChange = hooks.beforeChange
payload.db.bulkOperationsSingleTransaction = true
hooks.beforeChange = [
...(originalBeforeChange ?? []),
({ data, originalDoc }) => {
if (originalDoc?.title === 'b') {
throw new Error('Cannot update b')
}
return data
},
]
try {
const posts = await Promise.all(
['a', 'b', 'c'].map((title) =>
payload.create({ collection, data: { title }, overrideAccess: true }),
),
)
const where = { id: { in: posts.map((p) => p.id) } }
const result = await payload.update({
collection,
data: { title: 'updated' },
overrideAccess: true,
sort: 'title',
where,
})
const { docs } = await payload.find({ collection, overrideAccess: true, where })
expect(result.docs).toHaveLength(2)
expect(result.errors).toHaveLength(1)
expect(docs.map((doc) => doc.title).sort()).toStrictEqual(['b', 'updated', 'updated'])
} finally {
hooks.beforeChange = originalBeforeChange
payload.db.bulkOperationsSingleTransaction = originalValue
}
})
|
…rationsSingleTransaction (payloadcms#18494)
|
Thanks @Livog! I've added the suggested integration test to |
Summary of Changes
updateOperationinitialized an operation-level transaction whenbulkOperationsSingleTransactionwastrue, causing individual documentinitTransactioncalls to returnfalseand rolling back preceding successful updates if a later document errored.update.tswithdelete.tsby skipping the operation-levelinitTransactionwhenargs.req.payload.db.bulkOperationsSingleTransactionis enabled.Root Cause / Technical Context
updateOperationranconst shouldCommit = !args.disableTransaction && (await initTransaction(args.req))at the operation level. WhenbulkOperationsSingleTransactionwas active,args.reqwas assigned an operation-level transaction ID. Consequently,docShouldCommit = await initTransaction(req)inprocessDocumentreturnedfalse. If a document threw an error,killTransaction(req)aborted the shared transaction, rolling back all earlier documents while the operation still returned them indocs.Verification & Testing
docShouldCommitevaluates totruefor each document whenbulkOperationsSingleTransaction: true.