I have a Django application. I have .json fixture files containing test data, with unit tests that use the data to confirm the application is working properly. I also use South to migrate my database.
After doing a few database migrations, my fixtures are out of date, because the database has migrated, adding a new database column, for example, and the fixture data doesn't have that column, since it was captured before the database changed.
What's the best way to move my fixtures forward as I migrate my database?
In Django's migrations code, there's a squashmigrations command which: "Squashes the migrations for app_label up to and including migration_name down into fewer migrations, if possible." So, if you want to squash, say, the first 5 migrations, this will help.
Migrations are not required. They can be useful for creating and tracking database changes via code, but Django applications will run properly without them.
Here's the process I used:
Roll back the code to the revision that created the fixture in the first place. For example: svn up -r12345
.
Empty the database, then create it with manage.py syncdb --noinput --migrate
Load the fixture with manage.py loaddata my_fixture.json
Roll the code forward to now, with svn up
Migrate the database with manage.py migrate
Dump the data with manage.py dumpdata --indent=2 myapp >my_fixture.json
Note that you need to be careful when choosing the past revision to roll back to. In my case, I had some recent fixes that needed to be in place, so I actually had to pick and choose directories to roll back to specific revisions. Tedious, but better than hand-editing a 9,000-line JSON file.
Also, in step 6, be sure to dump the correct set of applications.
In the future, as I write migrations, I can do these steps again to keep all the fixtures up-to-date.
Why can't you simply create a fresh .json
file from your db. This is what I do when I need to create a new fixture.
python manage.py dumpdata <your_app> auth > test_data.json
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With