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

Check if shard names are integers when caching the configurations #312

Merged
merged 1 commit into from
Mar 28, 2023
Merged
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
12 changes: 8 additions & 4 deletions lib/active_record_shards/connection_switcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,6 @@ def current_shard_id
end

def shard_names
unless config_for_env.fetch(SHARD_NAMES_CONFIG_KEY, []).all? { |shard_name| shard_name.is_a?(Integer) }
raise "All shard names must be integers: #{config_for_env[SHARD_NAMES_CONFIG_KEY].inspect}."
end

config_for_env[SHARD_NAMES_CONFIG_KEY] || []
end

Expand All @@ -166,11 +162,19 @@ def config_for_env
raise "Did not find #{shard_env} in configurations, did you forget to add it to your database config? (configurations: #{configurations.to_h.keys.inspect})"
end

ensure_all_shard_names_are_integers(config)

config
end
end
alias_method :check_config_for_env, :config_for_env

def ensure_all_shard_names_are_integers(config)
unless config.fetch(SHARD_NAMES_CONFIG_KEY, []).all? { |shard_name| shard_name.is_a?(Integer) }
raise "All shard names must be integers: #{config.inspect}."
end
end

def switch_connection(options)
ensure_legacy_connection_handling if ActiveRecord.version >= Gem::Version.new('6.1')

Expand Down