Why database migrations?

First, let's start from the beginning and assume we have a project called Shiny and its primary deliverable is a piece of software called Shiny Soft that connects to a database called Shiny DB.

The simplest diagram to represent this could look something like this:

We have our software and our database. Great. And this could very well be all you need.

But on most projects, this simple view of the world very quickly translates into this:

We now not only have to deal with one copy of our environment, but with several. This presents a number of challenges.

We have been pretty good at solving them on the code side.

  • Version control is now universal with better tools everyday.
  • We have reproducible builds and continuous integration.
  • We have well defined release and deployment processes.

But what about the database?

Well unfortunately we have not been doing so well there. Many projects still rely on manually applied sql scripts. And sometimes not even that (a quick sql statement here or there to fix a problem). And soon many questions arise:

  • What state is the database in on this machine?
  • Has this script already been applied or not?
  • Has the quick fix in production been applied in test afterwards?
  • How do you set up a new database instance?

More often than not the answer to these questions is: We don't know.

Database migrations are a great way to regain control of this mess.

They allow you to:

  • Recreate a database from scratch
  • Make it clear at all times what state a database is in
  • Migrate in a deterministic way from your current version of the database to a newer one

How Flyway Works