From 2b7ba09db40078911d9562555fe7bf4d0612d9d7 Mon Sep 17 00:00:00 2001 From: Tobias Bengfort Date: Mon, 17 Jan 2022 20:06:22 +0100 Subject: [PATCH] do not run migrations on migrator.reset() --- django_test_migrations/migrator.py | 20 +++++++++++++++++++- tests/test_migrator/test_migrator.py | 13 +++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/django_test_migrations/migrator.py b/django_test_migrations/migrator.py index 6574bbe..415f611 100644 --- a/django_test_migrations/migrator.py +++ b/django_test_migrations/migrator.py @@ -1,5 +1,6 @@ from typing import Optional +from django.conf import settings from django.core.management import call_command from django.core.management.color import no_style from django.db import DEFAULT_DB_ALIAS, connections @@ -73,7 +74,24 @@ def reset(self) -> None: https://github.com/wemake-services/django-test-migrations/issues/128 """ - call_command('migrate', verbosity=0, database=self._database) + class DisableMigrations: + def __contains__(self, item: str) -> bool: + return True + + def __getitem__(self, item: str) -> None: + return None + + pre_migration_modules = settings.MIGRATION_MODULES + try: + settings.MIGRATION_MODULES = DisableMigrations() + call_command( + 'migrate', + verbosity=0, + database=self._database, + run_syncdb=True, + ) + finally: + settings.MIGRATION_MODULES = pre_migration_modules def _migrate( self, diff --git a/tests/test_migrator/test_migrator.py b/tests/test_migrator/test_migrator.py index 4df055f..6e1db96 100644 --- a/tests/test_migrator/test_migrator.py +++ b/tests/test_migrator/test_migrator.py @@ -26,3 +26,16 @@ def test_migrator_list(transactional_db): assert isinstance(old_state, ProjectState) assert isinstance(new_state, ProjectState) assert migrator.reset() is None + + +@pytest.mark.django_db() +def test_migrator_reset(transactional_db, mocker): + """Ensure reset does not execute any migrations.""" + migrator = Migrator() + old_state = migrator.apply_initial_migration([('main_app', None)]) + new_state = migrator.apply_tested_migration([('main_app', '0001_initial')]) + + run_python_mock = mocker.patch('django.db.migrations.RunPython') + migrator.reset() + + assert not run_python_mock.called