Skip to content

Instantly share code, notes, and snippets.

@buren
Last active June 1, 2017 07:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save buren/6961940 to your computer and use it in GitHub Desktop.
Save buren/6961940 to your computer and use it in GitHub Desktop.
Custom database for rails branch

Custom database for Rails branch

(Below is an example with MySQL)

Customize config/database.yml to handle multiple databases.

<%
  # How to setup a custom database for a branch
  branch = `git symbolic-ref HEAD 2>/dev/null`.chomp.sub('refs/heads/', '')
  suffix = `git config --bool branch.#{branch}.database`.chomp == 'true' ? "_#{branch}" : "_development"

  db_socket = ENV['DEV_MYSQL_SOCKET'] || "/tmp/mysql.sock" # Check env var for db socket path, defaults to OSX db socket path
%>
development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: portal<%= suffix %>
  pool: 5
  username: root
  password:
  socket: <%= db_socket %>

Create a new branch

git branch your_new_branch
git checkout your_new_branch

Opt-in for the branch specific database

git config --bool branch.your_new_branch.database true

Create the new database and load schema.rb

rake db:create

Initialize the database without previous database content run

rake db:schema:load

To copy the current development database to the newly created one (example for MySQL)

mysqldump -u root yourapp_development | mysql -u root yourapp_your_new_branch

(Make sure that the database you are copying from is the same branch as your_new_branch was branched form.)

Check: config/database.yml

Verify that the database has successfully been created

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment