Fail loudly when two migrations share a version number#186
Open
constkolesnyak wants to merge 1 commit into
Open
Fail loudly when two migrations share a version number#186constkolesnyak wants to merge 1 commit into
constkolesnyak wants to merge 1 commit into
Conversation
run_migrations skips anything at or below the recorded schema version, so if two files claim the same number one silently wins and the other never runs — on every database, forever, with no error. It surfaces much later as a missing column or index, and which of the two is missing depends on filesystem iteration order, so it reproduces on some machines and not others. discover_migrations now raises on a collision, naming both files. Failing at import is the lesser evil: the collision is a packaging mistake caught in seconds once it is named, whereas a silently skipped migration costs hours and can corrupt data in the meantime. Adds tests/test_migrations_runner.py (there were none), including a regression check that the real on-disk migration set has no duplicates — so a collision introduced later fails in CI rather than at a user startup. 113 tests pass.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
run_migrationsskips anything at or below the recorded schema version, so if two files claim the same number, one silently wins and the other never runs — on every database, forever, with no error.It surfaces much later as a missing column or index, and which of the two is missing depends on filesystem iteration order, so it reproduces on some machines and not others.
We hit this three times downstream while syncing this repo. The worst instance crash-looped the service 279 times before anyone noticed what the traceback actually meant.
Fix
discover_migrations()now raises on a collision, naming both files.Failing at import is the lesser evil here: a duplicate version is a packaging mistake that takes seconds to fix once it is named, whereas a silently skipped migration costs hours and can leave data half-migrated in the meantime.
Tests
tests/test_migrations_runner.pyis new — there were no runner tests. Beyond the guard itself it includes a regression check that the real on-disk migration set has no duplicates, so a collision introduced later fails in CI rather than at a user's startup.113 tests pass.