Skip to content

Conversation

@jasonyuezhang
Copy link
Owner

@jasonyuezhang jasonyuezhang commented Nov 19, 2025

While these FKs are supposed to have CASCADE on them according to our models, they are in fact not in the DB. Trying to run this to force them to be CASCADE in production


Copied from getsentry#103698
Original PR: getsentry#103698

Summary by CodeRabbit

  • Bug Fixes
    • Fixed cascade deletion behavior for monitor incident check-in relationships to ensure proper data consistency when monitor check-ins are deleted.

✏️ Tip: You can customize this high-level summary in your review settings.

While these FKs are supposed to have CASCADE on them according to our models, they are in
fact not in the DB. Trying to run this to force them to be CASCADE
@propel-test-bot
Copy link

Add migration to enforce CASCADE delete from MonitorCheckIn to MonitorIncident

This PR introduces a new Django migration that updates two foreign-key fields on the monitors_monitorincident table (starting_checkin and resolving_checkin) so that they use on_delete=CASCADE. The change aligns the actual database schema with the existing model definitions. The accompanying lockfile entry is bumped to reference the new migration.

Key Changes

• Added migration src/sentry/monitors/migrations/0013_fix_monitorincident_checkin_cascade.py which calls migrations.AlterField on starting_checkin and resolving_checkin, setting on_delete=django.db.models.deletion.CASCADE.
• Updated migrations_lockfile.txt to advance the monitors app to 0013_fix_monitorincident_checkin_cascade.

Affected Areas

monitors app database schema
• Migration lock management (migrations_lockfile.txt)

This summary was automatically generated by @propel-code-bot

@coderabbitai
Copy link

coderabbitai bot commented Nov 19, 2025

Walkthrough

Updates the monitors migration lockfile entry from 0012_remove_monitor_is_muted_field to 0013_fix_monitorincident_checkin_cascade and introduces a new Django migration that modifies MonitorIncident foreign key fields (starting_checkin, resolving_checkin) to use FlexibleForeignKey with cascade deletion and explicit nullability.

Changes

Cohort / File(s) Summary
Migration lockfile update
migrations_lockfile.txt
Updated monitors entry reference from 0012 to 0013 migration
New database migration
src/sentry/monitors/migrations/0013_fix_monitorincident_checkin_cascade.py
New CheckedMigration that alters MonitorIncident model's starting_checkin and resolving_checkin fields to use FlexibleForeignKey with cascade deletion, nullability, and reverse relation names

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

  • Verify that the cascade delete behavior on MonitorCheckIn is intentional and won't cause unintended data loss
  • Confirm the reverse relation names ("created_incidents", "resolved_incidents") don't conflict with existing model relationships
  • Ensure the migration is correctly ordered relative to dependent migrations

Poem

Cascade, cascade, down the chain,
Foreign keys now flow like rain,
Null and flexible, they dance with grace,
Incidents find their proper place. 🐰✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: fixing MonitorIncident foreign keys to MonitorCheckIn with CASCADE behavior.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch copy/pr-103698-yuvmen/fix-monitorinciden-checkin-fk-cascade

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0ddf3f6 and feedc76.

📒 Files selected for processing (2)
  • migrations_lockfile.txt (1 hunks)
  • src/sentry/monitors/migrations/0013_fix_monitorincident_checkin_cascade.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/sentry/monitors/migrations/0013_fix_monitorincident_checkin_cascade.py (1)
src/sentry/backup/dependencies.py (1)
  • dependencies (354-567)
🪛 Ruff (0.14.5)
src/sentry/monitors/migrations/0013_fix_monitorincident_checkin_cascade.py

25-27: Mutable class attributes should be annotated with typing.ClassVar

(RUF012)


29-50: Mutable class attributes should be annotated with typing.ClassVar

(RUF012)

🔇 Additional comments (5)
migrations_lockfile.txt (1)

20-20: LGTM! Lockfile correctly updated.

The lockfile entry properly reflects the new migration in the sequence.

src/sentry/monitors/migrations/0013_fix_monitorincident_checkin_cascade.py (4)

1-8: LGTM! Imports are correct.

All necessary imports for the migration operations are present.


25-27: LGTM! Dependency chain is correct.

The dependency on the previous migration is properly declared.

Note: The static analysis hint about ClassVar annotation is a false positive for Django migrations, which expect dependencies as a plain class attribute per the Django migration framework.


29-50: All verification checks pass—the migration is correct.

Model fields match the migration exactly: both starting_checkin and resolving_checkin are FlexibleForeignKey fields pointing to monitors.MonitorCheckIn with the same null=True and related_name values. The migration adds explicit on_delete=CASCADE behavior, which is necessary Django configuration. No manual cascade deletion logic was found in the codebase, and the related_name attributes don't conflict with existing code.


23-23: is_post_deployment = False is correct per Sentry's own migration guidelines.

The migration file itself contains explicit project documentation (lines 10-17) stating that is_post_deployment should not be used for schema alter operations like AlterField. The guidelines reserve this flag only for large data migrations and index additions, which are safe to run post-deployment. Changing on_delete behavior via AlterField is a standard schema operation that should run during deployment, as the project explicitly documents.

Likely an incorrect or invalid review comment.


Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants