diff options
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb | 8 | ||||
-rw-r--r-- | activerecord/test/cases/connection_management_test.rb | 34 |
2 files changed, 37 insertions, 5 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 ca83173d3a..ab07253e86 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb @@ -89,8 +89,8 @@ module ActiveRecord end end - def self.connection_url_to_hash(url) - config = URI.parse(url) + def self.connection_url_to_hash(url) # :nodoc: + config = URI.parse url adapter = config.scheme adapter = "postgresql" if adapter == "postgres" spec = { :adapter => adapter, @@ -99,14 +99,12 @@ module ActiveRecord :port => config.port, :database => config.path.sub(%r{^/},""), :host => config.host } - spec.reject!{ |key,value| value.nil? } + spec.reject!{ |_,value| !value } if config.query options = Hash[config.query.split("&").map{ |pair| pair.split("=") }].symbolize_keys spec.merge!(options) end spec - rescue URI::InvalidURIError - return nil end class << self diff --git a/activerecord/test/cases/connection_management_test.rb b/activerecord/test/cases/connection_management_test.rb index a1d1177289..f554ceef35 100644 --- a/activerecord/test/cases/connection_management_test.rb +++ b/activerecord/test/cases/connection_management_test.rb @@ -25,6 +25,40 @@ module ActiveRecord assert ActiveRecord::Base.connection_handler.active_connections? end + class FakeBase < ActiveRecord::Base + def self.establish_connection spec + String === spec ? super : spec + end + end + + def test_url_host_no_db + spec = FakeBase.establish_connection 'postgres://foo?encoding=utf8' + assert_equal({ + :adapter => "postgresql", + :database => "", + :host => "foo", + :encoding => "utf8" }, spec) + end + + def test_url_host_db + spec = FakeBase.establish_connection 'postgres://foo/bar?encoding=utf8' + assert_equal({ + :adapter => "postgresql", + :database => "bar", + :host => "foo", + :encoding => "utf8" }, spec) + end + + def test_url_port + spec = FakeBase.establish_connection 'postgres://foo:123?encoding=utf8' + assert_equal({ + :adapter => "postgresql", + :database => "", + :port => 123, + :host => "foo", + :encoding => "utf8" }, spec) + end + def test_app_delegation manager = ConnectionManagement.new(@app) |