Opened 15 minutes ago
#37575 assigned Cleanup/optimization
Improve MigrationAutodetector test coverage and stop it mutating its input states
| Reported by: | Konstantinos Giannopoulos | Owned by: | Konstantinos Giannopoulos |
|---|---|---|---|
| Component: | Migrations | Version: | dev |
| Severity: | Normal | Keywords: | autodetector, tests |
| Cc: | Konstantinos Giannopoulos | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
As discussed in this forum thread, this ticket collects a set of test additions and small, behaviour-preserving cleanups to MigrationAutodetector.
Motivation
Several recent autodetector regressions came from fixes tested with one model in one app that broke when a second, similar model or app took part in the same makemigrations run (#37551, #37552, #37553). For example, the fix for #35359 added _get_dependencies_for_generated_field(), which looked at fields added anywhere in the project, causing spurious cross-app dependencies, split migrations, and a CycleError when two models in one app added same-named fields. None of the existing GeneratedField autodetector tests asserted migration dependencies or changed a second model in the same run.
Plan
Delivered as a series of small commits (Refs #NNNNN -- ...), tests first, each reviewable on its own:
- Tests (no behaviour change):
- Rerun the existing rename / FK /
GeneratedField/ model-dependency scenarios with a look-alike model (same field names) in the same app and in a second app, changing in the same run, and assert the migrations'dependencies, not only the operations. - A test helper that applies the generated migrations and compares the resulting database constraints and indexes with the models (round trip).
- A check that
MigrationAutodetectordoes not modifyfrom_state/to_state.
- Rerun the existing rename / FK /
- Cleanups, each behaviour-preserving and covered by 1:
- Stop mutating the input states (e.g.
model_state.options.pop("indexes")ingenerate_created_models()andgenerate_deleted_models()act onto_state/from_statemodel states directly). - Possibly, only if measurements show a benefit: look up operations by model instead of scanning all of them for each dependency, and compute rename comparisons once.
- Stop mutating the input states (e.g.
Any bug the new tests uncover gets its own ticket. I'm happy to work on this.