Skip to content

Conversation

@cursor
Copy link
Contributor

@cursor cursor bot commented Nov 19, 2025

Fixes an IntegrityError during monitor deletion by ensuring MonitorIncident records are deleted before MonitorCheckIn records.

Previously, deleting MonitorCheckIn records could fail due to foreign key constraints from MonitorIncident (which references MonitorCheckIn via starting_checkin and resolving_checkin). This PR reorders the deletion tasks for Monitor and MonitorEnvironment to correctly cascade deletions, removing MonitorIncidents first.

New regression tests were added to reproduce the IntegrityError scenario and verify the fix for both Monitor and MonitorEnvironment deletions.

Legal Boilerplate

Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.


Open in Cursor Open in Web

Co-authored-by: jenn.muengtaweepongsa <jenn.muengtaweepongsa@sentry.io>
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Nov 19, 2025
@codecov
Copy link

codecov bot commented Nov 19, 2025

❌ 2 Tests Failed:

Tests completed Failed Passed Skipped
29868 2 29866 245
View the top 2 failed test(s) by shortest run time
tests.sentry.deletions.test_monitor_environment.DeleteMonitorEnvironmentTest::test_simple
Stack Traces | 2.02s run time
#x1B[1m#x1B[.../sentry/deletions/test_monitor_environment.py#x1B[0m:70: in test_simple
    assert not MonitorEnvBrokenDetection.objects.filter(id=detection.id).exists()
#x1B[1m#x1B[31mE   assert not True#x1B[0m
#x1B[1m#x1B[31mE    +  where True = <bound method QuerySet.exists of <BaseQuerySet [<MonitorEnvBrokenDetection at 0x7fed144c2410: id=6>]>>()#x1B[0m
#x1B[1m#x1B[31mE    +    where <bound method QuerySet.exists of <BaseQuerySet [<MonitorEnvBrokenDetection at 0x7fed144c2410: id=6>]>> = <BaseQuerySet [<MonitorEnvBrokenDetection at 0x7fed144f2a50: id=6>]>.exists#x1B[0m
#x1B[1m#x1B[31mE    +      where <BaseQuerySet [<MonitorEnvBrokenDetection at 0x7fed144f2a50: id=6>]> = <bound method QuerySet.filter of <sentry.db.models.manager.base.BaseManager object at 0x7fed76673230>>(id=6)#x1B[0m
#x1B[1m#x1B[31mE    +        where <bound method QuerySet.filter of <sentry.db.models.manager.base.BaseManager object at 0x7fed76673230>> = <sentry.db.models.manager.base.BaseManager object at 0x7fed76673230>.filter#x1B[0m
#x1B[1m#x1B[31mE    +          where <sentry.db.models.manager.base.BaseManager object at 0x7fed76673230> = MonitorEnvBrokenDetection.objects#x1B[0m
#x1B[1m#x1B[31mE    +        and   6 = <MonitorEnvBrokenDetection at 0x7fed144f2810: id=6>.id#x1B[0m
tests.sentry.deletions.test_monitor.DeleteMonitorTest::test_simple
Stack Traces | 2.36s run time
#x1B[1m#x1B[.../sentry/deletions/test_monitor.py#x1B[0m:57: in test_simple
    assert not MonitorEnvBrokenDetection.objects.filter(id=detection.id).exists()
#x1B[1m#x1B[31mE   assert not True#x1B[0m
#x1B[1m#x1B[31mE    +  where True = <bound method QuerySet.exists of <BaseQuerySet [<MonitorEnvBrokenDetection at 0x7fc30084a780: id=6>]>>()#x1B[0m
#x1B[1m#x1B[31mE    +    where <bound method QuerySet.exists of <BaseQuerySet [<MonitorEnvBrokenDetection at 0x7fc30084a780: id=6>]>> = <BaseQuerySet [<MonitorEnvBrokenDetection at 0x7fc323ee2090: id=6>]>.exists#x1B[0m
#x1B[1m#x1B[31mE    +      where <BaseQuerySet [<MonitorEnvBrokenDetection at 0x7fc323ee2090: id=6>]> = <bound method QuerySet.filter of <sentry.db.models.manager.base.BaseManager object at 0x7fc343493230>>(id=6)#x1B[0m
#x1B[1m#x1B[31mE    +        where <bound method QuerySet.filter of <sentry.db.models.manager.base.BaseManager object at 0x7fc343493230>> = <sentry.db.models.manager.base.BaseManager object at 0x7fc343493230>.filter#x1B[0m
#x1B[1m#x1B[31mE    +          where <sentry.db.models.manager.base.BaseManager object at 0x7fc343493230> = MonitorEnvBrokenDetection.objects#x1B[0m
#x1B[1m#x1B[31mE    +        and   6 = <MonitorEnvBrokenDetection at 0x7fc322506390: id=6>.id#x1B[0m

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants