Skip to content

Commit ed76d0f

Browse files
authored
Merge pull request rails#51091 from Shopify/pool-schema-cache
Refactor SchemaCache to hold a ConnectionPool
2 parents 024f2de + 1af13c6 commit ed76d0f

File tree

22 files changed

+189
-147
lines changed

22 files changed

+189
-147
lines changed

activerecord/lib/active_record/attribute_methods/primary_key.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def get_primary_key(base_name) # :nodoc:
109109
elsif base_name && primary_key_prefix_type == :table_name_with_underscore
110110
base_name.foreign_key
111111
elsif ActiveRecord::Base != self && table_exists?
112-
connection.schema_cache.primary_keys(table_name)
112+
schema_cache.primary_keys(table_name)
113113
else
114114
"id"
115115
end

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def schema_reflection
3636
SchemaReflection.new(nil)
3737
end
3838

39+
def schema_cache; end
3940
def connection_class; end
4041
def checkin(_); end
4142
def remove(_); end
@@ -117,7 +118,7 @@ class ConnectionPool
117118
include ConnectionAdapters::AbstractPool
118119

119120
attr_accessor :automatic_reconnect, :checkout_timeout
120-
attr_reader :db_config, :size, :reaper, :pool_config, :async_executor, :role, :shard
121+
attr_reader :db_config, :size, :reaper, :pool_config, :async_executor, :role, :shard, :schema_cache
121122

122123
delegate :schema_reflection, :schema_reflection=, :server_version, to: :pool_config
123124

@@ -166,6 +167,8 @@ def initialize(pool_config)
166167

167168
@async_executor = build_async_executor
168169

170+
@schema_cache = BoundSchemaReflection.new(schema_reflection, self)
171+
169172
@reaper = Reaper.new(self, db_config.reaping_frequency)
170173
@reaper.run
171174
end
@@ -588,6 +591,7 @@ def attempt_to_checkout_all_existing_connections(raise_on_acquisition_timeout =
588591
loop do
589592
synchronize do
590593
return if collected_conns.size == @connections.size && @now_connecting == 0
594+
591595
remaining_timeout = timeout_time - Process.clock_gettime(Process::CLOCK_MONOTONIC)
592596
remaining_timeout = 0 if remaining_timeout < 0
593597
conn = checkout_for_exclusive_access(remaining_timeout)

activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ def pool=(value)
5050
@schema_cache = nil
5151
@pool = value
5252

53-
@pool.schema_reflection.load!(self) if ActiveRecord.lazily_load_schema_cache
53+
if @pool && ActiveRecord.lazily_load_schema_cache
54+
@pool.schema_reflection.load!(@pool)
55+
end
5456
end
5557

5658
set_callback :checkin, :after, :enable_lazy_transactions!
@@ -321,7 +323,7 @@ def shard
321323
end
322324

323325
def schema_cache
324-
@schema_cache ||= BoundSchemaReflection.new(@pool.schema_reflection, self)
326+
@pool.schema_cache || (@schema_cache ||= BoundSchemaReflection.for_lone_connection(@pool.schema_reflection, self))
325327
end
326328

327329
# this method must only be called while holding connection pool's mutex

0 commit comments

Comments
 (0)