Optimizing and Squashing Django Migrations

Optimizing and Squashing Django Migrations

In this post we will give you information about Optimizing and Squashing Django Migrations. Hear we will give you detail about Optimizing and Squashing Django MigrationsAnd how to use it also give you demo for it if it is necessary.

Django migrations allow you to change , evolve and upgrade your database schema while keeping any existing databasedata intact .They also allow you to use version control tools such as Git with databases .

In this tutorial we are going to learn how to optimize database migrations by squashing or combining them .

Starting with Django 1.7 ,migrations became part of Django itself so you don’t need to install any third party apps such as South to work with database migrations .

To use migrations you frequently work with two commands which are :

python manage.py makemigrations 

Which generates migration files according to the change in your Django models .

And

python manage.py migrate      

Which actually migrates or upgrades the database schema .

But there are also other Django commands that can be used in certain situations such as when you need to optimize your migrations .

How to squach migrations in Django ?

Django has a management command that allows you to squach or combine multiple migration files into a single optimized migration file .

You can simply invoke this command using :

python manage.py squachmigrations 

Now lets consider this senario :

You have a Django app named myApp which has 4 migrations

- 0001_initial- 0002_change_2- 0003_change_3- 0004_change_4 

Now when you want to migrate your database ,applying these migrations takes time so you have decided to squach and optimize them .

manage.py squashmigrations myApp 0004

You pass the app name and migration file name (or just prefix) you want to squach up to .

You should get something like

Will squash the following migrations:- 0001_initial- 0002_change_2- 0003_change_3- 0004_change_4 Do you wish to proceed? [yN] yOptimizing...Optimized from 10 operations to 5 operations.Created new squashed migration /migrations/0001_squashed_0004_change_4.py 

After making sure everything is OK you can simply remove the old migration files and keep the squashed migration file alone .

As you can see from this simple example squashing is not only about combining migration files into one single file but also about optimizing the number of operations inside the final squashed file .


Hope this code and post will helped you for implement Optimizing and Squashing Django Migrations. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US