Skip to content

fix(migrations): Drop pending deletions before the drift comparison - #127468

Merged
joshuarli merged 3 commits into
masterfrom
ref/migrations-drift-pending-deletions
Oct 14, 2026
Merged

joshuarli merged 3 commits into
masterfrom
ref/migrations-drift-pending-deletions

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

migrations-drift has failed on every run for the last 100 runs (back to Sep 30), including on PRs that don't touch the drifting tables.

The check compares a database built by running migrations with one built from squashed migrations. Step 1 of a safe deletion (SafeRemoveField / SafeDeleteModel with DeletionAction.MOVE_TO_PENDING) takes a field or model out of the migration state but leaves it in the database until a later DELETE migration drops it. The squashed schema never has it, so the check fails for as long as any deletion is in flight. Its own output already says "drift due to step 1 of deletion is normal".

On current master that's sentry_release.new_id, which #127438 moved to pending deletion.

Fix: a new tools/migrations/drop_pending_deletions.py reads pending_deletion_models / pending_deletion_fields from SentryProjectState and applies the DELETE half of each one, the same way SafeRemoveField / SafeDeleteModel would:

  • scalar fields: ALTER TABLE ... DROP COLUMN
  • many-to-many fields and models: their auto-created through tables are dropped with them; explicit through models are left alone unless they're pending deletion themselves
  • through tables are dropped before the tables they join, so nothing needs CASCADE. There's no CASCADE or IF EXISTS, so an unexpected dependent object or a missing table fails the step instead of being silently dropped or skipped
  • each database only gets the statements the router would migrate there

The workflow runs it right before capturing schema-before, and now also triggers on changes to the helper.

Checked locally against master (79af5a0d), following the workflow's steps:

  • Without the new step: drift on sentry_release.new_id, the same failure CI shows.
  • With it: the step runs ALTER TABLE "sentry_release" DROP COLUMN "new_id" and tools.migrations.compare reports no drift.
  • tests/sentry/new_migrations/test_drop_pending_deletions.py builds real tables for a scalar field, an auto-created and an explicit-through M2M field, a model with an M2M, and unrelated models, then checks exactly what's dropped and what's kept. 4 of its 5 tests fail against the previous version of the helper. It lives under tests/sentry because it needs the test database; tests/tools runs without sentry installed.
  • ruff, mypy, flake8, actionlint and the workflow schema check pass.

via @joshuarli.

--

View Junior Session [Sentry]

The drift check compares a migrated database with one built from squashed
migrations. Columns and tables in step 1 of a safe deletion (MOVE_TO_PENDING)
are gone from the migration state but still in the database, so every PR fails
the check while any deletion is in flight.

Drop them from the migrated database before capturing its schema.

Co-Authored-By: Joshua Li <14209781+joshuarli@users.noreply.github.com>
@sentry-junior
sentry-junior Bot requested a review from joshuarli October 14, 2026 22:39
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Oct 14, 2026
@joshuarli
joshuarli marked this pull request as ready for review October 14, 2026 22:41
@joshuarli
joshuarli requested a review from a team as a code owner October 14, 2026 22:41
sentry-junior Bot and others added 2 commits October 14, 2026 22:52
…rift check

Mirror SafeRemoveField/SafeDeleteModel: drop auto-created M2M through
tables with their field or model, leave explicit through models alone,
and drop without CASCADE or IF EXISTS so unexpected dependents fail
loudly. Add regression tests and run the workflow when the helper changes.

Co-Authored-By: Joshua Li <14209781+joshuarli@users.noreply.github.com>
… suite

tests/tools runs under vanilla pytest without sentry installed, so the
DB-backed test can't be collected there.

Co-Authored-By: Joshua Li <14209781+joshuarli@users.noreply.github.com>
@joshuarli
joshuarli merged commit 4e8867b into master Oct 14, 2026
72 checks passed
@joshuarli
joshuarli deleted the ref/migrations-drift-pending-deletions branch October 14, 2026 23:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant