Skip to content

Commit

Permalink
Always manage connections when database is specified
Browse files Browse the repository at this point in the history
When a database is specified, we should always manage connections so
that we can ensure the cache is writable.

But if you just use the default connection for the rest of the app, we
won't attempt to manage that.

Fixes: #214
  • Loading branch information
djmb committed Sep 10, 2024
1 parent 5b5f410 commit f1d3f57
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/solid_cache/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def set_connects_to(database:, databases:, connects_to:)
@connects_to =
case
when database
{ database: { writing: database.to_sym } }
{ shards: { database.to_sym => { writing: database.to_sym } } }
when databases
{ shards: databases.map(&:to_sym).index_with { |database| { writing: database } } }
when connects_to
Expand Down
12 changes: 9 additions & 3 deletions test/models/solid_cache/record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module SolidCache
class RecordTest < ActiveSupport::TestCase
SINGLE_DB_CONFIGS = [ "config/solid_cache_no_database.yml", "config/solid_cache_database.yml", "config/solid_cache_unprepared_statements.yml" ]
SINGLE_DB_CONFIGS = [ "config/solid_cache_database.yml", "config/solid_cache_unprepared_statements.yml" ]
MULTI_DB_CONFIGS = [
"config/solid_cache_connects_to.yml",
"config/solid_cache_encrypted.yml",
Expand All @@ -16,8 +16,12 @@ class RecordTest < ActiveSupport::TestCase
test "each_shard" do
shards = SolidCache::Record.each_shard.map { SolidCache::Record.current_shard }
case ENV["SOLID_CACHE_CONFIG"]
when *SINGLE_DB_CONFIGS
when "config/solid_cache_no_database.yml"
assert_equal [ :default ], shards
when "config/solid_cache_database.yml"
assert_equal [ :primary ], shards
when "config/solid_cache_unprepared_statements.yml"
assert_equal [ :primary_unprepared_statements ], shards
when *MULTI_DB_CONFIGS
assert_equal [ :primary_shard_one, :primary_shard_two, :secondary_shard_one, :secondary_shard_two ], shards
else
Expand All @@ -28,8 +32,10 @@ class RecordTest < ActiveSupport::TestCase
test "each_shard uses the default role" do
role = ActiveRecord::Base.connected_to(role: :reading) { SolidCache::Record.each_shard.map { SolidCache::Record.current_role } }
case ENV["SOLID_CACHE_CONFIG"]
when *SINGLE_DB_CONFIGS
when "config/solid_cache_no_database.yml"
assert_equal [ :reading ], role
when *SINGLE_DB_CONFIGS
assert_equal [ :writing ], role
when *MULTI_DB_CONFIGS
assert_equal [ :writing, :writing, :writing, :writing ], role
else
Expand Down
6 changes: 5 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def uncached_entry_count
end

def first_shard_key
single_database? ? :default : SolidCache.configuration.shard_keys.first
default_database? ? :default : SolidCache.configuration.shard_keys.first
end

def second_shard_key
Expand All @@ -85,6 +85,10 @@ def single_database?
[ "config/solid_cache_database.yml", "config/solid_cache_no_database.yml", "config/solid_cache_unprepared_statements.yml" ].include?(ENV["SOLID_CACHE_CONFIG"])
end

def default_database?
ENV["SOLID_CACHE_CONFIG"] == "config/solid_cache_no_database.yml"
end

def shard_keys(cache, shard)
namespaced_keys = 100.times.map { |i| @cache.send(:normalize_key, "key#{i}", {}) }
shard_keys = cache.send(:connections).assign(namespaced_keys)[shard]
Expand Down
2 changes: 1 addition & 1 deletion test/unit/expiry_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class SolidCache::ExpiryTest < ActiveSupport::TestCase

setup do
@namespace = "test-#{SecureRandom.hex}"
@single_shard_cluster = single_database? ? {} : { shards: [ first_shard_key ] }
@single_shard_cluster = default_database? ? {} : { shards: [ first_shard_key ] }
end

teardown do
Expand Down

0 comments on commit f1d3f57

Please sign in to comment.