What are database migrations and why they are important?

Hey guys in the recent articles we learned about apis and how to write one. When you are writing any web service one thing that you will require is database. Without database you can save your data by different ways but it will not be that easy to manage it as compared to databases.

Again when we work with database we face problem related to alteration of structure and different versions of database. To solve these problem and to make it easy to maintain different state of database, we use database migrations. So lets see what are database migrations and why they are important?

what are database migrations and why it is important
Source: http://blog.balancedpayments.com/


What are Database Migrations?

Database migrations is used when we need to keep track of the changes that we made in the database schema. Doing so help us in rollback if case of any mishap and thus keeps us safe. We write migrations for  what will be the next state thus keeping track at each point what is the current state. This keeps on versioning the database.


Why Migrations?

Migrations are required to keep track of the database changes and state which help in detecting errors and rollback in case of any mishaps.

Also it makes it easy to manage database instead of writing mysql scripts for each database versions and doing it manually we generally use ORMs to do this.

With migrations you can also help the database to populate itself with required seeding data.

Many people can work with different versions of databases.


How you can create migrations?

For migrations almost every language or framework has some provisions or library. Few are listed below and you can use them at your convenience.

Python: Sqlalchemy which can be used with any python project. You can use it easily with flask. For Django it has inbuild migration system that you can use.

RubyRuby has Sequel migrator. You can use active records in ruby on rails.

Php: Laravel has migrations as well which you can use and read here.

Javascript/Node: You can use Node db migrate.

Go: You can use Migrate in Go

Java: Flyway is one good option for java. Other you can read here.


Keeping track of the changes you made is always a good idea. It helps you in many ways. It makes everything more structured, you know what all are changed and always have fallback options ready.

This is about what are database migrations and why they are important?

I will talk about ORMs in coming post which will tell you how you can write migrations for many different type of database at one. And your code will work with many databases.

Liked the articles please share and subscribe for more such stuff.

Subscribe for latest updates

Gaurav Yadav

Gaurav is cloud infrastructure engineer and a full stack web developer and blogger. Sportsperson by heart and loves football. Scale is something he loves to work for and always keen to learn new tech. Experienced with CI/CD, distributed cloud infrastructure, build systems and lot of SRE Stuff.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.