Skip to content
Helpers for the Branched Database Pattern in Ruby
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.



Branchy is a library for getting the name of the branch you're on. Lame, right? But its real purpose is to make it very easy to use the branched database pattern in Ruby, where each branch (and tag) in your project has its own database. Rails was the original use-case, but there's nothing Rails-specific in it - you should be able to use it with just about any Ruby ORM. That said, all examples assume Rails and ActiveRecord. Salt to taste.

Supported SCMs:

  • git

The Branched Database Pattern

Do you use feature branches for development? Are you in migration hell, fighting merge conflicts in schema.rb and running into exceptions caused by database changes from other branches? The branched database pattern might be what you're looking for. Each branch has its own database, isolating it from any other branch's changes.

NOTE Branched databases work best when everyone on your team uses them and practices good database migration habits. A more complete write-up on that subject can be found at These are the highlights:

  • Use schema.rb to create your database (rake db:schema:load or rake db:setup).

  • After running migrations locally, commit the resulting changes in schema.rb.

  • Use branched databases to keep each branch's schema changes isolated.

  • Remember to restart your local Rails server after switching branches.


In your Gemfile:

group :development, :test do
  gem 'branchy', git: 'git://'

Use in database.yml

You probably only want to use it in your development and test environments. For a branch called “widgets” the database would be “foobar_development_widgets”.

  database: <%= Branchy.git.db 'foobar_development' %>

A database for EVERY branch? Overkill much?

Maybe. If you have lots branches without schema changes, you might want to enable branchy on a per-branch basis. There's a command-line utility for this:

$ git checkout my_branch
$ branchy --enable

Then in your database.yml file use the “db_if_enabled” method. It the current branch isn't branchy-enabled it will simply return whatever string your passed.

  database: <%= Branchy.git.db_if_enabled 'foobar_development' %>

You don't even NEED this library

It's really just a convenient way to share the idea. You can get 90% of the functionality in database.yml with:

<% branch = `git rev-parse --abbrev-ref HEAD`.chomp %>

  database: my_project_development_<%= branch %>


Copyright 2014 Consolo Services Group

Licensed under the MIT License

You can’t perform that action at this time.