How to work with Git branches and Rails migrations

Kostas picture Kostas · Jan 19, 2011 · Viewed 23.1k times · Source

I am working on a rails app with quite a few git branches and many of them include db migrations. We try to be careful but occasionally some piece of code in master asks for a column that got removed/renamed in another branch.

  1. What would be a nice solution to "couple" git branches with DB states?

  2. What would these "states" actually be?

    We can't just duplicate a database if it's a few GBs in size.

  3. And what should happen with merges?

  4. Would the solution translate to noSQL databases as well?

    We currently use MySQL, mongodb and redis


EDIT: Looks like I forgot to mention a very important point, I am only interested in the development environment but with large databases (a few GBs in size).

Answer

Andy Lindeman picture Andy Lindeman · Jan 19, 2011

When you add a new migration in any branch, run rake db:migrate and commit both the migration and db/schema.rb

If you do this, in development, you'll be able to switch to another branch that has a different set of migrations and simply run rake db:schema:load.

Note that this will recreate the entire database, and existing data will be lost.

You'll probably only want to run production off of one branch which you're very careful with, so these steps don't apply there (just run rake db:migrate as usual there). But in development, it should be no big deal to recreate the database from the schema, which is what rake db:schema:load will do.