From 374f858db293d83ee298973759011f9b873b4439 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:14:39 +0100 Subject: [PATCH 01/13] chore: trace database worker with Span::add instead of Console logging Replace unstructured Console::info/error calls in the database worker with structured Span::add tags. Operation identifiers (project, database, collection, document) and queue metadata are recorded up front; swallowed errors (deleteByGroup, attribute NotFound) are surfaced as spans so they stay visible. Redundant Console::error calls before rethrows are dropped since the entry-point harness owns span error lifecycle. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Modules/Databases/Workers/Databases.php | 36 ++++++++----------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index ee8494b382..abdd678816 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -5,7 +5,6 @@ namespace Appwrite\Platform\Modules\Databases\Workers; use Appwrite\Event\Message\Database as DatabaseMessage; use Appwrite\Event\Realtime; use Exception; -use Utopia\Console; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Exception as DatabaseException; @@ -18,6 +17,7 @@ use Utopia\Database\Query; use Utopia\Logger\Log; use Utopia\Platform\Action; use Utopia\Queue\Message; +use Utopia\Span\Span; class Databases extends Action { @@ -73,12 +73,22 @@ class Databases extends Action $log->addTag('projectId', $project->getId()); $log->addTag('type', $type); + Span::add('project.id', $project->getId()); + Span::add('payload.type', $type); + Span::add('queue.pid', $message->getPid()); + Span::add('queue.name', $message->getQueue()); + Span::add('message.timestamp', (string) $message->getTimestamp()); + Span::add('database.collection.id', $collection->getId()); + Span::add('database.document.id', $document->getId()); + if ($database->isEmpty()) { throw new Exception('Missing database'); } $log->addTag('databaseId', $database->getId()); + Span::add('database.id', $database->getId()); + match (\strval($type)) { DATABASE_TYPE_DELETE_DATABASE => $this->deleteDatabase($database, $dbForProject, $dbForDatabases), DATABASE_TYPE_DELETE_COLLECTION => $this->deleteCollection($database, $collection, $dbForProject, $dbForDatabases), @@ -88,14 +98,6 @@ class Databases extends Action DATABASE_TYPE_DELETE_INDEX => $this->deleteIndex($database, $collection, $document, $project, $dbForPlatform, $dbForProject, $dbForDatabases, $queueForRealtime), default => throw new Exception('No database operation for type: ' . \strval($type)), }; - - Console::info("Finished processing database operation: \n" . \json_encode([ - 'type' => $type, - 'projectId' => $project->getId(), - 'databaseId' => $database->getId(), - 'collectionId' => $collection->getId(), - 'documentId' => $document->getId(), - ], JSON_PRETTY_PRINT)); } /** @@ -206,8 +208,6 @@ class Databases extends Action $dbForProject->updateDocument('attributes', $attribute->getId(), $attribute->setAttribute('status', 'available')); } catch (\Throwable $e) { - Console::error($e->getMessage()); - if ($e instanceof DatabaseException) { $attribute->setAttribute('error', $e->getMessage()); if (! $relatedAttribute->isEmpty()) { @@ -309,7 +309,7 @@ class Databases extends Action } } catch (NotFound $e) { - Console::error($e->getMessage()); + Span::add('database.attribute.not_found', $e->getMessage()); $dbForProject->deleteDocument('attributes', $attribute->getId()); @@ -318,8 +318,6 @@ class Databases extends Action } } catch (\Throwable $e) { - Console::error($e->getMessage()); - if ($e instanceof DatabaseException) { $attribute->setAttribute('error', $e->getMessage()); if (!$relatedAttribute->isEmpty()) { @@ -456,7 +454,6 @@ class Databases extends Action } $dbForProject->updateDocument('indexes', $index->getId(), $index->setAttribute('status', 'available')); } catch (\Throwable $e) { - Console::error($e->getMessage()); if ($e instanceof DatabaseException) { $index->setAttribute('error', $e->getMessage()); } @@ -512,8 +509,6 @@ class Databases extends Action $dbForProject->deleteDocument('indexes', $index->getId()); $index->setAttribute('status', 'deleted'); } catch (\Throwable $e) { - Console::error($e->getMessage()); - if ($e instanceof DatabaseException) { $index->setAttribute('error', $e->getMessage()); } @@ -613,8 +608,6 @@ class Databases extends Action */ protected function deleteByGroup(string $collectionId, array $queries, Database $database, ?callable $callback = null): void { - $start = \microtime(true); - try { $count = $database->deleteDocuments( $collectionId, @@ -624,12 +617,11 @@ class Databases extends Action ); } catch (\Throwable $th) { $tenant = $database->getSharedTables() ? 'Tenant:'.$database->getTenant() : ''; - Console::error("Failed to delete documents/rows for collection/table: {$database->getNamespace()}_{$collectionId} {$tenant} :{$th->getMessage()}"); + Span::add('delete_by_group.error', "Failed to delete documents/rows for collection/table: {$database->getNamespace()}_{$collectionId} {$tenant} :{$th->getMessage()}"); return; } - $end = \microtime(true); - Console::info("Deleted {$count} documents/rows by group in " . ($end - $start) . " seconds"); + Span::add('delete_by_group.count', $count); } /** From a5cedf4671337f56b8b36f6d02382af39c924332 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:18:37 +0100 Subject: [PATCH 02/13] chore: resolve-adjacent spans and structured delete_by_group attributes Move each Span::add next to where its value is resolved instead of clumping them. Split the delete_by_group error string into separate structured attributes (namespace, collection.id, tenant, error) so it is no longer an unstructured log embedded in a span. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Modules/Databases/Workers/Databases.php | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index abdd678816..2aaf86c9b0 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -61,11 +61,25 @@ class Databases extends Action throw new Exception('Missing payload'); } + Span::add('project.id', $project->getId()); + Span::add('queue.pid', $message->getPid()); + Span::add('queue.name', $message->getQueue()); + Span::add('message.timestamp', (string) $message->getTimestamp()); + $databaseMessage = DatabaseMessage::fromArray($payload); + $type = $databaseMessage->type; + Span::add('payload.type', $type); + $document = $databaseMessage->row ?? $databaseMessage->document ?? new Document(); + Span::add('database.document.id', $document->getId()); + $collection = $databaseMessage->table ?? $databaseMessage->collection ?? new Document(); + Span::add('database.collection.id', $collection->getId()); + $database = $databaseMessage->database ?? new Document(); + Span::add('database.id', $database->getId()); + /** * @var Database $dbForDatabases */ @@ -73,22 +87,12 @@ class Databases extends Action $log->addTag('projectId', $project->getId()); $log->addTag('type', $type); - Span::add('project.id', $project->getId()); - Span::add('payload.type', $type); - Span::add('queue.pid', $message->getPid()); - Span::add('queue.name', $message->getQueue()); - Span::add('message.timestamp', (string) $message->getTimestamp()); - Span::add('database.collection.id', $collection->getId()); - Span::add('database.document.id', $document->getId()); - if ($database->isEmpty()) { throw new Exception('Missing database'); } $log->addTag('databaseId', $database->getId()); - Span::add('database.id', $database->getId()); - match (\strval($type)) { DATABASE_TYPE_DELETE_DATABASE => $this->deleteDatabase($database, $dbForProject, $dbForDatabases), DATABASE_TYPE_DELETE_COLLECTION => $this->deleteCollection($database, $collection, $dbForProject, $dbForDatabases), @@ -616,8 +620,12 @@ class Databases extends Action $callback ); } catch (\Throwable $th) { - $tenant = $database->getSharedTables() ? 'Tenant:'.$database->getTenant() : ''; - Span::add('delete_by_group.error', "Failed to delete documents/rows for collection/table: {$database->getNamespace()}_{$collectionId} {$tenant} :{$th->getMessage()}"); + Span::add('delete_by_group.namespace', $database->getNamespace()); + Span::add('delete_by_group.collection.id', $collectionId); + if ($database->getSharedTables()) { + Span::add('delete_by_group.tenant', $database->getTenant()); + } + Span::add('delete_by_group.error', $th->getMessage()); return; } From 4433b10eafb57ed9ff6057b7bc52e4f04bfde275 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:23:17 +0100 Subject: [PATCH 03/13] chore: scope namespace/tenant spans under database Namespace and tenant are properties of the database, so trace them as database.namespace / database.tenant rather than under the operation. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Platform/Modules/Databases/Workers/Databases.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index 2aaf86c9b0..702d33c2f0 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -620,11 +620,11 @@ class Databases extends Action $callback ); } catch (\Throwable $th) { - Span::add('delete_by_group.namespace', $database->getNamespace()); - Span::add('delete_by_group.collection.id', $collectionId); + Span::add('database.namespace', $database->getNamespace()); if ($database->getSharedTables()) { - Span::add('delete_by_group.tenant', $database->getTenant()); + Span::add('database.tenant', $database->getTenant()); } + Span::add('delete_by_group.collection', $collectionId); Span::add('delete_by_group.error', $th->getMessage()); return; } From 85b5328f2ecf12f81f1fe7e58f80fc81f4ae30f6 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:24:32 +0100 Subject: [PATCH 04/13] chore: always record database namespace/tenant in deleteByGroup Namespace and tenant describe the database regardless of outcome, so record them (along with the target collection) up front instead of only on the error path. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Platform/Modules/Databases/Workers/Databases.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index 702d33c2f0..89181bfd39 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -612,6 +612,10 @@ class Databases extends Action */ protected function deleteByGroup(string $collectionId, array $queries, Database $database, ?callable $callback = null): void { + Span::add('database.namespace', $database->getNamespace()); + Span::add('database.tenant', $database->getTenant()); + Span::add('delete_by_group.collection', $collectionId); + try { $count = $database->deleteDocuments( $collectionId, @@ -620,11 +624,6 @@ class Databases extends Action $callback ); } catch (\Throwable $th) { - Span::add('database.namespace', $database->getNamespace()); - if ($database->getSharedTables()) { - Span::add('database.tenant', $database->getTenant()); - } - Span::add('delete_by_group.collection', $collectionId); Span::add('delete_by_group.error', $th->getMessage()); return; } From b3263138e8d12641fd2b57a0fb61bb3dcead3e7a Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:26:17 +0100 Subject: [PATCH 05/13] chore: suffix delete_by_group collection span with .id Identifiers end in .id by convention; rename delete_by_group.collection to delete_by_group.collection.id. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/Appwrite/Platform/Modules/Databases/Workers/Databases.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index 89181bfd39..f0765af2a4 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -614,7 +614,7 @@ class Databases extends Action { Span::add('database.namespace', $database->getNamespace()); Span::add('database.tenant', $database->getTenant()); - Span::add('delete_by_group.collection', $collectionId); + Span::add('delete_by_group.collection.id', $collectionId); try { $count = $database->deleteDocuments( From 844d21d7763da0d121bb8d89d735617363916ad9 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:27:40 +0100 Subject: [PATCH 06/13] chore: record database namespace/tenant once in action These describe the project database connection and apply to every operation, so trace them up front in action() (from dbForProject, which deleteByGroup always uses) rather than inside deleteByGroup. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/Appwrite/Platform/Modules/Databases/Workers/Databases.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index f0765af2a4..cb3e7078a0 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -79,6 +79,8 @@ class Databases extends Action $database = $databaseMessage->database ?? new Document(); Span::add('database.id', $database->getId()); + Span::add('database.namespace', $dbForProject->getNamespace()); + Span::add('database.tenant', $dbForProject->getTenant()); /** * @var Database $dbForDatabases @@ -612,8 +614,6 @@ class Databases extends Action */ protected function deleteByGroup(string $collectionId, array $queries, Database $database, ?callable $callback = null): void { - Span::add('database.namespace', $database->getNamespace()); - Span::add('database.tenant', $database->getTenant()); Span::add('delete_by_group.collection.id', $collectionId); try { From bf4d620a5cc3b01825532843e1e58cb43317ccd2 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:30:10 +0100 Subject: [PATCH 07/13] chore: let deleteByGroup failures reach the worker error handler Previously a failed batch delete was caught and swallowed, acking the message as if it succeeded even though the documents remained. Let the error propagate so the worker marks the message failed and retries. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Modules/Databases/Workers/Databases.php | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index cb3e7078a0..2474ed99f4 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -610,23 +610,20 @@ class Databases extends Action * @param Database $database * @param callable|null $callback * @return void - * @throws Exception + * @throws \Throwable */ protected function deleteByGroup(string $collectionId, array $queries, Database $database, ?callable $callback = null): void { Span::add('delete_by_group.collection.id', $collectionId); - try { - $count = $database->deleteDocuments( - $collectionId, - $queries, - Database::DELETE_BATCH_SIZE, - $callback - ); - } catch (\Throwable $th) { - Span::add('delete_by_group.error', $th->getMessage()); - return; - } + // A failed batch delete is non-recoverable: let it propagate to the + // worker's error handler so the message is marked failed and retried. + $count = $database->deleteDocuments( + $collectionId, + $queries, + Database::DELETE_BATCH_SIZE, + $callback + ); Span::add('delete_by_group.count', $count); } From 5218fc8209ae06c89176b2885c77d7e9503f3853 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:30:53 +0100 Subject: [PATCH 08/13] chore: drop unnecessary comment in deleteByGroup Co-Authored-By: Claude Opus 4.8 (1M context) --- src/Appwrite/Platform/Modules/Databases/Workers/Databases.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index 2474ed99f4..a763a62778 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -616,8 +616,6 @@ class Databases extends Action { Span::add('delete_by_group.collection.id', $collectionId); - // A failed batch delete is non-recoverable: let it propagate to the - // worker's error handler so the message is marked failed and retried. $count = $database->deleteDocuments( $collectionId, $queries, From 486d9271ac7e8458078b6b9d8e913e13c7770a05 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:36:27 +0100 Subject: [PATCH 09/13] chore: centralize project.id span; drop superfluous worker spans The project resource setter is the canonical owner of project.id (cloud already adds it there for every worker span). Add it to the OSS worker project resource too, and drop the redundant project.id from the database worker. Also drop queue.pid/queue.name/message.timestamp: queue.name duplicates the span action (worker.databases) and none were part of the logging this PR replaced. Co-Authored-By: Claude Opus 4.8 (1M context) --- app/init/worker/message.php | 7 ++++++- .../Platform/Modules/Databases/Workers/Databases.php | 5 ----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/init/worker/message.php b/app/init/worker/message.php index ea6ba13aec..7cbfe19b0f 100644 --- a/app/init/worker/message.php +++ b/app/init/worker/message.php @@ -18,6 +18,7 @@ use Utopia\Logger\Log; use Utopia\Pools\Group; use Utopia\Queue\Publisher; use Utopia\Registry\Registry; +use Utopia\Span\Span; use Utopia\Storage\Device\Telemetry as TelemetryDevice; use Utopia\System\System; use Utopia\Telemetry\Adapter as Telemetry; @@ -55,7 +56,11 @@ return function (Container $container): void { return $project; } - return $dbForPlatform->getDocument('projects', $project->getId()); + $project = $dbForPlatform->getDocument('projects', $project->getId()); + + Span::add('project.id', $project->getId()); + + return $project; }, ['message', 'dbForPlatform']); $container->set('dbForProject', fn (DatabaseFactory $databaseFactory, Document $project, Database $dbForPlatform) => $project->isEmpty() || $project->getId() === 'console' diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index a763a62778..333af87493 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -61,11 +61,6 @@ class Databases extends Action throw new Exception('Missing payload'); } - Span::add('project.id', $project->getId()); - Span::add('queue.pid', $message->getPid()); - Span::add('queue.name', $message->getQueue()); - Span::add('message.timestamp', (string) $message->getTimestamp()); - $databaseMessage = DatabaseMessage::fromArray($payload); $type = $databaseMessage->type; From 46b2a398ff9e08d8277e3bae98f6d9d485300c24 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:49:38 +0100 Subject: [PATCH 10/13] fix: attempt all collection cleanups before failing Dropping the deleteByGroup swallow meant a failure in the first cleanup (related-attribute purge) skipped the attribute and index cleanups, leaving orphaned metadata rows since the table was already dropped. Run every cleanup in deleteCollection regardless of individual failures, then rethrow the first error so the worker still marks the message failed and retries the outstanding work. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Modules/Databases/Workers/Databases.php | 65 ++++++++++++------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index 333af87493..4339a0e165 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -569,33 +569,50 @@ class Databases extends Action $dbForDatabases->deleteCollection('database_' . $databaseInternalId . '_collection_' . $collection->getSequence()); - /** - * Related collections relating to current collection - */ - $this->deleteByGroup( - 'attributes', - [ + // The table is already dropped, so attempt every metadata cleanup even + // if an earlier one fails -- skipping the rest would leave orphaned + // attribute/index rows. Re-throw the first failure afterwards so the + // worker marks the message failed and retries the outstanding work. + $cleanups = [ + /** + * Related collections relating to current collection + */ + fn () => $this->deleteByGroup( + 'attributes', + [ + Query::equal('databaseInternalId', [$databaseInternalId]), + Query::equal('type', [Database::VAR_RELATIONSHIP]), + Query::notEqual('collectionInternalId', $collectionInternalId), + Query::contains('options', ['"relatedCollection":"'. $collectionId .'"']), + ], + $dbForProject, + function ($attribute) use ($dbForProject, $databaseInternalId) { + $dbForProject->purgeCachedDocument('database_' . $databaseInternalId, $attribute->getAttribute('collectionId')); + $dbForProject->purgeCachedCollection('database_' . $databaseInternalId . '_collection_' . $attribute->getAttribute('collectionInternalId')); + } + ), + fn () => $this->deleteByGroup('attributes', [ Query::equal('databaseInternalId', [$databaseInternalId]), - Query::equal('type', [Database::VAR_RELATIONSHIP]), - Query::notEqual('collectionInternalId', $collectionInternalId), - Query::contains('options', ['"relatedCollection":"'. $collectionId .'"']), - ], - $dbForProject, - function ($attribute) use ($dbForProject, $databaseInternalId) { - $dbForProject->purgeCachedDocument('database_' . $databaseInternalId, $attribute->getAttribute('collectionId')); - $dbForProject->purgeCachedCollection('database_' . $databaseInternalId . '_collection_' . $attribute->getAttribute('collectionInternalId')); + Query::equal('collectionInternalId', [$collectionInternalId]) + ], $dbForProject), + fn () => $this->deleteByGroup('indexes', [ + Query::equal('databaseInternalId', [$databaseInternalId]), + Query::equal('collectionInternalId', [$collectionInternalId]) + ], $dbForProject), + ]; + + $error = null; + foreach ($cleanups as $cleanup) { + try { + $cleanup(); + } catch (\Throwable $e) { + $error ??= $e; } - ); + } - $this->deleteByGroup('attributes', [ - Query::equal('databaseInternalId', [$databaseInternalId]), - Query::equal('collectionInternalId', [$collectionInternalId]) - ], $dbForProject); - - $this->deleteByGroup('indexes', [ - Query::equal('databaseInternalId', [$databaseInternalId]), - Query::equal('collectionInternalId', [$collectionInternalId]) - ], $dbForProject); + if ($error !== null) { + throw $error; + } } From 1a4f23d0adf07b252b1a3ec71aaca4f7d8ec832c Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:51:05 +0100 Subject: [PATCH 11/13] refactor: extract attemptAll helper for collection cleanups Replace the inline cleanup array + accumulator loop with a small variadic attemptAll() helper, so deleteCollection reads as the intent: run all cleanups, rethrow the first failure. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Modules/Databases/Workers/Databases.php | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index 4339a0e165..2db37008b1 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -569,14 +569,11 @@ class Databases extends Action $dbForDatabases->deleteCollection('database_' . $databaseInternalId . '_collection_' . $collection->getSequence()); - // The table is already dropped, so attempt every metadata cleanup even - // if an earlier one fails -- skipping the rest would leave orphaned - // attribute/index rows. Re-throw the first failure afterwards so the - // worker marks the message failed and retries the outstanding work. - $cleanups = [ - /** - * Related collections relating to current collection - */ + // The table is already dropped, so run every metadata cleanup even if + // one fails -- skipping the rest would orphan attribute/index rows. + // attemptAll re-throws the first failure so the message is retried. + $this->attemptAll( + // Related collections relating to current collection fn () => $this->deleteByGroup( 'attributes', [ @@ -599,9 +596,21 @@ class Databases extends Action Query::equal('databaseInternalId', [$databaseInternalId]), Query::equal('collectionInternalId', [$collectionInternalId]) ], $dbForProject), - ]; + ); + } + /** + * Run every cleanup even if some fail, then re-throw the first failure so + * the worker marks the message failed and retries the outstanding work. + * + * @param callable ...$cleanups + * @return void + * @throws \Throwable + */ + private function attemptAll(callable ...$cleanups): void + { $error = null; + foreach ($cleanups as $cleanup) { try { $cleanup(); From 9036e03b400324ca74a413ff1010f808fec06518 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 13:54:32 +0100 Subject: [PATCH 12/13] chore: drop redundant inline comment in deleteCollection Co-Authored-By: Claude Opus 4.8 (1M context) --- src/Appwrite/Platform/Modules/Databases/Workers/Databases.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index 2db37008b1..fda1ebe923 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -569,9 +569,6 @@ class Databases extends Action $dbForDatabases->deleteCollection('database_' . $databaseInternalId . '_collection_' . $collection->getSequence()); - // The table is already dropped, so run every metadata cleanup even if - // one fails -- skipping the rest would orphan attribute/index rows. - // attemptAll re-throws the first failure so the message is retried. $this->attemptAll( // Related collections relating to current collection fn () => $this->deleteByGroup( From fad69ebd44684ef62a663d77888256f1a62594be Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 14:00:08 +0100 Subject: [PATCH 13/13] chore: keep deleteByGroup best-effort, swallowing failures Revert to the original swallow-and-continue: a failed batch delete is recorded on the span and skipped so the remaining cleanups still run. Drops the attemptAll helper and restores deleteCollection's sequential calls for a minimal diff. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Modules/Databases/Workers/Databases.php | 90 ++++++++----------- 1 file changed, 36 insertions(+), 54 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php index fda1ebe923..a6b13857a5 100644 --- a/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php +++ b/src/Appwrite/Platform/Modules/Databases/Workers/Databases.php @@ -569,56 +569,33 @@ class Databases extends Action $dbForDatabases->deleteCollection('database_' . $databaseInternalId . '_collection_' . $collection->getSequence()); - $this->attemptAll( - // Related collections relating to current collection - fn () => $this->deleteByGroup( - 'attributes', - [ - Query::equal('databaseInternalId', [$databaseInternalId]), - Query::equal('type', [Database::VAR_RELATIONSHIP]), - Query::notEqual('collectionInternalId', $collectionInternalId), - Query::contains('options', ['"relatedCollection":"'. $collectionId .'"']), - ], - $dbForProject, - function ($attribute) use ($dbForProject, $databaseInternalId) { - $dbForProject->purgeCachedDocument('database_' . $databaseInternalId, $attribute->getAttribute('collectionId')); - $dbForProject->purgeCachedCollection('database_' . $databaseInternalId . '_collection_' . $attribute->getAttribute('collectionInternalId')); - } - ), - fn () => $this->deleteByGroup('attributes', [ + /** + * Related collections relating to current collection + */ + $this->deleteByGroup( + 'attributes', + [ Query::equal('databaseInternalId', [$databaseInternalId]), - Query::equal('collectionInternalId', [$collectionInternalId]) - ], $dbForProject), - fn () => $this->deleteByGroup('indexes', [ - Query::equal('databaseInternalId', [$databaseInternalId]), - Query::equal('collectionInternalId', [$collectionInternalId]) - ], $dbForProject), - ); - } - - /** - * Run every cleanup even if some fail, then re-throw the first failure so - * the worker marks the message failed and retries the outstanding work. - * - * @param callable ...$cleanups - * @return void - * @throws \Throwable - */ - private function attemptAll(callable ...$cleanups): void - { - $error = null; - - foreach ($cleanups as $cleanup) { - try { - $cleanup(); - } catch (\Throwable $e) { - $error ??= $e; + Query::equal('type', [Database::VAR_RELATIONSHIP]), + Query::notEqual('collectionInternalId', $collectionInternalId), + Query::contains('options', ['"relatedCollection":"'. $collectionId .'"']), + ], + $dbForProject, + function ($attribute) use ($dbForProject, $databaseInternalId) { + $dbForProject->purgeCachedDocument('database_' . $databaseInternalId, $attribute->getAttribute('collectionId')); + $dbForProject->purgeCachedCollection('database_' . $databaseInternalId . '_collection_' . $attribute->getAttribute('collectionInternalId')); } - } + ); - if ($error !== null) { - throw $error; - } + $this->deleteByGroup('attributes', [ + Query::equal('databaseInternalId', [$databaseInternalId]), + Query::equal('collectionInternalId', [$collectionInternalId]) + ], $dbForProject); + + $this->deleteByGroup('indexes', [ + Query::equal('databaseInternalId', [$databaseInternalId]), + Query::equal('collectionInternalId', [$collectionInternalId]) + ], $dbForProject); } @@ -628,18 +605,23 @@ class Databases extends Action * @param Database $database * @param callable|null $callback * @return void - * @throws \Throwable + * @throws Exception */ protected function deleteByGroup(string $collectionId, array $queries, Database $database, ?callable $callback = null): void { Span::add('delete_by_group.collection.id', $collectionId); - $count = $database->deleteDocuments( - $collectionId, - $queries, - Database::DELETE_BATCH_SIZE, - $callback - ); + try { + $count = $database->deleteDocuments( + $collectionId, + $queries, + Database::DELETE_BATCH_SIZE, + $callback + ); + } catch (\Throwable $th) { + Span::add('delete_by_group.error', $th->getMessage()); + return; + } Span::add('delete_by_group.count', $count); }