Skip to content

fix(databases): check transaction conflicts against the row version seen at staging - #14574

Open
jackeddisciple wants to merge 10 commits into
appwrite:mainfrom
jackeddisciple:fix-transaction-conflict-check
Open

jackeddisciple wants to merge 10 commits into
appwrite:mainfrom
jackeddisciple:fix-transaction-conflict-check

Conversation

@jackeddisciple

@jackeddisciple jackeddisciple commented Oct 16, 2026 •

Copy link
Copy Markdown

What does this PR do?

The commit checked each row's $updatedAt against the time the operation was staged. A committing transaction stamps a row when it writes it, but nobody else sees that write until COMMIT. If another client stages and reads the row in between, it still sees the old value. The other writer's stamp is also older than this client's staging time, so its commit passes the check and silently overwrites the first claim. That's the double claim from the issue.

Staging now stores the row's $updatedAt on the transaction log (documentUpdatedAt), and the commit checks against that instead. The library always stamps a write after the row's previous $updatedAt, so any write that lands after staging conflicts. It no longer matters when the other write was stamped, or which node's clock stamped it. Logs staged before the upgrade have no value and fall back to the staging time, so transactions in flight during the upgrade keep working.

  1. transactionLogs gets a documentUpdatedAt datetime attribute, added to existing projects in V25.
  2. Update, upsert, delete and the batch operations endpoint record it when staging. They take it from the same read they already use for the row, so the recorded version is exactly the one the operation was based on. Upsert now always reads the row once while staging and uses it for both the inherited permissions and the version.
  3. A small TransactionLog document picks the timestamp, and the commit uses it. I renamed $createdAt to $timestamp in the commit handlers since it isn't always the creation time anymore. That rename is most of the diff in Transactions/Update.php.

Known limits, unchanged here:

  1. Bulk operations still check against the staging time. They target queries, not known rows, so there's no single version to record.
  2. The library never runs the timestamp check for increments and decrements. A row whose first operation in a transaction is an increment gets no conflict detection, before and after this PR.
  3. A row that is only read, never written, inside a transaction isn't checked at commit. The pattern from the issue stages a write first, which this covers.

Test Plan

I reproduced it with the script from the issue on a local stack (TablesDB, Postgres). I added 3 ms of latency to Postgres with netem so a 91 operation commit takes a few seconds, like on a separate DB server. Without that the window is too short to hit locally.

probe 90 300 10 both committed final owner
main 10 of 10 rounds c2
this branch 0 of 10, client 2 gets 409 c1

burst 10 20 30 gave one winner per round on both. I couldn't trigger that variant locally; the issue needed two API nodes for it.

I also ran it on a project created on main. Staging keeps working before the migration, using the old check. After migrate the column exists and the probe gives 0 of 10.

Tests:

  1. unit TransactionLogTest: against a real SQLite database, a write stamped before staging but landing after it now conflicts, an untouched row still commits, and older logs fall back to the staging time. Fails without the fix.
  2. unit MigrationVersionsTest::testV25LetsALegacyInstallStageTheRowVersion: a transactionLogs table without the column can store it after V25. Fails without the migration step.
  3. e2e testCommitConflictsWithWriteStampedBeforeStaging: stages through the batch endpoint, update, upsert (with and without permissions) and delete. It then writes the row from outside with a $updatedAt between the old version and the staging time, which is what an in flight commit leaves behind. The commit must fail and the outside write must survive. Passes on TablesDB and Legacy, fails on main.

The TablesDB and Legacy transaction suites and both ACID suites pass locally, except testCommitEventPayloadCarriesSurfaceIds. That one fails the same way on main because I didn't run the webhook request catcher. I didn't run the DocumentsDB variants (no Mongo locally), so CI covers those. Utopia, Migration, Databases and General unit suites pass, and pint, phpstan and rector are clean.

Related PRs and Issues

Fixes #14489

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

…een at staging

The commit compared a row's $updatedAt with the time the operation was staged. A concurrent commit stamps its rows when it writes them but only makes them visible at COMMIT, so a claim staged inside that window passed the check and silently overwrote the first one. Staging now records the row's $updatedAt and the commit checks against that, so any write that lands after staging conflicts, no matter when it was stamped.

Fixes appwrite#14489
@ghost

ghost commented Oct 16, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

Transaction logs now persist the row version observed when individual operations are staged, and commits use that version for conflict detection while preserving a staging-time fallback for legacy logs. The PR adds the transaction-log schema migration, captures versions across row-operation staging paths, and covers the behavior with unit and end-to-end tests. The newest changes also revise how bulk operations update transaction state and collect affected documents.

Latest changes: The newest commits adjust bulk-operation commit handling to track updated state and event documents directly, and rename a loop variable in batch permission normalization.

Verdict New comments Fixed Still open
💬 Commented 0 0 0

Note

@jackeddisciple does not have write access to this repository, so Hansi does not approve automatically. A maintainer can review and approve.

Couldn't attach to a line

These findings could not be placed on a changed line, so they were not posted as inline comments.

📂 Walkthrough · 11
File Change
app/config/collections/projects.php Adds the nullable documentUpdatedAt attribute to transaction logs.
src/Appwrite/Migration/Version/V25.php Adds the transaction-log timestamp attribute for existing projects.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Delete.php Records the observed row version when staging transactional deletes.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php Records the observed row version when staging transactional updates.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php Reuses the staging read for upsert permissions and row-version capture.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Operations/Create.php Captures row versions for batch operations and normalizes bulk permissions.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php Checks conflicts against logged row versions and updates bulk-operation state and affected-document tracking.
src/Appwrite/Utopia/Database/Documents/TransactionLog.php Selects the logged row version for conflict checks with a legacy fallback.
tests/e2e/Services/Databases/Transactions/TransactionsBase.php Adds end-to-end coverage for conflicts after staging through row and batch endpoints.
tests/unit/Migration/MigrationVersionsTest.php Verifies V25 adds the attribute to a legacy transaction-log collection.
tests/unit/Utopia/Database/Documents/TransactionLogTest.php Covers row-version conflict checks, unchanged rows, and legacy-log fallback.

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

ghost
ghost previously requested changes Oct 16, 2026

@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 B · 1 blocking finding to address. Summary

… against

Upsert read the row a second time just to get $updatedAt. A write landing between the two reads got recorded as the version the upsert was based on, so the commit could overwrite it without a conflict. Staging now reads the row once and uses it for both the inherited permissions and the recorded version, same as update and delete.

@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 S · Looks good to merge. Summary

@ghost
ghost dismissed their stale review October 16, 2026 05:06

The findings that requested changes are resolved. See the summary comment for what is still open.

@HarshMN2345

Copy link
Copy Markdown
Member

@copilot resolve the merge conflicts in this pull request

…taged row

bulkCreate and bulkUpsert reused $document as the loop variable, so documentUpdatedAt got read off an array and staging crashed.
@ArnabChatterjee20k
ArnabChatterjee20k self-requested a review October 29, 2026 11:11
@jackeddisciple

Copy link
Copy Markdown
Author

Hey @HarshMN2345 , any update on this PR?

@HarshMN2345

Copy link
Copy Markdown
Member

Hey @HarshMN2345 , any update on this PR?

Still in review. It hasn't been a priority yet, but We will get to it.

@appwrite

appwrite Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The new commits fix the open write-only upsert finding, and I found no new defects in the changed code.

Transaction conflict checks now compare against the row version the caller read when staging, not the staging time. Update, upsert, delete and the batch operations endpoint record that documentUpdatedAt on the transaction log, a V25 migration adds the attribute to existing projects, and TransactionLog falls back to the staging time for older logs. The commit handlers use that timestamp, and e2e and unit tests cover conflicts, fallback, and legacy installs.
[{"path": "app/config/collections/projects.php", "change": "Adds the optional documentUpdatedAt datetime attribute to transactionLogs."}, {"path": "src/Appwrite/Migration/Version/V25.php", "change": "Adds documentUpdatedAt to existing transactionLogs collections."}, {"path": "src/Appwrite/Utopia/Database/Documents/TransactionLog.php", "change": "Adds getRequestTimestamp(), which returns the recorded row version and falls back to the staging time for older logs."}, {"path": "src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php", "change": "Records the row's $updatedAt on the staged update log."}, {"path": "src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Delete.php", "change": "Records the row's $updatedAt on the staged delete log."}, {"path": "src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php", "change": "Reads the row once while staging, uses it for inherited permissions and the recorded version, and reads it without the caller's permissions so write-only upserts record a version too."}, {"path": "src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Operations/Create.php", "change": "Records the row version for each staged operation in the batch endpoint, and reads the row without the caller's permissions for upserts."}, {"path": "src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php", "change": "Uses the log's row-version timestamp for commit conflict checks, renaming $createdAt to $timestamp in the handlers."}, {"path": "tests/e2e/Services/Databases/Transactions/TransactionsBase.php", "change": "Adds e2e coverage that a write stamped before staging conflicts at commit for batch, update, upsert and delete staging."}, {"path": "tests/e2e/Services/Databases/Transactions/TransactionPermissionsBase.php", "change": "Adds a regression test that write-only upserts, inherited or supplied permissions and batched, conflict with an earlier write."}, {"path": "tests/unit/Migration/MigrationVersionsTest.php", "change": "Checks that V25 lets a legacy transactionLogs table store the row version."}, {"path": "tests/unit/Utopia/Database/Documents/TransactionLogTest.php", "change": "Covers version selection, the staging-time fallback, conflict detection, and unchanged-row commits."}]

Latest changes: Transactional upserts now read the row without the caller's permissions, in both the direct endpoint and the batch endpoint, so write-only upserts record the row version, and a new e2e test covers that case.

Verdict New comments Fixed Still open
💬 Commented 0 1 0

Note

@jackeddisciple does not have write access to this repository, so Hansi does not approve automatically. A maintainer can review and approve.

📂 Walkthrough · 10
File Change
app/config/collections/projects.php Adds the optional documentUpdatedAt timestamp to transactionLogs.
src/Appwrite/Migration/Version/V25.php Adds documentUpdatedAt to existing transactionLogs collections during V25.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Delete.php Records the staged row version for transactional deletes.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php Records the staged row version for transactional updates.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Operations/Create.php Records row versions for individual operations staged through the batch endpoint.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php Uses TransactionLog's version-or-staging timestamp for transaction commit handlers.
src/Appwrite/Utopia/Database/Documents/TransactionLog.php Provides the operation's row-version timestamp with a fallback for older logs.
tests/e2e/Services/Databases/Transactions/TransactionsBase.php Adds end-to-end conflict coverage for staged update, upsert, delete, and batch operations.
tests/unit/Migration/MigrationVersionsTest.php Verifies V25 lets a legacy transactionLogs collection store the row version.
tests/unit/Utopia/Database/Documents/TransactionLogTest.php Covers row-version selection, fallback behavior, conflict detection, and unchanged-row commits.
✅ Fixed since the last review · 1
  • Capture versions for write-only transactional upserts · src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php:161

Merged main (b03df9e); no changes to this PR's code since 9d83941 · Details · Comment @appwrite review to re-run, or mention @appwrite with a question.

@appwrite appwrite 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.

🔵 Tier A · See the inline comments. Summary

…permission

Upsert needs update permission, not read, but a staged upsert read its row through the caller's permissions. A caller who could update the row but not read it got an empty document, so the staged log recorded no version and the commit fell back to the staging time. A write stamped between the row's version and the staging then got overwritten without a conflict.

The upsert endpoint and upsert operations now read the row without the caller's permissions, as the library does when it writes the upsert, so the recorded version is the row's. The upsert endpoint also inherits the stored permissions in a transaction, as it already does outside one, instead of replacing them with the caller's defaults.

@appwrite appwrite 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.

🟢 Tier S · Looks good to merge. Summary

@jackeddisciple

Copy link
Copy Markdown
Author

Hey @HarshMN2345 @ArnabChatterjee20k , any update on this PR review?

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

2 participants