Skip to content

Commit

Permalink
standardize database_configuration to a hash
Browse files Browse the repository at this point in the history
make connection_url_to_hash a class method

This als prevents loading database.yml if it doesn't exist
but DATABASE_URL does
  • Loading branch information
hone committed Feb 20, 2013
1 parent ebae71a commit 4bdaf95
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def spec
private
def resolve_string_connection(spec) # :nodoc:
hash = configurations.fetch(spec) do |k|
connection_url_to_hash(k)
self.class.connection_url_to_hash(k)
end

raise(AdapterNotSpecified, "#{spec} database is not configured") unless hash
Expand Down Expand Up @@ -69,7 +69,7 @@ def resolve_hash_connection(spec) # :nodoc:
SIMPLE_INT = /\A\d+\z/
SIMPLE_FLOAT = /\A\d+\.\d+\z/

def connection_url_to_hash(url) # :nodoc:
def self.connection_url_to_hash(url) # :nodoc:
config = URI.parse url
adapter = config.scheme
adapter = "postgresql" if adapter == "postgres"
Expand Down
4 changes: 1 addition & 3 deletions activerecord/lib/active_record/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ class Railtie < Rails::Railtie # :nodoc:
# and then establishes the connection.
initializer "active_record.initialize_database" do |app|
ActiveSupport.on_load(:active_record) do
unless ENV['DATABASE_URL']
self.configurations = app.config.database_configuration
end
self.configurations = app.config.database_configuration
establish_connection
end
end
Expand Down
8 changes: 6 additions & 2 deletions railties/lib/rails/application/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,12 @@ def threadsafe!
# contents of the file are processed via ERB before being sent through
# YAML::load.
def database_configuration
require 'erb'
YAML.load ERB.new(IO.read(paths["config/database"].first)).result
if ENV['DATABASE_URL']
{Rails.env => ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.connection_url_to_hash(ENV['DATABASE_URL']).stringify_keys}
else
require 'erb'
YAML.load ERB.new(IO.read(paths["config/database"].first)).result
end
rescue Psych::SyntaxError => e
raise "YAML syntax error occurred while parsing #{paths["config/database"].first}. " \
"Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
Expand Down

0 comments on commit 4bdaf95

Please sign in to comment.