Skip to content

Commit

Permalink
Merge pull request #51034 from rails/rm-schema-cache-loading
Browse files Browse the repository at this point in the history
Remove initializer that was eager loading the schema cache dump
  • Loading branch information
rafaelfranca committed Feb 14, 2024
2 parents 41bb266 + f91cb4f commit a9a359a
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 135 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,7 @@
* Deprecated `ENV["SCHEMA_CACHE"]` in favor of `schema_cache_path` in the database configuration.

*Rafael Mendonça França*

* Add `ActiveRecord::Base.with_connection` as a shortcut for leasing a connection for a short duration.

The leased connection is yielded, and for the duration of the block, any call to `ActiveRecord::Base.connection`
Expand Down
Expand Up @@ -18,10 +18,6 @@ def initialize(cache_path, cache = nil)
@cache_path = cache_path
end

def set_schema_cache(cache)
@cache = cache
end

def clear!
@cache = empty_cache

Expand Down
46 changes: 4 additions & 42 deletions activerecord/lib/active_record/railtie.rb
Expand Up @@ -134,49 +134,11 @@ class Railtie < Rails::Railtie # :nodoc:
end
end

initializer "active_record.use_schema_cache_dump" do
ActiveRecord::ConnectionAdapters::SchemaReflection.use_schema_cache_dump = config.active_record.use_schema_cache_dump
end

initializer "active_record.check_schema_cache_dump" do
check_schema_cache_dump_version = config.active_record.check_schema_cache_dump_version

ActiveRecord::ConnectionAdapters::SchemaReflection.check_schema_cache_dump_version = check_schema_cache_dump_version
initializer "active_record.copy_schema_cache_config" do
active_record_config = config.active_record

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
next if db_config.nil?

filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(db_config)

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

if current_version.nil?
connection_pool.schema_reflection.clear!
next
elsif cache.schema_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.schema_version}."
connection_pool.schema_reflection.clear!
next
end
end

Rails.logger.info("Using schema cache file #{filename}")
connection_pool.schema_reflection.set_schema_cache(cache)
end
end
end
ActiveRecord::ConnectionAdapters::SchemaReflection.use_schema_cache_dump = active_record_config.use_schema_cache_dump
ActiveRecord::ConnectionAdapters::SchemaReflection.check_schema_cache_dump_version = active_record_config.check_schema_cache_dump_version
end

initializer "active_record.define_attribute_methods" do |app|
Expand Down
17 changes: 14 additions & 3 deletions activerecord/lib/active_record/tasks/database_tasks.rb
Expand Up @@ -441,10 +441,10 @@ def cache_dump_filename(db_config_or_name, schema_cache_path: nil)
if db_config_or_name.is_a?(DatabaseConfigurations::DatabaseConfig)
schema_cache_path ||
db_config_or_name.schema_cache_path ||
ENV["SCHEMA_CACHE"] ||
schema_cache_env ||
db_config_or_name.default_schema_cache_path(ActiveRecord::Tasks::DatabaseTasks.db_dir)
else
ActiveRecord.deprecator.deprecation_warning(<<~MSG.squish)
ActiveRecord.deprecator.warn(<<~MSG.squish)
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.
MSG
Expand All @@ -455,7 +455,7 @@ def cache_dump_filename(db_config_or_name, schema_cache_path: nil)
"#{db_config_or_name}_schema_cache.yml"
end

schema_cache_path || ENV["SCHEMA_CACHE"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, filename)
schema_cache_path || schema_cache_env || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, filename)
end
end

Expand Down Expand Up @@ -523,6 +523,17 @@ def migration_connection # :nodoc:
end

private
def schema_cache_env
if ENV["SCHEMA_CACHE"]
ActiveRecord.deprecator.warn(<<~MSG.squish)
Setting `ENV["SCHEMA_CACHE"]` is deprecated and will be removed in Rails 7.3.
Configure the `:schema_cache_path` in the database configuration instead.
MSG

nil
end
end

def with_temporary_pool(db_config, clobber: false)
original_db_config = migration_class.connection_db_config
pool = migration_class.connection_handler.establish_connection(db_config, clobber: clobber)
Expand Down
10 changes: 6 additions & 4 deletions activerecord/test/cases/tasks/database_tasks_test.rb
Expand Up @@ -388,8 +388,10 @@ def test_cache_dump_filename_with_env_override
config = DatabaseConfigurations::HashConfig.new("development", "primary", {})

ActiveRecord::Tasks::DatabaseTasks.stub(:db_dir, "db") do
path = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(config)
assert_equal "tmp/something.yml", path
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
ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(config)
end
assert_equal "db/schema_cache.yml", path
end
ensure
ENV["SCHEMA_CACHE"] = old_path
Expand All @@ -400,10 +402,10 @@ def test_deprecated_cache_dump_filename_with_env_override
ENV["SCHEMA_CACHE"] = "tmp/something.yml"

ActiveRecord::Tasks::DatabaseTasks.stub(:db_dir, "db") do
path = assert_deprecated(ActiveRecord.deprecator) do
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
ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename("primary")
end
assert_equal "tmp/something.yml", path
assert_equal "db/schema_cache.yml", path
end
ensure
ENV["SCHEMA_CACHE"] = old_path
Expand Down

0 comments on commit a9a359a

Please sign in to comment.