PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/activerecord/lib/active_record/railtie.rb

https://github.com/rails/rails
Ruby | 383 lines | 293 code | 56 blank | 34 comment | 39 complexity | 5408c6037d9d4065b2bf712a3fa87d41 MD5 | raw file
  1. # frozen_string_literal: true
  2. require "active_record"
  3. require "rails"
  4. require "active_support/core_ext/object/try"
  5. require "active_model/railtie"
  6. # For now, action_controller must always be present with
  7. # Rails, so let's make sure that it gets required before
  8. # here. This is needed for correctly setting up the middleware.
  9. # In the future, this might become an optional require.
  10. require "action_controller/railtie"
  11. module ActiveRecord
  12. # = Active Record Railtie
  13. class Railtie < Rails::Railtie # :nodoc:
  14. config.active_record = ActiveSupport::OrderedOptions.new
  15. config.active_record.encryption = ActiveSupport::OrderedOptions.new
  16. config.app_generators.orm :active_record, migration: true,
  17. timestamps: true
  18. config.action_dispatch.rescue_responses.merge!(
  19. "ActiveRecord::RecordNotFound" => :not_found,
  20. "ActiveRecord::StaleObjectError" => :conflict,
  21. "ActiveRecord::RecordInvalid" => :unprocessable_entity,
  22. "ActiveRecord::RecordNotSaved" => :unprocessable_entity
  23. )
  24. config.active_record.use_schema_cache_dump = true
  25. config.active_record.check_schema_cache_dump_version = true
  26. config.active_record.maintain_test_schema = true
  27. config.active_record.has_many_inversing = false
  28. config.active_record.sqlite3_production_warning = true
  29. config.active_record.query_log_tags_enabled = false
  30. config.active_record.query_log_tags = [ :application ]
  31. config.active_record.cache_query_log_tags = false
  32. config.active_record.queues = ActiveSupport::InheritableOptions.new
  33. config.eager_load_namespaces << ActiveRecord
  34. rake_tasks do
  35. namespace :db do
  36. task :load_config do
  37. if defined?(ENGINE_ROOT) && engine = Rails::Engine.find(ENGINE_ROOT)
  38. if engine.paths["db/migrate"].existent
  39. ActiveRecord::Tasks::DatabaseTasks.migrations_paths += engine.paths["db/migrate"].to_a
  40. end
  41. end
  42. end
  43. end
  44. load "active_record/railties/databases.rake"
  45. end
  46. # When loading console, force ActiveRecord::Base to be loaded
  47. # to avoid cross references when loading a constant for the
  48. # first time. Also, make it output to STDERR.
  49. console do |app|
  50. require "active_record/railties/console_sandbox" if app.sandbox?
  51. require "active_record/base"
  52. unless ActiveSupport::Logger.logger_outputs_to?(Rails.logger, STDERR, STDOUT)
  53. console = ActiveSupport::Logger.new(STDERR)
  54. console.level = Rails.logger.level
  55. Rails.logger.extend ActiveSupport::Logger.broadcast console
  56. end
  57. ActiveRecord.verbose_query_logs = false
  58. end
  59. runner do
  60. require "active_record/base"
  61. end
  62. initializer "active_record.initialize_timezone" do
  63. ActiveSupport.on_load(:active_record) do
  64. self.time_zone_aware_attributes = true
  65. end
  66. end
  67. initializer "active_record.logger" do
  68. ActiveSupport.on_load(:active_record) { self.logger ||= ::Rails.logger }
  69. end
  70. initializer "active_record.backtrace_cleaner" do
  71. ActiveSupport.on_load(:active_record) { LogSubscriber.backtrace_cleaner = ::Rails.backtrace_cleaner }
  72. end
  73. initializer "active_record.migration_error" do |app|
  74. if config.active_record.migration_error == :page_load
  75. config.app_middleware.insert_after ::ActionDispatch::Callbacks,
  76. ActiveRecord::Migration::CheckPending,
  77. file_watcher: app.config.file_watcher
  78. end
  79. end
  80. initializer "active_record.database_selector" do
  81. if options = config.active_record.database_selector
  82. resolver = config.active_record.database_resolver
  83. operations = config.active_record.database_resolver_context
  84. config.app_middleware.use ActiveRecord::Middleware::DatabaseSelector, resolver, operations, options
  85. end
  86. end
  87. initializer "Check for cache versioning support" do
  88. config.after_initialize do |app|
  89. ActiveSupport.on_load(:active_record) do
  90. if app.config.active_record.cache_versioning && Rails.cache
  91. unless Rails.cache.class.try(:supports_cache_versioning?)
  92. raise <<-end_error
  93. You're using a cache store that doesn't support native cache versioning.
  94. Your best option is to upgrade to a newer version of #{Rails.cache.class}
  95. that supports cache versioning (#{Rails.cache.class}.supports_cache_versioning? #=> true).
  96. Next best, switch to a different cache store that does support cache versioning:
  97. https://guides.rubyonrails.org/caching_with_rails.html#cache-stores.
  98. To keep using the current cache store, you can turn off cache versioning entirely:
  99. config.active_record.cache_versioning = false
  100. end_error
  101. end
  102. end
  103. end
  104. end
  105. end
  106. initializer "active_record.check_schema_cache_dump" do
  107. check_schema_cache_dump_version = config.active_record.check_schema_cache_dump_version
  108. if config.active_record.use_schema_cache_dump && !config.active_record.lazily_load_schema_cache
  109. config.after_initialize do |app|
  110. ActiveSupport.on_load(:active_record) do
  111. db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).first
  112. filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(
  113. db_config.name,
  114. schema_cache_path: db_config&.schema_cache_path
  115. )
  116. cache = ActiveRecord::ConnectionAdapters::SchemaCache.load_from(filename)
  117. next if cache.nil?
  118. if check_schema_cache_dump_version
  119. current_version = begin
  120. ActiveRecord::Migrator.current_version
  121. rescue ActiveRecordError => error
  122. warn "Failed to validate the schema cache because of #{error.class}: #{error.message}"
  123. nil
  124. end
  125. next if current_version.nil?
  126. if cache.version != current_version
  127. warn "Ignoring #{filename} because it has expired. The current schema version is #{current_version}, but the one in the schema cache file is #{cache.version}."
  128. next
  129. end
  130. end
  131. Rails.logger.info("Using schema cache file #{filename}")
  132. connection_pool.set_schema_cache(cache)
  133. end
  134. end
  135. end
  136. end
  137. initializer "active_record.define_attribute_methods" do |app|
  138. config.after_initialize do
  139. ActiveSupport.on_load(:active_record) do
  140. if app.config.eager_load
  141. begin
  142. descendants.each do |model|
  143. # If the schema cache was loaded from a dump, we can use it without connecting
  144. schema_cache = model.connection_pool.schema_cache
  145. # If there's no connection yet, we avoid connecting.
  146. schema_cache ||= model.connected? && model.connection.schema_cache
  147. # If the schema cache doesn't have the columns
  148. # hash for the model cached, `define_attribute_methods` would trigger a query.
  149. if schema_cache && schema_cache.columns_hash?(model.table_name)
  150. model.define_attribute_methods
  151. end
  152. end
  153. rescue ActiveRecordError => error
  154. # Regardless of whether there was already a connection or not, we rescue any database
  155. # error because it is critical that the application can boot even if the database
  156. # is unhealthy.
  157. warn "Failed to define attribute methods because of #{error.class}: #{error.message}"
  158. end
  159. end
  160. end
  161. end
  162. end
  163. initializer "active_record.warn_on_records_fetched_greater_than" do
  164. if config.active_record.warn_on_records_fetched_greater_than
  165. ActiveSupport.on_load(:active_record) do
  166. require "active_record/relation/record_fetch_warning"
  167. end
  168. end
  169. end
  170. SQLITE3_PRODUCTION_WARN = "You are running SQLite in production, this is generally not recommended."\
  171. " You can disable this warning by setting \"config.active_record.sqlite3_production_warning=false\"."
  172. initializer "active_record.sqlite3_production_warning" do
  173. if config.active_record.sqlite3_production_warning && Rails.env.production?
  174. ActiveSupport.on_load(:active_record_sqlite3adapter) do
  175. Rails.logger.warn(SQLITE3_PRODUCTION_WARN)
  176. end
  177. end
  178. end
  179. initializer "active_record.set_configs" do |app|
  180. configs = app.config.active_record
  181. config.after_initialize do
  182. configs.each do |k, v|
  183. next if k == :encryption
  184. setter = "#{k}="
  185. if ActiveRecord.respond_to?(setter)
  186. ActiveRecord.send(setter, v)
  187. end
  188. end
  189. end
  190. ActiveSupport.on_load(:active_record) do
  191. # Configs used in other initializers
  192. configs = configs.except(
  193. :migration_error,
  194. :database_selector,
  195. :database_resolver,
  196. :database_resolver_context,
  197. :query_log_tags_enabled,
  198. :query_log_tags,
  199. :cache_query_log_tags,
  200. :sqlite3_production_warning,
  201. :check_schema_cache_dump_version,
  202. :use_schema_cache_dump
  203. )
  204. configs.each do |k, v|
  205. next if k == :encryption
  206. setter = "#{k}="
  207. # Some existing initializers might rely on Active Record configuration
  208. # being copied from the config object to their actual destination when
  209. # `ActiveRecord::Base` is loaded.
  210. # So to preserve backward compatibility we copy the config a second time.
  211. if ActiveRecord.respond_to?(setter)
  212. ActiveRecord.send(setter, v)
  213. else
  214. send(setter, v)
  215. end
  216. end
  217. end
  218. end
  219. # This sets the database configuration from Configuration#database_configuration
  220. # and then establishes the connection.
  221. initializer "active_record.initialize_database" do
  222. ActiveSupport.on_load(:active_record) do
  223. if ActiveRecord.legacy_connection_handling
  224. self.connection_handlers = { ActiveRecord.writing_role => ActiveRecord::Base.default_connection_handler }
  225. end
  226. self.configurations = Rails.application.config.database_configuration
  227. establish_connection
  228. end
  229. end
  230. # Expose database runtime to controller for logging.
  231. initializer "active_record.log_runtime" do
  232. require "active_record/railties/controller_runtime"
  233. ActiveSupport.on_load(:action_controller) do
  234. include ActiveRecord::Railties::ControllerRuntime
  235. end
  236. end
  237. initializer "active_record.set_reloader_hooks" do
  238. ActiveSupport.on_load(:active_record) do
  239. ActiveSupport::Reloader.before_class_unload do
  240. if ActiveRecord::Base.connected?
  241. ActiveRecord::Base.clear_cache!
  242. ActiveRecord::Base.clear_reloadable_connections!
  243. end
  244. end
  245. end
  246. end
  247. initializer "active_record.set_executor_hooks" do
  248. ActiveRecord::QueryCache.install_executor_hooks
  249. ActiveRecord::AsynchronousQueriesTracker.install_executor_hooks
  250. end
  251. initializer "active_record.add_watchable_files" do |app|
  252. path = app.paths["db"].first
  253. config.watchable_files.concat ["#{path}/schema.rb", "#{path}/structure.sql"]
  254. end
  255. initializer "active_record.clear_active_connections" do
  256. config.after_initialize do
  257. ActiveSupport.on_load(:active_record) do
  258. # Ideally the application doesn't connect to the database during boot,
  259. # but sometimes it does. In case it did, we want to empty out the
  260. # connection pools so that a non-database-using process (e.g. a master
  261. # process in a forking server model) doesn't retain a needless
  262. # connection. If it was needed, the incremental cost of reestablishing
  263. # this connection is trivial: the rest of the pool would need to be
  264. # populated anyway.
  265. clear_active_connections!
  266. flush_idle_connections!
  267. end
  268. end
  269. end
  270. initializer "active_record.set_filter_attributes" do
  271. ActiveSupport.on_load(:active_record) do
  272. self.filter_attributes += Rails.application.config.filter_parameters
  273. end
  274. end
  275. initializer "active_record.set_signed_id_verifier_secret" do
  276. ActiveSupport.on_load(:active_record) do
  277. self.signed_id_verifier_secret ||= -> { Rails.application.key_generator.generate_key("active_record/signed_id") }
  278. end
  279. end
  280. initializer "active_record_encryption.configuration" do |app|
  281. ActiveRecord::Encryption.configure \
  282. primary_key: app.credentials.dig(:active_record_encryption, :primary_key),
  283. deterministic_key: app.credentials.dig(:active_record_encryption, :deterministic_key),
  284. key_derivation_salt: app.credentials.dig(:active_record_encryption, :key_derivation_salt),
  285. **config.active_record.encryption
  286. ActiveSupport.on_load(:active_record) do
  287. # Support extended queries for deterministic attributes and validations
  288. if ActiveRecord::Encryption.config.extend_queries
  289. ActiveRecord::Encryption::ExtendedDeterministicQueries.install_support
  290. ActiveRecord::Encryption::ExtendedDeterministicUniquenessValidator.install_support
  291. end
  292. end
  293. ActiveSupport.on_load(:active_record_fixture_set) do
  294. # Encrypt active record fixtures
  295. if ActiveRecord::Encryption.config.encrypt_fixtures
  296. ActiveRecord::Fixture.prepend ActiveRecord::Encryption::EncryptedFixtures
  297. end
  298. end
  299. # Filtered params
  300. ActiveSupport.on_load(:action_controller) do
  301. if ActiveRecord::Encryption.config.add_to_filter_parameters
  302. ActiveRecord::Encryption.install_auto_filtered_parameters(app)
  303. end
  304. end
  305. end
  306. initializer "active_record.query_log_tags_config" do |app|
  307. config.after_initialize do
  308. if app.config.active_record.query_log_tags_enabled
  309. ActiveRecord.query_transformers << ActiveRecord::QueryLogs
  310. ActiveRecord::QueryLogs.taggings.merge!(
  311. application: Rails.application.class.name.split("::").first,
  312. pid: -> { Process.pid },
  313. socket: -> { ActiveRecord::Base.connection_db_config.socket },
  314. db_host: -> { ActiveRecord::Base.connection_db_config.host },
  315. database: -> { ActiveRecord::Base.connection_db_config.database }
  316. )
  317. if app.config.active_record.query_log_tags.present?
  318. ActiveRecord::QueryLogs.tags = app.config.active_record.query_log_tags
  319. end
  320. if app.config.active_record.cache_query_log_tags
  321. ActiveRecord::QueryLogs.cache_query_log_tags = true
  322. end
  323. end
  324. end
  325. end
  326. end
  327. end