Skip to content

Instantly share code, notes, and snippets.

@zeroeth
Created March 16, 2011 22:54
Show Gist options
  • Select an option

  • Save zeroeth/873501 to your computer and use it in GitHub Desktop.

Select an option

Save zeroeth/873501 to your computer and use it in GitHub Desktop.
use your git branch and some regex rules to determine which db to use
# This file reads in the current git branch and bases your database name on the rules you setup here
# if no output is given, use the capture from the select.
# if output given, lambda may access the matcher regex object, or just return a string
# match multiple branches, all share a db.
shared_db_for_all_cats:
select: ^(cat)
# match multiple, individual db
db_for_each_cat:
select: (feature_\w?)
# match multiple branches, each has individual db
db_for_each_feature:
select: /^test_feature_\w?/
output: lambda { |matcher| matcher.to_s }
# match multiple branches, all share a db.
shared_db_for_branches:
select: /_similar_branch/
output: lambda { "shared_branch_name" }
defaults: &defaults
adapter: mysql
username: abcd
password: abcd
host: localhost
<%
branch_file = "#{RAILS_ROOT}/config/branch_databases.yml"
branches = File.file?(branch_file) ? YAML.load_file(branch_file) : {}
git_branches = `cd "#{RAILS_ROOT}"; git branch --no-color`
output = ''
if $?.to_i == 0
current_branch = git_branches.split("\n").detect { |b| b.match(/\A\*/) }.gsub(/\A\* /, '')
match = branches.detect{|name,branch| current_branch.match branch['select'] }
if match
if match[1]['output']
output = "_#{eval(match[1]['output']).call(current_branch)}"
else
output = "_#{$&}"
end
end
end
puts "-- Branch database in use: #{output}" unless output.blank?
%>
development:
database: myapp_development<%= output %>
<<: *defaults
production:
database: myapp_<%= output %>
<<: *defaults
test: &test
database: myapp_test<%= output %>
<<: *defaults
cucumber:
<<: *test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment