Skip to content

fix: reorder subject index migration to be MySQL FK-safe (production hotfix) - #31

Merged
timh8127 merged 1 commit into
mainfrom
fix/subject-index-migration-order
Jun 17, 2026
Merged

fix: reorder subject index migration to be MySQL FK-safe (production hotfix)#31
timh8127 merged 1 commit into
mainfrom
fix/subject-index-migration-order

Conversation

@timh8127

@timh8127 timh8127 commented Jun 17, 2026

Copy link
Copy Markdown
Owner

Production hotfix

db.Database.Migrate() crashes at startup with:

MySqlException: Cannot drop index 'IX_Subjects_UserId': needed in a foreign key constraint

Root cause

The AddSubjectNameUniqueIndex migration dropped IX_Subjects_UserId before creating its replacement. On MySQL that index is the sole backing for FK_Subjects_Users_UserId, so the DROP INDEX is rejected and the whole Migrate() call throws on boot.

CI didn't catch it because the test suite uses the in-memory provider, which ignores relational migrations.

Fix

Reorder Up() so the FK always has a backing index at every step:

  1. AlterColumn — set the Name collation (utf8mb4_general_ci)
  2. CreateIndex — composite unique IX_Subjects_UserId_Name (leading UserId can serve the FK)
  3. DropIndex — drop the old single-column IX_Subjects_UserId

Down() reverses in the mirror-safe order (recreate single-column index → drop composite → revert collation).

Why this is safe to deploy

The original migration failed on its first statement (the drop), so nothing was applied and it was never recorded in __EFMigrationsHistory. The corrected migration runs from scratch on the next deploy and applies cleanly. No schema-shape change versus the original intent — only statement order differs.

Testing

  • dotnet build -c Release — green, 0 warnings.
  • Statement order verified by hand against MySQL's FK-index-drop rule.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Chores
    • Database schema optimization for improved subject data uniqueness management.

The AddSubjectNameUniqueIndex migration dropped IX_Subjects_UserId
before creating its replacement. On MySQL that index is the sole
backing for FK_Subjects_Users_UserId, so the DROP is rejected and
db.Database.Migrate() crashes at startup (production outage).

Reorder Up() so the FK always has a backing index: alter the Name
collation, create the composite unique IX_Subjects_UserId_Name (whose
leading UserId column can serve the FK), and only then drop the old
single-column index. Down() reverses in the mirror-safe order.

No schema-shape change versus the previous migration intent; only the
statement order differs. The original migration failed on its first
statement, so it was never recorded in __EFMigrationsHistory and the
corrected version applies cleanly on the next deploy.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 4f5ca544-0906-41e3-a9cd-b80472ec7159

📥 Commits

Reviewing files that changed from the base of the PR and between e3e6a7b and af8eab2.

📒 Files selected for processing (1)
  • WSIST/WSIST.Engine/Migrations/20260616205925_AddSubjectNameUniqueIndex.cs

📝 Walkthrough

Walkthrough

A single EF Core migration file is updated to fix the operation sequence for MySQL compatibility. In Up, index drop, charset alteration, and composite unique index creation are reordered with inline comments. In Down, the single-column index is recreated before the composite index is dropped, and a duplicate recreation step is removed.

Changes

Migration Operation Reorder

Layer / File(s) Summary
Up/Down operation reorder and duplicate removal
WSIST/WSIST.Engine/Migrations/20260616205925_AddSubjectNameUniqueIndex.cs
Adds MySQL ordering comments to Up, compacts the IX_Subjects_UserId drop, reorders Down to recreate IX_Subjects_UserId before dropping IX_Subjects_UserId_Name, and removes the duplicate Down index recreation step.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 Hop, hop, the index falls first,
Then charset is tweaked — no need to thirst!
The composite blooms in its rightful place,
No duplicate steps to muddy the race.
Down reverses in orderly grace. ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: reordering a database migration to fix a MySQL foreign key constraint issue that was causing production outages.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/subject-index-migration-order

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

@timh8127
timh8127 merged commit 401108b into main Jun 17, 2026
2 checks passed
@timh8127
timh8127 deleted the fix/subject-index-migration-order branch June 27, 2026 20:36
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.

1 participant