From 8cbf8c815d60fe6e490bd19bfb5b671952816aaf Mon Sep 17 00:00:00 2001 From: Donal McBreen Date: Tue, 5 Dec 2023 22:20:06 +0000 Subject: [PATCH] Use the default SolidCache::Record role Don't let the ActiveRecord::Base role bleed into the SolidCache connections. Also ensure that writes are not prevented. We'll always set up the shard if configured as we may be pointing to the default shard but with the wrong role and prevent writes setting. --- app/models/solid_cache/record.rb | 4 ++-- lib/solid_cache.rb | 2 +- test/unit/solid_cache_test.rb | 9 +++++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/app/models/solid_cache/record.rb b/app/models/solid_cache/record.rb index d3454fa..b06128a 100644 --- a/app/models/solid_cache/record.rb +++ b/app/models/solid_cache/record.rb @@ -14,8 +14,8 @@ def disable_instrumentation end def with_shard(shard, &block) - if shard && shard != Record.current_shard - Record.connected_to(shard: shard, &block) + if shard && SolidCache.connects_to + connected_to(shard: shard, role: default_role, prevent_writes: false, &block) else block.call end diff --git a/lib/solid_cache.rb b/lib/solid_cache.rb index 7d69479..6f61cad 100644 --- a/lib/solid_cache.rb +++ b/lib/solid_cache.rb @@ -22,7 +22,7 @@ def self.each_shard if (shards = all_shards_config&.keys) shards.each do |shard| - Record.connected_to(shard: shard) { yield } + Record.with_shard(shard) { yield } end else yield diff --git a/test/unit/solid_cache_test.rb b/test/unit/solid_cache_test.rb index 3a07ceb..3346c4b 100644 --- a/test/unit/solid_cache_test.rb +++ b/test/unit/solid_cache_test.rb @@ -33,6 +33,15 @@ class SolidCacheTest < ActiveSupport::TestCase end end + test "each_shard uses the default role" do + role = ActiveRecord::Base.connected_to(role: :reading) { SolidCache.each_shard.map { SolidCache::Record.current_role } } + if ENV["NO_CONNECTS_TO"] + assert_equal [ :reading ], role + else + assert_equal [ :writing, :writing, :writing, :writing, :writing ], role + end + end + test "max key bytesize" do cache = lookup_store(max_key_bytesize: 100) assert_equal 100, cache.send(:normalize_key, SecureRandom.hex(200), {}).bytesize