Skip to content

Instantly share code, notes, and snippets.

@chuckbergeron
Created March 15, 2012 17:37
Show Gist options
  • Save chuckbergeron/2045523 to your computer and use it in GitHub Desktop.
Save chuckbergeron/2045523 to your computer and use it in GitHub Desktop.
One Database Per Branch, Per Environment
CURRENT_BRANCH = `git status | head -1`.to_s.gsub('# On branch ','').chomp
<%
database_prefix = 'app'
environments = %W( development test )
%>
defaults: &defaults
pool: 5
adapter: mysql2
encoding: utf8
reconnect: false
username: root
password:
host: localhost
<% environments.each do |environment| %>
<%= environment %>:
<<: *defaults
database: <%= [ database_prefix, CURRENT_BRANCH, environment ].join('_').underscore %>
<% end %>
@chuckbergeron
Copy link
Author

If you want to include this dynamic database file in a rake or capistrano task you may need to do something like this:

  CURRENT_BRANCH = `git status | head -1`.to_s.gsub('# On branch ','').chomp
  @environment_info = YAML::load( ERB.new( IO.read( File.join( 'config', 'database.yml' ) ) ).result )["development"]

@chuckbergeron
Copy link
Author

Just added a .underscore call at the end of the interpolated database string. Wasn't happy when a git branch had a "-" in it.

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