Skip to content

Commit 84e010e

Browse files
committed
update migration since other changes
1 parent d4d626c commit 84e010e

File tree

2 files changed

+51
-22
lines changed

2 files changed

+51
-22
lines changed

migrations_lockfile.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ social_auth: 0002_default_auto_field
2121

2222
uptime: 0018_add_trace_sampling_field_to_uptime
2323

24-
workflow_engine: 0014_model_additions_for_milestones
24+
workflow_engine: 0015_migrate_alert_rules

src/sentry/workflow_engine/migrations/0012_migrate_metric_alerts.py renamed to src/sentry/workflow_engine/migrations/0015_migrate_alert_rules.py

+50-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated by Django 5.1.1 on 2024-11-08 23:16
1+
# Generated by Django 5.1.1 on 2024-11-25 20:59
22

33
from enum import Enum, IntEnum
44

@@ -54,34 +54,57 @@ class AlertRuleStatus(Enum):
5454
NOT_ENOUGH_DATA = 6
5555

5656

57+
class AlertRuleActivityType(Enum):
58+
CREATED = 1
59+
DELETED = 2
60+
UPDATED = 3
61+
ENABLED = 4
62+
DISABLED = 5
63+
SNAPSHOT = 6
64+
ACTIVATED = 7
65+
DEACTIVATED = 8
66+
67+
5768
def migrate_metric_alerts(apps: Apps, schema_editor: BaseDatabaseSchemaEditor) -> None:
5869
AlertRule = apps.get_model("sentry", "AlertRule")
5970
AlertRuleProjects = apps.get_model("sentry", "AlertRuleProjects")
6071
AlertRuleTrigger = apps.get_model("sentry", "AlertRuleTrigger")
6172
AlertRuleTriggerAction = apps.get_model("sentry", "AlertRuleTriggerAction")
62-
# AlertRuleActivity = apps.get_model("sentry", "AlertRuleActivity")
73+
AlertRuleActivity = apps.get_model("sentry", "AlertRuleActivity")
74+
RuleSnooze = apps.get_model("sentry", "RuleSnooze")
6375
QuerySubscription = apps.get_model("sentry", "QuerySubscription")
6476
Incident = apps.get_model("sentry", "Incident")
6577

66-
DataSource = apps.get_model("sentry", "DataSource")
67-
DataConditionGroup = apps.get_model("sentry", "DataConditionGroup")
68-
Workflow = apps.get_model("sentry", "Workflow")
69-
Detector = apps.get_model("sentry", "Detector")
70-
DetectorState = apps.get_model("sentry", "DetectorState")
71-
DataCondition = apps.get_model("sentry", "DataCondition")
72-
Action = apps.get_model("sentry", "Action")
73-
DataConditionGroupAction = apps.get_model("sentry", "DataConditionGroupAction")
74-
DataSourceDetector = apps.get_model("sentry", "DataSourceDetector")
75-
DetectorWorkflow = apps.get_model("sentry", "DetectorWorkflow")
76-
WorkflowDataConditionGroup = apps.get_model("sentry", "WorkflowDataConditionGroup")
77-
78-
for rule in RangeQuerySetWrapperWithProgressBarApprox(AlertRule.objects.all()):
78+
DataSource = apps.get_model("workflow_engine", "DataSource")
79+
DataConditionGroup = apps.get_model("workflow_engine", "DataConditionGroup")
80+
Workflow = apps.get_model("workflow_engine", "Workflow")
81+
Detector = apps.get_model("workflow_engine", "Detector")
82+
DetectorState = apps.get_model("workflow_engine", "DetectorState")
83+
DataCondition = apps.get_model("workflow_engine", "DataCondition")
84+
Action = apps.get_model("workflow_engine", "Action")
85+
DataConditionGroupAction = apps.get_model("workflow_engine", "DataConditionGroupAction")
86+
DataSourceDetector = apps.get_model("workflow_engine", "DataSourceDetector")
87+
DetectorWorkflow = apps.get_model("workflow_engine", "DetectorWorkflow")
88+
WorkflowDataConditionGroup = apps.get_model("workflow_engine", "WorkflowDataConditionGroup")
89+
90+
for rule in RangeQuerySetWrapperWithProgressBarApprox(AlertRule.objects_with_snapshots.all()):
7991
if rule.status in [AlertRuleStatus.DISABLED, AlertRuleStatus.SNAPSHOT]:
8092
continue
81-
# what about the date_added and date_updated fields? can I write to those to preserve history?
82-
# description column will be added to the Detector model, but at the time of writing it isn't there yet. will need to update this
83-
# same for threshold_period, will likely be added to the Detector model but isn't there yet
84-
# AlertRuleActivity creation rows need to be backfilled into the Detector model, needs new created_by and created_at columns
93+
# TODO: need to modify the DefaultFieldsModel auto_now and auto_now_add
94+
# so we can write to the date_added and date_updated fields
95+
96+
snoozed = None
97+
try:
98+
snoozed = RuleSnooze.objects.get(alert_rule_id=rule.id, user_id=None)
99+
except RuleSnooze.DoesNotExist:
100+
pass
101+
102+
enabled = True if snoozed is not None else False
103+
104+
create_activity = AlertRuleActivity.objects.get(
105+
alert_rule_id=rule.id, type=AlertRuleActivityType.CREATED.value
106+
)
107+
alert_rule_project = AlertRuleProjects.objects.get(alert_rule_id=rule.id)
85108

86109
query_subscription = QuerySubscription.objects.get(snuba_query=rule.snuba_query_id)
87110
data_source = DataSource.update_or_create(
@@ -97,15 +120,21 @@ def migrate_metric_alerts(apps: Apps, schema_editor: BaseDatabaseSchemaEditor) -
97120
name=rule.name,
98121
organization_id=rule.organization_id,
99122
when_condition_group=data_condition_group.id,
123+
enabled=enabled,
124+
created_by_id=create_activity.user_id,
100125
)
101126
detector = Detector.update_or_create(
102-
organization_id=rule.organization_id,
127+
project_id=alert_rule_project.project_id,
128+
enabled=enabled,
129+
created_by_id=create_activity.user_id,
103130
name=rule.name,
104131
data_sources=data_source,
105132
workflow_condition_group=data_condition_group.id,
106133
type=MetricAlertFire.slug,
134+
description=rule.description,
107135
owner_user_id=rule.user_id,
108136
owner_team=rule.team,
137+
config={"threshold_type": rule.threshold_type}, # schema to come
109138
)
110139

111140
# state is based on incident status
@@ -194,7 +223,7 @@ class Migration(CheckedMigration):
194223
is_post_deployment = True
195224

196225
dependencies = [
197-
("workflow_engine", "0011_action_updates"),
226+
("workflow_engine", "0014_model_additions_for_milestones"),
198227
]
199228

200229
operations = [

0 commit comments

Comments
 (0)