diff options
author | Eugene Kenny <elkenny@gmail.com> | 2018-09-26 03:10:51 +0100 |
---|---|---|
committer | Eugene Kenny <elkenny@gmail.com> | 2018-09-28 21:16:21 +0100 |
commit | 0007501669879a8560090897f78cf1d1b69bf30d (patch) | |
tree | a84babeffa2c3d9995ccd48c8dd3ae0ee333069f /activerecord/lib/active_record/railtie.rb | |
parent | b658743ac2a69d196d283e780816f5ad4a305753 (diff) | |
download | rails-0007501669879a8560090897f78cf1d1b69bf30d.tar.gz rails-0007501669879a8560090897f78cf1d1b69bf30d.tar.bz2 rails-0007501669879a8560090897f78cf1d1b69bf30d.zip |
Only define attribute methods from schema cache
To define the attribute methods for a model, Active Record needs to know
the schema of the underlying table, which is usually achieved by making
a request to the database. This is undesirable behaviour while the app
is booting, for two reasons: it makes the boot process dependent on the
availability of the database, and it means every new process will make
one query for each table, which can cause issues for large applications.
However, if the application is using the schema cache dump feature, then
the schema cache already contains the necessary information, and we can
define the attribute methods without causing any extra database queries.
Diffstat (limited to 'activerecord/lib/active_record/railtie.rb')
-rw-r--r-- | activerecord/lib/active_record/railtie.rb | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index 81ad9ef3a2..74ccb98159 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -140,7 +140,19 @@ end_error initializer "active_record.define_attribute_methods" do |app| config.after_initialize do ActiveSupport.on_load(:active_record) do - descendants.each(&:define_attribute_methods) if app.config.eager_load + if app.config.eager_load + descendants.each do |model| + # SchemaMigration and InternalMetadata both override `table_exists?` + # to bypass the schema cache, so skip them to avoid the extra queries. + next if model._internal? + + # If there's no connection yet, or the schema cache doesn't have the columns + # hash for the model cached, `define_attribute_methods` would trigger a query. + next unless model.connected? && model.connection.schema_cache.columns_hash?(model.table_name) + + model.define_attribute_methods + end + end end end end |