Browse Source

feat(issue_alert_status): Add RuleFireHistory to cleanup.py and group deletions (#32267)

Adding cleanup tasks for `RuleFireHistory`. This makes sure we delete rows after 90 days and
properly handle them when we delete a `Group`.
Dan Fuller 3 years ago
parent
commit
9ced05b0cf

+ 1 - 1
migrations_lockfile.txt

@@ -6,5 +6,5 @@ To resolve this, rebase against latest master and regenerate your migration. Thi
 will then be regenerated, and you should be able to merge without conflicts.
 
 nodestore: 0002_nodestore_no_dictfield
-sentry: 0275_rule_fire_history
+sentry: 0276_rulefirehistory_date_added_index
 social_auth: 0001_initial

+ 1 - 0
src/sentry/deletions/defaults/group.py

@@ -29,6 +29,7 @@ DIRECT_GROUP_RELATED_MODELS = (
     models.GroupEmailThread,
     models.GroupSubscription,
     models.GroupHistory,
+    models.RuleFireHistory,
 )
 
 _GROUP_RELATED_MODELS = DIRECT_GROUP_RELATED_MODELS + (

+ 37 - 0
src/sentry/migrations/0276_rulefirehistory_date_added_index.py

@@ -0,0 +1,37 @@
+# Generated by Django 2.2.24 on 2022-03-03 18:45
+
+import django.utils.timezone
+from django.db import migrations, models
+
+from sentry.new_migrations.migrations import CheckedMigration
+
+
+class Migration(CheckedMigration):
+    # This flag is used to mark that a migration shouldn't be automatically run in production. For
+    # the most part, this should only be used for operations where it's safe to run the migration
+    # after your code has deployed. So this should not be used for most operations that alter the
+    # schema of a table.
+    # Here are some things that make sense to mark as dangerous:
+    # - Large data migrations. Typically we want these to be run manually by ops so that they can
+    #   be monitored and not block the deploy for a long period of time while they run.
+    # - Adding indexes to large tables. Since this can take a long time, we'd generally prefer to
+    #   have ops run this and not block the deploy. Note that while adding an index is a schema
+    #   change, it's completely safe to run the operation after the code has deployed.
+    is_dangerous = False
+
+    # This flag is used to decide whether to run this migration in a transaction or not. Generally
+    # we don't want to run in a transaction here, since for long running operations like data
+    # back-fills this results in us locking an increasing number of rows until we finally commit.
+    atomic = False
+
+    dependencies = [
+        ("sentry", "0275_rule_fire_history"),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name="rulefirehistory",
+            name="date_added",
+            field=models.DateTimeField(db_index=True, default=django.utils.timezone.now),
+        ),
+    ]

+ 1 - 1
src/sentry/models/rulefirehistory.py

@@ -10,7 +10,7 @@ class RuleFireHistory(Model):  # type: ignore
     project = FlexibleForeignKey("sentry.Project", db_constraint=False)
     rule = FlexibleForeignKey("sentry.Rule")
     group = FlexibleForeignKey("sentry.Group", db_constraint=False)
-    date_added = DateTimeField(default=timezone.now)
+    date_added = DateTimeField(default=timezone.now, db_index=True)
 
     class Meta:
         db_table = "sentry_rulefirehistory"

+ 1 - 0
src/sentry/runner/commands/cleanup.py

@@ -177,6 +177,7 @@ def cleanup(days, project, concurrency, silent, model, router, timed):
             (models.UserReport, "date_added", None),
             (models.GroupEmailThread, "date", None),
             (models.GroupRuleStatus, "date_added", None),
+            (models.RuleFireHistory, "date_added", None),
         ] + EXTRA_BULK_QUERY_DELETES
 
         # Deletions that use the `deletions` code path (which handles their child relations)