Skip to content

Commit d5f7936

Browse files
authored
Merge pull request #253 from redis-rb/db-nil-url
Fix `db: nil` having precedence over db in URL.
2 parents 7238557 + e4bb9e5 commit d5f7936

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Unreleased
22

3+
- Fix precedence of `db: nil` initialization parameter.
4+
5+
```ruby
6+
Redis.new(url: "redis://localhost:6379/3", db: nil).db
7+
```
8+
9+
Before: `0`
10+
After: `3`
11+
312
# 0.25.3
413

514
- Fix `hiredis-client` compilation with `clang 21`.

lib/redis_client/config.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,22 +191,23 @@ def initialize(
191191
path: nil,
192192
username: nil,
193193
password: nil,
194+
db: nil,
194195
**kwargs
195196
)
196197
if url
197198
url_config = URLConfig.new(url)
198199
kwargs = {
199200
ssl: url_config.ssl?,
200-
db: url_config.db,
201201
}.compact.merge(kwargs)
202+
db ||= url_config.db
202203
host ||= url_config.host
203204
port ||= url_config.port
204205
path ||= url_config.path
205206
username ||= url_config.username
206207
password ||= url_config.password
207208
end
208209

209-
super(username: username, password: password, **kwargs)
210+
super(username: username, password: password, db: db, **kwargs)
210211

211212
if @path = path
212213
@host = nil

test/redis_client/config_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,23 @@ def test_overriding
209209
assert_equal [%w[HELLO 3 AUTH george hunter2], %w[SELECT 5]], config.connection_prelude
210210
end
211211

212+
def test_overriding_nil
213+
config = Config.new(
214+
url: "redis://p%40ssw0rd@redis-16379.hosted.com:16379/12",
215+
username: nil,
216+
password: nil,
217+
host: nil,
218+
port: nil,
219+
db: nil,
220+
)
221+
222+
assert_equal "redis-16379.hosted.com", config.host
223+
assert_equal 16379, config.port
224+
assert_equal "default", config.username
225+
assert_equal "p@ssw0rd", config.password
226+
assert_equal 12, config.db
227+
end
228+
212229
def test_server_url
213230
assert_equal "redis://localhost:6379", Config.new.server_url
214231
assert_equal "redis://localhost:6379", Config.new(username: "george", password: "hunter2").server_url

0 commit comments

Comments
 (0)