Repository navigation
fix(databases): apply filters, order and paging to staged writes in a transaction read - #14590
Conversation
… transaction read A list inside a transaction read the stored page and then patched the staged writes in: staged creates were appended whatever the filters, staged updates were not checked against them, orderDesc and limit returned extra rows out of order, a cursor or offset counted only stored rows, and the total was counted from the first stored page. A read without joins or distinct now merges the stored rows with the staged writes before paging. Staged creates and updated rows are matched against the filters with their staged values; the database decides for a stored row whose staged changes leave the filtered attributes alone. The merged rows are ordered by the read's orders and the $sequence tie break, with staged creates after every stored row, then the cursor, offset and limit apply. The stored rows are read past the cursor in a page large enough that removing every staged row still fills it. The total is the stored count adjusted by the staged rows that join or leave the filter. A cursor may name a row staged in the transaction. Reads with joins or distinct inside a transaction keep the in-place overlay. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Review complete. 🟠 2 high 💬 Inline comments (1)
📍 Findings outside the diff (1) — 🟠 1 high — defects on lines GitHub can't attach comments to🟠 High — NULL attributes mishandled in Equal/NotEqual/negated filters · TransactionState.php:1126–1130 · // src/Appwrite/Databases/TransactionState.php
1126 case Method::NotEqual:
1127 if (\in_array($docValue, $values)) {
1128 return false;
1129 }
1130 break;In 🧹 Nitpicks (1) — 🟢 1 low
The change introduces staged-read overlay support for database transactions: pending staged writes are merged into list results alongside committed rows, with PHP-side emulation of query filters, ordering, and cursor/offset pagination for rows not yet in the backing store.
Reviewed commit: 9cc7249 |
Security rulesNo new WARNING or ERROR findings from security rules. 19 existing findings tracked in
|
🟢 Tier S · Ready to mergeThis pull request merges staged transaction writes with stored documents for plain transaction reads, applying filters, ordering, paging, and totals to the combined result. It also supports staged documents as cursors and adds unit and API coverage for staged filtering, ordering, and pagination. Latest changes: The newest commits preserve stored sort keys before removing internal order-only fields, include exists/notExists attributes when detecting staged filter changes, and use Query grouping plus a non-match fallback for filters the PHP matcher cannot evaluate.
📂 Walkthrough · 6
Reviewed the commits since |
There was a problem hiding this comment.
This PR adds transaction-scoped staged reads so documents created, updated, or deleted inside a pending transaction are visible to list/get/count within that same transaction, including in-PHP filter, order, and pagination emulation for staged rows.
Key findings
- 🟠 Sort keys built after removing order attributes — TransactionState.php:311
- 🟠 NULL attributes mishandled in Equal/NotEqual/negated filters — TransactionState.php:1126
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
…match staged writes only on filters PHP can evaluate The sort key of a stored row was taken after removing order attributes the read had not selected, so those rows ordered as if the value were null. The key is now taken first. exists and notExists name their attributes in their values, so a staged change to one of them was not seen as touching the filter and the stored match was reused. Filters only the engine evaluates (search, regex, spatial, vector, JSON, elemMatch) no longer match a staged value, where they matched every one; filters now come from Query::groupByType, so joins, orders and selects never reach the matcher. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The findings that requested changes are resolved. See the summary comment for what is still open.
What
Inside a transaction,
GET …/rows?transactionId=T(and the documents equivalent) read the stored page and then patched in the staged writes. Staging QA reported:orderDesc+limitreturned extra rows, unsorted.cursor/offsetonly counted stored rows, so staged rows showed up on every page.totalwas off: it was counted from the default first page of stored rows.Fix (plain reads only)
TransactionState::listDocuments()/countDocuments(): a read with no joins and nodistinctnow merges stored rows and staged writes before paging.documentMatchesFilters()takes its filters fromQuery::groupByType(), returns false for a method it can't evaluate, and learnsand/or,notContains,containsAny/containsAll, thenot…string andbetweenvariants,exists/notExists, substringcontains, and null-safe comparisons.$sequencetie break the database adds. Staged creates sort after every stored row, in staging order, as they will once committed.offset + limit + staged count, so dropping the staged IDs still leaves a full page.cursorBeforemirrors the database (nearest rows before the cursor).Documents/XList.phpfalls back toTransactionState::getDocument()when the stored lookup is empty. That's a 3-line change in the cursor block, away from chore(deps): bump utopia-php/database to 8.3.4 #14576's hunk.Per the decision on this batch,
distinct/grouped/join reads inside a transaction keep the existing in-place overlay: no read-time rehearsal and no new refusal. Aggregates stay refused as before.Known gaps:
search,regex, spatial, vector, JSON,elemMatch) don't match a staged value. Stored rows still match through the database.Tests
Unit tests, at the fixing layer (
tests/unit/Databases/TransactionStateTest.php, SQLite):origin/maintestStagedCreateThatMissesTheFilterIsLeftOuttestStagedUpdateIsMatchedAgainstTheFilterWithItsNewValuestestOrderAndLimitApplyToTheMergedSet(orderDesc/orderAsc + limit; total 4)testDefaultOrderPutsStagedCreatesAfterStoredRows(with a staged delete)testOffsetAndCursorPageThroughTheMergedSet(offset, cursorAfter/Before on stored and staged rows)testUnjoinedReadInATransactionOrdersStagedWritesWithStoredRows(was…AppendsCreates, which encoded the bug)testOrderOnAnAttributeTheReadSelectsAwayStillOrdersStoredRows(review)testExistsFilterSeesAStagedUpdateThatSetsTheAttribute(review)testStagedWriteIsNotMatchedByAFilterOnlyTheEngineEvaluates(review: staged bulkDelete byregex)API E2E (
tests/e2e/Services/Databases/Transactions/StagedReads.php, inTransactionsBase): stored rows Ada and Bob; the transaction creates Cy and Abe and renames Ada to Zed.mainmaintestListInATransactionFiltersStagedWrites(equal Bob → 1, equal Ada → 0, equal Zed → Ada; totals)testListInATransactionOrdersAndPagesStagedWrites(orderDesc+limit 2 → [Zed, Cy], total 4; offset; cursorAfter a stored row; cursorAfter a staged row)Also passing locally: the full TablesDB (server and client), Legacy and DocumentsDB transaction suites, both transaction-permission suites, both ACID suites, and
tests/unit/Databases. The one exception istestCommitEventPayloadCarriesSurfaceIds, which fails the same way onmainin my local stack because of webhook delivery.🤖 Generated with Claude Code