Migration loader fails to load pair of squashed apps when dependent app's squashed migrations are partially applied
|Ready for checkin
|Patch needs improvement:
Consider the following set of migrations (taken directly from the test suite; for clarity I removed the repetitive
_auto suffix from the migration names, and renamed the apps
B instead of
A.3 has a dependency on
B.2, and there also exist two squashed migrations
A.3 are both applied or unapplied (that is, their replacement by
A.2_squashed_3 is allowed to take effect), this migration set will load fine.
A.2 is applied and
A.3 is not, this migration set will fail to load with "django.db.migrations.exceptions.NodeNotFoundError: Migration A.3 dependencies reference nonexistent parent node ('B', '2')".
When the loader iterates over all "replacing" migrations, it finds
B.1_squashed_2. Then it looks at each migration replaced by
B.1_squashed_2, and one of those is
B.2. It checks what migrations depend on
B.2, so it can replace that dependency with a dependency on
B.1_squashed_2. It finds that
A.3 depends on
B.2. So far so good.
But at this point, before replacing
A.3's dependency on
B.2 with a dependency on
B.1_squashed_2, it first checks whether
A.3 is itself replaced by anything. If so, it tries to update the dependencies of the replacing migration (that is
A.2_squashed_3) and doesn't bother to update the dependencies of
A.3 itself. But it fails to check whether, given the already-applied migrations,
A.2_squashed_3 will _actually_ be allowed to replace
A.3 -- it just assumes the replacement will occur. And in the situation where the replacement doesn't occur (because
A.2 is applied and
A.3 is not), the dependency of
B.2 is never updated to account for the replacement of
B.1_squashed_2, and thus there is a dependency error.
I believe this fix should be backported to the 1.8.x branch, since it is a crashing bug.