aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorGlenn Gillen <me@glenngillen.com>2011-06-28 19:10:57 +0100
committerAaron Patterson <aaron.patterson@gmail.com>2011-07-20 16:23:31 -0700
commit89357c8f834f638210d004ae9426aaa052dd519b (patch)
tree1e25154b15baff421da517c54c84f25aae3e483c /activerecord/lib/active_record
parent22e47279bc5a905b2535b9bbf7061ca72e8d3b71 (diff)
downloadrails-89357c8f834f638210d004ae9426aaa052dd519b.tar.gz
rails-89357c8f834f638210d004ae9426aaa052dd519b.tar.bz2
rails-89357c8f834f638210d004ae9426aaa052dd519b.zip
Provide database connection settings as a URL.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb22
1 files changed, 22 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb
index bcd3abc08d..d008aabcae 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb
@@ -58,6 +58,8 @@ module ActiveRecord
when Symbol, String
if configuration = configurations[spec.to_s]
establish_connection(configuration)
+ elsif spec.is_a?(String) && hash = connection_url_to_hash(spec)
+ establish_connection(hash)
else
raise AdapterNotSpecified, "#{spec} database is not configured"
end
@@ -81,6 +83,26 @@ module ActiveRecord
end
end
+ def self.connection_url_to_hash(url)
+ config = URI.parse(url)
+ adapter = config.scheme
+ adapter = "postgresql" if adapter == "postgres"
+ spec = { :adapter => adapter,
+ :username => config.user,
+ :password => config.password,
+ :port => config.port,
+ :database => config.path.sub(%r{^/},""),
+ :host => config.host }
+ spec.reject!{ |key,value| value.nil? }
+ if config.query
+ options = Hash[query.split("&").map{ |pair| pair.split("=") }].symbolize_keys
+ spec.merge!(options)
+ end
+ spec
+ rescue URI::InvalidURIError
+ return nil
+ end
+
class << self
# Returns the connection currently associated with the class. This can
# also be used to "borrow" the connection to do database work unrelated