Search code examples
pythondjangomigrationsquash

Using a fresh initial migration to squash migrations


I have an app with 35 migrations which take a while to run (for instance before tests), so I would like to squash them.

The squashmigrations command reduces the operations from 99 to 88, but it is still far from optimal. This is probably due to the fact that I have multiple RunPython operations preventing Django from optimizing other operations. All these RunPython operations are useless in the squashed migration because the database is empty. In Django 1.10 the elidable parameter will allow to skip them in this case, but still, a lot of clutter remains.

What I had in mind for the squashed migration was closer to the initial migrations Django generates, hence my question:

Is it advisable to use a fresh initial migration as a squashed version of a long list of migrations? How would you do that?


Solution

  • If you don't have any important data in your test or production databases your can use fresh initial migration and it will be appropriate solution.

    I've used this trick a lot of times and it works for me.

    A few thoughts:

    • sometimes, first you need to create migrations for one of your local application and then for all the others;

    • to be sure that all be ok you can commit your migrations and backup you db before you run ./migrate with empty db.

    NOTE: to speed up your tests you can try to run in memory tests and/or run tests with sqlite if its possible.