Skip to content

Commit 3cbc727

Browse files
authored
Merge pull request rails#45564 from eileencodes/replace-owner-and-spec-with-connection_name
Ensure connection_name is used everywhere
2 parents ce9d2a1 + 3dffb0d commit 3cbc727

13 files changed

+72
-72
lines changed

activerecord/lib/active_record/connection_adapters/abstract/connection_handler.rb

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class ConnectionHandler
5656
FINALIZER = lambda { |_| ActiveSupport::ForkTracker.check! }
5757
private_constant :FINALIZER
5858

59-
class StringConnectionOwner # :nodoc:
59+
class StringConnectionName # :nodoc:
6060
attr_reader :name
6161

6262
def initialize(name)
@@ -73,8 +73,8 @@ def current_preventing_writes
7373
end
7474

7575
def initialize
76-
# These caches are keyed by pool_config.connection_specification_name (PoolConfig#connection_specification_name).
77-
@owner_to_pool_manager = Concurrent::Map.new(initial_capacity: 2)
76+
# These caches are keyed by pool_config.connection_name (PoolConfig#connection_name).
77+
@connection_name_to_pool_manager = Concurrent::Map.new(initial_capacity: 2)
7878

7979
# Backup finalizer: if the forked child skipped Kernel#fork the early discard has not occurred
8080
ObjectSpace.define_finalizer self, FINALIZER
@@ -89,24 +89,25 @@ def prevent_writes=(prevent_writes) # :nodoc:
8989
end
9090

9191
def connection_pool_names # :nodoc:
92-
owner_to_pool_manager.keys
92+
connection_name_to_pool_manager.keys
9393
end
9494

9595
def all_connection_pools
96-
owner_to_pool_manager.values.flat_map { |m| m.pool_configs.map(&:pool) }
96+
connection_name_to_pool_manager.values.flat_map { |m| m.pool_configs.map(&:pool) }
9797
end
9898

9999
def connection_pool_list(role = ActiveRecord::Base.current_role)
100-
owner_to_pool_manager.values.flat_map { |m| m.pool_configs(role).map(&:pool) }
100+
connection_name_to_pool_manager.values.flat_map { |m| m.pool_configs(role).map(&:pool) }
101101
end
102102
alias :connection_pools :connection_pool_list
103103

104104
def establish_connection(config, owner_name: Base, role: ActiveRecord::Base.current_role, shard: Base.current_shard)
105-
owner_name = StringConnectionOwner.new(config.to_s) if config.is_a?(Symbol)
105+
owner_name = StringConnectionName.new(config.to_s) if config.is_a?(Symbol)
106+
106107
pool_config = resolve_pool_config(config, owner_name, role, shard)
107108
db_config = pool_config.db_config
108109

109-
pool_manager = set_pool_manager(pool_config.connection_specification_name)
110+
pool_manager = set_pool_manager(pool_config.connection_name)
110111

111112
# If there is an existing pool with the same values as the pool_config
112113
# don't remove the connection. Connections should only be removed if we are
@@ -121,7 +122,7 @@ def establish_connection(config, owner_name: Base, role: ActiveRecord::Base.curr
121122
pool_manager.set_pool_config(role, shard, pool_config)
122123

123124
payload = {
124-
spec_name: pool_config.connection_specification_name,
125+
connection_name: pool_config.connection_name,
125126
shard: shard,
126127
config: db_config.configuration_hash
127128
}
@@ -167,18 +168,18 @@ def flush_idle_connections!(role = ActiveRecord::Base.current_role)
167168
# active or defined connection: if it is the latter, it will be
168169
# opened and set as the active connection for the class it was defined
169170
# for (not necessarily the current class).
170-
def retrieve_connection(spec_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard) # :nodoc:
171-
pool = retrieve_connection_pool(spec_name, role: role, shard: shard)
171+
def retrieve_connection(connection_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard) # :nodoc:
172+
pool = retrieve_connection_pool(connection_name, role: role, shard: shard)
172173

173174
unless pool
174175
if shard != ActiveRecord::Base.default_shard
175-
message = "No connection pool for '#{spec_name}' found for the '#{shard}' shard."
176+
message = "No connection pool for '#{connection_name}' found for the '#{shard}' shard."
176177
elsif ActiveRecord::Base.connection_handler != ActiveRecord::Base.default_connection_handler
177-
message = "No connection pool for '#{spec_name}' found for the '#{ActiveRecord::Base.current_role}' role."
178+
message = "No connection pool for '#{connection_name}' found for the '#{ActiveRecord::Base.current_role}' role."
178179
elsif role != ActiveRecord::Base.default_role
179-
message = "No connection pool for '#{spec_name}' found for the '#{role}' role."
180+
message = "No connection pool for '#{connection_name}' found for the '#{role}' role."
180181
else
181-
message = "No connection pool for '#{spec_name}' found."
182+
message = "No connection pool for '#{connection_name}' found."
182183
end
183184

184185
raise ConnectionNotEstablished, message
@@ -189,36 +190,36 @@ def retrieve_connection(spec_name, role: ActiveRecord::Base.current_role, shard:
189190

190191
# Returns true if a connection that's accessible to this class has
191192
# already been opened.
192-
def connected?(spec_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard)
193-
pool = retrieve_connection_pool(spec_name, role: role, shard: shard)
193+
def connected?(connection_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard)
194+
pool = retrieve_connection_pool(connection_name, role: role, shard: shard)
194195
pool && pool.connected?
195196
end
196197

197-
def remove_connection_pool(owner, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard)
198-
if pool_manager = get_pool_manager(owner)
198+
def remove_connection_pool(connection_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard)
199+
if pool_manager = get_pool_manager(connection_name)
199200
disconnect_pool_from_pool_manager(pool_manager, role, shard)
200201
end
201202
end
202203

203-
# Retrieving the connection pool happens a lot, so we cache it in @owner_to_pool_manager.
204+
# Retrieving the connection pool happens a lot, so we cache it in @connection_name_to_pool_manager.
204205
# This makes retrieving the connection pool O(1) once the process is warm.
205206
# When a connection is established or removed, we invalidate the cache.
206-
def retrieve_connection_pool(owner, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard)
207-
pool_config = get_pool_manager(owner)&.get_pool_config(role, shard)
207+
def retrieve_connection_pool(connection_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard)
208+
pool_config = get_pool_manager(connection_name)&.get_pool_config(role, shard)
208209
pool_config&.pool
209210
end
210211

211212
private
212-
attr_reader :owner_to_pool_manager
213+
attr_reader :connection_name_to_pool_manager
213214

214-
# Returns the pool manager for an owner.
215-
def get_pool_manager(owner)
216-
owner_to_pool_manager[owner]
215+
# Returns the pool manager for a connection name / identifier.
216+
def get_pool_manager(connection_name)
217+
connection_name_to_pool_manager[connection_name]
217218
end
218219

219220
# Get the existing pool manager or initialize and assign a new one.
220-
def set_pool_manager(owner)
221-
owner_to_pool_manager[owner] ||= PoolManager.new
221+
def set_pool_manager(connection_name)
222+
connection_name_to_pool_manager[connection_name] ||= PoolManager.new
222223
end
223224

224225
def disconnect_pool_from_pool_manager(pool_manager, role, shard)
@@ -240,7 +241,7 @@ def disconnect_pool_from_pool_manager(pool_manager, role, shard)
240241
# pool_config.db_config.configuration_hash
241242
# # => { host: "localhost", database: "foo", adapter: "sqlite3" }
242243
#
243-
def resolve_pool_config(config, owner_name, role, shard)
244+
def resolve_pool_config(config, connection_name, role, shard)
244245
db_config = Base.configurations.resolve(config)
245246

246247
raise(AdapterNotSpecified, "database configuration does not specify adapter") unless db_config.adapter
@@ -270,7 +271,7 @@ def resolve_pool_config(config, owner_name, role, shard)
270271
raise AdapterNotFound, "database configuration specifies nonexistent #{db_config.adapter} adapter"
271272
end
272273

273-
ConnectionAdapters::PoolConfig.new(owner_name, db_config, role, shard)
274+
ConnectionAdapters::PoolConfig.new(connection_name, db_config, role, shard)
274275
end
275276
end
276277
end

activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,17 @@ def migration_context # :nodoc:
171171
def schema_migration # :nodoc:
172172
@schema_migration ||= begin
173173
conn = self
174-
spec_name = conn.pool.pool_config.connection_specification_name
174+
connection_name = conn.pool.pool_config.connection_name
175175

176-
return ActiveRecord::SchemaMigration if spec_name == "ActiveRecord::Base"
176+
return ActiveRecord::SchemaMigration if connection_name == "ActiveRecord::Base"
177177

178-
schema_migration_name = "#{spec_name}::SchemaMigration"
178+
schema_migration_name = "#{connection_name}::SchemaMigration"
179179

180180
Class.new(ActiveRecord::SchemaMigration) do
181181
define_singleton_method(:name) { schema_migration_name }
182182
define_singleton_method(:to_s) { schema_migration_name }
183183

184-
self.connection_specification_name = spec_name
184+
self.connection_specification_name = connection_name
185185
end
186186
end
187187
end

activerecord/lib/active_record/connection_adapters/pool_config.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def initialize(connection_class, db_config, role, shard)
2727
INSTANCES[self] = self
2828
end
2929

30-
def connection_specification_name
30+
def connection_name
3131
if connection_class.primary_class?
3232
"ActiveRecord::Base"
3333
else

activerecord/lib/active_record/connection_handling.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ module ConnectionHandling
4848
# may be returned on an error.
4949
def establish_connection(config_or_env = nil)
5050
config_or_env ||= DEFAULT_ENV.call.to_sym
51-
db_config, owner_name = resolve_config_for_connection(config_or_env)
52-
connection_handler.establish_connection(db_config, owner_name: owner_name, role: current_role, shard: current_shard)
51+
db_config, connection_class = resolve_config_for_connection(config_or_env)
52+
connection_handler.establish_connection(db_config, owner_name: connection_class, role: current_role, shard: current_shard)
5353
end
5454

5555
# Connects a model to the databases specified. The +database+ keyword
@@ -87,18 +87,18 @@ def connects_to(database: {}, shards: {})
8787
connections = []
8888

8989
database.each do |role, database_key|
90-
db_config, owner_name = resolve_config_for_connection(database_key)
90+
db_config, connection_class = resolve_config_for_connection(database_key)
9191

9292
self.connection_class = true
93-
connections << connection_handler.establish_connection(db_config, owner_name: owner_name, role: role)
93+
connections << connection_handler.establish_connection(db_config, owner_name: connection_class, role: role)
9494
end
9595

9696
shards.each do |shard, database_keys|
9797
database_keys.each do |role, database_key|
98-
db_config, owner_name = resolve_config_for_connection(database_key)
98+
db_config, connection_class = resolve_config_for_connection(database_key)
9999

100100
self.connection_class = true
101-
connections << connection_handler.establish_connection(db_config, owner_name: owner_name, role: role, shard: shard.to_sym)
101+
connections << connection_handler.establish_connection(db_config, owner_name: connection_class, role: role, shard: shard.to_sym)
102102
end
103103
end
104104

@@ -315,8 +315,8 @@ def clear_cache! # :nodoc:
315315
def resolve_config_for_connection(config_or_env)
316316
raise "Anonymous class is not allowed." unless name
317317

318-
owner_name = primary_class? ? Base.name : name
319-
self.connection_specification_name = owner_name
318+
connection_name = primary_class? ? Base.name : name
319+
self.connection_specification_name = connection_name
320320

321321
db_config = Base.configurations.resolve(config_or_env)
322322
[db_config, self]

activerecord/lib/active_record/test_fixtures.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,13 @@ def setup_fixtures(config = ActiveRecord::Base)
115115

116116
# When connections are established in the future, begin a transaction too
117117
@connection_subscriber = ActiveSupport::Notifications.subscribe("!connection.active_record") do |_, _, _, _, payload|
118-
spec_name = payload[:spec_name] if payload.key?(:spec_name)
118+
connection_name = payload[:connection_name] if payload.key?(:connection_name)
119119
shard = payload[:shard] if payload.key?(:shard)
120120
setup_shared_connection_pool
121121

122-
if spec_name
122+
if connection_name
123123
begin
124-
connection = ActiveRecord::Base.connection_handler.retrieve_connection(spec_name, shard: shard)
124+
connection = ActiveRecord::Base.connection_handler.retrieve_connection(connection_name, shard: shard)
125125
rescue ConnectionNotEstablished
126126
connection = nil
127127
end
@@ -179,7 +179,7 @@ def setup_shared_connection_pool
179179
handler = ActiveRecord::Base.connection_handler
180180

181181
handler.connection_pool_names.each do |name|
182-
pool_manager = handler.send(:owner_to_pool_manager)[name]
182+
pool_manager = handler.send(:connection_name_to_pool_manager)[name]
183183
pool_manager.shard_names.each do |shard_name|
184184
writing_pool_config = pool_manager.get_pool_config(ActiveRecord.writing_role, shard_name)
185185
@saved_pool_configs[name][shard_name] ||= {}
@@ -198,7 +198,7 @@ def teardown_shared_connection_pool
198198
handler = ActiveRecord::Base.connection_handler
199199

200200
@saved_pool_configs.each_pair do |name, shards|
201-
pool_manager = handler.send(:owner_to_pool_manager)[name]
201+
pool_manager = handler.send(:connection_name_to_pool_manager)[name]
202202
shards.each_pair do |shard_name, roles|
203203
roles.each_pair do |role, pool_config|
204204
next unless pool_manager.get_pool_config(role, shard_name)

activerecord/test/cases/connection_adapters/connection_handler_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ConnectionHandlerTest < ActiveRecord::TestCase
1212

1313
def setup
1414
@handler = ConnectionHandler.new
15-
@owner_name = "ActiveRecord::Base"
15+
@connection_name = "ActiveRecord::Base"
1616
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
1717
@pool = @handler.establish_connection(db_config)
1818
end
@@ -210,19 +210,19 @@ def test_symbolized_configurations_assignment
210210
end
211211

212212
def test_retrieve_connection
213-
assert @handler.retrieve_connection(@owner_name)
213+
assert @handler.retrieve_connection(@connection_name)
214214
end
215215

216216
def test_active_connections?
217217
assert_not_predicate @handler, :active_connections?
218-
assert @handler.retrieve_connection(@owner_name)
218+
assert @handler.retrieve_connection(@connection_name)
219219
assert_predicate @handler, :active_connections?
220220
@handler.clear_active_connections!
221221
assert_not_predicate @handler, :active_connections?
222222
end
223223

224224
def test_retrieve_connection_pool
225-
assert_not_nil @handler.retrieve_connection_pool(@owner_name)
225+
assert_not_nil @handler.retrieve_connection_pool(@connection_name)
226226
end
227227

228228
def test_retrieve_connection_pool_with_invalid_id
@@ -391,7 +391,7 @@ def test_retrieve_connection_pool_copies_schema_cache_from_ancestor_pool
391391

392392
pid = fork {
393393
rd.close
394-
pool = @handler.retrieve_connection_pool(@owner_name)
394+
pool = @handler.retrieve_connection_pool(@connection_name)
395395
wr.write Marshal.dump pool.schema_cache.size
396396
wr.close
397397
exit!

activerecord/test/cases/connection_adapters/connection_handlers_multi_db_test.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ConnectionHandlersMultiDbTest < ActiveRecord::TestCase
1212

1313
def setup
1414
@handler = ConnectionHandler.new
15-
@owner_name = "ActiveRecord::Base"
15+
@connection_name = "ActiveRecord::Base"
1616
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
1717
@rw_pool = @handler.establish_connection(db_config)
1818
@ro_pool = @handler.establish_connection(db_config, role: :reading)
@@ -325,15 +325,15 @@ def test_connection_pools
325325
end
326326

327327
def test_retrieve_connection
328-
assert @handler.retrieve_connection(@owner_name)
329-
assert @handler.retrieve_connection(@owner_name, role: :reading)
328+
assert @handler.retrieve_connection(@connection_name)
329+
assert @handler.retrieve_connection(@connection_name, role: :reading)
330330
end
331331

332332
def test_active_connections?
333333
assert_not_predicate @handler, :active_connections?
334334

335-
assert @handler.retrieve_connection(@owner_name)
336-
assert @handler.retrieve_connection(@owner_name, role: :reading)
335+
assert @handler.retrieve_connection(@connection_name)
336+
assert @handler.retrieve_connection(@connection_name, role: :reading)
337337

338338
assert_predicate @handler, :active_connections?
339339

@@ -342,8 +342,8 @@ def test_active_connections?
342342
end
343343

344344
def test_retrieve_connection_pool
345-
assert_not_nil @handler.retrieve_connection_pool(@owner_name)
346-
assert_not_nil @handler.retrieve_connection_pool(@owner_name, role: :reading)
345+
assert_not_nil @handler.retrieve_connection_pool(@connection_name)
346+
assert_not_nil @handler.retrieve_connection_pool(@connection_name, role: :reading)
347347
end
348348

349349
def test_retrieve_connection_pool_with_invalid_id

activerecord/test/cases/connection_adapters/connection_handlers_sharding_db_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class ConnectionHandlersShardingDbTest < ActiveRecord::TestCase
1212

1313
def setup
1414
@handler = ConnectionHandler.new
15-
@owner_name = "ActiveRecord::Base"
1615
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
1716
@rw_pool = @handler.establish_connection(db_config)
1817
@ro_pool = @handler.establish_connection(db_config, role: :reading)
@@ -29,7 +28,7 @@ def test_establishing_a_connection_in_connected_to_block_uses_current_role_and_s
2928
ActiveRecord::Base.establish_connection(db_config)
3029
assert_nothing_raised { Person.first }
3130

32-
assert_equal [:default, :shard_one], ActiveRecord::Base.connection_handler.send(:owner_to_pool_manager).fetch("ActiveRecord::Base").instance_variable_get(:@name_to_role_mapping).values.flat_map(&:keys).uniq
31+
assert_equal [:default, :shard_one], ActiveRecord::Base.connection_handler.send(:connection_name_to_pool_manager).fetch("ActiveRecord::Base").instance_variable_get(:@name_to_role_mapping).values.flat_map(&:keys).uniq
3332
end
3433
end
3534

activerecord/test/cases/connection_pool_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -508,8 +508,8 @@ def test_connection_notification_is_called
508508

509509
@connection_test_model_class.establish_connection :arunit
510510

511-
assert_equal [:config, :shard, :spec_name], payloads[0].keys.sort
512-
assert_equal @connection_test_model_class.name, payloads[0][:spec_name]
511+
assert_equal [:config, :connection_name, :shard], payloads[0].keys.sort
512+
assert_equal @connection_test_model_class.name, payloads[0][:connection_name]
513513
assert_equal ActiveRecord::Base.default_shard, payloads[0][:shard]
514514
ensure
515515
ActiveSupport::Notifications.unsubscribe(subscription) if subscription
@@ -522,8 +522,8 @@ def test_connection_notification_is_called_for_shard
522522
end
523523
@connection_test_model_class.connects_to shards: { shard_two: { writing: :arunit } }
524524

525-
assert_equal [:config, :shard, :spec_name], payloads[0].keys.sort
526-
assert_equal @connection_test_model_class.name, payloads[0][:spec_name]
525+
assert_equal [:config, :connection_name, :shard], payloads[0].keys.sort
526+
assert_equal @connection_test_model_class.name, payloads[0][:connection_name]
527527
assert_equal :shard_two, payloads[0][:shard]
528528
ensure
529529
ActiveSupport::Notifications.unsubscribe(subscription) if subscription

0 commit comments

Comments
 (0)