aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorArthur Neves <arthurnn@gmail.com>2016-05-04 14:05:31 -0500
committerArthur Neves <arthurnn@gmail.com>2016-05-05 15:29:11 -0500
commit79154a3281eb25a573dfcb5d5db31c3c481311f9 (patch)
treec824f527cbead1f3e6bdaa452ace58b0fa3cceec /activerecord
parentc1bc0d83def740648fdbed05fcc3283dcef1f07d (diff)
downloadrails-79154a3281eb25a573dfcb5d5db31c3c481311f9.tar.gz
rails-79154a3281eb25a573dfcb5d5db31c3c481311f9.tar.bz2
rails-79154a3281eb25a573dfcb5d5db31c3c481311f9.zip
Use spec key, when given as spec_id
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/connection_adapters/connection_specification.rb9
-rw-r--r--activerecord/test/cases/connection_adapters/connection_handler_test.rb34
2 files changed, 23 insertions, 20 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/connection_specification.rb b/activerecord/lib/active_record/connection_adapters/connection_specification.rb
index 5a18e95bcd..f8cdf3ca0c 100644
--- a/activerecord/lib/active_record/connection_adapters/connection_specification.rb
+++ b/activerecord/lib/active_record/connection_adapters/connection_specification.rb
@@ -164,7 +164,7 @@ module ActiveRecord
# spec.config
# # => { "host" => "localhost", "database" => "foo", "adapter" => "sqlite3" }
#
- def spec(config, id = "primary")
+ def spec(config, id = nil)
spec = resolve(config).symbolize_keys
raise(AdapterNotSpecified, "database configuration does not specify adapter") unless spec.key?(:adapter)
@@ -179,6 +179,13 @@ module ActiveRecord
end
adapter_method = "#{spec[:adapter]}_connection"
+
+ id ||=
+ if config.is_a?(Symbol)
+ config.to_s
+ else
+ "primary"
+ end
ConnectionSpecification.new(id, spec, adapter_method)
end
diff --git a/activerecord/test/cases/connection_adapters/connection_handler_test.rb b/activerecord/test/cases/connection_adapters/connection_handler_test.rb
index c3cdb29380..47e8486ded 100644
--- a/activerecord/test/cases/connection_adapters/connection_handler_test.rb
+++ b/activerecord/test/cases/connection_adapters/connection_handler_test.rb
@@ -6,10 +6,19 @@ module ActiveRecord
def setup
@handler = ConnectionHandler.new
resolver = ConnectionAdapters::ConnectionSpecification::Resolver.new Base.configurations
- spec = resolver.spec(:arunit)
-
@spec_id = "primary"
- @pool = @handler.establish_connection(spec)
+ @pool = @handler.establish_connection(resolver.spec(:arunit, @spec_id))
+ end
+
+ def test_establish_connection_uses_spec_id
+ config = {"readonly" => {"adapter" => 'sqlite3'}}
+ resolver = ConnectionAdapters::ConnectionSpecification::Resolver.new(config)
+ spec = resolver.spec(:readonly)
+ @handler.establish_connection(spec)
+
+ assert_not_nil @handler.retrieve_connection_pool('readonly')
+ ensure
+ @handler.remove_connection('readonly')
end
def test_retrieve_connection
@@ -24,31 +33,18 @@ module ActiveRecord
assert !@handler.active_connections?
end
-# def test_retrieve_connection_pool_with_ar_base
-# assert_nil @handler.retrieve_connection_pool(ActiveRecord::Base)
-# end
-
def test_retrieve_connection_pool
assert_not_nil @handler.retrieve_connection_pool(@spec_id)
end
-# def test_retrieve_connection_pool_uses_superclass_when_no_subclass_connection
-# assert_not_nil @handler.retrieve_connection_pool(@subklass)
-# end
-
-# def test_retrieve_connection_pool_uses_superclass_pool_after_subclass_establish_and_remove
-# sub_pool = @handler.establish_connection(@subklass, Base.connection_pool.spec)
-# assert_same sub_pool, @handler.retrieve_connection_pool(@subklass)
-#
-# @handler.remove_connection @subklass
-# assert_same @pool, @handler.retrieve_connection_pool(@subklass)
-# end
+ def test_retrieve_connection_pool_with_invalid_id
+ assert_nil @handler.retrieve_connection_pool("foo")
+ end
def test_connection_pools
assert_equal([@pool], @handler.connection_pools)
end
- # TODO
if Process.respond_to?(:fork)
def test_connection_pool_per_pid
object_id = ActiveRecord::Base.connection.object_id