From 5f23b0e8049f5239a079483ee7784dd7c4523b22 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 12 Jun 2026 23:22:02 +1200 Subject: [PATCH] fix: resolve data-plane database before writing collection metadata For a dedicated product database (documentsdb/vectorsdb on Cloud), the data-plane resolver throws while the database is still provisioning (the DSN is not published yet). Both collection create paths wrote the control-plane metadata row before resolving the data plane, so every create attempted against a provisioning database returned 409 but still persisted a collection row with no backing collection. The orphan lists fine in the API and console, but every document operation against it fails with 'Invalid document structure: Missing collection attribute $collection'. Resolve the data-plane database first so a provisioning (or otherwise unreachable) backend rejects the request before any metadata is written. Co-Authored-By: Claude Fable 5 --- .../Http/Databases/Collections/Create.php | 14 +++++++++----- .../Http/VectorsDB/Collections/Create.php | 9 +++++++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Create.php index a0ee24db87..b2e783bccd 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Create.php @@ -103,6 +103,15 @@ class Create extends Action // Map aggregate permissions into the multiple permissions they represent. $permissions = Permission::aggregate($permissions) ?? []; + // Resolve the data-plane database BEFORE writing the metadata row. For a + // dedicated product database this throws while provisioning (no DSN yet); + // writing the row first would leave an orphaned collection with no backing + // collection, which lists fine but fails every document operation. + /** + * @var Database $dbForDatabases + */ + $dbForDatabases = $getDatabasesDB($database); + try { $collection = $dbForProject->createDocument('database_' . $database->getSequence(), new Document([ '$id' => $collectionId, @@ -122,11 +131,6 @@ class Create extends Action throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]); } - /** - * @var Database $dbForDatabases - */ - $dbForDatabases = $getDatabasesDB($database); - $collectionKey = 'database_' . $database->getSequence() . '_collection_' . $collection->getSequence(); $databaseKey = 'database_' . $database->getSequence(); diff --git a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Create.php index 58433c7deb..3b346bb7c0 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Create.php @@ -94,6 +94,13 @@ class Create extends CollectionAction // Map aggregate permissions into the multiple permissions they represent. $permissions = Permission::aggregate($permissions) ?? []; + // Resolve the data-plane database BEFORE writing the metadata row. For a + // dedicated product database this throws while provisioning (no DSN yet); + // writing the row first would leave an orphaned collection with no backing + // collection, which lists fine but fails every document operation. + /** @var Database $dbForDatabases */ + $dbForDatabases = $getDatabasesDB($database); + try { $collection = $dbForProject->createDocument('database_' . $database->getSequence(), new Document([ '$id' => $collectionId, @@ -114,8 +121,6 @@ class Create extends CollectionAction } catch (NotFoundException) { throw new Exception(Exception::DATABASE_NOT_FOUND); } - /** @var Database $dbForDatabases */ - $dbForDatabases = $getDatabasesDB($database); $attributes = []; $indexes = [];