Last Updated: January 23, 2019
·
1.561K
· ahadshafiq

A separate dev-database per git branch

Git branches allow you to develop features separate from the main code base. However, since the database is still in common between the main and feature branches, any database changes that you make inside of feature branches will also affect the main branch.

Adding some erb to your database.yml file will allow you to work on a database which is named after the git branch you're currently working in. So now your code is isolated from the main branches and so is the database.

In your Gemfile:

group :test, :development do
    gem 'git'
end

Inside config/database.yml:

development: ...
adapter: ...
database: appname_development_<%= Git.open('.').current_branch %>
min_messages: ...
host: ...
pool: ...
timeout: ... 
host_names: ...