Skip to content

Commit

Permalink
Use the default SolidCache::Record role
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
djmb committed Dec 7, 2023
1 parent b0b3a94 commit 8cbf8c8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/models/solid_cache/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/solid_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/unit/solid_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8cbf8c8

Please sign in to comment.