Make TestModelsLoaderMixin more transaction-friendly and evolution-friendly.
Review Request #11268 — Created Nov. 5, 2020 and submitted
TestModelsLoaderMixinattempted to set up some initial state for a
test suite, and then manage the database state per-test. The way it did
this made some bad assumptions that held up in earlier versions of
Django and with older versions of Django Evolution, but weren't quite
correct, and fails with Django Evolution 2.1.
The old code tried to perform a
migrateat the start of each test, and
flushat the end. These were often being done within a transaction
(set up by
TestCase.setUpClass()), which currently fails with Django
Evolution (since it cancels the upgrade if in a transaction) and would
fail with Django's own
migrateif run on MySQL (for the same reason).
The new code has been reworked to perform all the necessary operations
tearDownClass(), outside of any transaction
management. This allows us to populate the database up-front, enter a
transaction, and roll it back per-test, rather than flushing the whole
A huge benefit is that there's a major performance gain for many of our
unit tests, dropping it from 90 seconds to 18 seconds on my local
All unit tests pass.