Skip to content
PLAY VIDEO PLAY VIDEO PLAY VIDEO
By Mikel Lindsaar

Branch Specific Database YML

At reInteractive we use 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: appnamedevelopment
  timeout: 5000
  host: /var/pgsqlsocket

test: adapter: postgresql database: appnametest timeout: 5000 host: /var/pgsqlsocket

It looks like this:

development:
  adapter: postgresql
  <% if git symbolic-ref HEAD 2&gt;/dev/null.chomp.sub('refs/heads/', '').gsub(/\W/, '') =~ /hotfix|release/ %>
  database: appnamedevelopmentmaster
  <% else %>
  database: appnamedevelopment<%= git symbolic-ref HEAD 2&gt;/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&gt;/dev/null.chomp.sub('refs/heads/', '').gsub(/\W/, '') =~ /hotfix|release/ %> database: appnametestmaster <% else %> database: appnametest<%= git symbolic-ref HEAD 2&gt;/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.

Latest Articles by Our Team

Our expert team of designers and developers love what the do and enjoy sharing their knowledge with the world.

We Hire Only the Best

reinteractive is Australia’s largest dedicated Ruby on Rails development company. We don’t cut corners and we know what we are doing.

We are an organisation made up of amazing individuals and we take pride in our team. We are 100% remote work enabling us to choose the best talent no matter which part of the country they live in. reinteractive is dedicated to making it a great place for any developer to work.

Free Community Workshops

We created the Ruby on Rails InstallFest and Ruby on Rails Development Hub to help introduce new people to software development and to help existing developers hone their skills. These workshops provide invaluable mentorship to train developers, addressing key skills shortages in the industry. Software development is a great career choice for all ages and these events help you get started and skilled up.

  • Webinars

    Webinars

    Webinars are our online portal for tips, tricks and lessons learned in everything we do. Make the most of this free resource to help you become a better developer.

    Learn more about webinars

  • Installfest

    Installfest

    The Ruby on Rails Installfest includes a full setup of your development environment and step-by-step instructions on how to build your first app hosted on Heroku. Over 1,800 attendees to date and counting.

    Learn more about Installfest

  • Development Hub

    Development Hub

    The Ruby on Rails Development Hub is a monthly event where you will get the chance to spend time with our team and others in the community to improve and hone your Ruby on Rails skills.

    Learn more about Development Hub

Get the “reinteractive Review” Monthly Email