Skip to content

Commit 43d8973

Browse files
snigdhasandrewshie-sentry
authored andcommitted
fix(aci): Drop metric_alert_fire detectors (#89924)
We're renaming `MetricAlertFire` to `MetricIssue` ([PR](#89896)). To do so, we need to drop existing detectors that use the `metric_alert_fire` slug. There's ~230 rows that match that filter and the table has ~3k rows. All the rows in the table were written during a test of the dual write and are safe to drop as they'll be rewritten again once the flag is on. I've marked this as post-deploy but it probably doesn't need to be.
1 parent 2b047df commit 43d8973

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

migrations_lockfile.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ tempest: 0002_make_message_type_nullable
2929

3030
uptime: 0033_uptime_backfill_to_detectors
3131

32-
workflow_engine: 0045_add_unique_constraint_alert_rule_detector
32+
workflow_engine: 0046_drop_metric_alert_fire_detectors
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Generated by Django 5.1.7 on 2025-04-18 01:22
2+
3+
from django.db import migrations
4+
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
5+
from django.db.migrations.state import StateApps
6+
7+
from sentry.new_migrations.migrations import CheckedMigration
8+
9+
10+
def delete_metric_alert_fire_detectors(
11+
apps: StateApps, schema_editor: BaseDatabaseSchemaEditor
12+
) -> None:
13+
Detector = apps.get_model("workflow_engine", "Detector")
14+
DetectorWorkflow = apps.get_model("workflow_engine", "DetectorWorkflow")
15+
Workflow = apps.get_model("workflow_engine", "Workflow")
16+
17+
detectors = Detector.objects.filter(type="metric_alert_fire")
18+
workflow_ids = DetectorWorkflow.objects.filter(detector__in=detectors).values_list(
19+
"workflow__id", flat=True
20+
)
21+
workflows = Workflow.objects.filter(id__in=workflow_ids)
22+
23+
workflows.delete()
24+
detectors.delete()
25+
26+
27+
class Migration(CheckedMigration):
28+
# This flag is used to mark that a migration shouldn't be automatically run in production.
29+
# This should only be used for operations where it's safe to run the migration after your
30+
# code has deployed. So this should not be used for most operations that alter the schema
31+
# of a table.
32+
# Here are some things that make sense to mark as post deployment:
33+
# - Large data migrations. Typically we want these to be run manually so that they can be
34+
# monitored and not block the deploy for a long period of time while they run.
35+
# - Adding indexes to large tables. Since this can take a long time, we'd generally prefer to
36+
# run this outside deployments so that we don't block them. Note that while adding an index
37+
# is a schema change, it's completely safe to run the operation after the code has deployed.
38+
# Once deployed, run these manually via: https://develop.sentry.dev/database-migrations/#migration-deployment
39+
40+
is_post_deployment = True
41+
42+
dependencies = [
43+
("workflow_engine", "0045_add_unique_constraint_alert_rule_detector"),
44+
]
45+
46+
operations = [
47+
migrations.RunPython(
48+
code=delete_metric_alert_fire_detectors,
49+
reverse_code=migrations.RunPython.noop,
50+
hints={"tables": ["workflow_engine.Detector"]},
51+
),
52+
]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from sentry.models.organization import Organization
2+
from sentry.testutils.cases import TestMigrations
3+
from sentry.workflow_engine.models.detector import Detector
4+
5+
6+
class DropMetricAlertFireDetectors(TestMigrations):
7+
app = "workflow_engine"
8+
migrate_from = "0045_add_unique_constraint_alert_rule_detector"
9+
migrate_to = "0046_drop_metric_alert_fire_detectors"
10+
11+
def setup_before_migration(self, app):
12+
self.organization = Organization.objects.create(name="test", slug="test")
13+
self.project = self.create_project(organization=self.organization)
14+
15+
detector_field_values = {
16+
"project_id": self.project.id,
17+
"name": "test",
18+
"description": "test",
19+
"workflow_condition_group": None,
20+
"owner_user_id": None,
21+
"owner_team": None,
22+
"config": {
23+
"threshold_period": 1,
24+
"comparison_delta": 300,
25+
"detection_type": "static",
26+
},
27+
"enabled": True,
28+
"created_by_id": None,
29+
"type": "metric_alert_fire",
30+
}
31+
32+
Detector.objects.create(**detector_field_values)
33+
detector_field_values["type"] = "monitor_check_in_failure"
34+
Detector.objects.create(**detector_field_values)
35+
36+
assert Detector.objects.filter(type="metric_alert_fire").count() == 1
37+
38+
def test(self):
39+
assert Detector.objects.filter(type="metric_alert_fire").count() == 0

0 commit comments

Comments
 (0)