Skip to content

Commit

Permalink
Correctly read the cache_format_version setting on boot
Browse files Browse the repository at this point in the history
Fixes #45289
  • Loading branch information
ghiculescu committed Jun 8, 2022
1 parent 2d62fd0 commit 08352a2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ def [](version)
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
Expand Down
3 changes: 3 additions & 0 deletions railties/lib/rails/application/bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ module Bootstrap

# 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)

Expand Down
48 changes: 47 additions & 1 deletion railties/test/application/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1758,7 +1758,7 @@ def index
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
Expand Down Expand Up @@ -3787,6 +3787,52 @@ def new(app); self; end
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
Expand Down

0 comments on commit 08352a2

Please sign in to comment.