Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable migrations when running unit tests in Django 1.7

People also ask

How do I stop migrations in Django?

You can selectively disable migration for one or more Django-Models by setting managed = False in Django Model Meta options.

Does Django test run migrations?

The Django test runner begins each run by creating a new database and running all migrations in it. This ensures that every test is running against the current schema the project expects, but we'll need to work around this setup in order to test those migrations.

How do I stop last migration in Django?

MIGRATION_NUMBER_PREFIX is the number prefix of the migration you want to revert to, for instance 0001 to go to 0001_initial.py migration. then you can delete that migration. You can use zero as your migration number to revert all migrations of an app.

Is Django migration necessary?

Migrations are not required. They can be useful for creating and tracking database changes via code, but Django applications will run properly without them.


Look at this workaround, posted by Bernie Sumption to the Django developers mailing list:

If makemigrations has not yet been run, the "migrate" command treats an app as unmigrated, and creates tables directly from the models just like syncdb did in 1.6. I defined a new settings module just for unit tests called "settings_test.py", which imports * from the main settings module and adds this line:

MIGRATION_MODULES = {"myapp": "myapp.migrations_not_used_in_tests"}

Then I run tests like this:

DJANGO_SETTINGS_MODULE="myapp.settings_test" python manage.py test

This fools migrate into thinking that the app is unmigrated, and so every time a test database is created it reflects the current structure of models.py.

In Django 1.9, this situation is improved somewhat, and you can set the value to None:

MIGRATION_MODULES = {"myapp": None}


Here is the end of my settings file :

class DisableMigrations(object):

    def __contains__(self, item):
        return True

    def __getitem__(self, item):
        return None


TESTS_IN_PROGRESS = False
if 'test' in sys.argv[1:] or 'jenkins' in sys.argv[1:]:
    logging.disable(logging.CRITICAL)
    PASSWORD_HASHERS = (
        'django.contrib.auth.hashers.MD5PasswordHasher',
    )
    DEBUG = False
    TEMPLATE_DEBUG = False
    TESTS_IN_PROGRESS = True
    MIGRATION_MODULES = DisableMigrations()

based on this snippet

I disabled migrations only when tests are running


django-test-without-migrations adds a --nomigrations flag to manage.py test. Works like a charm.


Update: Never mind, this change was reverted before 1.10 final was released. Hopefully it will return in a future version.


Note that as of Django 1.10 this can be controlled by a test database setting.

MIGRATE

Default: True

If set to False, Django won’t use migrations to create the test database.


https://gist.github.com/apollovy/22826f493ad2d06d9a9a22464730ce0b

MIGRATION_MODULES = {
    app[app.rfind('.') + 1:]: 'my_app.migrations_not_used_in_tests'
    for app in INSTALLED_APPS
}

For django 1.9 and up the answer of Guillaume Vincent does not work anymore, so here's a new solution:

I'm using this snippet in my settings file, after the definition of the INSTALLED_APPS

if os.environ.get('TESTS_WITHOUT_MIGRATIONS', False):
    MIGRATION_MODULES = {
        app.split('.')[-1]: None for app in INSTALLED_APPS
    }

It iterates over all installed apps and marks each as having no migration module. See the django docs for more information.

Using this snippet you can run your tests, setting the environment variable TESTS_WITHOUT_MIGRATIONS, e.g.:

TESTS_WITHOUT_MIGRATIONS=1 ./manage.py test