method
establish_connection
v7.1.3.2 -
Show latest stable
-
0 notes -
Class: ActiveRecord::ConnectionAdapters::ConnectionHandler
establish_connection(config, owner_name: Base, role: Base.current_role, shard: Base.current_shard, clobber: false)
public
Hide source
# File activerecord/lib/active_record/connection_adapters/abstract/connection_handler.rb, line 131 def establish_connection(config, owner_name: Base, role: Base.current_role, shard: Base.current_shard, clobber: false) owner_name = determine_owner_name(owner_name, config) pool_config = resolve_pool_config(config, owner_name, role, shard) db_config = pool_config.db_config pool_manager = set_pool_manager(pool_config.connection_name) # If there is an existing pool with the same values as the pool_config # don't remove the connection. Connections should only be removed if we are # establishing a connection on a class that is already connected to a different # configuration. existing_pool_config = pool_manager.get_pool_config(role, shard) if !clobber && existing_pool_config && existing_pool_config.db_config == db_config # Update the pool_config's connection class if it differs. This is used # for ensuring that ActiveRecord::Base and the primary_abstract_class use # the same pool. Without this granular swapping will not work correctly. if owner_name.primary_class? && (existing_pool_config.connection_class != owner_name) existing_pool_config.connection_class = owner_name end existing_pool_config.pool else disconnect_pool_from_pool_manager(pool_manager, role, shard) pool_manager.set_pool_config(role, shard, pool_config) payload = { connection_name: pool_config.connection_name, role: role, shard: shard, config: db_config.configuration_hash } ActiveSupport::Notifications.instrumenter.instrument("!connection.active_record", payload) do pool_config.pool end end end