Skip to content

Commit

Permalink
Update migration tests to handle squashed migrations.
Browse files Browse the repository at this point in the history
  • Loading branch information
rtibbles committed Jun 25, 2024
1 parent aea789a commit 70b4b7d
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/testapp/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,9 @@ def update_state(self, stage=None, stage_status=None):

class TestMigrationsMixin(object):
# Modified from https://www.caktusgroup.com/blog/2016/02/02/writing-unit-tests-django-migrations/
# Note that this has been updated to handle running migration tests for previously squashed migrations.
# It is possible this will no longer work for testing migrations that are part of or
# subsequent to a squashed migration.

migrate_from = None
migrate_to = None
Expand All @@ -469,6 +472,10 @@ def setUp(self):
migrate_from = [(self.app, self.migrate_from)]
migrate_to = [(self.app, self.migrate_to)]
executor = MigrationExecutor(connection)
executor.migrate([(self.app, None)])
executor.loader.replace_migrations = False
executor.loader.build_graph() # reload.

old_apps = executor.loader.project_state(migrate_from).apps

# Reverse to the original migration
Expand All @@ -490,6 +497,8 @@ def setUpBeforeMigration(self, apps):
def tearDownClass(cls):
# revert migration back to latest migration
executor = MigrationExecutor(connection)
executor.migrate([(cls.app, None)])
executor.loader.replace_migrations = True
executor.loader.build_graph()
executor.migrate([cls.latest_migration])

Expand Down

0 comments on commit 70b4b7d

Please sign in to comment.