How to combine sequential database migrations in django 1.9+?

Migrations allow you to convert from one database schema to another while saving current data to the database. Django allows you to create migrations using the python manage.py makemigrations

Each time makemigrations run makemigrations , a new 000n.. migration file is created based on the changes found in the models.py file.

Sometimes, after making small changes to models.py, I want to run makemigrations, but I do not want a new migration to be created because previous migrations have not been used yet, which allows us to combine them, primarily because they start each migration in the production process it can take a long time when there is a lot of data in the database, so it may be preferable to combine migrations into hands.

Is there a way to allow a new transfer of 000n .. with an existing migration of 000 (n-1) .. ?

+7
django django-migrations
source share
2 answers

The team you are looking for is squashmigrations . It will merge all unapplied migrations of this application into one file.

+8
source share

I want to run makemigrations, but I don’t want the new migration to be because previous migrations have not been used yet

This is not a problem, Django starts migrations from top to bottom, which means: your last migration file will wait until other previous migration files are started.

since starting each migration during production can take a long time when there is a lot of data in the database

How much data do you have in the database? If this is true, then you already have replication and redundant servers. In this case, switch the read and write to one, say, a slave server, start the migration in the main. and then switch the traffic to the main one, and before that make sure that the gap between them is 0, and the new scheme is replicated correctly among them.

+1
source share

All Articles