Correctly read the `cache_format_version` setting on boot

Fixes https://github.com/rails/rails/issues/45289
This commit is contained in:
Alex Ghiculescu 2022-06-07 16:05:00 -05:00
parent 2d62fd01b6
commit 08352a2478
3 changed files with 51 additions and 2 deletions

View File

@ -873,7 +873,7 @@ module ActiveSupport
when 7.0
Rails70Coder
else
raise ArgumentError, "Unknown ActiveSupport::Cache.format_version #{Cache.format_version.inspect}"
raise ArgumentError, "Unknown ActiveSupport::Cache.format_version: #{Cache.format_version.inspect}"
end
end
end

View File

@ -63,6 +63,9 @@ module Rails
# Initialize cache early in the stack so railties can make use of it.
initializer :initialize_cache, group: :all do
cache_format_version = config.active_support.delete(:cache_format_version)
ActiveSupport.cache_format_version = cache_format_version if cache_format_version
unless Rails.cache
Rails.cache = ActiveSupport::Cache.lookup_store(*config.cache_store)

View File

@ -1758,7 +1758,7 @@ module ApplicationTests
test "config.log_file_size returns no limit in production" do
app "production"
assert_equal nil, app.config.log_file_size
assert_nil app.config.log_file_size
end
test "rake_tasks block works at instance level" do
@ -3787,6 +3787,52 @@ module ApplicationTests
assert_equal :fiber, ActiveSupport::IsolatedExecutionState.isolation_level
end
test "cache_format_version in a new app" do
add_to_config <<-RUBY
config.cache_store = :null_store
RUBY
app "development"
assert_equal ActiveSupport::Cache::Coders::Rails70Coder, Rails.cache.instance_variable_get(:@coder)
end
test "cache_format_version with explicit 7.0 defaults" do
add_to_config <<-RUBY
config.cache_store = :null_store
RUBY
remove_from_config '.*config\.load_defaults.*\n'
add_to_config 'config.load_defaults "7.0"'
app "development"
assert_equal ActiveSupport::Cache::Coders::Rails70Coder, Rails.cache.instance_variable_get(:@coder)
end
test "cache_format_version with 6.1 defaults" do
add_to_config <<-RUBY
config.cache_store = :null_store
RUBY
remove_from_config '.*config\.load_defaults.*\n'
add_to_config 'config.load_defaults "6.1"'
app "development"
assert_equal ActiveSupport::Cache::Coders::Rails61Coder, Rails.cache.instance_variable_get(:@coder)
end
test "cache_format_version **cannot** be set via new framework defaults" do
add_to_config <<-RUBY
config.cache_store = :null_store
RUBY
remove_from_config '.*config\.load_defaults.*\n'
add_to_config 'config.load_defaults "6.1"'
app_file "config/initializers/new_framework_defaults_7_0.rb", <<-RUBY
Rails.application.config.active_support.cache_format_version = 7.0
RUBY
app "development"
assert_equal ActiveSupport::Cache::Coders::Rails61Coder, Rails.cache.instance_variable_get(:@coder)
end
private
def set_custom_config(contents, config_source = "custom".inspect)
app_file "config/custom.yml", contents