Repository navigation
fix(databases): read staged increments on top of the stored value - #14751
Conversation
Reads inside a transaction replay its staged operations, and replay got increments and decrements wrong in two ways: - It looked only for `data.attribute`, but TablesDB stages `data.column`, so every TablesDB increment or decrement was invisible to getDocument, listDocuments, countDocuments and the staged view used to authorize writes. Replay now resolves the key with the same attribute/column fallback commit uses, now shared as TransactionState::attributeOf(). - With no staged value to build on, it staged the delta as the value, and readers laid it over the stored row: a stored total of 15 incremented by 5 read back as 5. After a staged update of another attribute it started from 0 instead of the stored value. Such increments are now kept as pending deltas per document and added to the stored value wherever staged state is merged with stored rows, including the filter, order and count of a list. A staged create, or an attribute a staged write sets, stays absolute; a later write of the attribute replaces the delta, a delete drops it, and consecutive increments sum. Min and max are still enforced only at commit. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Review complete. 🟠 1 high · 🟡 4 medium 💬 Inline comments (1)
📍 Findings outside the diff (4) — 🟠 1 high · 🟡 3 medium — defects on lines GitHub can't attach comments to🟠 High — Join/distinct // src/Appwrite/Databases/TransactionState.php
278 } elseif ($docState['action'] === 'update' || $docState['action'] === 'upsert') {
279 $wasInResults = isset($committedRows[$docId]);
280 $nowMatches = $this->documentMatchesFilters($docState['document'], $filters);
281
282 if (!$wasInResults && $nowMatches && $docState['action'] === 'upsert') {
283 $adjustedCount++;
284 } elseif ($wasInResults && !$nowMatches) {
285 $adjustedCount -= $committedRows[$docId];
286 } elseif (!$wasInResults && $nowMatches) {
287 // Update shouldn't add a new doc, but upsert might have
288 if ($docState['action'] === 'upsert') {
289 $adjustedCount++;The join/distinct branch of 🟡 Medium — Join/distinct // src/Appwrite/Databases/TransactionState.php
161 if ($docState['action'] === 'create' || $docState['action'] === 'upsert') {
162 $documents[] = $this->applyProjection($docState['document'], $queries);
163 }In the join/distinct branch of 🟡 Medium — Staged increment on nonexistent document reads inconsistently · TransactionState.php:96–107 · // src/Appwrite/Databases/TransactionState.php
96 if ($docState['action'] === 'update' || $docState['action'] === 'upsert') {
97 // Merge with committed version
98 $committedDoc = $dbForDatabases->getDocument($collectionId, $documentId, $queries);
99 if (!$committedDoc->isEmpty()) {
100 // Reapply projection in case transaction added new fields
101 return $this->applyProjection($this->merge($committedDoc, $docState), $queries);
102 } elseif ($docState['action'] === 'upsert') {
103 return $this->applyProjection($this->increment($docState['document'], $docState), $queries);
104 }
105 }
106 }
107 return $dbForDatabases->getDocument($collectionId, $documentId, $queries);An increment staged against a document id with no stored row diverges between the two read paths. In 🟡 Medium — Staged bulkUpdate/bulkDelete match on pre-increment values · TransactionState.php:915–918 · // src/Appwrite/Databases/TransactionState.php
915 $document = $entry['document'];
916 $filters = $this->extractFilters($queries);
917
918 if ($this->documentMatchesFilters($document, $filters)) {In This change extends
Reviewed commit: 027b9e9 |
🟢 Tier S · Ready to merge
Transaction replay now keeps increments and decrements as deltas over stored values, with shared Latest changes: The latest commit applies pending increments to staged create/upsert rows appended by the distinct/join list path and adds a regression test for a new staged upsert.
📂 Walkthrough · 3
Reviewed the commits since |
There was a problem hiding this comment.
Adds staged-increment support to TransactionState with read-your-writes visibility in transaction reads, but several read paths (join/distinct count, staged bulk operations, min/max clamping) diverge from the commit path.
Key findings
- 🟠 Join/distinct
countDocumentsignores stagedincrements— TransactionState.php:278 - 🟡 Join/distinct
listDocumentsdrops increments from staged upserts — TransactionState.php:161 - 🟡 Staged increment on nonexistent document reads inconsistently — TransactionState.php:96
- 🟡 Staged read view ignores increment
min/maxbounds — TransactionState.php:876 - 🟡 Staged bulkUpdate/bulkDelete match on pre-increment values — TransactionState.php:915
Security rulesNo new WARNING or ERROR findings from security rules. 19 existing findings tracked in
|
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
…stinct read appends The join and distinct list path appends staged creates and upserts the stored read did not return, and laid out the staged upsert without the increments staged after it, so the same row read differently there than through getDocument or a plain list. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Re the review findings outside the diff:
|
…d-increments fix(databases): read staged increments on top of the stored value (cherry picked from commit 51dc61d)
What does this PR do?
Fixes two bugs in how reads inside a transaction see staged increments and decrements (
TransactionState::replay()).data.attribute, but TablesDB stagesdata.column(the operation validator requirescolumnfortablesdb). Every TablesDB increment/decrement was ignored bygetDocument,listDocuments/countDocumentsand thestagedViewused for authorization, even though commit applied it. Replay now uses the same attribute/column fallback as commit, moved fromTransactions/Update.phpintoTransactionState::attributeOf()so both use one helper.±valueas the attribute's value, and readers laid it over the stored row: storedtotal15 + increment 5 read back as 5. After a staged update of another attribute, it started from 0 instead of the stored value. These increments are now kept as pending deltas (incrementsper staged document) and added to the stored value wherever staged state is merged with stored rows:getDocument, the join overlay and the staged creates/upserts it appends, the plain list/count path (filter matching and ordering see the new value), andstagedView.Semantics:
Test Plan
New cases in
tests/unit/Databases/TransactionStateTest.php. Each fails onorigin/mainand passes with this change:column), via getDocument and stagedViewattributetotal: 30, increment +2total: 7, then increment +3total > 18ordered desc includes the incremented row, drops an incremented-then-deleted row; count agrees[ada 20]≠[bob 25, ada 20]totalnull ≠ 3The full file runs 29 tests / 67 assertions green locally. Pint passes.
Related PRs and Issues
🤖 Generated with Claude Code