Skip to content

Commit 9be394b

Browse files
committed
Merge pull request #104 from seamusabshere/from_uri_implicit_arg
Make ENV['MONGODB_URI'] the default first arg for `Mongo::Connection.from_uri`
2 parents c0a4ae6 + 90773eb commit 9be394b

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,14 @@ of v1.3.0, the Ruby driver detects forking and reconnects automatically.
175175

176176
## Environment variable `MONGODB_URI`
177177

178-
`Mongo::Connection.new` and `Mongo::ReplSetConnection.new` will use <code>ENV["MONGODB_URI"]</code> if no other args are provided.
178+
`Mongo::Connection.from_uri`, `Mongo::Connection.new` and `Mongo::ReplSetConnection.new` will use <code>ENV["MONGODB_URI"]</code> if no other args are provided.
179179

180180
The URI must fit this specification:
181181

182182
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
183183

184+
If the type of connection (direct or replica set) should be determined entirely from <code>ENV["MONGODB_URI"]</code>, you may want to use `Mongo::Connection.from_uri` because it will return either `Mongo::Connection` or a `Mongo::ReplSetConnection` depending on how many hosts are specified. Trying to use `Mongo::Connection.new` with multiple hosts in <code>ENV["MONGODB_URI"]</code> will raise an exception.
185+
184186
## String Encoding
185187

186188
The BSON ("Binary JSON") format used to communicate with Mongo requires that

lib/mongo/connection.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,17 @@ def self.multi(nodes, opts={})
162162
ReplSetConnection.new(*(nodes+[opts]))
163163
end
164164

165-
# Initialize a connection to MongoDB using the MongoDB URI spec:
165+
# Initialize a connection to MongoDB using the MongoDB URI spec.
166+
#
167+
# Since Connection.new cannot be used with any <code>ENV["MONGODB_URI"]</code> that has multiple hosts (implying a replicaset), you may use this when the type of your connection varies by environment and should be determined solely from <code>ENV["MONGODB_URI"]</code>.
166168
#
167169
# @param uri [String]
168170
# A string of the format mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/database]
169171
#
170172
# @param opts Any of the options available for Connection.new
171173
#
172174
# @return [Mongo::Connection, Mongo::ReplSetConnection]
173-
def self.from_uri(uri, extra_opts={})
175+
def self.from_uri(uri = ENV['MONGODB_URI'], extra_opts={})
174176
parser = URIParser.new uri, extra_opts
175177
parser.connection
176178
end

test/connection_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ def test_env_mongodb_uri
7070
end
7171
end
7272

73+
def test_from_uri_implicit_mongodb_uri
74+
begin
75+
old_mongodb_uri = ENV['MONGODB_URI']
76+
ENV['MONGODB_URI'] = "mongodb://#{host_port}"
77+
con = Connection.from_uri
78+
assert_equal mongo_host, con.primary_pool.host
79+
assert_equal mongo_port, con.primary_pool.port
80+
ensure
81+
ENV['MONGODB_URI'] = old_mongodb_uri
82+
end
83+
end
84+
7385
def test_server_version
7486
assert_match(/\d\.\d+(\.\d+)?/, @conn.server_version.to_s)
7587
end

test/replica_sets/connect_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,20 @@ def test_connect_with_connection_string_in_env_var
119119
ENV['MONGODB_URI'] = old_mongodb_uri
120120
end
121121
end
122+
123+
def test_connect_with_connection_string_in_implicit_mongodb_uri
124+
begin
125+
old_mongodb_uri = ENV['MONGODB_URI']
126+
ENV['MONGODB_URI'] = "mongodb://#{@rs.host}:#{@rs.ports[0]},#{@rs.host}:#{@rs.ports[1]}?replicaset=#{@rs.name}"
127+
silently do
128+
@conn = Connection.from_uri
129+
end
130+
assert @conn.is_a?(ReplSetConnection)
131+
assert @conn.connected?
132+
ensure
133+
ENV['MONGODB_URI'] = old_mongodb_uri
134+
end
135+
end
122136

123137
def test_connect_with_new_seed_format
124138
@conn = ReplSetConnection.new build_seeds(3)

0 commit comments

Comments
 (0)