From ffb3dac3d3eb1cbe1cc686451598bf3e8238bde4 Mon Sep 17 00:00:00 2001 From: Colleen O'Rourke Date: Mon, 25 Nov 2024 12:11:22 -0800 Subject: [PATCH 1/4] Prep to remove excluded projects trigger exclusion --- migrations_lockfile.txt | 2 +- src/sentry/incidents/models/alert_rule.py | 40 ------------------- .../0796_rm_excluded_projects_triggers.py | 36 +++++++++++++++++ src/sentry/testutils/helpers/backups.py | 11 +---- 4 files changed, 38 insertions(+), 51 deletions(-) create mode 100644 src/sentry/migrations/0796_rm_excluded_projects_triggers.py diff --git a/migrations_lockfile.txt b/migrations_lockfile.txt index 0cc7c1a7896037..b7e184bb9ee00d 100644 --- a/migrations_lockfile.txt +++ b/migrations_lockfile.txt @@ -15,7 +15,7 @@ remote_subscriptions: 0003_drop_remote_subscription replays: 0004_index_together -sentry: 0795_drop_included_excluded_projects +sentry: 0796_rm_excluded_projects_triggers social_auth: 0002_default_auto_field diff --git a/src/sentry/incidents/models/alert_rule.py b/src/sentry/incidents/models/alert_rule.py index ca213c53e9cfb0..75eb7696b6c8f6 100644 --- a/src/sentry/incidents/models/alert_rule.py +++ b/src/sentry/incidents/models/alert_rule.py @@ -241,26 +241,6 @@ def conditionally_subscribe_project_to_alert_rules( return [] -@region_silo_model -class AlertRuleExcludedProjects(Model): - """ - Excludes a specific project from an AlertRule - - NOTE: This feature is not currently utilized. - """ - - __relocation_scope__ = RelocationScope.Organization - - alert_rule = FlexibleForeignKey("sentry.AlertRule", db_index=False, db_constraint=False) - project = FlexibleForeignKey("sentry.Project", db_constraint=False) - date_added = models.DateTimeField(default=timezone.now) - - class Meta: - app_label = "sentry" - db_table = "sentry_alertruleexcludedprojects" - unique_together = (("alert_rule", "project"),) - - @region_silo_model class AlertRuleProjects(Model): """ @@ -473,26 +453,6 @@ class Meta: unique_together = (("alert_rule", "label"),) -@region_silo_model -class AlertRuleTriggerExclusion(Model): - """ - Allows us to define a specific trigger to be excluded from a query subscription - """ - - __relocation_scope__ = RelocationScope.Organization - - alert_rule_trigger = FlexibleForeignKey( - "sentry.AlertRuleTrigger", related_name="exclusions", db_constraint=False - ) - query_subscription = FlexibleForeignKey("sentry.QuerySubscription", db_constraint=False) - date_added = models.DateTimeField(default=timezone.now) - - class Meta: - app_label = "sentry" - db_table = "sentry_alertruletriggerexclusion" - unique_together = (("alert_rule_trigger", "query_subscription"),) - - class AlertRuleTriggerActionMethod(StrEnum): FIRE = "fire" RESOLVE = "resolve" diff --git a/src/sentry/migrations/0796_rm_excluded_projects_triggers.py b/src/sentry/migrations/0796_rm_excluded_projects_triggers.py new file mode 100644 index 00000000000000..710cea5d0cc5f7 --- /dev/null +++ b/src/sentry/migrations/0796_rm_excluded_projects_triggers.py @@ -0,0 +1,36 @@ +# Generated by Django 5.1.1 on 2024-11-25 20:06 + +from sentry.new_migrations.migrations import CheckedMigration +from sentry.new_migrations.monkey.models import SafeDeleteModel +from sentry.new_migrations.monkey.state import DeletionAction + + +class Migration(CheckedMigration): + # This flag is used to mark that a migration shouldn't be automatically run in production. + # 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 post deployment: + # - Large data migrations. Typically we want these to be run manually 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 + # run this outside deployments so that we don't block them. Note that while adding an index + # is a schema change, it's completely safe to run the operation after the code has deployed. + # Once deployed, run these manually via: https://develop.sentry.dev/database-migrations/#migration-deployment + + is_post_deployment = False + + dependencies = [ + ("sentry", "0795_drop_included_excluded_projects"), + ] + + operations = [ + SafeDeleteModel( + name="AlertRuleExcludedProjects", + deletion_action=DeletionAction.MOVE_TO_PENDING, + ), + SafeDeleteModel( + name="AlertRuleTriggerExclusion", + deletion_action=DeletionAction.MOVE_TO_PENDING, + ), + ] diff --git a/src/sentry/testutils/helpers/backups.py b/src/sentry/testutils/helpers/backups.py index a5912a71043342..9261c144b304ab 100644 --- a/src/sentry/testutils/helpers/backups.py +++ b/src/sentry/testutils/helpers/backups.py @@ -44,11 +44,7 @@ from sentry.backup.validate import validate from sentry.data_secrecy.models import DataSecrecyWaiver from sentry.db.models.paranoia import ParanoidModel -from sentry.incidents.models.alert_rule import ( - AlertRuleExcludedProjects, - AlertRuleMonitorTypeInt, - AlertRuleTriggerExclusion, -) +from sentry.incidents.models.alert_rule import AlertRuleMonitorTypeInt from sentry.incidents.models.incident import ( IncidentActivity, IncidentSnapshot, @@ -511,20 +507,15 @@ def create_exhaustive_organization( ) # AlertRule* - other_project = self.create_project(name=f"other-project-{slug}", teams=[team]) alert = self.create_alert_rule( organization=org, projects=[project], user=owner, ) - AlertRuleExcludedProjects.objects.create(alert_rule=alert, project=other_project) alert.user_id = owner_id alert.save() trigger = self.create_alert_rule_trigger(alert_rule=alert) assert alert.snuba_query is not None - AlertRuleTriggerExclusion.objects.create( - alert_rule_trigger=trigger, query_subscription=alert.snuba_query.subscriptions.get() - ) self.create_alert_rule_trigger_action(alert_rule_trigger=trigger) activated_alert = self.create_alert_rule( organization=org, From e6c62c1ebeacec3b636a3a7ee05a28bf7be1c97d Mon Sep 17 00:00:00 2001 From: Colleen O'Rourke Date: Mon, 25 Nov 2024 13:19:36 -0800 Subject: [PATCH 2/4] update tests --- tests/sentry/backup/test_imports.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/sentry/backup/test_imports.py b/tests/sentry/backup/test_imports.py index 429ef600d77eb4..7d4e8c065676c9 100644 --- a/tests/sentry/backup/test_imports.py +++ b/tests/sentry/backup/test_imports.py @@ -687,13 +687,12 @@ def test_import_signaling_organization(self): assert OrganizationMember.objects.count() == 3 - # The exhaustive org has 2 projects which automatically get 1 key and 3 options each. - assert Project.objects.count() == 2 + # The exhaustive org has 1 project which automatically gets 1 key and 3 options. + assert Project.objects.count() == 1 assert Project.objects.filter(name="project-some-org").exists() - assert Project.objects.filter(name="other-project-some-org").exists() - assert ProjectKey.objects.count() == 2 - assert ProjectOption.objects.count() == 2 + assert ProjectKey.objects.count() == 1 + assert ProjectOption.objects.count() == 1 assert ProjectOption.objects.filter(key="sentry:option-epoch").exists() with assume_test_silo_mode(SiloMode.CONTROL): @@ -1514,7 +1513,7 @@ def test_colliding_project_key(self, expected_models: list[type[Model]]): with open(tmp_path, "rb") as tmp_file: import_in_organization_scope(tmp_file, printer=NOOP_PRINTER) - assert ProjectKey.objects.count() == 4 + assert ProjectKey.objects.count() == 3 assert ProjectKey.objects.filter(public_key=colliding.public_key).count() == 1 assert ProjectKey.objects.filter(secret_key=colliding.secret_key).count() == 1 From d701ebfbc2701c6cb3a67ac87b2f866d63c3f695 Mon Sep 17 00:00:00 2001 From: Colleen O'Rourke Date: Mon, 25 Nov 2024 13:51:59 -0800 Subject: [PATCH 3/4] update snapshots --- .../test_default_comparators.pysnap | 14 +------------- .../PreprocessingTransferTest/test_success.pysnap | 4 ++-- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/tests/sentry/backup/snapshots/test_comparators/test_default_comparators.pysnap b/tests/sentry/backup/snapshots/test_comparators/test_default_comparators.pysnap index 2bc307a10ccbde..9d2f1cfd632c27 100644 --- a/tests/sentry/backup/snapshots/test_comparators/test_default_comparators.pysnap +++ b/tests/sentry/backup/snapshots/test_comparators/test_default_comparators.pysnap @@ -1,5 +1,5 @@ --- -created: '2024-11-25T20:44:29.207064+00:00' +created: '2024-11-25T21:49:40.423739+00:00' creator: sentry source: tests/sentry/backup/test_comparators.py --- @@ -120,12 +120,6 @@ source: tests/sentry/backup/test_comparators.py - previous_alert_rule - user_id model_name: sentry.alertruleactivity -- comparators: - - class: ForeignKeyComparator - fields: - - alert_rule - - project - model_name: sentry.alertruleexcludedprojects - comparators: - class: ForeignKeyComparator fields: @@ -144,12 +138,6 @@ source: tests/sentry/backup/test_comparators.py - integration_id - sentry_app_id model_name: sentry.alertruletriggeraction -- comparators: - - class: ForeignKeyComparator - fields: - - alert_rule_trigger - - query_subscription - model_name: sentry.alertruletriggerexclusion - comparators: - class: HashObfuscatingComparator fields: diff --git a/tests/sentry/tasks/snapshots/PreprocessingTransferTest/test_success.pysnap b/tests/sentry/tasks/snapshots/PreprocessingTransferTest/test_success.pysnap index 973f822b2df6a8..a28377cf4694b8 100644 --- a/tests/sentry/tasks/snapshots/PreprocessingTransferTest/test_success.pysnap +++ b/tests/sentry/tasks/snapshots/PreprocessingTransferTest/test_success.pysnap @@ -1,5 +1,5 @@ --- -created: '2024-11-15T23:09:46.023372+00:00' +created: '2024-11-25T21:50:50.814552+00:00' creator: sentry source: tests/sentry/tasks/test_relocation.py --- @@ -96,7 +96,7 @@ steps: - -U - postgres - -c - - TRUNCATE sentry_controloption,sentry_integration,sentry_option,sentry_organization,sentry_organizationintegration,sentry_organizationoptions,sentry_projecttemplate,sentry_projecttemplateoption,sentry_relay,sentry_relayusage,sentry_repository,sentry_team,auth_user,sentry_userip,sentry_userpermission,sentry_userrole,sentry_userrole_users,workflow_engine_dataconditiongroup,workflow_engine_datasource,workflow_engine_datacondition,sentry_savedsearch,sentry_recentsearch,sentry_project,sentry_orgauthtoken,sentry_organizationmember,sentry_organizationaccessrequest,sentry_monitor,sentry_groupsearchview,sentry_environment,sentry_email,sentry_datasecrecywaiver,sentry_dashboardtombstone,sentry_dashboard,sentry_customdynamicsamplingrule,sentry_projectcounter,sentry_authprovider,sentry_authidentity,auth_authenticator,sentry_apikey,sentry_apiapplication,workflow_engine_workflow,workflow_engine_detector,workflow_engine_datasourcedetector,sentry_useroption,sentry_useremail,sentry_snubaquery,sentry_sentryapp,sentry_rule,sentry_querysubscription,sentry_projectteam,sentry_projectredirect,sentry_projectownership,sentry_projectoptions,sentry_projectkey,sentry_projectintegration,sentry_projectbookmark,sentry_organizationmember_teams,sentry_notificationaction,sentry_neglectedrule,sentry_environmentproject,sentry_dashboardwidget,sentry_dashboardpermissions,sentry_customdynamicsamplingruleproject,sentry_apitoken,sentry_apigrant,sentry_apiauthorization,sentry_alertrule,workflow_engine_workflowdataconditiongroup,workflow_engine_detectorworkflow,sentry_snubaqueryeventtype,sentry_sentryappinstallation,sentry_sentryappcomponent,sentry_rulesnooze,sentry_ruleactivity,sentry_notificationactionproject,sentry_dashboardwidgetquery,sentry_dashboardpermissionsteam,sentry_alertruletrigger,sentry_alertruleprojects,sentry_alertruleexcludedprojects,sentry_alertruleactivity,sentry_alertruleactivationcondition,sentry_servicehook,sentry_incident,sentry_dashboardwidgetqueryondemand,sentry_alertruletriggerexclusion,sentry_alertruletriggeraction,sentry_timeseriessnapshot,sentry_pendingincidentsnapshot,sentry_incidenttrigger,sentry_incidentsubscription,sentry_incidentsnapshot,sentry_incidentactivity + - TRUNCATE sentry_controloption,sentry_integration,sentry_option,sentry_organization,sentry_organizationintegration,sentry_organizationoptions,sentry_projecttemplate,sentry_projecttemplateoption,sentry_relay,sentry_relayusage,sentry_repository,sentry_team,auth_user,sentry_userip,sentry_userpermission,sentry_userrole,sentry_userrole_users,workflow_engine_dataconditiongroup,workflow_engine_datasource,workflow_engine_datacondition,sentry_savedsearch,sentry_recentsearch,sentry_project,sentry_orgauthtoken,sentry_organizationmember,sentry_organizationaccessrequest,sentry_monitor,sentry_groupsearchview,sentry_environment,sentry_email,sentry_datasecrecywaiver,sentry_dashboardtombstone,sentry_dashboard,sentry_customdynamicsamplingrule,sentry_projectcounter,sentry_authprovider,sentry_authidentity,auth_authenticator,sentry_apikey,sentry_apiapplication,workflow_engine_workflow,workflow_engine_detector,workflow_engine_datasourcedetector,sentry_useroption,sentry_useremail,sentry_snubaquery,sentry_sentryapp,sentry_rule,sentry_querysubscription,sentry_projectteam,sentry_projectredirect,sentry_projectownership,sentry_projectoptions,sentry_projectkey,sentry_projectintegration,sentry_projectbookmark,sentry_organizationmember_teams,sentry_notificationaction,sentry_neglectedrule,sentry_environmentproject,sentry_dashboardwidget,sentry_dashboardpermissions,sentry_customdynamicsamplingruleproject,sentry_apitoken,sentry_apigrant,sentry_apiauthorization,sentry_alertrule,workflow_engine_workflowdataconditiongroup,workflow_engine_detectorworkflow,sentry_snubaqueryeventtype,sentry_sentryappinstallation,sentry_sentryappcomponent,sentry_rulesnooze,sentry_ruleactivity,sentry_notificationactionproject,sentry_dashboardwidgetquery,sentry_dashboardpermissionsteam,sentry_alertruletrigger,sentry_alertruleprojects,sentry_alertruleactivity,sentry_alertruleactivationcondition,sentry_servicehook,sentry_incident,sentry_dashboardwidgetqueryondemand,sentry_alertruletriggeraction,sentry_timeseriessnapshot,sentry_pendingincidentsnapshot,sentry_incidenttrigger,sentry_incidentsubscription,sentry_incidentsnapshot,sentry_incidentactivity RESTART IDENTITY CASCADE; id: clear-database name: gcr.io/cloud-builders/docker From 908077a4564b3254b68f5fb7963abf8db0afca43 Mon Sep 17 00:00:00 2001 From: Colleen O'Rourke Date: Mon, 25 Nov 2024 15:05:16 -0800 Subject: [PATCH 4/4] more snapshots and fixture update --- .../backup/model_dependencies/detailed.json | 56 ------------------- fixtures/backup/model_dependencies/flat.json | 8 --- .../backup/model_dependencies/sorted.json | 2 - .../backup/model_dependencies/truncate.json | 2 - .../test_clean_pks.pysnap | 27 +-------- 5 files changed, 1 insertion(+), 94 deletions(-) diff --git a/fixtures/backup/model_dependencies/detailed.json b/fixtures/backup/model_dependencies/detailed.json index 0064b3760112d6..aae54f78099416 100644 --- a/fixtures/backup/model_dependencies/detailed.json +++ b/fixtures/backup/model_dependencies/detailed.json @@ -486,34 +486,6 @@ "table_name": "sentry_alertruleactivity", "uniques": [] }, - "sentry.alertruleexcludedprojects": { - "dangling": false, - "foreign_keys": { - "alert_rule": { - "kind": "FlexibleForeignKey", - "model": "sentry.alertrule", - "nullable": false - }, - "project": { - "kind": "FlexibleForeignKey", - "model": "sentry.project", - "nullable": false - } - }, - "model": "sentry.alertruleexcludedprojects", - "relocation_dependencies": [], - "relocation_scope": "Organization", - "silos": [ - "Region" - ], - "table_name": "sentry_alertruleexcludedprojects", - "uniques": [ - [ - "alert_rule", - "project" - ] - ] - }, "sentry.alertruleprojects": { "dangling": false, "foreign_keys": { @@ -593,34 +565,6 @@ "table_name": "sentry_alertruletriggeraction", "uniques": [] }, - "sentry.alertruletriggerexclusion": { - "dangling": false, - "foreign_keys": { - "alert_rule_trigger": { - "kind": "FlexibleForeignKey", - "model": "sentry.alertruletrigger", - "nullable": false - }, - "query_subscription": { - "kind": "FlexibleForeignKey", - "model": "sentry.querysubscription", - "nullable": false - } - }, - "model": "sentry.alertruletriggerexclusion", - "relocation_dependencies": [], - "relocation_scope": "Organization", - "silos": [ - "Region" - ], - "table_name": "sentry_alertruletriggerexclusion", - "uniques": [ - [ - "alert_rule_trigger", - "query_subscription" - ] - ] - }, "sentry.apiapplication": { "dangling": false, "foreign_keys": { diff --git a/fixtures/backup/model_dependencies/flat.json b/fixtures/backup/model_dependencies/flat.json index 19a7288b784fd6..be2e561c779936 100644 --- a/fixtures/backup/model_dependencies/flat.json +++ b/fixtures/backup/model_dependencies/flat.json @@ -68,10 +68,6 @@ "sentry.alertrule", "sentry.user" ], - "sentry.alertruleexcludedprojects": [ - "sentry.alertrule", - "sentry.project" - ], "sentry.alertruleprojects": [ "sentry.alertrule", "sentry.project" @@ -84,10 +80,6 @@ "sentry.integration", "sentry.sentryapp" ], - "sentry.alertruletriggerexclusion": [ - "sentry.alertruletrigger", - "sentry.querysubscription" - ], "sentry.apiapplication": [ "sentry.user" ], diff --git a/fixtures/backup/model_dependencies/sorted.json b/fixtures/backup/model_dependencies/sorted.json index ed161f7396d898..fc4fbb2c04246f 100644 --- a/fixtures/backup/model_dependencies/sorted.json +++ b/fixtures/backup/model_dependencies/sorted.json @@ -225,7 +225,6 @@ "sentry.dashboardpermissionsteam", "sentry.alertruletrigger", "sentry.alertruleprojects", - "sentry.alertruleexcludedprojects", "sentry.alertruleactivity", "sentry.alertruleactivations", "sentry.alertruleactivationcondition", @@ -234,7 +233,6 @@ "sentry.sentryappinstallationforprovider", "sentry.incident", "sentry.dashboardwidgetqueryondemand", - "sentry.alertruletriggerexclusion", "sentry.alertruletriggeraction", "sentry.timeseriessnapshot", "sentry.servicehookproject", diff --git a/fixtures/backup/model_dependencies/truncate.json b/fixtures/backup/model_dependencies/truncate.json index ca18f7967a33b4..ad922a9a06ffba 100644 --- a/fixtures/backup/model_dependencies/truncate.json +++ b/fixtures/backup/model_dependencies/truncate.json @@ -225,7 +225,6 @@ "sentry_dashboardpermissionsteam", "sentry_alertruletrigger", "sentry_alertruleprojects", - "sentry_alertruleexcludedprojects", "sentry_alertruleactivity", "sentry_alertruleactivations", "sentry_alertruleactivationcondition", @@ -234,7 +233,6 @@ "sentry_sentryappinstallationforprovider", "sentry_incident", "sentry_dashboardwidgetqueryondemand", - "sentry_alertruletriggerexclusion", "sentry_alertruletriggeraction", "sentry_timeseriessnapshot", "sentry_servicehookproject", diff --git a/tests/sentry/backup/snapshots/SanitizationExhaustiveTests/test_clean_pks.pysnap b/tests/sentry/backup/snapshots/SanitizationExhaustiveTests/test_clean_pks.pysnap index 5cfdb6869774b8..9da08496431855 100644 --- a/tests/sentry/backup/snapshots/SanitizationExhaustiveTests/test_clean_pks.pysnap +++ b/tests/sentry/backup/snapshots/SanitizationExhaustiveTests/test_clean_pks.pysnap @@ -1,5 +1,5 @@ --- -created: '2024-11-15T23:09:32.075671+00:00' +created: '2024-11-25T23:04:25.041092+00:00' creator: sentry source: tests/sentry/backup/test_sanitize.py --- @@ -265,12 +265,6 @@ source: tests/sentry/backup/test_sanitize.py - date_added - name - slug -- model_name: sentry.project - ordinal: 4 - sanitized_fields: - - date_added - - name - - slug - model_name: sentry.orgauthtoken ordinal: 1 sanitized_fields: @@ -599,9 +593,6 @@ source: tests/sentry/backup/test_sanitize.py - model_name: sentry.projectteam ordinal: 1 sanitized_fields: [] -- model_name: sentry.projectteam - ordinal: 2 - sanitized_fields: [] - model_name: sentry.projectredirect ordinal: 1 sanitized_fields: @@ -620,9 +611,6 @@ source: tests/sentry/backup/test_sanitize.py - model_name: sentry.projectoption ordinal: 3 sanitized_fields: [] -- model_name: sentry.projectoption - ordinal: 4 - sanitized_fields: [] - model_name: sentry.projectkey ordinal: 1 sanitized_fields: @@ -638,11 +626,6 @@ source: tests/sentry/backup/test_sanitize.py sanitized_fields: - date_added - secret_key -- model_name: sentry.projectkey - ordinal: 4 - sanitized_fields: - - date_added - - secret_key - model_name: sentry.projectintegration ordinal: 1 sanitized_fields: @@ -814,10 +797,6 @@ source: tests/sentry/backup/test_sanitize.py ordinal: 3 sanitized_fields: - date_added -- model_name: sentry.alertruleexcludedprojects - ordinal: 1 - sanitized_fields: - - date_added - model_name: sentry.alertruleactivity ordinal: 1 sanitized_fields: @@ -860,10 +839,6 @@ source: tests/sentry/backup/test_sanitize.py sanitized_fields: - date_added - date_modified -- model_name: sentry.alertruletriggerexclusion - ordinal: 1 - sanitized_fields: - - date_added - model_name: sentry.alertruletriggeraction ordinal: 1 sanitized_fields: