-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
ref(crons): Add migration to backfill MonitorEnvironment.is_muted #103324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
evanpurkhiser
wants to merge
1
commit into
master
Choose a base branch
from
evanpurkhiser/ref-crons-add-migration-to-backfill-monitorenvironment-is-muted
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/sentry/monitors/migrations/0011_backfill_monitor_environment_is_muted.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # Generated by Django 5.2.1 on 2025-11-13 19:27 | ||
|
|
||
| from django.db import migrations | ||
| from django.db.backends.base.schema import BaseDatabaseSchemaEditor | ||
| from django.db.migrations.state import StateApps | ||
|
|
||
| from sentry.new_migrations.migrations import CheckedMigration | ||
| from sentry.utils.query import RangeQuerySetWrapperWithProgressBar | ||
|
|
||
|
|
||
| def backfill_monitor_environment_is_muted( | ||
| apps: StateApps, schema_editor: BaseDatabaseSchemaEditor | ||
| ) -> None: | ||
| """ | ||
| Backfill MonitorEnvironment.is_muted from Monitor.is_muted for all muted monitors. | ||
| After this migration: | ||
| - is_muted=True monitors will have ALL environments muted | ||
| - is_muted=False monitors will have environments unchanged (some, none, or all unmuted) | ||
| This gets us into the correct state for the dual-write implementation where: | ||
| - Monitor is muted if and only if ALL environments are muted | ||
| - Monitor is unmuted if ANY environment is unmuted | ||
| """ | ||
| Monitor = apps.get_model("monitors", "Monitor") | ||
| MonitorEnvironment = apps.get_model("monitors", "MonitorEnvironment") | ||
|
|
||
| # Filter is_muted in memory to avoid query timeouts (no composite index on id, is_muted) | ||
| all_monitors = Monitor.objects.all() | ||
|
|
||
| for monitor in RangeQuerySetWrapperWithProgressBar(all_monitors): | ||
| if monitor.is_muted: | ||
| MonitorEnvironment.objects.filter(monitor=monitor).update(is_muted=True) | ||
evanpurkhiser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| 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 = True | ||
|
|
||
| dependencies = [ | ||
| ("monitors", "0010_delete_orphaned_detectors"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython( | ||
| backfill_monitor_environment_is_muted, | ||
| migrations.RunPython.noop, | ||
| hints={"tables": ["monitors_monitor", "monitors_monitorenvironment"]}, | ||
| ), | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
tests/sentry/monitors/migrations/test_0010_delete_orphaned_detectors.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
tests/sentry/monitors/migrations/test_0011_backfill_monitor_environment_is_muted.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| from sentry.testutils.cases import TestMigrations | ||
|
|
||
|
|
||
| class BackfillMonitorEnvironmentIsMutedTest(TestMigrations): | ||
| migrate_from = "0010_delete_orphaned_detectors" | ||
| migrate_to = "0011_backfill_monitor_environment_is_muted" | ||
| app = "monitors" | ||
| connection = "secondary" | ||
|
|
||
| def setup_initial_state(self) -> None: | ||
| # Create muted monitor with environments | ||
| self.muted_monitor = self.create_monitor(name="Muted Monitor", is_muted=True) | ||
| self.muted_env1 = self.create_monitor_environment( | ||
| monitor=self.muted_monitor, | ||
| environment_id=self.environment.id, | ||
| is_muted=False, # Initially not muted | ||
| ) | ||
| env2 = self.create_environment(name="production", project=self.project) | ||
| self.muted_env2 = self.create_monitor_environment( | ||
| monitor=self.muted_monitor, | ||
| environment_id=env2.id, | ||
| is_muted=False, # Initially not muted | ||
| ) | ||
|
|
||
| # Create unmuted monitor with environments | ||
| self.unmuted_monitor = self.create_monitor(name="Unmuted Monitor", is_muted=False) | ||
| self.unmuted_env1 = self.create_monitor_environment( | ||
| monitor=self.unmuted_monitor, | ||
| environment_id=self.environment.id, | ||
| is_muted=False, | ||
| ) | ||
| env3 = self.create_environment(name="staging", project=self.project) | ||
| self.unmuted_env2 = self.create_monitor_environment( | ||
| monitor=self.unmuted_monitor, | ||
| environment_id=env3.id, | ||
| is_muted=False, | ||
| ) | ||
|
|
||
| # Create muted monitor without environments | ||
| self.muted_monitor_no_envs = self.create_monitor( | ||
| name="Muted Monitor No Envs", is_muted=True | ||
| ) | ||
|
|
||
| # Verify initial state | ||
| assert self.muted_monitor.is_muted is True | ||
| assert self.muted_env1.is_muted is False | ||
| assert self.muted_env2.is_muted is False | ||
| assert self.unmuted_monitor.is_muted is False | ||
| assert self.unmuted_env1.is_muted is False | ||
| assert self.unmuted_env2.is_muted is False | ||
|
|
||
| def test(self) -> None: | ||
| # Refresh from DB to get updated state after migration | ||
| self.muted_monitor.refresh_from_db() | ||
| self.muted_env1.refresh_from_db() | ||
| self.muted_env2.refresh_from_db() | ||
| self.unmuted_monitor.refresh_from_db() | ||
| self.unmuted_env1.refresh_from_db() | ||
| self.unmuted_env2.refresh_from_db() | ||
| self.muted_monitor_no_envs.refresh_from_db() | ||
|
|
||
| # Verify muted monitor has all environments muted | ||
| assert self.muted_monitor.is_muted is True | ||
| assert self.muted_env1.is_muted is True | ||
| assert self.muted_env2.is_muted is True | ||
|
|
||
| # Verify unmuted monitor environments remain unchanged | ||
| assert self.unmuted_monitor.is_muted is False | ||
| assert self.unmuted_env1.is_muted is False | ||
| assert self.unmuted_env2.is_muted is False | ||
|
|
||
| # Verify muted monitor without environments still muted | ||
| assert self.muted_monitor_no_envs.is_muted is True |
3 changes: 3 additions & 0 deletions
3
tests/sentry/workflow_engine/migrations/test_0069_rename_error_detectors.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...s/sentry/workflow_engine/migrations/test_0094_backfill_issue_stream_detector_workflows.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
tests/sentry/workflow_engine/migrations/test_0096_delete_non_single_written_fire_history.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.