Skip to content

Commit 3738580

Browse files
committed
don't require a database name in the URI (fixes #117)
1 parent 4353c58 commit 3738580

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

docs/index.rst

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ You can use :attr:`~flask_pymongo.PyMongo.db` directly in views:
4646
return render_template("index.html",
4747
online_users=online_users)
4848
49+
.. note::
50+
51+
Previous versions of Flask-PyMongo required that the MongoDB URI
52+
contained a database name; as of 2.2, this requirement is lifted. If
53+
there is no database name, the :attr:`~flask_pymongo.PyMongo.db`
54+
attribute will be ``None``.
55+
4956

5057
Compatibility
5158
-------------
@@ -135,7 +142,7 @@ Classes
135142

136143
.. attribute:: db
137144

138-
The :class:`~flask_pymongo.wrappers.Database` if a URI was used and it
145+
The :class:`~flask_pymongo.wrappers.Database` if the URI used
139146
named a database, and ``None`` otherwise.
140147

141148

@@ -167,6 +174,12 @@ History and Contributors
167174

168175
Changes:
169176

177+
- 2.2.0: November 1, 2018
178+
179+
- `#114 <https://github.com/dcrosta/flask-pymongo/pull/114>`_ Accept
180+
keyword arguments to :meth:`~flask_pymongo.PyMongo.save_file` (Andrew C.
181+
Hawkins).
182+
170183
- 2.1.0: August 6, 2018
171184

172185
- `#114 <https://github.com/dcrosta/flask-pymongo/pull/114>`_ Accept

flask_pymongo/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ def init_app(self, app, uri=None, *args, **kwargs):
130130
The caller is responsible for ensuring that additional positional
131131
and keyword arguments result in a valid call.
132132
133+
.. versionchanged:: 2.2
134+
135+
The ``uri`` is no longer required to contain a database name. If it
136+
does not, then the :attr:`db` attribute will be ``None``.
137+
133138
.. versionchanged:: 2.0
134139
135140
Flask-PyMongo no longer accepts many of the configuration variables
@@ -149,16 +154,13 @@ def init_app(self, app, uri=None, *args, **kwargs):
149154
parsed_uri = uri_parser.parse_uri(uri)
150155
database_name = parsed_uri["database"]
151156

152-
# Avoid a more confusing error later when we try to get the DB
153-
if not database_name:
154-
raise ValueError("Your URI must specify a database name")
155-
156157
# Try to delay connecting, in case the app is loaded before forking, per
157158
# http://api.mongodb.com/python/current/faq.html#is-pymongo-fork-safe
158159
kwargs.setdefault("connect", False)
159160

160161
self.cx = MongoClient(*args, **kwargs)
161-
self.db = self.cx[database_name]
162+
if database_name:
163+
self.db = self.cx[database_name]
162164

163165
app.url_map.converters["ObjectId"] = BSONObjectIdConverter
164166

flask_pymongo/tests/test_config.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from contextlib import contextmanager
12
import time
23

34
import pymongo
@@ -11,6 +12,14 @@ class CouldNotConnect(Exception):
1112
pass
1213

1314

15+
@contextmanager
16+
def doesnt_raise(exc=BaseException):
17+
try:
18+
yield
19+
except exc:
20+
pytest.fail("{} was raised but should not have been".format(exc))
21+
22+
1423
class FlaskPyMongoConfigTest(FlaskRequestTest):
1524

1625
def setUp(self):
@@ -81,11 +90,13 @@ def test_it_doesnt_connect_by_default(self):
8190
with pytest.raises(CouldNotConnect):
8291
_wait_until_connected(mongo, timeout=0.2)
8392

84-
def test_it_requires_db_name_in_uri(self):
93+
def test_it_doesnt_require_db_name_in_uri(self):
8594
uri = "mongodb://localhost:{}".format(self.port)
8695

87-
with pytest.raises(ValueError):
88-
flask_pymongo.PyMongo(self.app, uri)
96+
with doesnt_raise(Exception):
97+
mongo = flask_pymongo.PyMongo(self.app, uri)
98+
99+
assert mongo.db is None
89100

90101

91102
def _wait_until_connected(mongo, timeout=1.0):

0 commit comments

Comments
 (0)