Skip to content

Commit a9a359a

Browse files
authored
Merge pull request rails#51034 from rails/rm-schema-cache-loading
Remove initializer that was eager loading the schema cache dump
2 parents 41bb266 + f91cb4f commit a9a359a

File tree

7 files changed

+96
-135
lines changed

7 files changed

+96
-135
lines changed

activerecord/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Deprecated `ENV["SCHEMA_CACHE"]` in favor of `schema_cache_path` in the database configuration.
2+
3+
*Rafael Mendonça França*
4+
15
* Add `ActiveRecord::Base.with_connection` as a shortcut for leasing a connection for a short duration.
26

37
The leased connection is yielded, and for the duration of the block, any call to `ActiveRecord::Base.connection`

activerecord/lib/active_record/connection_adapters/schema_cache.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ def initialize(cache_path, cache = nil)
1818
@cache_path = cache_path
1919
end
2020

21-
def set_schema_cache(cache)
22-
@cache = cache
23-
end
24-
2521
def clear!
2622
@cache = empty_cache
2723

activerecord/lib/active_record/railtie.rb

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -134,49 +134,11 @@ class Railtie < Rails::Railtie # :nodoc:
134134
end
135135
end
136136

137-
initializer "active_record.use_schema_cache_dump" do
138-
ActiveRecord::ConnectionAdapters::SchemaReflection.use_schema_cache_dump = config.active_record.use_schema_cache_dump
139-
end
140-
141-
initializer "active_record.check_schema_cache_dump" do
142-
check_schema_cache_dump_version = config.active_record.check_schema_cache_dump_version
143-
144-
ActiveRecord::ConnectionAdapters::SchemaReflection.check_schema_cache_dump_version = check_schema_cache_dump_version
137+
initializer "active_record.copy_schema_cache_config" do
138+
active_record_config = config.active_record
145139

146-
if config.active_record.use_schema_cache_dump && !config.active_record.lazily_load_schema_cache
147-
config.after_initialize do |app|
148-
ActiveSupport.on_load(:active_record) do
149-
db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).first
150-
next if db_config.nil?
151-
152-
filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(db_config)
153-
154-
cache = ActiveRecord::ConnectionAdapters::SchemaCache._load_from(filename)
155-
next if cache.nil?
156-
157-
if check_schema_cache_dump_version
158-
current_version = begin
159-
ActiveRecord::Migrator.current_version
160-
rescue ActiveRecordError => error
161-
warn "Failed to validate the schema cache because of #{error.class}: #{error.message}"
162-
nil
163-
end
164-
165-
if current_version.nil?
166-
connection_pool.schema_reflection.clear!
167-
next
168-
elsif cache.schema_version != current_version
169-
warn "Ignoring #{filename} because it has expired. The current schema version is #{current_version}, but the one in the schema cache file is #{cache.schema_version}."
170-
connection_pool.schema_reflection.clear!
171-
next
172-
end
173-
end
174-
175-
Rails.logger.info("Using schema cache file #{filename}")
176-
connection_pool.schema_reflection.set_schema_cache(cache)
177-
end
178-
end
179-
end
140+
ActiveRecord::ConnectionAdapters::SchemaReflection.use_schema_cache_dump = active_record_config.use_schema_cache_dump
141+
ActiveRecord::ConnectionAdapters::SchemaReflection.check_schema_cache_dump_version = active_record_config.check_schema_cache_dump_version
180142
end
181143

182144
initializer "active_record.define_attribute_methods" do |app|

activerecord/lib/active_record/tasks/database_tasks.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,10 @@ def cache_dump_filename(db_config_or_name, schema_cache_path: nil)
441441
if db_config_or_name.is_a?(DatabaseConfigurations::DatabaseConfig)
442442
schema_cache_path ||
443443
db_config_or_name.schema_cache_path ||
444-
ENV["SCHEMA_CACHE"] ||
444+
schema_cache_env ||
445445
db_config_or_name.default_schema_cache_path(ActiveRecord::Tasks::DatabaseTasks.db_dir)
446446
else
447-
ActiveRecord.deprecator.deprecation_warning(<<~MSG.squish)
447+
ActiveRecord.deprecator.warn(<<~MSG.squish)
448448
Passing a database name to `cache_dump_filename` is deprecated and will be removed in Rails 7.3. Pass a
449449
`ActiveRecord::DatabaseConfigurations::DatabaseConfig` object instead.
450450
MSG
@@ -455,7 +455,7 @@ def cache_dump_filename(db_config_or_name, schema_cache_path: nil)
455455
"#{db_config_or_name}_schema_cache.yml"
456456
end
457457

458-
schema_cache_path || ENV["SCHEMA_CACHE"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, filename)
458+
schema_cache_path || schema_cache_env || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, filename)
459459
end
460460
end
461461

@@ -523,6 +523,17 @@ def migration_connection # :nodoc:
523523
end
524524

525525
private
526+
def schema_cache_env
527+
if ENV["SCHEMA_CACHE"]
528+
ActiveRecord.deprecator.warn(<<~MSG.squish)
529+
Setting `ENV["SCHEMA_CACHE"]` is deprecated and will be removed in Rails 7.3.
530+
Configure the `:schema_cache_path` in the database configuration instead.
531+
MSG
532+
533+
nil
534+
end
535+
end
536+
526537
def with_temporary_pool(db_config, clobber: false)
527538
original_db_config = migration_class.connection_db_config
528539
pool = migration_class.connection_handler.establish_connection(db_config, clobber: clobber)

activerecord/test/cases/tasks/database_tasks_test.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,10 @@ def test_cache_dump_filename_with_env_override
388388
config = DatabaseConfigurations::HashConfig.new("development", "primary", {})
389389

390390
ActiveRecord::Tasks::DatabaseTasks.stub(:db_dir, "db") do
391-
path = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(config)
392-
assert_equal "tmp/something.yml", path
391+
path = assert_deprecated(/Setting `ENV\["SCHEMA_CACHE"\]` is deprecated and will be removed in Rails 7\.3\. Configure the `:schema_cache_path` in the database configuration instead\. \(/, ActiveRecord.deprecator) do
392+
ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(config)
393+
end
394+
assert_equal "db/schema_cache.yml", path
393395
end
394396
ensure
395397
ENV["SCHEMA_CACHE"] = old_path
@@ -400,10 +402,10 @@ def test_deprecated_cache_dump_filename_with_env_override
400402
ENV["SCHEMA_CACHE"] = "tmp/something.yml"
401403

402404
ActiveRecord::Tasks::DatabaseTasks.stub(:db_dir, "db") do
403-
path = assert_deprecated(ActiveRecord.deprecator) do
405+
path = assert_deprecated(/Passing a database name to `cache_dump_filename` is deprecated and will be removed in Rails 7\.3\. Pass a `ActiveRecord::DatabaseConfigurations::DatabaseConfig` object instead\. \(/, ActiveRecord.deprecator) do
404406
ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename("primary")
405407
end
406-
assert_equal "tmp/something.yml", path
408+
assert_equal "db/schema_cache.yml", path
407409
end
408410
ensure
409411
ENV["SCHEMA_CACHE"] = old_path

0 commit comments

Comments
 (0)