TbBranchDb

Rake tasks to easy-peasy branch a database in your Rails project.

The tasks basically implement the commands describe here:

mislav.uniqpath.com/rails/branching-the-database-along-with-your-code/

See the above link for further information about the branching problem and the way it works.

Requirements

  • Git as version control system

  • MySQL as database

  • Rails (this gem works only inside a Rails project)

Installation

Add the gem to your bundler Gemfile:

# Gemfile
...
group :development do
  ...
  gem 'tb_branch_db'
end

And run:

$ bundle install

Usage

First update your config/database.yml to allow branching:

$ rake db:branch:setup

This will update database.yml like so:

# config/database.yml
<%
  # http://mislav.uniqpath.com/rails/branching-the-database-along-with-your-code/
  branch = `git symbolic-ref HEAD 2>/dev/null`.chomp.sub('refs/heads/', '')
  suffix = `git config --bool branch.#{branch}.database`.chomp == 'true' ? "_#{branch}" : ""
%>
development:
  ...
  database: onappto_development<%= suffix %>
...

A backup of the original database.yml is saved to config/database.yml.orig.

Then create a branch (a clone) of your master database:

$ rake db:branch

This will first create an entry in the .git/config and then clones the master database.

# example output
git config --bool branch.BRANCH.database true
rake db:create
mysqldump -u root foo_development | mysql -u root foo_development_BRANCH

If you want to cleanup the branch run:

$ rake db:branch:cleanup

# example output
git config --bool branch.BRANCH.database false
mysql -u root -e 'drop database onappto_development_BRANCH'

Alternatives

Credits