Skip to content

Commit 72b1a6b

Browse files
authored
fix(crons): Fix project transfer to properly update the environment of the monitor (#70407)
We don't update the environment of `MonitorEnvironment` rows when we transfer projects - this fixes transfer to do this correctly. I will look into whether it's possible to fix existing rows via backfills - I think that in most cases, a new monitor environment will have already been created, and it won't be worth the effort to transfer the existing checkins onto the new environment.
1 parent 659af46 commit 72b1a6b

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/sentry/models/project.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from sentry.models.options.option import OptionMixin
4040
from sentry.models.outbox import OutboxCategory, OutboxScope, RegionOutbox, outbox_context
4141
from sentry.models.team import Team
42+
from sentry.monitors.models import MonitorEnvironment, MonitorStatus
4243
from sentry.services.hybrid_cloud.notifications import notifications_service
4344
from sentry.services.hybrid_cloud.user import RpcUser
4445
from sentry.services.hybrid_cloud.user.service import user_service
@@ -470,7 +471,7 @@ def transfer_to(self, organization):
470471
rules_by_environment_id[environment_id].add(rule_id)
471472

472473
environment_names = dict(
473-
Environment.objects.filter(id__in=rules_by_environment_id).values_list("id", "name")
474+
Environment.objects.filter(organization_id=old_org_id).values_list("id", "name")
474475
)
475476

476477
for environment_id, rule_ids in rules_by_environment_id.items():
@@ -487,6 +488,14 @@ def transfer_to(self, organization):
487488
if monitor.slug in new_monitors:
488489
RegionScheduledDeletion.schedule(monitor, days=0)
489490
else:
491+
for monitor_env_id, env_id in MonitorEnvironment.objects.filter(
492+
monitor_id=monitor.id, status=MonitorStatus.ACTIVE
493+
).values_list("id", "environment_id"):
494+
MonitorEnvironment.objects.filter(id=monitor_env_id).update(
495+
environment_id=Environment.get_or_create(
496+
self, name=environment_names.get(env_id, None)
497+
).id
498+
)
490499
monitor.update(organization_id=organization.id)
491500

492501
# Remove alert owners not in new org

tests/sentry/models/test_project.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from sentry.models.rule import Rule
1818
from sentry.models.scheduledeletion import RegionScheduledDeletion
1919
from sentry.models.user import User
20-
from sentry.monitors.models import Monitor, MonitorType, ScheduleType
20+
from sentry.monitors.models import Monitor, MonitorEnvironment, MonitorType, ScheduleType
2121
from sentry.notifications.types import NotificationSettingEnum
2222
from sentry.notifications.utils.participants import get_notification_recipients
2323
from sentry.services.hybrid_cloud.actor import RpcActor
@@ -66,6 +66,11 @@ def test_transfer_to_organization(self):
6666
label="Golden Rule",
6767
data={},
6868
)
69+
environment_from_new = self.create_environment(organization=from_org)
70+
environment_from_existing = self.create_environment(organization=from_org)
71+
environment_to_existing = self.create_environment(
72+
organization=to_org, name=environment_from_existing.name
73+
)
6974

7075
monitor = Monitor.objects.create(
7176
name="test-monitor",
@@ -84,6 +89,12 @@ def test_transfer_to_organization(self):
8489
type=MonitorType.CRON_JOB,
8590
config={"schedule": [1, "month"], "schedule_type": ScheduleType.INTERVAL},
8691
)
92+
monitor_env_new = MonitorEnvironment.objects.create(
93+
monitor=monitor_also, environment_id=environment_from_new.id
94+
)
95+
monitor_env_existing = MonitorEnvironment.objects.create(
96+
monitor=monitor_also, environment_id=environment_from_existing.id
97+
)
8798

8899
monitor_other = Monitor.objects.create(
89100
name="test-monitor-other",
@@ -124,6 +135,12 @@ def test_transfer_to_organization(self):
124135
assert updated_monitor.id == monitor_also.id
125136
assert updated_monitor.organization_id == to_org.id
126137
assert updated_monitor.project_id == project.id
138+
monitor_env_new.refresh_from_db()
139+
environment_to_new = Environment.objects.get(id=monitor_env_new.environment_id)
140+
assert environment_to_new.organization_id == to_org.id
141+
assert environment_to_new.name == environment_from_new.name
142+
monitor_env_existing.refresh_from_db()
143+
assert monitor_env_existing.environment_id == environment_to_existing.id
127144

128145
unmoved_monitor = Monitor.objects.get(slug="test-monitor-other")
129146
assert unmoved_monitor.id == monitor_other.id

0 commit comments

Comments
 (0)