Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion migrations_lockfile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ hybridcloud: 0024_add_project_distribution_scope

insights: 0002_backfill_team_starred

monitors: 0010_delete_orphaned_detectors
monitors: 0011_backfill_monitor_environment_is_muted

nodestore: 0001_squashed_0002_nodestore_no_dictfield

Expand Down
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)


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"]},
),
]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Any

import pytest
from django.conf import settings

from sentry.monitors.processing_errors.errors import ProcessingErrorType
Expand All @@ -18,6 +19,7 @@ def _get_cluster() -> Any:
return redis.redis_clusters.get(settings.SENTRY_MONITORS_REDIS_CLUSTER)


@pytest.mark.skip(reason="migration test no longer needed")
class FixProcessingErrorKeysTest(TestMigrations):
migrate_from = "0007_monitors_json_field"
migrate_to = "0008_fix_processing_error_keys"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import pytest

from sentry.monitors.types import DATA_SOURCE_CRON_MONITOR
from sentry.monitors.utils import ensure_cron_detector, get_detector_for_monitor
from sentry.testutils.cases import TestMigrations
from sentry.workflow_engine.models import DataSource, DataSourceDetector, Detector


@pytest.mark.skip(reason="migration test no longer needed")
class DeleteOrphanedDetectorsTest(TestMigrations):
migrate_from = "0009_backfill_monitor_detectors"
migrate_to = "0010_delete_orphaned_detectors"
Expand Down
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import pytest

from sentry.testutils.cases import TestMigrations


@pytest.mark.skip(reason="migration test no longer needed")
class RenameErrorDetectorsTest(TestMigrations):
app = "workflow_engine"
migrate_from = "0068_migrate_anomaly_detection_alerts"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from sentry.testutils.cases import TestMigrations


@pytest.mark.skip
@pytest.mark.skip(reason="migration test no longer needed")
class UpdateMetricDetectorConfigFieldsTest(TestMigrations):
migrate_from = "0079_add_unique_constraint_to_detector_group"
migrate_to = "0080_update_metric_detector_config_fields"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from sentry.models.rule import Rule, RuleSource
from sentry.testutils.cases import TestMigrations
from sentry.workflow_engine.migration_helpers.issue_alert_migration import IssueAlertMigrator
Expand All @@ -9,6 +11,7 @@
)


@pytest.mark.skip(reason="migration test no longer needed")
class FixCronToCronWorkflowLinksTest(TestMigrations):
migrate_from = "0085_crons_link_detectors_to_all_workflows"
migrate_to = "0086_fix_cron_to_cron_workflow_links"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from sentry.testutils.cases import TestMigrations
from sentry.workflow_engine.models import (
Action,
Expand All @@ -10,6 +12,7 @@
)


@pytest.mark.skip(reason="migration test no longer needed")
class TestUpdateCronWorkflowNames(TestMigrations):
migrate_from = "0088_remove_monitor_slug_conditions"
migrate_to = "0089_update_cron_workflow_names"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from sentry.testutils.cases import TestMigrations
from sentry.workflow_engine.models import (
Action,
Expand All @@ -10,6 +12,7 @@
)


@pytest.mark.skip(reason="migration test no longer needed")
class TestFixEmailNotificationNames(TestMigrations):
migrate_from = "0090_add_detectorgroup_detector_date_index"
migrate_to = "0091_fix_email_notification_names"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import pytest

from sentry.testutils.cases import TestMigrations
from sentry.workflow_engine.models import Detector, DetectorWorkflow


@pytest.mark.skip(reason="migration test no longer needed")
class TestBackfillIssueStreamDetectorWorkflows(TestMigrations):
migrate_from = "0093_add_action_config_index"
migrate_to = "0094_backfill_issue_stream_detector_workflows"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import pytest

from sentry.testutils.cases import TestMigrations
from sentry.workflow_engine.models import Detector, Workflow, WorkflowFireHistory


@pytest.mark.skip(reason="migration test no longer needed")
class DeleteNonSingleWrittenFireHistoryTest(TestMigrations):
migrate_from = "0095_unique_detectorgroup_group"
migrate_to = "0096_delete_non_single_written_fire_history"
Expand Down
Loading