Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Dynamic Octopus configuration for master/slave horizontal DB scaling with a Rails application on the Heroku stack. See the wiki page for more info: https://github.com/tchandy/octopus/wiki/Replication-with-Rails-on-Heroku. Props to Heroku for the idea, gleaned from the dynamic database.yml they inject at build-time for rails apps.
<%
require 'cgi'
require 'uri'
def attribute(name, value, force_string = false)
if value
value_string =
if force_string
'"' + value + '"'
else
value
end
"#{name}: #{value_string}"
else
""
end
end
configs = case Rails.env
when 'development', 'test'
# use dev and test DB as feaux 'follower'
Array.new(2){YAML::load_file(File.open("config/database.yml"))[Rails.env]}
else
# staging, production, etc with Heroku config vars for follower DBs
master_url = ENV['DATABASE_URL']
slave_keys = ENV.keys.select{|k| k =~ /HEROKU_POSTGRESQL_.*_URL/}
slave_keys.delete_if{ |k| ENV[k] == master_url }
slave_keys.map do |env_key|
config = {}
begin
uri = URI.parse(ENV["#{env_key}"])
rescue URI::InvalidURIError
raise "Invalid DATABASE_URL"
end
raise "No RACK_ENV or RAILS_ENV found" unless ENV["RAILS_ENV"] || ENV["RACK_ENV"]
config['color'] = env_key.match(/HEROKU_POSTGRESQL_(.*)_URL/)[1].downcase
config['adapter'] = uri.scheme
config['adapter'] = "postgresql" if config['adapter'] == "postgres"
config['database'] = (uri.path || "").split("/")[1]
config['username'] = uri.user
config['password'] = uri.password
config['host'] = uri.host
config['port'] = uri.port
config['params'] = CGI.parse(uri.query || "")
config
end
end
whitelist = ENV['SLAVE_ENABLED_FOLLOWERS'].downcase.split(', ') rescue nil
blacklist = ENV['SLAVE_DISABLED_FOLLOWERS'].downcase.split(', ') rescue nil
configs.delete_if do |c|
( whitelist && !c['color'].in?(whitelist) ) || ( blacklist && c['color'].in?(blacklist) )
end
%>
octopus:
replicated: true
fully_replicated: false
environments:
<% if configs.present? %>
<%= "- #{ENV["RAILS_ENV"] || ENV["RACK_ENV"] || Rails.env}" %>
<%= ENV["RAILS_ENV"] || ENV["RACK_ENV"] || Rails.env %>:
followers:
<% configs.each_with_index do |c, i| %>
<%= c.has_key?('color') ? "#{c['color']}_follower" : "follower_#{i + 1}" %>:
<%= attribute "adapter", c['adapter'] %>
<%= attribute "database", c['database'] %>
<%= attribute "username", c['username'] %>
<%= attribute "password", c['password'], true %>
<%= attribute "host", c['host'] %>
<%= attribute "port", c['port'] %>
<% (c['params'] || {}).each do |key, value| %>
<%= key %>: <%= value.first %>
<% end %>
<% end %>
<% else %>
- none
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.