Blog

Branch Specific Database YML

Team Avatar - Mikel Lindsaar
Mikel Lindsaar
August 15, 2012

At reinteractive we use [Git Flow](http://jeffkreeftmeijer.com/2010/why-arent-you-using-git-flow/) for all our projects.

But with lots of branches comes a problem. What happens when you have migrations that are branch specific? If you have used branching a lot on an in development Rails application, you are sure to have run into the problem of changing branches and having an inconsistent database state against your code.

The solution we use at reInteractive (or at least the solution I use :) is to take advantage of the way Rails reads in YAML files and embed ERB to change the database name according to the current git branch.

So instead of a database.yml that looks like this:





development:
  adapter: postgresql
  database: appname_development
  timeout: 5000
  host: /var/pgsql_socket
test:
  adapter: postgresql
  database: appname_test
  timeout: 5000
  host: /var/pgsql_socket

It looks like this:

development:
  adapter: postgresql
  <% if `git symbolic-ref HEAD 2>/dev/null`.chomp.sub('refs/heads/', '').gsub(/\W/, '_') =~ /hotfix|release/ %>
  database: appname_development_master
  <% else %>
  database: appname_development_<%= `git symbolic-ref HEAD 2>/dev/null`.chomp.sub('refs/heads/', '').gsub(/\W/, '_')[0..20] %>
  <% end %>
  timeout: 5000
  host: /var/pgsql_socket
test:
  adapter: postgresql
  <% if `git symbolic-ref HEAD 2>/dev/null`.chomp.sub('refs/heads/', '').gsub(/\W/, '_') =~ /hotfix|release/ %>
  database: appname_test_master
  <% else %>
  database: appname_test_<%= `git symbolic-ref HEAD 2>/dev/null`.chomp.sub('refs/heads/', '').gsub(/\W/, '_')[0..20] %>
  <% end %>
  timeout: 5000
  host: /var/pgsql_socket
 



This does two things. Firstly, every feature branch you have gets its own database, separate from all the other branch databases. Secondly all hotfix branches use the master branch's database. I use the same database for hotfix and master because hotfixes get merged into master very rapidly and you don't want to deal with this complexity when you are making a quick fix. Also, usually in development our master branch database has good test data, so we can test our hotfix changes rapidly.

Another point if you are doing this, is that it requires a couple of extra steps.

After you checkout your new feature branch, you need to do a `rake db:setup` to setup the new database for the new branch. Also, you need to restart your development web server on each branch change to make sure the app is looking at the right database.

These could be put into git hooks, but I haven't bothered for my own system as it's trivial to do.