Skip to content

fix(databases): answer a bulk update that breaks a unique index with 409 - #14575

Merged
abnegate merged 3 commits into
mainfrom
qfix-bulk-update-unique
Oct 16, 2026
Merged

abnegate merged 3 commits into
mainfrom
qfix-bulk-update-unique

Conversation

@abnegate

Copy link
Copy Markdown
Member

What

PATCH /v1/tablesdb/{databaseId}/tables/{tableId}/rows (and the legacy PATCH .../collections/{collectionId}/documents) with data that gives two rows the same value under a unique index answered 500 general_unknown. The single-row update and both upserts already answer 409.

Staging QA repro: {"data":{"name":"same"},"queries":[equal grp "uq"]} where name has a unique index.

Why

Documents/Bulk/Update.php never caught Utopia\Database\Exception\Unique, which updateDocuments throws on a unique-index violation. It now maps it to row_unique_constraint_violation / document_unique_constraint_violation (409), the same as Documents/Update.php and Bulk/Upsert.php. A bulk update can't change a row ID (removeReadonlyAttributes strips $id), so the parent Duplicate (ID conflict) isn't reachable and isn't mapped.

Test

RejectedValues::testBulkUpdateIntoAUniqueIndexIsAConflict creates a unique index, two rows, and bulk-updates both to the same value. It asserts 409, the unique-constraint type, and that neither row changed. It runs for TablesDB and the legacy Databases API; DocumentsDB skips it because it has no attributes. The fix is in the HTTP action, so this E2E is the test at the fixing layer.

Test (Postgres) origin/main this PR
TablesDBCustomServerTest::testBulkUpdateIntoAUniqueIndexIsAConflict ❌ 500 general_unknown ✅ 409 row_unique_constraint_violation
LegacyCustomServerTest::testBulkUpdateIntoAUniqueIndexIsAConflict ❌ 500 general_unknown ✅ 409 document_unique_constraint_violation

Not included

The earlier sibling commit on qfix-errors ("regex/geometry errors from the database engine return 400") is left out on purpose. utopia-php/database 8.3.2 refuses an invalid regex, and the malformed geometry, in the library. I ran that commit's testInvalidRegexPatternIsRejected (list, bulk update and bulk delete × 4 patterns) against main's server code with no engine-error backstop:

testInvalidRegexPatternIsRejected, TablesDB on Postgres database 8.3.0 database 8.3.2
result ❌ 500 ✅ 400 general_query_invalid

The 8.3.2 bump (separate PR) brings its own regex and WKT rejection E2Es.

🤖 Generated with Claude Code

A bulk update that gives two documents the same value under a unique index
raised a Unique exception the action never caught, so it answered 500
general_unknown. The single-document update and both upserts already answer
409; the bulk update now maps Unique the same way. A bulk update cannot
change a document ID, so the parent Duplicate is left unmapped.

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. No issues found — approved ✅.


This PR teaches the bulk document update endpoint to surface unique-index violations as the proper 409 conflict error and locks that behavior in with an end-to-end test.

Files Change
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Update.php Catches UniqueException from the bulk update and maps it to the existing unique-constraint HTTP exception, mirroring the sibling Create/Update/Upsert actions.
tests/e2e/Services/Databases/Queries/RejectedValues.php Adds testBulkUpdateIntoAUniqueIndexIsAConflict, which builds a unique index, waits for it to become available, creates two records, bulk-updates them onto colliding values, and asserts a 409 with the correct per-resource error type plus that no records were left changed.

Reviewed commit: 9c62907

@ghost

ghost commented Oct 16, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

No issues were found in the incremental changes, and the unique-index bulk-update regression remains covered end to end.

Bulk row and document updates that collide with a unique index now return the API-specific 409 error instead of a 500. End-to-end coverage verifies that conflict and unchanged records, and exercises rejected queries for aggregate overflow, invalid values and spatial inputs, malformed WKT, regexes, and cursor ordering.

Latest changes: The newest commits expand RejectedValues coverage with aggregate-overflow and query-validation cases, and relocate the existing bulk-unique regression test to the end of the trait.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 2
File Change
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Update.php Maps unique-index violations from bulk updates to the API-specific 409 conflict error.
tests/e2e/Services/Databases/Queries/RejectedValues.php Covers rejected query values and aggregate overflow, and verifies bulk unique-index conflicts leave records unchanged.

Reviewed the commits since 801e4d5 · 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 S · Looks good to merge. Summary

@github-actions

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.

@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → qfix-bulk-update-unique (after).

Metric Before After Change
🚀 Requests/sec 212.74 219.04 ⚪ +3%
⏱️ Latency P50 80.97 ms 79.31 ms ⚪ -2.1%
⏱️ Latency P95 189.71 ms 183.75 ms ⚪ -3.1%
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.31 183.75 13,680 219.04 -5.96
Account 147.57 282.42 720 12.26 +4.27
TablesDB 78.27 146.25 7,440 121.6 +9.28
Storage 69.96 157.85 3,600 60.58 -22.69
Functions 113.72 219.42 1,920 33.16 -31.09

Top API waits (after)

API request Max wait (ms)
account.name.update 476.51
functions.create 471.76
functions.variables.update 359.26
account.prefs.update 351.23
functions.variables.delete 336.77

@abnegate
abnegate merged commit 9066a1a into main Oct 16, 2026
1 check was pending
@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