Skip to content

Commit d1cc52d

Browse files
committed
start to unwind this a little, also fixed an N+1
1 parent db37f11 commit d1cc52d

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

src/sentry/workflow_engine/migration_helpers/issue_alert_migration.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def run(self) -> Workflow:
7777

7878
return workflow
7979

80-
def _create_detector_lookups(self) -> list[Detector | None]:
80+
def _create_detector_lookups(self) -> list[Detector]:
8181
if self.rule.source == RuleSource.CRON_MONITOR:
8282
# Find the cron detector that was created before the rule
8383
monitor_slug = None
@@ -87,7 +87,7 @@ def _create_detector_lookups(self) -> list[Detector | None]:
8787
break
8888

8989
if not monitor_slug:
90-
return [None]
90+
return []
9191

9292
try:
9393
with in_test_hide_transaction_boundary():
@@ -105,7 +105,7 @@ def _create_detector_lookups(self) -> list[Detector | None]:
105105
except (Monitor.DoesNotExist, Detector.DoesNotExist):
106106
pass
107107

108-
return [None]
108+
return []
109109

110110
if self.is_dry_run:
111111
error_detector = Detector.objects.filter(
@@ -139,9 +139,25 @@ def _create_detector_lookups(self) -> list[Detector | None]:
139139

140140
def _connect_default_detectors(self, workflow: Workflow) -> None:
141141
default_detectors = self._create_detector_lookups()
142-
for detector in default_detectors:
143-
if detector:
144-
DetectorWorkflow.objects.get_or_create(detector=detector, workflow=workflow)
142+
143+
# do not add references to both issue stream and error group types
144+
# it seems like other types might be relying on this as well,
145+
# so this just says not to link the error groups.
146+
# TODO - provide helpers to more easily create these classes
147+
# and references in code, so we can remove the reliance on this code
148+
references_to_create = [
149+
DetectorWorkflow(
150+
detector=detector,
151+
workflow=workflow,
152+
)
153+
for detector in default_detectors
154+
if detector.type != ErrorGroupType.slug
155+
]
156+
157+
DetectorWorkflow.objects.bulk_create(
158+
references_to_create,
159+
ignore_conflicts=True,
160+
)
145161

146162
def _bulk_create_data_conditions(
147163
self,

tests/sentry/workflow_engine/migration_helpers/test_issue_alert_migration.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,12 @@ def assert_error_detector_migrated(self, issue_alert: Rule, workflow: Workflow)
148148
assert error_detector.type == ErrorGroupType.slug
149149
assert error_detector.config == {}
150150

151-
error_detector_workflow = DetectorWorkflow.objects.get(detector=error_detector)
152-
assert error_detector_workflow.workflow == workflow
151+
assert not DetectorWorkflow.objects.filter(detector=error_detector).exists()
152+
assert DetectorWorkflow.objects.filter(
153+
detector__type=IssueStreamGroupType.slug,
154+
detector__project=self.project,
155+
workflow=workflow,
156+
).exists()
153157

154158
return error_detector
155159

0 commit comments

Comments
 (0)