Skip to content

Commit

Permalink
update migration since other changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ceorourke committed Nov 25, 2024
1 parent d4d626c commit 84e010e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
2 changes: 1 addition & 1 deletion migrations_lockfile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ social_auth: 0002_default_auto_field

uptime: 0018_add_trace_sampling_field_to_uptime

workflow_engine: 0014_model_additions_for_milestones
workflow_engine: 0015_migrate_alert_rules
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.1.1 on 2024-11-08 23:16
# Generated by Django 5.1.1 on 2024-11-25 20:59

from enum import Enum, IntEnum

Expand Down Expand Up @@ -54,34 +54,57 @@ class AlertRuleStatus(Enum):
NOT_ENOUGH_DATA = 6


class AlertRuleActivityType(Enum):
CREATED = 1
DELETED = 2
UPDATED = 3
ENABLED = 4
DISABLED = 5
SNAPSHOT = 6
ACTIVATED = 7
DEACTIVATED = 8


def migrate_metric_alerts(apps: Apps, schema_editor: BaseDatabaseSchemaEditor) -> None:
AlertRule = apps.get_model("sentry", "AlertRule")
AlertRuleProjects = apps.get_model("sentry", "AlertRuleProjects")
AlertRuleTrigger = apps.get_model("sentry", "AlertRuleTrigger")
AlertRuleTriggerAction = apps.get_model("sentry", "AlertRuleTriggerAction")
# AlertRuleActivity = apps.get_model("sentry", "AlertRuleActivity")
AlertRuleActivity = apps.get_model("sentry", "AlertRuleActivity")
RuleSnooze = apps.get_model("sentry", "RuleSnooze")
QuerySubscription = apps.get_model("sentry", "QuerySubscription")
Incident = apps.get_model("sentry", "Incident")

DataSource = apps.get_model("sentry", "DataSource")
DataConditionGroup = apps.get_model("sentry", "DataConditionGroup")
Workflow = apps.get_model("sentry", "Workflow")
Detector = apps.get_model("sentry", "Detector")
DetectorState = apps.get_model("sentry", "DetectorState")
DataCondition = apps.get_model("sentry", "DataCondition")
Action = apps.get_model("sentry", "Action")
DataConditionGroupAction = apps.get_model("sentry", "DataConditionGroupAction")
DataSourceDetector = apps.get_model("sentry", "DataSourceDetector")
DetectorWorkflow = apps.get_model("sentry", "DetectorWorkflow")
WorkflowDataConditionGroup = apps.get_model("sentry", "WorkflowDataConditionGroup")

for rule in RangeQuerySetWrapperWithProgressBarApprox(AlertRule.objects.all()):
DataSource = apps.get_model("workflow_engine", "DataSource")
DataConditionGroup = apps.get_model("workflow_engine", "DataConditionGroup")
Workflow = apps.get_model("workflow_engine", "Workflow")
Detector = apps.get_model("workflow_engine", "Detector")
DetectorState = apps.get_model("workflow_engine", "DetectorState")
DataCondition = apps.get_model("workflow_engine", "DataCondition")
Action = apps.get_model("workflow_engine", "Action")
DataConditionGroupAction = apps.get_model("workflow_engine", "DataConditionGroupAction")
DataSourceDetector = apps.get_model("workflow_engine", "DataSourceDetector")
DetectorWorkflow = apps.get_model("workflow_engine", "DetectorWorkflow")
WorkflowDataConditionGroup = apps.get_model("workflow_engine", "WorkflowDataConditionGroup")

for rule in RangeQuerySetWrapperWithProgressBarApprox(AlertRule.objects_with_snapshots.all()):
if rule.status in [AlertRuleStatus.DISABLED, AlertRuleStatus.SNAPSHOT]:
continue
# what about the date_added and date_updated fields? can I write to those to preserve history?
# 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
# same for threshold_period, will likely be added to the Detector model but isn't there yet
# AlertRuleActivity creation rows need to be backfilled into the Detector model, needs new created_by and created_at columns
# TODO: need to modify the DefaultFieldsModel auto_now and auto_now_add
# so we can write to the date_added and date_updated fields

snoozed = None
try:
snoozed = RuleSnooze.objects.get(alert_rule_id=rule.id, user_id=None)
except RuleSnooze.DoesNotExist:
pass

enabled = True if snoozed is not None else False

create_activity = AlertRuleActivity.objects.get(
alert_rule_id=rule.id, type=AlertRuleActivityType.CREATED.value
)
alert_rule_project = AlertRuleProjects.objects.get(alert_rule_id=rule.id)

query_subscription = QuerySubscription.objects.get(snuba_query=rule.snuba_query_id)
data_source = DataSource.update_or_create(
Expand All @@ -97,15 +120,21 @@ def migrate_metric_alerts(apps: Apps, schema_editor: BaseDatabaseSchemaEditor) -
name=rule.name,
organization_id=rule.organization_id,
when_condition_group=data_condition_group.id,
enabled=enabled,
created_by_id=create_activity.user_id,
)
detector = Detector.update_or_create(
organization_id=rule.organization_id,
project_id=alert_rule_project.project_id,
enabled=enabled,
created_by_id=create_activity.user_id,
name=rule.name,
data_sources=data_source,
workflow_condition_group=data_condition_group.id,
type=MetricAlertFire.slug,
description=rule.description,
owner_user_id=rule.user_id,
owner_team=rule.team,
config={"threshold_type": rule.threshold_type}, # schema to come
)

# state is based on incident status
Expand Down Expand Up @@ -194,7 +223,7 @@ class Migration(CheckedMigration):
is_post_deployment = True

dependencies = [
("workflow_engine", "0011_action_updates"),
("workflow_engine", "0014_model_additions_for_milestones"),
]

operations = [
Expand Down

0 comments on commit 84e010e

Please sign in to comment.