From f6e9c87930112c4635643707989b90a6290d1e42 Mon Sep 17 00:00:00 2001 From: Colleen O'Rourke Date: Fri, 31 Jan 2025 13:45:23 -0800 Subject: [PATCH] Add test that makees me realize the solution doesnt work --- src/sentry/deletions/defaults/detector.py | 1 + tests/sentry/deletions/test_detector.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/sentry/deletions/defaults/detector.py b/src/sentry/deletions/defaults/detector.py index 0215e13f60fa88..b387f2b2baa37f 100644 --- a/src/sentry/deletions/defaults/detector.py +++ b/src/sentry/deletions/defaults/detector.py @@ -13,6 +13,7 @@ def get_child_relations(self, instance: Detector) -> list[BaseRelation]: data_sources = DataSource.objects.filter(detector=instance.id) delete = True + # this doesn't work if a data source is also connected to a different detector that's not being deleted for data_source in data_sources: for detector in data_source.detectors.all(): if detector.id != instance.id: diff --git a/tests/sentry/deletions/test_detector.py b/tests/sentry/deletions/test_detector.py index ed3457e07897a2..1666ec8cc7a9a1 100644 --- a/tests/sentry/deletions/test_detector.py +++ b/tests/sentry/deletions/test_detector.py @@ -76,3 +76,24 @@ def test_data_source_not_deleted(self): assert not DataCondition.objects.filter(id=self.data_condition.id).exists() assert DataSource.objects.filter(id=self.data_source.id).exists() assert DataSourceDetector.objects.filter(id=data_source_detector_2.id).exists() + + def test_multiple_data_sources(self): + """ + Test that if we have multiple data sources where one is connected to another Detector but the other one isn't, we only delete one + """ + data_condition_group = self.create_data_condition_group() + self.create_data_condition(condition_group=data_condition_group) + detector = self.create_detector( + project_id=self.project.id, + name="Testy Detector", + type=MetricAlertFire.slug, + workflow_condition_group=data_condition_group, + ) + data_source_2 = self.create_data_source(organization=self.organization) + + # multiple data sources for one detector + self.create_data_source_detector(data_source=data_source_2, detector=self.detector) + # but the data source is also connected to a different detector + self.create_data_source_detector(data_source=data_source_2, detector=detector) + assert not DataSource.objects.filter(id=self.data_source.id).exists() + assert DataSource.objects.filter(id=data_source_2.id).exists()