diff --git a/app/lib/three_scale/redis_config.rb b/app/lib/three_scale/redis_config.rb index 605e79264e..dd76b79ee6 100644 --- a/app/lib/three_scale/redis_config.rb +++ b/app/lib/three_scale/redis_config.rb @@ -7,6 +7,7 @@ def initialize(redis_config = {}) sentinels = raw_config.delete(:sentinels).presence raw_config.delete_if { |key, value| value.blank? } raw_config[:size] ||= raw_config.delete(:pool_size) if raw_config.key?(:pool_size) + raw_config[:db] ||= URI.parse(raw_config[:url].to_s).path[1..] @config = ActiveSupport::OrderedOptions.new.merge(raw_config) config.sentinels = parse_sentinels(sentinels) if sentinels @@ -14,14 +15,6 @@ def initialize(redis_config = {}) attr_reader :config - def db - value = config.db.presence - return value.to_i if value - url = config.url.presence - return unless url - URI.parse(url).path[1..-1].to_s.to_i - end - def reverse_merge(other) other.merge(config) end diff --git a/test/unit/three_scale/redis_config_test.rb b/test/unit/three_scale/redis_config_test.rb index e26002d894..73d0ae6f4d 100644 --- a/test/unit/three_scale/redis_config_test.rb +++ b/test/unit/three_scale/redis_config_test.rb @@ -4,12 +4,6 @@ module ThreeScale class RedisConfigTest < ActiveSupport::TestCase - test '#db' do - assert_equal 4, RedisConfig.new(db: 4).db - assert_equal 3, RedisConfig.new(url: 'redis://my-redis/3').db - assert_equal 0, RedisConfig.new(url: 'redis://my-redis').db - end - test '#reverse_merge' do config_1 = RedisConfig.new(host: 'localhost', db: 1) config_2 = RedisConfig.new(db: 2, password: 'passwd') @@ -37,6 +31,14 @@ class RedisConfigTest < ActiveSupport::TestCase assert_equal expected_sentinels, config.sentinels end + test 'extracts the Redis logical DB from the URL' do + config = RedisConfig.new(url: 'redis://localhost:6379/6') + + assert config.key? :db + assert_equal '6', config[:db] + assert_equal '6', config.db + end + test ':pool_size is renamed to :size' do config = RedisConfig.new(pool_size: 5)