/
railtie.rb
437 lines (374 loc) · 16.5 KB
/
railtie.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# frozen_string_literal: true
require "active_record"
require "rails"
require "active_support/core_ext/object/try"
require "active_model/railtie"
# For now, action_controller must always be present with
# Rails, so let's make sure that it gets required before
# here. This is needed for correctly setting up the middleware.
# In the future, this might become an optional require.
require "action_controller/railtie"
module ActiveRecord
# = Active Record Railtie
class Railtie < Rails::Railtie # :nodoc:
config.active_record = ActiveSupport::OrderedOptions.new
config.active_record.encryption = ActiveSupport::OrderedOptions.new
config.app_generators.orm :active_record, migration: true,
timestamps: true
config.action_dispatch.rescue_responses.merge!(
"ActiveRecord::RecordNotFound" => :not_found,
"ActiveRecord::StaleObjectError" => :conflict,
"ActiveRecord::RecordInvalid" => :unprocessable_entity,
"ActiveRecord::RecordNotSaved" => :unprocessable_entity
)
config.active_record.use_schema_cache_dump = true
config.active_record.check_schema_cache_dump_version = true
config.active_record.maintain_test_schema = true
config.active_record.has_many_inversing = false
config.active_record.sqlite3_production_warning = true
config.active_record.query_log_tags_enabled = false
config.active_record.query_log_tags = [ :application ]
config.active_record.query_log_tags_format = :legacy
config.active_record.cache_query_log_tags = false
config.active_record.raise_on_assign_to_attr_readonly = false
config.active_record.belongs_to_required_validates_foreign_key = true
config.active_record.queues = ActiveSupport::InheritableOptions.new
config.eager_load_namespaces << ActiveRecord
rake_tasks do
namespace :db do
task :load_config do
if defined?(ENGINE_ROOT) && engine = Rails::Engine.find(ENGINE_ROOT)
if engine.paths["db/migrate"].existent
ActiveRecord::Tasks::DatabaseTasks.migrations_paths += engine.paths["db/migrate"].to_a
end
end
end
end
load "active_record/railties/databases.rake"
end
# 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.
console do |app|
require "active_record/railties/console_sandbox" if app.sandbox?
require "active_record/base"
unless ActiveSupport::Logger.logger_outputs_to?(Rails.logger, STDERR, STDOUT)
console = ActiveSupport::Logger.new(STDERR)
console.level = Rails.logger.level
Rails.logger.extend ActiveSupport::Logger.broadcast console
end
ActiveRecord.verbose_query_logs = false
end
runner do
require "active_record/base"
end
initializer "active_record.deprecator", before: :load_environment_config do |app|
app.deprecators[:active_record] = ActiveRecord.deprecator
end
initializer "active_record.initialize_timezone" do
ActiveSupport.on_load(:active_record) do
self.time_zone_aware_attributes = true
end
end
initializer "active_record.postgresql_time_zone_aware_types" do
ActiveSupport.on_load(:active_record_postgresqladapter) do
ActiveRecord::Base.time_zone_aware_types << :timestamptz
end
end
initializer "active_record.logger" do
ActiveSupport.on_load(:active_record) { self.logger ||= ::Rails.logger }
end
initializer "active_record.backtrace_cleaner" do
ActiveSupport.on_load(:active_record) { LogSubscriber.backtrace_cleaner = ::Rails.backtrace_cleaner }
end
initializer "active_record.migration_error" do |app|
if config.active_record.migration_error == :page_load
config.app_middleware.insert_after ::ActionDispatch::Callbacks,
ActiveRecord::Migration::CheckPending,
file_watcher: app.config.file_watcher
end
end
initializer "active_record.cache_versioning_support" do
config.after_initialize do |app|
ActiveSupport.on_load(:active_record) do
if app.config.active_record.cache_versioning && Rails.cache
unless Rails.cache.class.try(:supports_cache_versioning?)
raise <<-end_error
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).
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:
config.active_record.cache_versioning = false
end_error
end
end
end
end
end
initializer "active_record.check_schema_cache_dump" do
check_schema_cache_dump_version = config.active_record.check_schema_cache_dump_version
if config.active_record.use_schema_cache_dump && !config.active_record.lazily_load_schema_cache
config.after_initialize do |app|
ActiveSupport.on_load(:active_record) do
db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).first
filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(
db_config.name,
schema_cache_path: db_config.schema_cache_path
)
cache = ActiveRecord::ConnectionAdapters::SchemaCache.load_from(filename)
next if cache.nil?
if check_schema_cache_dump_version
current_version = begin
ActiveRecord::Migrator.current_version
rescue ActiveRecordError => error
warn "Failed to validate the schema cache because of #{error.class}: #{error.message}"
nil
end
next if current_version.nil?
if cache.version != current_version
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}."
next
end
end
Rails.logger.info("Using schema cache file #{filename}")
connection_pool.set_schema_cache(cache)
end
end
end
end
initializer "active_record.define_attribute_methods" do |app|
config.after_initialize do
ActiveSupport.on_load(:active_record) do
if app.config.eager_load
begin
descendants.each do |model|
# If the schema cache was loaded from a dump, we can use it without connecting
schema_cache = model.connection_pool.schema_cache
# If there's no connection yet, we avoid connecting.
schema_cache ||= model.connected? && model.connection.schema_cache
# If the schema cache doesn't have the columns
# hash for the model cached, `define_attribute_methods` would trigger a query.
if schema_cache && schema_cache.columns_hash?(model.table_name)
model.define_attribute_methods
end
end
rescue ActiveRecordError => error
# Regardless of whether there was already a connection or not, we rescue any database
# error because it is critical that the application can boot even if the database
# is unhealthy.
warn "Failed to define attribute methods because of #{error.class}: #{error.message}"
end
end
end
end
end
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
require "active_record/relation/record_fetch_warning"
end
end
end
SQLITE3_PRODUCTION_WARN = "You are running SQLite in production, this is generally not recommended."\
" You can disable this warning by setting \"config.active_record.sqlite3_production_warning=false\"."
initializer "active_record.sqlite3_production_warning" do
if config.active_record.sqlite3_production_warning && Rails.env.production?
ActiveSupport.on_load(:active_record_sqlite3adapter) do
Rails.logger.warn(SQLITE3_PRODUCTION_WARN)
end
end
end
initializer "active_record.sqlite3_adapter_strict_strings_by_default" do
config.after_initialize do
if config.active_record.sqlite3_adapter_strict_strings_by_default
ActiveSupport.on_load(:active_record_sqlite3adapter) do
self.strict_strings_by_default = true
end
end
end
end
initializer "active_record.set_configs" do |app|
configs = app.config.active_record
config.after_initialize do
configs.each do |k, v|
next if k == :encryption
setter = "#{k}="
if ActiveRecord.respond_to?(setter)
ActiveRecord.send(setter, v)
end
end
end
ActiveSupport.on_load(:active_record) do
# Configs used in other initializers
configs = configs.except(
:migration_error,
:database_selector,
:database_resolver,
:database_resolver_context,
:shard_selector,
:shard_resolver,
:query_log_tags_enabled,
:query_log_tags,
:query_log_tags_format,
:cache_query_log_tags,
:sqlite3_production_warning,
:sqlite3_adapter_strict_strings_by_default,
:check_schema_cache_dump_version,
:use_schema_cache_dump
)
configs.each do |k, v|
next if k == :encryption
setter = "#{k}="
# Some existing initializers might rely on Active Record configuration
# being copied from the config object to their actual destination when
# `ActiveRecord::Base` is loaded.
# So to preserve backward compatibility we copy the config a second time.
if ActiveRecord.respond_to?(setter)
ActiveRecord.send(setter, v)
else
send(setter, v)
end
end
end
end
# This sets the database configuration from Configuration#database_configuration
# and then establishes the connection.
initializer "active_record.initialize_database" do
ActiveSupport.on_load(:active_record) do
self.configurations = Rails.application.config.database_configuration
establish_connection
end
end
# Expose database runtime for logging.
initializer "active_record.log_runtime" do
require "active_record/railties/controller_runtime"
ActiveSupport.on_load(:action_controller) do
include ActiveRecord::Railties::ControllerRuntime
end
require "active_record/railties/job_runtime"
ActiveSupport.on_load(:active_job) do
include ActiveRecord::Railties::JobRuntime
end
end
initializer "active_record.set_reloader_hooks" do
ActiveSupport.on_load(:active_record) do
ActiveSupport::Reloader.before_class_unload do
if ActiveRecord::Base.connected?
ActiveRecord::Base.clear_cache!
ActiveRecord::Base.connection_handler.clear_reloadable_connections!(:all)
end
end
end
end
initializer "active_record.set_executor_hooks" do
ActiveRecord::QueryCache.install_executor_hooks
ActiveRecord::AsynchronousQueriesTracker.install_executor_hooks
end
initializer "active_record.add_watchable_files" do |app|
path = app.paths["db"].first
config.watchable_files.concat ["#{path}/schema.rb", "#{path}/structure.sql"]
end
initializer "active_record.clear_active_connections" do
config.after_initialize do
ActiveSupport.on_load(:active_record) do
# 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.
connection_handler.clear_active_connections!(:all)
connection_handler.flush_idle_connections!(:all)
end
end
end
initializer "active_record.set_filter_attributes" do
ActiveSupport.on_load(:active_record) do
self.filter_attributes += Rails.application.config.filter_parameters
end
end
initializer "active_record.set_signed_id_verifier_secret" do
ActiveSupport.on_load(:active_record) do
self.signed_id_verifier_secret ||= -> { Rails.application.key_generator.generate_key("active_record/signed_id") }
end
end
initializer "active_record.generated_token_verifier" do
config.after_initialize do |app|
ActiveSupport.on_load(:active_record) do
self.generated_token_verifier ||= app.message_verifier("active_record/token_for")
end
end
end
initializer "active_record_encryption.configuration" do |app|
ActiveRecord::Encryption.configure \
primary_key: app.credentials.dig(:active_record_encryption, :primary_key),
deterministic_key: app.credentials.dig(:active_record_encryption, :deterministic_key),
key_derivation_salt: app.credentials.dig(:active_record_encryption, :key_derivation_salt),
**config.active_record.encryption
ActiveSupport.on_load(:active_record) do
# Support extended queries for deterministic attributes and validations
if ActiveRecord::Encryption.config.extend_queries
ActiveRecord::Encryption::ExtendedDeterministicQueries.install_support
ActiveRecord::Encryption::ExtendedDeterministicUniquenessValidator.install_support
end
end
ActiveSupport.on_load(:active_record_fixture_set) do
# Encrypt active record fixtures
if ActiveRecord::Encryption.config.encrypt_fixtures
ActiveRecord::Fixture.prepend ActiveRecord::Encryption::EncryptedFixtures
end
end
# Filtered params
if ActiveRecord::Encryption.config.add_to_filter_parameters
ActiveRecord::Encryption.install_auto_filtered_parameters_hook(app)
end
end
initializer "active_record.query_log_tags_config" do |app|
config.after_initialize do
if app.config.active_record.query_log_tags_enabled
ActiveRecord.query_transformers << ActiveRecord::QueryLogs
ActiveRecord::QueryLogs.taggings.merge!(
application: Rails.application.class.name.split("::").first,
pid: -> { Process.pid.to_s },
socket: ->(context) { context[:connection].pool.db_config.socket },
db_host: ->(context) { context[:connection].pool.db_config.host },
database: ->(context) { context[:connection].pool.db_config.database }
)
if app.config.active_record.query_log_tags.present?
ActiveRecord::QueryLogs.tags = app.config.active_record.query_log_tags
end
if app.config.active_record.query_log_tags_format
ActiveRecord::QueryLogs.update_formatter(app.config.active_record.query_log_tags_format)
end
if app.config.active_record.cache_query_log_tags
ActiveRecord::QueryLogs.cache_query_log_tags = true
end
end
end
end
initializer "active_record.unregister_current_scopes_on_unload" do |app|
config.after_initialize do
if app.config.reloading_enabled?
Rails.autoloaders.main.on_unload do |_cpath, value, _abspath|
# Conditions are written this way to be robust against custom
# implementations of value#is_a? or value#<.
if Class === value && ActiveRecord::Base > value
value.current_scope = nil
end
end
end
end
end
initializer "active_record.message_pack" do
ActiveSupport.on_load(:message_pack) do
ActiveSupport.on_load(:active_record) do
require "active_record/message_pack"
ActiveRecord::MessagePack::Extensions.install(ActiveSupport::MessagePack::CacheSerializer)
end
end
end
end
end