Skip to content

Commit 3c6adf2

Browse files
authored
Merge pull request rails#51168 from Shopify/pool-reflection-assignation
ConnectionPool: handle schema_reflection being re-assigned
2 parents 6534a5d + 932e029 commit 3c6adf2

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ class ConnectionPool
118118
include ConnectionAdapters::AbstractPool
119119

120120
attr_accessor :automatic_reconnect, :checkout_timeout
121-
attr_reader :db_config, :size, :reaper, :pool_config, :async_executor, :role, :shard, :schema_cache
121+
attr_reader :db_config, :size, :reaper, :pool_config, :async_executor, :role, :shard
122122

123-
delegate :schema_reflection, :schema_reflection=, :server_version, to: :pool_config
123+
delegate :schema_reflection, :server_version, to: :pool_config
124124

125125
# Creates a new ConnectionPool object. +pool_config+ is a PoolConfig
126126
# object which describes database connection information (e.g. adapter,
@@ -167,12 +167,21 @@ def initialize(pool_config)
167167

168168
@async_executor = build_async_executor
169169

170-
@schema_cache = BoundSchemaReflection.new(schema_reflection, self)
170+
@schema_cache = nil
171171

172172
@reaper = Reaper.new(self, db_config.reaping_frequency)
173173
@reaper.run
174174
end
175175

176+
def schema_cache
177+
@schema_cache ||= BoundSchemaReflection.new(schema_reflection, self)
178+
end
179+
180+
def schema_reflection=(schema_reflection)
181+
pool_config.schema_reflection = schema_reflection
182+
@schema_cache = nil
183+
end
184+
176185
def migration_context # :nodoc:
177186
MigrationContext.new(migrations_paths, schema_migration, internal_metadata)
178187
end

activerecord/test/cases/connection_pool_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,17 @@ def test_connection_notification_is_called_for_shard
594594
ActiveSupport::Notifications.unsubscribe(subscription) if subscription
595595
end
596596

597+
def test_sets_pool_schema_reflection
598+
pool.schema_cache.add(:posts)
599+
assert pool.schema_cache.cached?(:posts)
600+
601+
pool.schema_reflection = SchemaReflection.new("does-not-exist")
602+
assert_not pool.schema_cache.cached?(:posts)
603+
604+
pool.schema_cache.add(:posts)
605+
assert pool.schema_cache.cached?(:posts)
606+
end
607+
597608
def test_pool_sets_connection_schema_cache
598609
pool.schema_cache.add(:posts)
599610
connection = pool.checkout

0 commit comments

Comments
 (0)