Squash Django migrations

Stock blog image 5

Maintaining a Django (and Wagtail) platform over multiple years comes with some challenges. Although Django is build to scale there are some things to consider while keeping your codebase clean. One of them is the amount of migration files checked into Git. Especially Wagtail migrations are growing hard when building a lot of page types and content blocks (due the fact StreamFields internally supports migrations as well).

Luckely Django has support to squash migrations. This makes squashing a lot easier (remember connecting to remote databases and manualy apply migrations with Django South) and fully compatible with your common used Continuous Deployment architectures.

$ ./manage.py squashmigrations myapp 0004 Will squash the following migrations: - 0001_initial - 0002_some_change - 0003_another_change - 0004_undo_something Do you wish to proceed? [yN] y Optimizing... Optimized from 12 operations to 7 operations. Created new squashed migration /home/andrew/Programs/DjangoTest/test/migrations/0001_squashed_0004_undo_something.py You should commit this migration but leave the old ones in place; the new migration will be used for new installs. Once you are sure all instances of the codebase have applied the migrations you squashed, you can delete them.

A more detailed explanation about squashing migrations can be found at the Django documentation.