Make 0001_initial idempotent and swappable-user-model safe (0.5.0) - #1
Merged
Merged
Conversation
Build the dynamic_columns_* tables through the schema editor (create_model), guarded to run only when the tables are absent, instead of a plain CreateModel. This fixes two problems for downstream projects: - Pre-existing schema no longer collides. Projects extracted from an older in-tree app (notably BPP) already carry these tables; applying 0001_initial onto such a database now detects them and no-ops, so the MIGRATION_MODULES override they used to ship is no longer needed. - Custom user models resolve correctly on a from-empty build. The user FK is materialised against settings.AUTH_USER_MODEL by the schema editor, so a fresh migrate on a project with a swapped user model (e.g. bpp.BppUser -> bpp_bppuser) no longer fails resolving a hard-coded auth_user table. Migration state is unchanged (CreateModel pair + constraints under SeparateDatabaseAndState); already-applied DBs are untouched and makemigrations detects no drift. Adds a regression test that re-applies the initial migration onto pre-existing tables. Bumps to 0.5.0.
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.
Summary
Rework
0001_initialso it builds thedynamic_columns_*tables through the schema editor (create_model), guarded to run only when the tables are absent, instead of a plainCreateModel. The model side stays instate_operations, so Django's migration state is unchanged.Two payoffs for downstream projects:
dynamic_columnsapp created these tables years ago) can apply0001_initialonto a database that already carries the tables — it detects them and no-ops. Such projects no longer need aMIGRATION_MODULESoverride; they can consume this migration directly.userFK is materialised by the schema editor againstsettings.AUTH_USER_MODEL, so a freshmigrate/migrate --create-dbon a project with a swapped user model points the FK at the right table instead of a hard-codedauth_user.Why it's safe to change a released migration
CreateModelpair + constraints, now underSeparateDatabaseAndState), so databases that already recorded0001_initialas applied are untouched andmakemigrations --checkreports no drift.create_modelproduces exactly the same DDL as the oldCreateModel.Tests
tests/test_migrations.py: re-applies the initial migration onto pre-existing tables and asserts no collision (the legacy-upgrade scenario). Fails against the old plainCreateModel, passes now.Bumps version to 0.5.0 + CHANGELOG.
Downstream
BPP drops its
bpp.migration_overrides.dynamic_admin_columnsoverride and requires>=0.5.0once this is released (iplweb/bpp PR forthcoming).🤖 Generated with Claude Code