Skip to content

Commit 1b5f1ef

Browse files
authored
fix(crons): Fix backfill to handle multiple objects returned (#98268)
There shouldn't be multiple objects here, but there's no unique constraint and so they can happen during a race condition. If these rows exist at all we have nothing to do, so just skip <!-- Describe your PR here. -->
1 parent 43604db commit 1b5f1ef

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/sentry/monitors/migrations/0009_backfill_monitor_detectors.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,16 @@ def backfill_monitor_detectors(apps: StateApps, schema_editor: BaseDatabaseSchem
3030

3131
# Copied from `ensure_cron_detector`
3232
with transaction.atomic(router.db_for_write(DataSource)):
33-
data_source, created = DataSource.objects.get_or_create(
34-
type=DATA_SOURCE_CRON_MONITOR,
35-
organization_id=monitor.organization_id,
36-
source_id=str(monitor.id),
37-
)
33+
try:
34+
data_source, created = DataSource.objects.get_or_create(
35+
type=DATA_SOURCE_CRON_MONITOR,
36+
organization_id=monitor.organization_id,
37+
source_id=str(monitor.id),
38+
)
39+
except DataSource.MultipleObjectsReturned:
40+
# If these rows already exist just skip. We shouldn't have multiple rows, but because there's no unique
41+
# key on `type, organization_id, source_id` we can have race conditions.
42+
continue
3843
if created:
3944
detector = Detector.objects.create(
4045
# MonitorIncidentType.slug

0 commit comments

Comments
 (0)