Skip to content

Commit

Permalink
PoolConfig double-checks its connection class reference for reloading
Browse files Browse the repository at this point in the history
Keeping a reference to the class caused subtle issues when combined
with reloading in development. Now, we take care to try to re-acquire
the class reference by constantizing its name.

That lookup may fail in scenarios where classes are given names but
are not assigned to a constant. In that case, the class can't/won't be
reloaded, so we're not worried about it.

Fixes rails#54343.
  • Loading branch information
flavorjones committed Jan 26, 2025
1 parent 2ca006f commit f985f78
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
7 changes: 7 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
* PoolConfig is careful to handle class reloading.

Keeping a reference to the class caused subtle issues when combined with reloading in
development. Fixes #54343.

*Mike Dalessio*

* Fix SQL notifications sometimes not sent when using async queries.

```ruby
Expand Down
23 changes: 19 additions & 4 deletions activerecord/lib/active_record/connection_adapters/pool_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ class PoolConfig # :nodoc:
include MonitorMixin

attr_reader :db_config, :role, :shard
attr_writer :schema_reflection, :server_version
attr_accessor :connection_class
attr_writer :schema_reflection, :server_version, :connection_class

def schema_reflection
@schema_reflection ||= SchemaReflection.new(db_config.lazy_schema_cache_path)
Expand Down Expand Up @@ -41,11 +40,27 @@ def server_version(connection)
@server_version || synchronize { @server_version ||= connection.get_database_version }
end

def connection_class
case @connection_class
when ConnectionHandler::StringConnectionName
@connection_class
else
if defined?(Rails) && ::Rails.respond_to?(:configuration) && Rails.configuration.reloading_enabled?
begin
@connection_class = @connection_class.name.constantize
rescue NameError
end
end
@connection_class
end
end

def connection_name
if connection_class.primary_class?
klass = connection_class
if klass.primary_class?
"ActiveRecord::Base"
else
connection_class.name
klass.name
end
end

Expand Down

0 comments on commit f985f78

Please sign in to comment.