Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable data migrations in Django when creating test database

I have an app with a fair number of migrations including data migrations to set foreign keys on some models.

When I try to run tests.py, it fails because the data migration is querying the database for data that doesn't exist in the test database.

Is there a way to disable the data migrations? (I want to keep schema migrations, not disable migrations completely).

Or alternatively to load data from fixtures before running the data migrations?

like image 587
wobbily_col Avatar asked Jun 12 '17 09:06

wobbily_col


2 Answers

For check either test are running or not:

'test' in sys.argv
like image 50
Vladimir Avatar answered Oct 23 '22 08:10

Vladimir


I was inspired by Disable migrations while testing and @A. Grinenko. I did not customize TestRunner and I just checked whether 0002_data_migration.py was called by django test.
code:

class Migration(migrations.Migration):
dependencies = [
    ('backend', '0001_initial'),
]

operations = [
    migrations.RunPython(setup_data, rollback_setup, atomic=True)
] if 'test' not in sys.argv[1:] else []
like image 3
Ivan Avatar answered Oct 23 '22 07:10

Ivan