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] 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; + } }