Repository: getsentry/sentry

Issue:
Create a new table for rule muting and snoozing
Create a table to store which alert rules are muted / snoozed by which user. See the [tech spec](https://www.notion.so/sentry/Mute-Alerts-Tech-Spec-53a968fbb82b4312bdd4b48ad1b54353?pvs=4#a214ffea3f8a406fbc704fb6a2e5f013) for which columns to include, and read the notes. Ensure it's set up with foreign keys to relevant tables for automatic cleanup. Naming might be the hardest part here.


Discussion:

Resolving patch:
diff --git a/src/sentry/migrations/0401_create_rulesnooze_table.py b/src/sentry/migrations/0401_create_rulesnooze_table.py
new file mode 100644
--- /dev/null
+++ b/src/sentry/migrations/0401_create_rulesnooze_table.py
@@ -0,0 +1,80 @@
+# Generated by Django 2.2.28 on 2023-04-04 19:02
+
+import django.db.models.deletion
+from django.db import migrations, models
+
+import sentry.db.models.fields.bounded
+import sentry.db.models.fields.foreignkey
+import sentry.db.models.fields.hybrid_cloud_foreign_key
+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
+
+    dependencies = [
+        ("sentry", "0400_add_unique_constraint_indexer"),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name="RuleSnooze",
+            fields=[
+                (
+                    "id",
+                    sentry.db.models.fields.bounded.BoundedBigAutoField(
+                        primary_key=True, serialize=False
+                    ),
+                ),
+                (
+                    "user_id",
+                    sentry.db.models.fields.hybrid_cloud_foreign_key.HybridCloudForeignKey(
+                        "sentry.User", db_index=True, on_delete="CASCADE"
+                    ),
+                ),
+                ("until", models.DateTimeField(db_index=True, null=True)),
+                (
+                    "alert_rule",
+                    sentry.db.models.fields.foreignkey.FlexibleForeignKey(
+                        null=True,
+                        on_delete=django.db.models.deletion.CASCADE,
+                        to="sentry.AlertRule",
+                    ),
+                ),
+                (
+                    "rule",
+                    sentry.db.models.fields.foreignkey.FlexibleForeignKey(
+                        null=True, on_delete=django.db.models.deletion.CASCADE, to="sentry.Rule"
+                    ),
+                ),
+            ],
+            options={
+                "db_table": "sentry_rulesnooze",
+            },
+        ),
+        migrations.AddConstraint(
+            model_name="rulesnooze",
+            constraint=models.CheckConstraint(
+                check=models.Q(
+                    models.Q(("alert_rule__isnull", True), ("rule__isnull", False)),
+                    models.Q(("alert_rule__isnull", False), ("rule__isnull", True)),
+                    _connector="OR",
+                ),
+                name="rule_or_alert_rule",
+            ),
+        ),
+        migrations.AlterUniqueTogether(
+            name="rulesnooze",
+            unique_together={("user_id", "alert_rule"), ("user_id", "rule")},
+        ),
+    ]
diff --git a/src/sentry/models/__init__.py b/src/sentry/models/__init__.py
--- a/src/sentry/models/__init__.py
+++ b/src/sentry/models/__init__.py
@@ -98,6 +98,7 @@
 from .reprocessingreport import *  # NOQA
 from .rule import *  # NOQA
 from .rulefirehistory import RuleFireHistory  # NOQA
+from .rulesnooze import RuleSnooze  # NOQA
 from .savedsearch import *  # NOQA
 from .scheduledeletion import *  # NOQA
 from .scheduledjob import *  # NOQA
diff --git a/src/sentry/models/rulesnooze.py b/src/sentry/models/rulesnooze.py
new file mode 100644
--- /dev/null
+++ b/src/sentry/models/rulesnooze.py
@@ -0,0 +1,33 @@
+from django.db import models
+from django.db.models import CheckConstraint, Q
+
+from sentry.db.models import FlexibleForeignKey, Model, region_silo_only_model, sane_repr
+from sentry.db.models.fields.hybrid_cloud_foreign_key import HybridCloudForeignKey
+
+
+@region_silo_only_model
+class RuleSnooze(Model):
+    """
+    Duration an issue alert or metric alert is snoozed for a user. Null `until` value means snoozed forever.
+    """
+
+    __include_in_export__ = True
+
+    user_id = HybridCloudForeignKey("sentry.User", on_delete="CASCADE")
+    rule = FlexibleForeignKey("sentry.Rule", null=True)
+    alert_rule = FlexibleForeignKey("sentry.AlertRule", null=True)
+    until = models.DateTimeField(null=True, db_index=True)
+
+    class Meta:
+        db_table = "sentry_rulesnooze"
+        app_label = "sentry"
+        unique_together = (("user_id", "rule"), ("user_id", "alert_rule"))
+        constraints = [
+            CheckConstraint(
+                check=Q(rule__isnull=False, alert_rule__isnull=True)
+                | Q(rule__isnull=True, alert_rule__isnull=False),
+                name="rule_or_alert_rule",
+            )
+        ]
+
+    __repr__ = sane_repr("user_id", "rule_id", "alert_rule_id", "until")


Test patch:
diff --git a/tests/sentry/deletions/test_project.py b/tests/sentry/deletions/test_project.py
--- a/tests/sentry/deletions/test_project.py
+++ b/tests/sentry/deletions/test_project.py
@@ -17,6 +17,7 @@
     Release,
     ReleaseCommit,
     Repository,
+    RuleSnooze,
     ScheduledDeletion,
     ServiceHook,
 )
@@ -82,6 +83,7 @@ def test_simple(self):
         metric_alert_rule = self.create_alert_rule(
             organization=project.organization, projects=[project]
         )
+        rule_snooze = RuleSnooze.objects.create(user_id=self.user.id, alert_rule=metric_alert_rule)
 
         deletion = ScheduledDeletion.schedule(project, days=0)
         deletion.update(in_progress=True)
@@ -103,6 +105,7 @@ def test_simple(self):
         assert not File.objects.filter(id=file.id).exists()
         assert not ServiceHook.objects.filter(id=hook.id).exists()
         assert not AlertRule.objects.filter(id=metric_alert_rule.id).exists()
+        assert not RuleSnooze.objects.filter(id=rule_snooze.id).exists()
 
     def test_delete_error_events(self):
         keeper = self.create_project(name="keeper")