11# frozen_string_literal: true
22
3- require "monitor"
4-
53module ActiveRecord
64 module ModelSchema
75 # Encapsulates all schema-context-dependent state for a model.
@@ -17,7 +15,6 @@ def initialize(model_class, context_key)
1715 @model_class = model_class
1816 @context_key = context_key
1917 @schema_loaded = false
20- @load_schema_monitor = Monitor . new
2118
2219 # Schema-context-dependent state
2320 @columns_hash = nil
@@ -41,7 +38,7 @@ def initialize(model_class, context_key)
4138
4239 # Returns the columns hash for this schema context
4340 def columns_hash
44- load_schema unless @columns_hash
41+ model_class . send ( : load_schema) unless @columns_hash
4542 @columns_hash
4643 end
4744
@@ -115,7 +112,7 @@ def attributes_builder
115112
116113 # Returns column defaults hash
117114 def column_defaults
118- load_schema
115+ model_class . send ( : load_schema)
119116 @column_defaults ||= _default_attributes . deep_dup . to_hash . freeze
120117 end
121118
@@ -196,42 +193,30 @@ def initialize_find_by_cache
196193 @find_by_statement_cache = { true => Concurrent ::Map . new , false => Concurrent ::Map . new }
197194 end
198195
199- # Load schema information from the schema cache
200- def load_schema
201- return if schema_loaded?
202- @load_schema_monitor . synchronize do
203- return if schema_loaded?
196+ # Populate this schema context's columns and default attributes
197+ # from the schema cache. Called by the model class's load_schema!.
198+ def load_schema!
199+ return if @schema_loaded
204200
205- model_class . send ( :load_schema! )
206- @schema_loaded = true
207- rescue
208- reload_schema_from_cache
209- raise
201+ unless table_name
202+ raise ActiveRecord ::TableNotSpecified , "#{ model_class } has no table configured. Set one with #{ model_class } .table_name="
210203 end
211- end
212204
213- private
214- def schema_loaded?
215- @schema_loaded
205+ columns_hash = schema_cache . columns_hash ( table_name )
206+ if model_class . only_columns . present?
207+ columns_hash = columns_hash . slice ( *model_class . only_columns )
208+ elsif model_class . ignored_columns . present?
209+ columns_hash = columns_hash . except ( *model_class . ignored_columns )
216210 end
211+ @columns_hash = columns_hash . freeze
217212
218- def load_schema!
219- unless table_name
220- raise ActiveRecord ::TableNotSpecified , "#{ model_class } has no table configured. Set one with #{ model_class } .table_name="
221- end
213+ # Precompute default attributes to cache DB-dependent attribute types
214+ _default_attributes
222215
223- columns_hash = schema_cache . columns_hash ( table_name )
224- if model_class . only_columns . present?
225- columns_hash = columns_hash . slice ( *model_class . only_columns )
226- elsif model_class . ignored_columns . present?
227- columns_hash = columns_hash . except ( *model_class . ignored_columns )
228- end
229- @columns_hash = columns_hash . freeze
230-
231- # Precompute default attributes to cache DB-dependent attribute types
232- _default_attributes
233- end
216+ @schema_loaded = true
217+ end
234218
219+ private
235220 def schema_cache
236221 connection_pool . schema_cache
237222 end
0 commit comments