Skip to content

Commit 765271f

Browse files
author
Brandon Black
committed
RUBY-658 fixing issue with default db from connection uri
1 parent 652d349 commit 765271f

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

lib/mongo/mongo_client.rb

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class MongoClient
3030
DEFAULT_HOST = 'localhost'
3131
DEFAULT_PORT = 27017
3232
DEFAULT_DB_NAME = 'test'
33-
GENERIC_OPTS = [:auths, :logger, :connect]
33+
GENERIC_OPTS = [:auths, :logger, :connect, :default_db]
3434
TIMEOUT_OPTS = [:timeout, :op_timeout, :connect_timeout]
3535
SSL_OPTS = [:ssl, :ssl_key, :ssl_cert, :ssl_verify, :ssl_ca_cert]
3636
POOL_OPTS = [:pool_size, :pool_timeout]
@@ -199,7 +199,7 @@ def self.multi(nodes, opts={})
199199
#
200200
# @return [Mongo::MongoClient, Mongo::MongoReplicaSetClient]
201201
def self.from_uri(uri = ENV['MONGODB_URI'], extra_opts={})
202-
parser = URIParser.new uri
202+
parser = URIParser.new(uri)
203203
parser.connection(extra_opts)
204204
end
205205

@@ -210,7 +210,7 @@ def parse_init(host, port, opts)
210210
raise MongoArgumentError,
211211
"ENV['MONGODB_URI'] implies a replica set."
212212
end
213-
opts.merge! parser.connection_options
213+
opts.merge!(parser.connection_options)
214214
[parser.host, parser.port]
215215
else
216216
[host || DEFAULT_HOST, port || DEFAULT_PORT]
@@ -358,13 +358,7 @@ def database_names
358358
# @return [Mongo::DB]
359359
#
360360
# @core databases db-instance_method
361-
def db(db_name=nil, opts={})
362-
if !db_name && uri = ENV['MONGODB_URI']
363-
db_name = uri[%r{/([^/\?]+)(\?|$)}, 1]
364-
end
365-
366-
db_name ||= DEFAULT_DB_NAME
367-
361+
def db(db_name = @default_db, opts = {})
368362
DB.new(db_name, self, opts)
369363
end
370364

@@ -689,6 +683,7 @@ def setup(opts)
689683
end
690684
Mongo::ReadPreference::validate(@read)
691685

686+
@default_db = opts.delete(:default_db) || DEFAULT_DB_NAME
692687
@tag_sets = opts.delete(:tag_sets) || []
693688
@acceptable_latency = opts.delete(:secondary_acceptable_latency_ms) || 15
694689

lib/mongo/util/uri_parser.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class URIParser
109109
:w => lambda { |arg| Mongo::Support.is_i?(arg) ? arg.to_i : arg.to_sym },
110110
:wtimeout => lambda { |arg| arg.to_i },
111111
:wtimeoutms => lambda { |arg| arg.to_i }
112-
}
112+
}
113113

114114
attr_reader :auths,
115115
:connect,
@@ -254,6 +254,7 @@ def connection_options
254254
opts[:name] = replicaset
255255
end
256256

257+
opts[:default_db] = @db
257258
opts[:connect] = connect?
258259

259260
opts
@@ -278,7 +279,7 @@ def parse_hosts(uri_without_proto)
278279
uname = matches[2]
279280
pwd = matches[3]
280281
hosturis = matches[4].split(',')
281-
db = matches[8]
282+
@db = matches[8]
282283

283284
hosturis.each do |hosturi|
284285
# If port is present, use it, otherwise use default port
@@ -296,14 +297,16 @@ def parse_hosts(uri_without_proto)
296297
raise MongoArgumentError, "No nodes specified. Please ensure that you've provided at least one node."
297298
end
298299

299-
if uname && pwd && db
300-
uname = URI.unescape(uname)
301-
pwd = URI.unescape(pwd)
302-
303-
auths << {:db_name => db, :username => uname, :password => pwd}
300+
if uname && pwd && @db
301+
auths << {
302+
:db_name => @db,
303+
:username => URI.unescape(uname),
304+
:password => URI.unescape(pwd)
305+
}
304306
elsif uname || pwd
305-
raise MongoArgumentError, "MongoDB URI must include username, password, "
306-
"and db if username and password are specified."
307+
raise MongoArgumentError, 'MongoDB URI must include username, ' +
308+
'password, and db if username and ' +
309+
'password are specified.'
307310
end
308311
end
309312

test/functional/connection_test.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,24 @@ def test_db_from_uri_exists_no_db_name
122122
ENV['MONGODB_URI'] = "mongodb://#{host_port}/"
123123
con = MongoClient.from_uri
124124
db = con.db
125-
assert_equal db.name, Mongo::MongoClient::DEFAULT_DB_NAME
125+
assert_equal db.name, MongoClient::DEFAULT_DB_NAME
126126
ensure
127127
ENV['MONGODB_URI'] = old_mongodb_uri
128128
end
129129
end
130130

131+
def test_db_from_uri_from_string_param
132+
db_name = "_database"
133+
db = MongoClient.from_uri("mongodb://#{host_port}/#{db_name}").db
134+
assert_equal db.name, db_name
135+
end
136+
137+
def test_db_from_uri_from_string_param_no_db_name
138+
db = MongoClient.from_uri("mongodb://#{host_port}").db
139+
assert_equal db.name, MongoClient::DEFAULT_DB_NAME
140+
end
141+
142+
131143
def test_server_version
132144
assert_match(/\d\.\d+(\.\d+)?/, @client.server_version.to_s)
133145
end

0 commit comments

Comments
 (0)