perf(migrations): speed up state replay and snapshots - #2274
Open
tsifrer wants to merge 3 commits into
Open
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.
Description
This change improves migration performance by avoiding repeated reconstruction of the complete historical model state.
It introduces lightweight migration state snapshots that:
ModelStatedata, including field dictionaries.old_state.old_stateandnew_stateoperation API for forwards migrations, rollbacks, custom operations, and SQL collection.It also changes model reloads so initial querysets are rebuilt only for models that were actually re-rendered. Previously, every operation rebuilt querysets for every model accumulated in the migration state.
Motivation and Context
Migration execution scaled approximately with the number of operations multiplied by the size of the accumulated schema.
Two repeated operations caused most of the overhead:
This was particularly visible in a large project containing over 2,500 migration operations and approximately 900 models.
Running SQL generation for a late migration previously took approximately three minutes:
With lightweight snapshots alone, the command still took approximately three minutes because state replay, not snapshot creation was the dominant cost for this migration.
After limiting queryset rebuilding to changed models, the same command completed in approximately five seconds:
How Has This Been Tested?
Testing was performed on macOS arm64 with Python 3.13.14.
The complete migration test directory was run:
results
304 passed, 6 skippedRegression tests were added to verify that:
The sqlmigrate command above and the same migrate command was run using an editable installation of this branch to measure the complete CLI path.
Ruff formatting and targeted lint checks also passed.
Checklist: