Fix: Handle duplicate relationship attribute gracefully in Databases worker

When a queue message is retried and the relationship already exists in the
database, the worker now catches the Duplicate exception and marks the
attribute as 'available' instead of 'failed'. This prevents spurious Sentry
errors (CLOUD-3JA4) from queue retries.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
ClaudearandClaude Opus 4.6 committed 2026-03-11 09:40:58 +00:00
1 parent a5fa09b4ce
commit de160624b2
2 files changed
+285

No files matched your search

@@ -10,6 +10,7 @@ use Utopia\Database\Document;
use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Exception\Authorization;
use Utopia\Database\Exception\Conflict;
use Utopia\Database\Exception\Duplicate as DuplicateException;
use Utopia\Database\Exception\NotFound;
use Utopia\Database\Exception\Restricted;
use Utopia\Database\Exception\Structure;
@@ -187,6 +188,18 @@ class Databases extends Action
}
$dbForProject->updateDocument('attributes', $attribute->getId(), $attribute->setAttribute('status', 'available'));
} catch (DuplicateException) {
// Attribute/relationship already exists (e.g. queue retry), treat as success
Console::warning('Attribute already exists, marking as available');
$dbForProject->updateDocument('attributes', $attribute->getId(), $attribute->setAttribute('status', 'available'));
if ($type === Database::VAR_RELATIONSHIP && $options['twoWay'] && !$relatedCollection->isEmpty()) {
$relatedAttribute = $dbForProject->getDocument('attributes', $database->getSequence() . '_' . $relatedCollection->getSequence() . '_' . $options['twoWayKey']);
if (!$relatedAttribute->isEmpty()) {
$dbForProject->updateDocument('attributes', $relatedAttribute->getId(), $relatedAttribute->setAttribute('status', 'available'));
}
}
} catch (\Throwable $e) {
Console::error($e->getMessage());