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