Fix evolution optimization issues when renaming models.

Review Request #8085 — Created March 30, 2016 and submitted — Latest diff uploaded

Information

Django Evolution
release-0.7.x
df2ba20...

Reviewers

Django Evolution's evolution optimization code had issues when applying
a series of evolutions that add a ForeignKey field to a newly-introduced
model that is then renamed in the same batch. The resulting field would
still point to the original model, resulting in a KeyError.

This was partly caused by the optimization code not attempting to update
the related_model attribute of the new field when merging field
attributes, and partly due to the RenameModel mutation not being
available during the evolution process for a newly-introduced model (due
to it being filtered out earlier on).

This change fixes this by adding code for tracking model renames and
deletions, ensuring the RenameModel is always present in the
optimization steps, and making sure that related_model attribute is set
for newly-introduced fields when the specified model has been renamed.

Optimizations were also added to condense model renames, and to remove
any mutations that rename models that will later be deleted.

Unit tests pass for SQLite, MySQL, and PostgreSQL.

Tested against a database that hit this particular bug. I received a
KeyError before the fix, but after, I was able to successfully upgrade.