Skip to content

Commit dfbf0af

Browse files
BYKcursoragent
andauthored
fix(migrations): Fix data integrity error in 0925 (#94264)
Fixes getsentry/self-hosted#3571 The `0925_backfill_open_periods.py` migration was updated to robustly handle data inconsistencies. * `DataError` was imported from `django.db` and added to the exception handling in the `_backfill_group_open_periods` function. * The `except` block for `GroupOpenPeriod.objects.bulk_create` now catches both `IntegrityError` and `DataError`. This prevents migration failures caused by invalid date ranges (e.g., start date after end date) that trigger `DataError` in PostgreSQL. * Previously, only `IntegrityError` was caught, leading to migration failures for users with such data. * A new test case was introduced in `tests/sentry/migrations/test_0925_backfill_open_periods.py` to specifically validate the handling of groups with invalid date ranges that would trigger a `DataError`, confirming the migration's resilience. --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com>
1 parent f8c30f1 commit dfbf0af

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/sentry/migrations/0925_backfill_open_periods.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import Any
88

99
from django.conf import settings
10-
from django.db import IntegrityError, migrations, router, transaction
10+
from django.db import DataError, IntegrityError, migrations, router, transaction
1111
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
1212
from django.db.migrations.state import StateApps
1313

@@ -170,7 +170,7 @@ def _backfill_group_open_periods(
170170
with transaction.atomic(router.db_for_write(GroupOpenPeriod)):
171171
try:
172172
GroupOpenPeriod.objects.bulk_create(open_periods)
173-
except IntegrityError as e:
173+
except (IntegrityError, DataError) as e:
174174
logger.exception(
175175
"Error creating open period",
176176
extra={"group_ids": group_ids, "error": e},

tests/sentry/migrations/test_0925_backfill_open_periods.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,31 @@ def setup_before_migration(self, app):
331331
)
332332
)
333333

334+
# Create a group with invalid date range that would cause DataError
335+
# This tests the DataError exception handling
336+
group_with_invalid_dates = Group.objects.create(
337+
project=self.project,
338+
status=GroupStatus.RESOLVED,
339+
substatus=None,
340+
first_seen=self.now - timedelta(days=1), # Start after the resolution
341+
)
342+
Activity.objects.create(
343+
group=group_with_invalid_dates,
344+
project=self.project,
345+
type=ActivityType.SET_RESOLVED.value,
346+
datetime=self.now - timedelta(days=2), # Resolution before first_seen
347+
)
348+
# This group should be skipped due to invalid date range
349+
self.test_cases.append(
350+
(
351+
"group_with_invalid_date_range",
352+
group_with_invalid_dates,
353+
[], # No open periods expected due to invalid dates
354+
[],
355+
[],
356+
)
357+
)
358+
334359
def test(self):
335360
for description, group, starts, ends, activities in self.test_cases:
336361
group.refresh_from_db()

0 commit comments

Comments
 (0)