Skip to content

fix(databases): resolve physical collections through the databases handle - #14130

Merged
abnegate merged 2 commits into
mainfrom
fix-relationship-databases-handle
Oct 6, 2026
Merged

abnegate merged 2 commits into
mainfrom
fix-relationship-databases-handle

Conversation

@abnegate

@abnegate abnegate commented Oct 5, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

On a dedicated database, the physical collections (database_N_collection_M, their _metadata rows and their row cache) live on the database's own compute, reached through getDatabasesDB($database). Several HTTP actions in Modules/Databases still read or changed those collections through $dbForProject, which only holds the platform metadata (databases, database_N, attributes, indexes). In CE both handles resolve to the same database, so nothing is visible there; on cloud dedicated databases:

  • Relationship create returns 404 table_not_found for every relationship. Both physical collections were looked up with $dbForProject->getCollection(...). Found by the cloud dedicated TablesDB e2e (TablesDBTest::testDeleteColumnOnDedicatedDatabase area): "Creating the two-way relationship failed: Table with the requested ID ... could not be found".
  • Every attribute/column update fails. updateAttribute(), updateAttributeDefault() and updateRelationship() targeted a collection that does not exist on the project database.
  • Relationship checks and transaction commits ran against the wrong database. Document create/update read existing nested related rows from the project database. Legacy and TablesDB transaction commits resolved their handle from the project DSN, so staged operations were written to the project database and their schemas read from it.
  • Cleanup, limit checks, purges. Collection-create cleanup dropped the physical collection on the wrong database. Attribute capability and limit checks, the required-spatial row count and the row-cache purges used the project adapter or cache key, and the cache key includes the adapter hostname.

The fix follows the pattern already used by Collections/Create, Collections/Update, Indexes/Create and the Databases worker: inject getDatabasesDB, resolve $dbForDatabases = $getDatabasesDB($database) once the database document is found, and run every physical-collection operation through it. Metadata documents stay on $dbForProject.

Sites fixed

File Site Before → after
Databases/Collections/Attributes/Relationship/Create.php getCollection of the parent and related physical collections; getSupportForRelationships $dbForProject → $dbForDatabases (support check moved after the database lookup)
Databases/Collections/Attributes/Action.php createAttribute() spatial support, getSupportForSpatialIndexNull, count() of the physical collection, checkAttribute() (both sides), 4 physical purgeCachedCollection → $dbForDatabases
Databases/Collections/Attributes/Action.php updateAttribute() updateRelationship, updateAttribute, updateAttributeDefault → $dbForDatabases
Databases/Collections/Attributes/Delete.php (+ TablesDB Columns/Delete.php) getSupportForCastIndexArray, 2 physical purges → $dbForDatabases
Databases/Collections/Create.php physical purge; cleanup() deleteCollection → $dbForDatabases (cleanup() takes both handles)
Databases/Collections/Documents/Create.php nested related-row getDocument → $dbForDatabases (resolved before the permission closure)
Databases/Collections/Documents/Update.php nested related-row getDocument → $dbForDatabases
Databases/Transactions/Update.php commit handle for legacy and TablesDB was built from the project DSN, so staged operations were applied to the project database; commit-time getCollection($collectionId) resolved from the first operation's database for every type (as DocumentsDB and VectorsDB already did). One withTransaction() is only atomic on one handle, so operations spanning databases on different instances are refused with transaction_invalid; databases sharing the project database (all of CE) commit together as before. The handle*Operation helpers already received $dbForDatabases but named the parameter $dbForProject, so it is renamed (rename only).
Workers/Databases.php 7 physical purgeCachedCollection → $dbForDatabases (the handle the worker already uses for the DDL)

Plumbing: getDatabasesDB is injected into all 18 attribute Create and 18 Update actions and into Attributes Delete, plus their TablesDB Columns/* subclasses (inject only, since they reuse the parent action()). It is passed to createAttribute() / updateAttribute(). After the change, a multi-line audit of Modules/Databases finds no $dbForProject call on a database_*_collection_* id.

Not changed: the per-type spatial pre-checks in Point/Line/Polygon Create/Update and the support check in Relationship/Update still read the project adapter before the database is resolved. They are not physical-collection operations. createAttribute() now repeats the spatial check against the dedicated adapter, and an update needs an attribute that already exists.

Test Plan

  • Full PHPStan locally: [OK] No errors. php -l on all 80 changed files.
  • No new test in this repo. Per AGENTS.md, Http actions are covered by e2e, not unit tests. CE e2e can't show the bug because getDatabasesDB($database) and dbForProject resolve to the same database in CE (Factory::tenant() takes the project DSN when the database document has no database DSN), so the old and new code read the same rows. The existing CE Databases/TablesDB relationship, attribute-update, document and transaction e2e suites must stay green and are the regression guard for CE behaviour.
  • Dedicated coverage is in cloud: TablesDBTest::testDeleteColumnOnDedicatedDatabase (appwrite-labs/cloud, real-Edge dedicated TablesDB lane) reproduced the relationship-create 404 on two pins (runs 37280286280 / 37281427657). Once cloud re-pins server-ce to a commit with this fix, that test will be extended to create and delete a two-way relationship on the dedicated database. It covers this change end to end.

Related PRs and Issues

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? (No API metadata changed: only an injected resource, so specs are unaffected.)

🤖 Generated with Claude Code

…ndle

A dedicated database keeps its physical collections (database_N_collection_M,
their _metadata rows and their row cache) on its own compute, reached through
getDatabasesDB($database). Several HTTP actions still read or changed those
collections through dbForProject, which only holds the platform metadata
(databases, database_N, attributes, indexes). In CE both handles point at the
same database so nothing showed; on a dedicated database:

- relationship create answered 404 for every relationship, because both
  physical collections were looked up on the project database
- every attribute/column update (updateAttribute, updateAttributeDefault,
  updateRelationship) targeted a collection that does not exist there
- nested related rows on document create/update were read from the project
  database, and a transaction commit loaded its collection schema from it,
  so relationship permission checks ran against an empty schema
- collection-create cleanup dropped the physical collection on the wrong
  database, and attribute capability/limit checks, the required-spatial row
  count and the row-cache purges used the project adapter and cache

Thread getDatabasesDB into the attribute/column create, update and delete
actions (Collections API and TablesDB) and route every physical-collection
operation through it, matching Collections/Create, Indexes/Create and the
Databases worker. Metadata documents stay on dbForProject. The worker's own
row-cache purges move to dbForDatabases for the same reason, and the
transaction commit helpers' parameter is renamed to the handle it receives.

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

ghost commented Oct 5, 2026 •

Copy link
Copy Markdown

🔵 Tier A · Mergeable after minor fixes

The transaction-resolution finding is fixed, but the previously reported callable-injection violation remains open.

The PR routes physical collection operations, nested related-document reads, capability checks, and cache invalidation through the databases handle while leaving platform metadata on the project handle. It threads database resolution through attribute actions and TablesDB subclasses, and updates collection cleanup and worker purges. Transaction commits now resolve the actual database document for every database type and reject operations spanning different database DSNs before executing writes.

Latest changes: The newest commit replaces project-only transaction resolution with an actual database lookup and checks that all operation databases use the same DSN before committing.

Verdict New comments Fixed Still open
✅ Approved 0 1 1
Fix with agent prompt
### Issue 1
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/Relationship/Create.php:86
**Inject the database factory object rather than a callable**

AGENTS.md's DI and Reuse rules explicitly require object dependencies and forbid injecting callbacks; the newly added `getDatabasesDB` injections extend that prohibited pattern across the attribute actions and TablesDB subclasses. Please inject the factory object and put database resolution on that domain object instead.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
📂 Walkthrough · 8
File Change
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/Action.php Uses the resolved physical database for attribute validation, updates, and cache invalidation.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/*/{Create,Update}.php Injects and forwards the resolver; relationship creation reads physical schemas through the databases handle.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/Delete.php Uses the physical adapter for index dependency checks and collection cache purges.
src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Columns/** Adds resolver injection to match inherited attribute action signatures.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Create.php Uses the physical database handle for cleanup and collection cache purging.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/{Create,Update}.php Loads nested related documents through the resolved physical database.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php Resolves the actual operation database, rejects mixed-DSN transactions, and uses the physical handle for schemas and operation helpers.
src/Appwrite/Platform/Modules/Databases/Workers/Databases.php Purges physical collection caches through the handle used for DDL.
⏳ Still open from earlier reviews · 1
✅ Fixed since the last review · 1
  • Resolve the dedicated database before reading transaction schemas · src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php:221

Reviewed the commits since 8e502a2 · 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 B · 1 blocking finding to address. Summary

@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix-relationship-databases-handle (after).

Metric Before After Change
🚀 Requests/sec 199.6 195.9 ⚪ -1.9%
⏱️ Latency P50 86.24 ms 87.79 ms ⚪ +1.8%
⏱️ Latency P95 198.39 ms 212.4 ms 🔴 +7.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 87.79 212.4 12,540 195.9 +14.01
Account 174.42 331.48 660 10.95 +37.58
TablesDB 84.54 158.95 6,820 108.08 +4.58
Storage 80.68 171.91 3,300 54.24 -14.55
Functions 131.28 271.86 1,760 29.65 +36.4

Top API waits (after)

API request Max wait (ms)
account.name.update 699.85
tablesdb.rows.create 495.17
account.prefs.update 475.05
functions.delete 426.4
functions.create 396.74

…base

Commit resolved the handle for legacy and TablesDB transactions from the
project DSN, so on a dedicated database the staged operations were applied
to (and their schemas read from) the project database, where those
collections do not exist. Resolve the handle from the first operation's
database for every type, as DocumentsDB and VectorsDB already did.

One withTransaction() can only be atomic on one handle, so a transaction
whose operations target databases on different database instances is now
refused with transaction_invalid instead of silently writing to the wrong
one. Databases sharing the project database (all of CE) still commit
together.

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
abnegate merged commit 89bfe6d into main Oct 6, 2026
58 checks passed
abnegate added a commit that referenced this pull request Oct 6, 2026
Brings in #14130 (route physical collection reads, schema checks, cache
purges and transaction commits through the databases handle), #14120's
merge commit, #14137 (VCS tag globs) and #14139 (project SMTP failures
finish the mail job).

Conflicts were resolved by keeping the database 8.0 forms the train
already uses (ColumnType cases, Capability checks via supports(),
Attribute::fromArray for checkAttribute, supportsSpatial/
supportsRelationships helpers) while taking main's databases-handle
routing, so dedicated databases get their own adapter's capabilities
and schema. The spatial capability check moves after the database
lookup because it now needs that database's adapter.

Documents Create/Update keep the train's RelationshipValues, which
already reads stored related documents through the databases handle;
main's inline permission walk is superseded by it.

Transactions increment/decrement now build their Counter from the
databases handle, since main renamed the handler parameter and the
counted collection's schema lives on that handle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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