#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:

  1. Tests (no behaviour change):
    1. 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.
    2. A test helper that applies the generated migrations and compares the resulting database constraints and indexes with the models (round trip).
    3. A check that MigrationAutodetector does not modify from_state / to_state.
  2. Cleanups, each behaviour-preserving and covered by 1:
    1. Stop mutating the input states (e.g. model_state.options.pop("indexes") in generate_created_models() and generate_deleted_models() act on to_state / from_state model states directly).
    2. 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.

Any bug the new tests uncover gets its own ticket. I'm happy to work on this.

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top