Skip to content

fix: skip operation transaction in bulk update when bulkOperationsSingleTransaction is enabled (#18494) - #18496

Open
PINYOPATTANAWASANPORN wants to merge 2 commits into
payloadcms:mainfrom
PINYOPATTANAWASANPORN:fix/bulk-update-single-transaction-isolation-18494
Open

PINYOPATTANAWASANPORN wants to merge 2 commits into
payloadcms:mainfrom
PINYOPATTANAWASANPORN:fix/bulk-update-single-transaction-isolation-18494

Conversation

@PINYOPATTANAWASANPORN

Copy link
Copy Markdown
Contributor

Summary of Changes

  • Fixes bulkOperationsSingleTransaction: bulk update and delete return documents that were rolled back #18494 where updateOperation initialized an operation-level transaction when bulkOperationsSingleTransaction was true, causing individual document initTransaction calls to return false and rolling back preceding successful updates if a later document errored.
  • Aligned update.ts with delete.ts by skipping the operation-level initTransaction when args.req.payload.db.bulkOperationsSingleTransaction is enabled.

Root Cause / Technical Context

updateOperation ran const shouldCommit = !args.disableTransaction && (await initTransaction(args.req)) at the operation level. When bulkOperationsSingleTransaction was active, args.req was assigned an operation-level transaction ID. Consequently, docShouldCommit = await initTransaction(req) in processDocument returned false. If a document threw an error, killTransaction(req) aborted the shared transaction, rolling back all earlier documents while the operation still returned them in docs.

Verification & Testing

  • Verified that docShouldCommit evaluates to true for each document when bulkOperationsSingleTransaction: true.
  • Verified that failing documents rollback only their own individual transaction and successfully committed documents persist in the database.

@Livog

Livog commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Thanks for picking this up! I ran the reproduction from #18494 against 4.0.0-canary.37 with this change, and bulk update now keeps the documents that were processed before the failing one.

While this fixes the problem, nothing stops it from being reverted later without anyone noticing: the existing should bulk update with bulkOperationsSingleTransaction: true test only covers the case where every document succeeds, so it passes with or without this change. Locking it down with a test where a document in the middle fails would be preferred, for example next to that test in test/database/int.spec.ts:

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
  }
})

sort: 'title' makes sure a is processed before b fails. On main the test fails with expected [ 'a', 'b', 'updated' ] to strictly equal [ 'b', 'updated', 'updated' ] (a is returned as updated but was rolled back), and with this PR it passes.

@PINYOPATTANAWASANPORN

Copy link
Copy Markdown
Contributor Author

Thanks @Livog! I've added the suggested integration test to test/database/int.spec.ts to lock down partial failure handling when bulkOperationsSingleTransaction: true is enabled.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bulkOperationsSingleTransaction: bulk update and delete return documents that were rolled back

2 participants