﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
37575	Improve MigrationAutodetector test coverage and stop it mutating its input states	Konstantinos Giannopoulos	Konstantinos Giannopoulos	"As discussed in [https://forum.djangoproject.com/t/improving-migrationautodetector-robustness-tests-and-a-few-targeted-refactors/46573 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):
   a. 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.
   b. A test helper that applies the generated migrations and compares the resulting database constraints and indexes with the models (round trip).
   c. A check that `MigrationAutodetector` does not modify `from_state` / `to_state`.
 2. Cleanups, each behaviour-preserving and covered by 1:
   a. 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).
   b. 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."	Cleanup/optimization	assigned	Migrations	dev	Normal		autodetector, tests	Konstantinos Giannopoulos	Unreviewed	0	0	0	0	0	0
