Skip to content

fix(databases): see a batch's own writes when staging transaction operations - #14578

Merged
abnegate merged 5 commits into
mainfrom
qfix-txn-same-batch-delete
Oct 16, 2026
Merged

abnegate merged 5 commits into
mainfrom
qfix-txn-same-batch-delete

Conversation

@abnegate

@abnegate abnegate commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

What

Staging QA: POST /v1/tablesdb/transactions/{T}/operations with [{bulkCreate rowId bc2}, {delete bc2}] (and upsert then delete) answered 404 "Document with the requested ID 'bc2' could not be found". It said "Document" even on TablesDB.

Why

Transactions/Operations/Create.php checked existence against the stored rows plus earlier requests' staged log. It patched in the current batch through a $dependants map that only recorded create, so a row written by bulkCreate, bulkUpsert or upsert earlier in the same batch looked missing. The same map also let a create, delete, update batch through, because it never forgot the create.

Fixing staging exposed a second bug at commit time: an upsert staged through createOperations never set $id from rowId/documentId, so commit created the row under a random ID. The upsert endpoint sets it; handleCreateOperation does too.

Fix

  • TransactionState::getDocument() takes the request's pending operations and replays them after the stored log, the same replay reads in the transaction use. The staging check uses it, and $dependants is gone. replay() now skips a bulkCreate entry without $id instead of indexing a missing key.
  • Not-found errors in the transactions actions use row_not_found/table_not_found on TablesDB, through new getNotFoundException()/getParentNotFoundException() in the transactions Action. That covers the staging check, the collection lookup, the permission pre-check and the commit's NotFound path.
  • handleUpsertOperation and handleCreateOperation always write under the operation's rowId/documentId, even when data.$id names a different one, as the create and upsert endpoints do.

Tests

Unit tests, at the fixing layer (tests/unit/Databases/TransactionStateTest.php):

Test origin/main this PR
testDocumentAnEarlierOperationOfTheSameRequestWritesExists (bulkCreate, bulkUpsert, upsert, create pending) ❌ no way to see pending operations ✅
testDocumentAnEarlierOperationOfTheSameRequestDeletesIsMissing ❌ ✅

API E2E (tests/e2e/Services/Databases/Transactions/StagedDependants.php, in TransactionsBase), on Postgres plus Mongo for DocumentsDB:

Test Suite origin/main this PR
testDeleteOfARecordAnEarlierOperationInTheSameBatchWrites: bulkCreate/bulkUpsert/upsert/create, each followed by delete in one batch, then commit and confirm each row is gone TablesDB ❌ 404 document_not_found ✅
Legacy ❌ 404 ✅
DocumentsDB ❌ 404 ✅
testOperationOnARecordAnEarlierOperationInTheSameBatchDeletesIsNotFound: create, delete, update in one batch, expecting 404 with the row_not_found wording on TablesDB TablesDB ❌ 201 ✅ 404 row_not_found
Legacy ❌ 201 ✅ 404 document_not_found
DocumentsDB ❌ 201 ✅
testCreateAndUpsertOperationsCommitUnderTheirRecordId: staged upserts of a new row, an existing row, and a row whose data.$id names another ID, plus a create whose data.$id names another ID; commit, then read each TablesDB ❌ new row 404 ✅
DocumentsDB ❌ 404 ✅

Full TablesDBTransactionsCustomServerTest, TablesDBTransactionsCustomClientTest, LegacyTransactionsCustomServerTest, DocumentsDBTransactionsCustomServerTest and TablesDBTransactionPermissionsCustomClientTest pass locally, except testCommitEventPayloadCarriesSurfaceIds. That test fails the same way on origin/main in my local stack (a webhook-delivery environment difference); CI covers it.

🤖 Generated with Claude Code

…rations

Staging operations checked a record's existence against the stored state plus
the operations of earlier requests, and patched in only the creates of the
same request. A delete, update, increment or decrement of a record a
bulkCreate, bulkUpsert or upsert earlier in the same batch answered 404, and
an operation on a record the same batch had deleted was accepted.

The check now replays the batch's earlier operations on top of the stored
log, the same way reads in the transaction see it, and answers row_not_found
or table_not_found on TablesDB instead of the documents wording.

Committing an upsert staged through createOperations also ignored the row ID
it carried, so it created a row under a random ID; the commit now upserts
under the staged ID, as create already did.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@luxor-code-review

luxor-code-review Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Review complete. 🟠 1 high

💬 Inline comments (1)

  • 🟠 Commit upsert honors conflicting data.$id over staged ID — Update.php:767

This PR makes operations that depend on documents created earlier in the same transaction work: staged operations are replayed as a pending overlay when validating later operations, replacing the removed dependants-tracking map, and commit-time upsert handling backfills the document ID into the staged data. Transaction not-found exceptions are centralized in shared helpers on the transactions Action.

Files Change
src/Appwrite/Databases/TransactionState.php Replays pending staged operations over stored ones for existence checks and stagedView, skipping bulk rows without $id.
.../Transactions/Operations/Create.php, .../Transactions/Update.php Drop the dependants map in favor of the pending overlay; add the commit-time $id backfill for create/upsert.
.../Transactions/Action.php Adds shared getNotFoundException()/getParentNotFoundException() helpers for collections-vs-tables error mapping.
tests/unit/Databases/TransactionStateTest.php, tests/e2e/.../StagedDependants.php, TransactionsBase.php Unit coverage for the replay state key and an e2e trait exercising staged-dependants semantics across collections and tables suites.

Reviewed commit: 80d2590

@ghost

ghost commented Oct 16, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The incremental test change correctly adds observable coverage for staged-create ID precedence, with no issues found in the new lines.

Transaction staging now replays earlier writes and deletes from the same request when checking record state, and uses surface-specific not-found errors. Commit handling applies staged creates and upserts under the operation’s record ID. Unit and E2E tests cover same-batch dependencies, delete behavior, and ID precedence for both creates and upserts.

Latest changes: The newest commit extends the commit-ID E2E test to verify staged creates, like upserts, use the operation’s record ID when data.$id differs.

Verdict New comments Fixed Still open
💬 Commented 0 0 0

[!NOTE]

Newer commits exist (`d5edfdc`) and will be reviewed. This review is of `abb50b8` and does not approve it.
📂 Walkthrough · 7
File Change
src/Appwrite/Databases/TransactionState.php Replays pending same-request operations when reading transaction state and skips bulk-create entries without IDs.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Action.php Adds helpers that select not-found errors for the relevant API surface.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Operations/Create.php Validates staged operations against earlier writes and maps missing-resource errors by API surface.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php Maps commit not-found errors by API surface and commits staged creates/upserts under their operation IDs.
tests/e2e/Services/Databases/Transactions/StagedDependants.php Covers same-batch writes and deletes, post-delete failures, and ID precedence for staged creates and upserts.
tests/e2e/Services/Databases/Transactions/TransactionsBase.php Includes staged-dependant scenarios in transaction test suites.
tests/unit/Databases/TransactionStateTest.php Tests pending same-request writes and deletes in transaction-state reads.

Merged main (9066a1a); no changes to this PR's code since d5edfdc · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · See the inline comments. Summary

@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

19 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (12)
  • php.appwrite.secret-compare-timing (5)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@luxor-code-review luxor-code-review Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds read-your-writes for staged transaction operations via a pending-replay in TransactionState, backfills upsert $id at commit, and parameterizes transaction not-found exceptions, with new unit and e2e coverage.

Key findings

  • 🟠 Commit upsert honors conflicting data.$id over staged ID — Update.php:767

@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → qfix-txn-same-batch-delete (after).

Metric Before After Change
🚀 Requests/sec 193.02 215.1 🟢 +11.4%
⏱️ Latency P50 89.11 ms 79.94 ms 🟢 -10.3%
⏱️ Latency P95 207.49 ms 189.06 ms 🟢 -8.9%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 79.94 189.06 13,566 215.1 -18.43
Account 151.31 306.86 714 11.99 +7
TablesDB 76.95 141.8 7,378 120.56 -31.07
Storage 73.13 168.36 3,570 59.9 -19.12
Functions 117.79 227.13 1,904 32.48 +0.61

Top API waits (after)

API request Max wait (ms)
account.name.update 595.32
storage.buckets.create 409.29
functions.variables.delete 390.58
account.get 374.63
tablesdb.rows.create 371.87

…s record ID

A staged create or upsert whose data also carried a different $id was written
under that $id instead of the rowId or documentId the operation targets. The
create and upsert endpoints always use the path ID; the commit now does too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

abnegate and others added 3 commits October 16, 2026 21:37
A staged create whose data carries a different $id commits under the
operation's record ID, as a staged upsert does; the test now proves both.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…elete

# Conflicts:
#	tests/unit/Databases/TransactionStateTest.php
@abnegate
abnegate merged commit 4b6f6c1 into main Oct 16, 2026
36 checks passed
@ChiragAgg5k ChiragAgg5k mentioned this pull request Oct 21, 2026
6 tasks
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.

1 participant