Skip to content

Using db: nil forces the db number to be zero #252

@sobrinho

Description

@sobrinho

Hi there,

We found an issue with redis-client to a setup like this:

RedisClient.config(url: "redis://localhost:6379/3").db
#=> 3
RedisClient.config(url: "redis://localhost:6379/3", db: 2).db
#=> 2
RedisClient.config(url: "redis://localhost:6379/3", db: nil).db
#=> 0

The problem here is that we use an environment variable that can be set or not and this lead to the wrong db.

The actual code is more like this:

RedisClient.config(url: ENV["REDIS_URL"], db: ENV["REDIS_DB"]&.to_i).db

The Redis gem on the other hand, works as expected:

Redis.new(url: "redis://localhost:6379/3", db: nil)
#=> keeps the 3 db

Redis.new(url: "redis://localhost:6379/3", db: 2)
#=> override to the 2 db

The fix is to not set the db to nil but this was unexpected:

config = { url: ENV["REDIS_URL"] }
config[:db] = ENV["REDIS_DB"].to_i if ENV["REDIS_DB"]
RedisClient.config(config).db
#=> now works as expected if set or unset

We have a few options here:

  1. Raise an error if nil
  2. Raise an error if db is set to something different than the url db
  3. Respect the original dbnumber if set to nil (my choice)

WDYT?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions