Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always manage connections when database is specified #219

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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