Repository navigation
fix: align migrations with models for bigint ids and required deck_id - #91
Merged
Merged
Conversation
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.
alembic checkagainst a fully upgraded database reported drift that existed before the idempotency key work. The init migration createddecks.idandcards.idasINTEGERand leftcards.deck_idnullable, while the models say otherwise. This adds a migration that brings the schema in line with the models, and apytest-alembicsuite that fails whenever the two drift apart again.Which side is right
decks.idandcards.id: the model is right.BigIntAuditBasemaps them asBIGINT, and theidcolumns are the only key the API exposes, so a 32-bit cap would be a hard limit at about 2.1 billion rows. Moving the model toIntegerwould mean dropping advanced-alchemy's base class for a hand-rolled one. The migration also runsALTER SEQUENCE ... AS bigint. Postgres 10 and later createSERIALsequencesAS integer, and changing the column type leaves the sequence alone, so without it the ids would still stop at 2^31 - 1.cards.deck_idnullability: the model is right.orm.Mapped[int]makes it NOT NULL, and the API only creates cards under a deck (CardsRepository.add_cardsalways setsdeck_id).cards.deck_idtype: both change. The model mapped it assa.Integerwhile it referencesdecks.id. Autogenerate didn't flag this because model and database agreed, but oncedecks.idisBIGINTa foreign key stuck atINTEGERcan't hold every deck id. The model now sayssa.BigIntegerand the migration converts the column.The init migration is unchanged.
Notes for existing databases
INTEGERtoBIGINTrewrites both tables under anACCESS EXCLUSIVElock. That is fine for a template, but a large deployment should plan for it.cards.deck_idNOT NULL fails if a card with a NULLdeck_idexists. The migration doesn't delete such rows on its own; that call belongs to whoever owns the data.INTEGERrange.Migration tests
tests/migrations/runs pytest-alembic's four built-in tests against an empty database, the same way chat-app does: single head, upgrade, up/down consistency for every revision, and model definitions matching the DDL, which isalembic checkas a test. The defaultpytestrun skips the directory (--ignoreinaddopts,omitin coverage) because migrating the schema would break the transaction-rollback fixture. It runs withjust test-migrations, and in CI as its own matrix leg next to the app suite.These tests don't check sequence types, so nothing catches a
SERIALsequence leftAS integer.Testing
On Postgres 14,
test_model_definitions_match_ddlfails with this migration removed and the old model, and all four tests pass with it. ruff, ty and pytest (100% coverage) pass.