Home > Software engineering >  Mark specific Django migrations as fake migrations during test DB setup
Mark specific Django migrations as fake migrations during test DB setup

Time:11-17

I have some database migrations, some are structural migrations some are data migrations

eg.

0001_initial.py
0002_import_data.py
0003_add_field.py
0004_import_more_data.py

I would like to skip those data migrations (0002 and 0004) as I don't want to spend the effort to fake the source for those data migrations but still run 0001 and 0003 when running python manage.py test

Is it possible?

CodePudding user response:

You can edit the data migration files and conditionally set operations to an empty list, or remove just the operations you want to skip. You then need to detect when you are running under a test, this can be done by having a separate settings file or setting an environment variable

class Migration(migrations.Migration):

    dependencies = [
        ('app', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(import_function),
    ]

    if settings.TEST: # if os.environ('SKIP_DATA_MIGRATIONS') == 'true':
        operations = []
  • Related