Opened 7 years ago

Closed 5 years ago

#29039 closed Bug (fixed)

Disabling migrations doesn't work with --keepdb

Reported by: Vackar Afzal Owned by: nobody
Component: Testing framework Version: dev
Severity: Normal Keywords: database, tests, migrations
Cc: Davor Lučić Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

We have a large number of migrations in our application now, and as a result it can take 20 mins to create a test DB.
To make this faster I am settings MIGRATION_MODULES in my settings as follows:

MIGRATION_MODULES = {}
for app in INSTALLED_APPS:
    if app.startswith('django.contrib.'):
        app=app.replace('django.contrib.', '')
    MIGRATION_MODULES[app] = None

I've found that this breaks the --keepdb option as it Django wants to run sync_db internally every time.

The workaround I found is to modify django.db.backends.creation.py (lines 64 to 70) from

    call_command(
            'migrate',
            verbosity=max(verbosity - 1, 0),
            interactive=False,
            database=self.connection.alias,
            run_syncdb=true,
        )

To

    call_command(
            'migrate',
            verbosity=max(verbosity - 1, 0),
            interactive=False,
            database=self.connection.alias,
            run_syncdb=not keepdb,
        )

Change History (4)

comment:8 by Tim Graham, 7 years ago

Component: MigrationsTesting framework
Easy pickings: unset
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

comment:10 by Sergey Yurchenko, 7 years ago

I have the same problems with creation of test db.
For tests I don`t use migrations but models directly like in older versions of Django without migrations.
Tests with migrations run is CI to check that migrations are correct so nobody cares about time.

class DisableMigrations(object):
    def __contains__(self, item):
        return True

    def __getitem__(self, item):
        return None

in your settings.py

    MIGRATION_MODULES = DisableMigrations()

comment:11 by Davor Lučić, 7 years ago

Cc: Davor Lučić added

comment:12 by Mariusz Felisiak, 5 years ago

Resolution: fixed
Status: newclosed
Version: 1.11master

This should be fixed in Django 3.1 by using the new MIGRATE test database setting that allows disabling of migrations during a test database creation (see #25388).

Fixed in f5ebdfce5c417f9844e86bccc2f12577064d4bad.

Note: See TracTickets for help on using tickets.
Back to Top