Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multiple databases in database.yml #39

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 35 additions & 27 deletions lib/language_pack/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -445,18 +445,9 @@ def create_database_yml
File.open("config/database.yml", "w") do |file|
file.puts <<-DATABASE_YML
<%

require 'cgi'
require 'uri'

begin
uri = URI.parse(ENV["DATABASE_URL"])
rescue URI::InvalidURIError
raise "Invalid DATABASE_URL"
end

raise "No RACK_ENV or RAILS_ENV found" unless ENV["RAILS_ENV"] || ENV["RACK_ENV"]

def attribute(name, value, force_string = false)
if value
value_string =
Expand All @@ -471,31 +462,48 @@ def attribute(name, value, force_string = false)
end
end

adapter = uri.scheme
adapter = "postgresql" if adapter == "postgres"
def parse_connection(name, uri)
begin
uri = URI.parse(uri)
rescue
raise "\#{name} has an invalid database uri"
end
connection = {
name: name,
adapter: (uri.scheme == "postgres" ? "postgresql" : uri.scheme),
database: (uri.path || "").split("/")[1],
username: uri.user,
password: uri.password,
host: uri.host,
port: uri.port,
params: CGI.parse(uri.query || "")
}
end

database = (uri.path || "").split("/")[1]
raise "No RACK_ENV or RAILS_ENV found" unless ENV["RAILS_ENV"] || ENV["RACK_ENV"]

username = uri.user
password = uri.password
databases = []

host = uri.host
port = uri.port
databases << parse_connection(ENV["RAILS_ENV"] || ENV["RACK_ENV"], ENV["DATABASE_URL"])

params = CGI.parse(uri.query || "")
ENV["DATABASES"].split(',').each do |database_name|
databases << parse_connection(database_name, ENV["DATABASE_\#{database_name.upcase}_URL"])
end if ENV["DATABASES"]

%>

<%= ENV["RAILS_ENV"] || ENV["RACK_ENV"] %>:
<%= attribute "adapter", adapter %>
<%= attribute "database", database %>
<%= attribute "username", username %>
<%= attribute "password", password, true %>
<%= attribute "host", host %>
<%= attribute "port", port %>

<% params.each do |key, value| %>
<%= key %>: <%= value.first %>
<% databases.each do |database| %>
<%= database[:name] %>:
<%= attribute "adapter", database[:adapter] %>
<%= attribute "database", database[:database] %>
<%= attribute "username", database[:username] %>
<%= attribute "password", database[:password], true %>
<%= attribute "host", database[:host] %>
<%= attribute "port", database[:port] %>

<% database[:params].each do |key, value| %>
<%= key %>: <%= value.first %>
<% end %>
<% end %>
DATABASE_YML
end
Expand Down