Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use run_syncdb in reset #263

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion django_test_migrations/migrator.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions tests/test_migrator/test_migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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