Skip to content

feat(crons): Blank owner_team_id when deleting teams #69035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/sentry/deletions/defaults/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ def mark_deletion_in_progress(self, instance_list):
def delete_instance(self, instance):
from sentry.incidents.models.alert_rule import AlertRule
from sentry.models.rule import Rule
from sentry.monitors.models import Monitor

AlertRule.objects.filter(owner_id=instance.actor_id).update(owner=None)
Rule.objects.filter(owner_id=instance.actor_id).update(owner=None)
Monitor.objects.filter(owner_team_id=instance.actor_id).update(owner_team_id=None)
super().delete_instance(instance)
20 changes: 20 additions & 0 deletions tests/sentry/deletions/test_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from sentry.models.projectteam import ProjectTeam
from sentry.models.rule import Rule
from sentry.models.team import Team
from sentry.monitors.models import Monitor, MonitorType
from sentry.tasks.deletion.scheduled import run_scheduled_deletions
from sentry.testutils.cases import TestCase
from sentry.testutils.hybrid_cloud import HybridCloudTestMixin
Expand Down Expand Up @@ -44,3 +45,22 @@ def test_alert_blanking(self):
rule.refresh_from_db()
assert rule.owner_id is None, "Should be blank when team is deleted."
assert alert_rule.owner_id is None, "Should be blank when team is deleted."

def test_monitor_blanking(self):
team = self.create_team(name="test")
monitor = Monitor.objects.create(
organization_id=self.organization.id,
project_id=self.project.id,
type=MonitorType.CRON_JOB,
name="My Awesome Monitor",
owner_team_id=team.actor.id,
)
self.ScheduledDeletion.schedule(team, days=0)

with self.tasks():
run_scheduled_deletions()

assert not Team.objects.filter(id=team.id).exists()

monitor.refresh_from_db()
assert monitor.owner_team_id is None, "Should be blank when team is deleted."
Loading