2017-07-09 13:41:28 -04:00
# frozen_string_literal: true
2010-02-04 12:29:18 -05:00
require " active_record "
require " rails "
2019-08-01 05:43:25 -04:00
require " active_support/core_ext/object/try "
2010-02-04 12:29:18 -05:00
require " active_model/railtie "
2009-12-23 17:55:12 -05:00
# For now, action_controller must always be present with
2016-07-12 02:51:36 -04:00
# Rails, so let's make sure that it gets required before
2009-12-23 17:55:12 -05:00
# here. This is needed for correctly setting up the middleware.
# In the future, this might become an optional require.
2009-12-31 16:11:54 -05:00
require " action_controller/railtie "
2009-12-23 17:55:12 -05:00
module ActiveRecord
2010-06-16 13:45:04 -04:00
# = Active Record Railtie
2012-09-22 00:38:18 -04:00
class Railtie < Rails :: Railtie # :nodoc:
2010-03-26 13:47:55 -04:00
config . active_record = ActiveSupport :: OrderedOptions . new
2009-12-27 07:32:40 -05:00
2016-08-06 13:37:57 -04:00
config . app_generators . orm :active_record , migration : true ,
timestamps : true
2010-01-28 13:45:25 -05:00
2011-12-01 13:16:19 -05:00
config . action_dispatch . rescue_responses . merge! (
2016-08-06 12:24:04 -04:00
" ActiveRecord::RecordNotFound " = > :not_found ,
" ActiveRecord::StaleObjectError " = > :conflict ,
" ActiveRecord::RecordInvalid " = > :unprocessable_entity ,
" ActiveRecord::RecordNotSaved " = > :unprocessable_entity
2011-12-01 13:16:19 -05:00
)
2012-08-01 10:16:04 -04:00
config . active_record . use_schema_cache_dump = true
2013-12-29 06:05:04 -05:00
config . active_record . maintain_test_schema = true
2019-10-10 15:44:03 -04:00
config . active_record . has_many_inversing = false
2012-08-01 10:16:04 -04:00
2017-07-13 19:01:49 -04:00
config . active_record . sqlite3 = ActiveSupport :: OrderedOptions . new
config . active_record . sqlite3 . represent_boolean_as_integer = nil
2012-08-01 14:54:22 -04:00
config . eager_load_namespaces << ActiveRecord
2009-12-28 20:37:18 -05:00
rake_tasks do
2013-06-20 21:46:49 -04:00
namespace :db do
task :load_config do
2014-01-01 17:33:59 -05:00
ActiveRecord :: Tasks :: DatabaseTasks . database_configuration = Rails . application . config . database_configuration
2013-12-23 14:15:52 -05:00
2016-01-20 18:11:58 -05:00
if defined? ( ENGINE_ROOT ) && engine = Rails :: Engine . find ( ENGINE_ROOT )
2016-08-06 12:24:04 -04:00
if engine . paths [ " db/migrate " ] . existent
ActiveRecord :: Tasks :: DatabaseTasks . migrations_paths += engine . paths [ " db/migrate " ] . to_a
2013-06-20 21:46:49 -04:00
end
end
2013-04-24 09:37:55 -04:00
end
end
2009-12-30 22:24:00 -05:00
load " active_record/railties/databases.rake "
2009-12-28 20:37:18 -05:00
end
2011-05-04 10:31:06 -04:00
# When loading console, force ActiveRecord::Base to be loaded
# to avoid cross references when loading a constant for the
# first time. Also, make it output to STDERR.
2011-05-24 19:37:55 -04:00
console do | app |
2017-10-21 09:17:03 -04:00
require " active_record/railties/console_sandbox " if app . sandbox?
require " active_record/base "
2016-01-05 17:23:43 -05:00
unless ActiveSupport :: Logger . logger_outputs_to? ( Rails . logger , STDERR , STDOUT )
console = ActiveSupport :: Logger . new ( STDERR )
Rails . logger . extend ActiveSupport :: Logger . broadcast console
end
2018-01-12 09:50:50 -05:00
ActiveRecord :: Base . verbose_query_logs = false
2010-07-17 04:59:41 -04:00
end
2013-03-14 01:17:13 -04:00
runner do
2017-10-21 09:17:03 -04:00
require " active_record/base "
2012-05-29 10:43:46 -04:00
end
2010-01-22 14:44:29 -05:00
initializer " active_record.initialize_timezone " do
2010-03-29 20:08:08 -04:00
ActiveSupport . on_load ( :active_record ) do
2010-03-07 09:24:30 -05:00
self . time_zone_aware_attributes = true
self . default_timezone = :utc
end
2010-01-22 14:44:29 -05:00
end
2010-01-24 19:06:12 -05:00
initializer " active_record.logger " do
2010-03-29 20:08:08 -04:00
ActiveSupport . on_load ( :active_record ) { self . logger || = :: Rails . logger }
2010-01-24 19:06:12 -05:00
end
2018-08-05 21:06:01 -04:00
initializer " active_record.backtrace_cleaner " do
ActiveSupport . on_load ( :active_record ) { LogSubscriber . backtrace_cleaner = :: Rails . backtrace_cleaner }
end
2019-10-04 00:49:41 -04:00
initializer " active_record.migration_error " do | app |
2012-06-05 20:15:16 -04:00
if config . active_record . delete ( :migration_error ) == :page_load
2015-08-08 16:15:28 -04:00
config . app_middleware . insert_after :: ActionDispatch :: Callbacks ,
2019-10-04 00:49:41 -04:00
ActiveRecord :: Migration :: CheckPending ,
file_watcher : app . config . file_watcher
2012-06-05 20:15:16 -04:00
end
end
2019-01-17 13:33:48 -05:00
initializer " active_record.database_selector " do
2019-02-01 10:16:34 -05:00
if options = config . active_record . delete ( :database_selector )
2019-01-17 13:33:48 -05:00
resolver = config . active_record . delete ( :database_resolver )
2019-02-07 18:36:08 -05:00
operations = config . active_record . delete ( :database_resolver_context )
2019-02-01 10:16:34 -05:00
config . app_middleware . use ActiveRecord :: Middleware :: DatabaseSelector , resolver , operations , options
2019-01-17 13:33:48 -05:00
end
end
2018-09-20 14:56:07 -04:00
initializer " Check for cache versioning support " do
config . after_initialize do | app |
ActiveSupport . on_load ( :active_record ) do
if app . config . active_record . cache_versioning && Rails . cache
2018-09-20 21:40:31 -04:00
unless Rails . cache . class . try ( :supports_cache_versioning? )
2018-09-20 14:56:07 -04:00
raise <<-end_error
2018-09-20 21:40:31 -04:00
You 're using a cache store that doesn' t support native cache versioning .
Your best option is to upgrade to a newer version of #{Rails.cache.class}
that supports cache versioning ( #{Rails.cache.class}.supports_cache_versioning? #=> true).
2018-09-20 14:56:07 -04:00
2018-09-20 21:40:31 -04:00
Next best , switch to a different cache store that does support cache versioning :
https : / / guides . rubyonrails . org / caching_with_rails . html #cache-stores.
To keep using the current cache store , you can turn off cache versioning entirely :
2018-09-20 14:56:07 -04:00
2018-09-20 21:40:31 -04:00
config . active_record . cache_versioning = false
2018-09-20 14:56:07 -04:00
2019-11-23 19:44:32 -05:00
end_error
2018-09-20 14:56:07 -04:00
end
end
end
end
end
2012-09-08 03:11:42 -04:00
initializer " active_record.check_schema_cache_dump " do
2012-08-01 10:16:04 -04:00
if config . active_record . delete ( :use_schema_cache_dump )
config . after_initialize do | app |
ActiveSupport . on_load ( :active_record ) do
2016-11-13 22:18:14 -05:00
filename = File . join ( app . config . paths [ " db " ] . first , " schema_cache.yml " )
2012-09-22 00:38:18 -04:00
2012-08-01 10:16:04 -04:00
if File . file? ( filename )
2017-12-01 04:16:06 -05:00
current_version = ActiveRecord :: Migrator . current_version
2018-01-10 10:25:13 -05:00
2017-12-01 04:16:06 -05:00
next if current_version . nil?
2016-11-13 22:18:14 -05:00
cache = YAML . load ( File . read ( filename ) )
2017-12-01 04:16:06 -05:00
if cache . version == current_version
2017-01-05 03:20:57 -05:00
connection_pool . schema_cache = cache . dup
2012-08-01 10:16:04 -04:00
else
2017-12-01 04:16:06 -05:00
warn " Ignoring db/schema_cache.yml because it has expired. The current schema version is #{ current_version } , but the one in the cache is #{ cache . version } . "
2012-08-01 10:16:04 -04:00
end
end
end
end
end
end
2018-09-23 22:20:35 -04:00
initializer " active_record.define_attribute_methods " do | app |
config . after_initialize do
ActiveSupport . on_load ( :active_record ) do
2018-09-25 22:10:51 -04:00
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
2018-09-23 22:20:35 -04:00
end
end
end
2015-02-07 15:33:13 -05:00
initializer " active_record.warn_on_records_fetched_greater_than " do
if config . active_record . warn_on_records_fetched_greater_than
ActiveSupport . on_load ( :active_record ) do
2017-10-21 09:17:03 -04:00
require " active_record/relation/record_fetch_warning "
2015-02-07 15:33:13 -05:00
end
end
end
2009-12-23 17:55:12 -05:00
initializer " active_record.set_configs " do | app |
2010-03-29 20:08:08 -04:00
ActiveSupport . on_load ( :active_record ) do
2019-01-14 18:06:01 -05:00
configs = app . config . active_record
represent_boolean_as_integer = configs . sqlite3 . delete ( :represent_boolean_as_integer )
unless represent_boolean_as_integer . nil?
ActiveSupport . on_load ( :active_record_sqlite3adapter ) do
ActiveRecord :: ConnectionAdapters :: SQLite3Adapter . represent_boolean_as_integer = represent_boolean_as_integer
end
end
2017-07-13 19:01:49 -04:00
configs . delete ( :sqlite3 )
2019-01-14 18:06:01 -05:00
2017-07-13 19:01:49 -04:00
configs . each do | k , v |
2010-03-07 09:24:30 -05:00
send " #{ k } = " , v
end
2009-12-23 17:55:12 -05:00
end
end
# This sets the database configuration from Configuration#database_configuration
# and then establishes the connection.
2015-10-05 22:56:02 -04:00
initializer " active_record.initialize_database " do
2010-03-29 20:08:08 -04:00
ActiveSupport . on_load ( :active_record ) do
2019-02-01 11:33:48 -05:00
self . connection_handlers = { writing_role = > ActiveRecord :: Base . default_connection_handler }
2014-04-01 17:45:15 -04:00
self . configurations = Rails . application . config . database_configuration
2018-10-03 20:19:48 -04:00
establish_connection
2010-03-07 09:24:30 -05:00
end
2009-12-23 17:55:12 -05:00
end
2009-12-30 22:24:00 -05:00
# Expose database runtime to controller for logging.
2013-03-14 01:17:13 -04:00
initializer " active_record.log_runtime " do
2017-10-21 09:17:03 -04:00
require " active_record/railties/controller_runtime "
2010-03-29 20:08:08 -04:00
ActiveSupport . on_load ( :action_controller ) do
2010-03-07 09:24:30 -05:00
include ActiveRecord :: Railties :: ControllerRuntime
end
2009-12-30 22:24:00 -05:00
end
2017-11-20 23:38:21 -05:00
initializer " active_record.collection_cache_association_loading " do
require " active_record/railties/collection_cache_association_loading "
ActiveSupport . on_load ( :action_view ) do
ActionView :: PartialRenderer . prepend ( ActiveRecord :: Railties :: CollectionCacheAssociationLoading )
end
end
2016-02-21 20:25:52 -05:00
initializer " active_record.set_reloader_hooks " do
2012-06-15 07:09:19 -04:00
ActiveSupport . on_load ( :active_record ) do
2016-02-21 20:25:52 -05:00
ActiveSupport :: Reloader . before_class_unload do
2012-11-12 20:24:10 -05:00
if ActiveRecord :: Base . connected?
ActiveRecord :: Base . clear_cache!
2015-06-11 12:37:53 -04:00
ActiveRecord :: Base . clear_reloadable_connections!
2012-11-12 20:24:10 -05:00
end
2010-03-07 09:24:30 -05:00
end
2010-01-14 13:53:07 -05:00
end
end
2016-02-21 20:25:52 -05:00
initializer " active_record.set_executor_hooks " do
2018-09-12 10:09:59 -04:00
ActiveRecord :: QueryCache . install_executor_hooks
2016-02-21 20:25:52 -05:00
end
2011-12-12 16:51:33 -05:00
initializer " active_record.add_watchable_files " do | app |
2012-10-08 07:53:25 -04:00
path = app . paths [ " db " ] . first
config . watchable_files . concat [ " #{ path } /schema.rb " , " #{ path } /structure.sql " ]
2011-12-12 16:51:33 -05:00
end
2017-02-17 18:56:48 -05:00
initializer " active_record.clear_active_connections " do
config . after_initialize do
ActiveSupport . on_load ( :active_record ) do
2017-11-25 00:35:13 -05:00
# Ideally the application doesn't connect to the database during boot,
# but sometimes it does. In case it did, we want to empty out the
# connection pools so that a non-database-using process (e.g. a master
# process in a forking server model) doesn't retain a needless
# connection. If it was needed, the incremental cost of reestablishing
# this connection is trivial: the rest of the pool would need to be
# populated anyway.
2017-02-17 18:56:48 -05:00
clear_active_connections!
2017-11-25 00:35:13 -05:00
flush_idle_connections!
2017-02-17 18:56:48 -05:00
end
end
end
2017-07-06 12:59:33 -04:00
2018-09-06 21:46:54 -04:00
initializer " active_record.set_filter_attributes " do
ActiveSupport . on_load ( :active_record ) do
self . filter_attributes += Rails . application . config . filter_parameters
end
end
2009-12-23 17:55:12 -05:00
end
2009-12-30 22:24:00 -05:00
end