Skip to content

Commit 8dc7755

Browse files
skipkayhilandrewn617
authored andcommitted
cleanup, cleanup, everybody everywhere
1 parent f6aef29 commit 8dc7755

3 files changed

Lines changed: 14 additions & 32 deletions

File tree

activerecord/lib/active_record/database_configurations/hash_config.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,7 @@ def use_metadata_table? # :nodoc:
195195
configuration_hash.fetch(:use_metadata_table, true)
196196
end
197197

198-
# The schema context key groups connections that share a schema shape.
199-
# Multiple pools (read replicas, shards) can share the same key, meaning
200-
# they share cached schema information. Defaults to "default".
201-
def schema_context_key # :nodoc:
198+
def schema_context # :nodoc:
202199
configuration_hash.fetch(:schema_context, "default").to_s
203200
end
204201

activerecord/lib/active_record/model_schema.rb

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -200,23 +200,17 @@ def self.derive_join_table_name(first_table, second_table) # :nodoc:
200200
end
201201

202202
module ClassMethods
203-
# Returns the current schema context key from the connection configuration.
204-
# Defaults to "default" if no connection is available or no context is configured.
205-
def current_schema_context_key # :nodoc:
206-
connection_db_config.schema_context_key
203+
def current_schema_context # :nodoc:
204+
connection_db_config.schema_context
207205
rescue ConnectionNotDefined
208206
"default"
209207
end
210208

211-
# Returns the Schema instance for the current schema context.
212-
# Each context has its own Schema instance that caches schema-dependent state.
213209
def model_schema # :nodoc:
214-
context_key = current_schema_context_key
215-
@model_schemas ||= {}
216-
@model_schemas[context_key] ||= Schema.new(self, context_key)
210+
context_key = current_schema_context
211+
model_schemas[context_key] ||= Schema.new(self, context_key)
217212
end
218213

219-
# Returns all Schema instances for this model (across all contexts).
220214
def model_schemas # :nodoc:
221215
@model_schemas ||= {}
222216
end
@@ -575,9 +569,9 @@ def load_schema
575569

576570
model_schema.load_schema!
577571

578-
unless @model_schema_loaded
572+
unless @any_schema_loaded
579573
load_schema!
580-
@model_schema_loaded = true
574+
@any_schema_loaded = true
581575
end
582576
rescue
583577
reload_schema_from_cache # If the schema loading failed half way through, we must reset the state.
@@ -591,7 +585,7 @@ def initialize_load_schema_monitor
591585
end
592586

593587
def reload_schema_from_cache(recursive = true)
594-
@model_schema_loaded = false
588+
@any_schema_loaded = false
595589
@attribute_names = nil
596590

597591
# Reset all Schema instances
@@ -608,15 +602,15 @@ def reload_schema_from_cache(recursive = true)
608602
def inherited(child_class)
609603
super
610604
child_class.initialize_load_schema_monitor
605+
child_class.reload_schema_from_cache(false)
611606
child_class.class_eval do
612-
@model_schemas = {}
613607
@ignored_columns = nil
614608
@only_columns = nil
615609
end
616610
end
617611

618612
def schema_loaded?
619-
model_schema.instance_variable_get(:@schema_loaded)
613+
model_schema.schema_loaded?
620614
end
621615

622616
def load_schema!

activerecord/lib/active_record/model_schema/schema.rb

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def load_schema!
160160
raise ActiveRecord::TableNotSpecified, "#{model_class} has no table configured. Set one with #{model_class}.table_name="
161161
end
162162

163-
columns_hash = schema_cache.columns_hash(table_name)
163+
columns_hash = model_class.connection_pool.schema_cache.columns_hash(table_name)
164164
if model_class.only_columns.present?
165165
columns_hash = columns_hash.slice(*model_class.only_columns)
166166
elsif model_class.ignored_columns.present?
@@ -174,18 +174,9 @@ def load_schema!
174174
@schema_loaded = true
175175
end
176176

177-
private
178-
def schema_cache
179-
connection_pool.schema_cache
180-
end
181-
182-
def connection_pool
183-
model_class.connection_pool
184-
end
185-
186-
def connection
187-
model_class.lease_connection
188-
end
177+
def schema_loaded?
178+
@schema_loaded
179+
end
189180
end
190181
end
191182
end

0 commit comments

Comments
 (0)