Skip to content

Commit ea62ce5

Browse files
committed
PYTHON-1685 - Renovate get_default_database
(cherry picked from commit c55a662)
1 parent 8f4b0c5 commit ea62ce5

File tree

5 files changed

+73
-28
lines changed

5 files changed

+73
-28
lines changed

doc/api/pymongo/mongo_client.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
.. automethod:: list_database_names
4141
.. automethod:: database_names
4242
.. automethod:: drop_database
43+
.. automethod:: get_default_database
4344
.. automethod:: get_database
4445
.. automethod:: server_info
4546
.. automethod:: close_cursor
@@ -48,4 +49,3 @@
4849
.. automethod:: watch
4950
.. automethod:: fsync
5051
.. automethod:: unlock
51-
.. automethod:: get_default_database

doc/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Changes in Version 3.8.0.dev0
2626
``Database``'s :attr:`~pymongo.database.Database.codec_options`
2727
when decoding the command response. Previously the codec_options
2828
was only used when the MongoDB server version was <= 3.0.
29+
- Undeprecated :meth:`~pymongo.mongo_client.MongoClient.get_default_database`
30+
and added the ``default`` parameter.
2931
- TLS Renegotiation is now disabled when possible.
3032
- Custom types can now be directly encoded to, and decoded from MongoDB using
3133
the :class:`~bson.codec_options.TypeCodec` and

pymongo/mongo_client.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,8 +1752,9 @@ def drop_database(self, name_or_database, session=None):
17521752
parse_write_concern_error=True,
17531753
session=session)
17541754

1755-
def get_default_database(self):
1756-
"""DEPRECATED - Get the database named in the MongoDB connection URI.
1755+
def get_default_database(self, default=None, codec_options=None,
1756+
read_preference=None, write_concern=None, read_concern=None):
1757+
"""Get the database named in the MongoDB connection URI.
17571758
17581759
>>> uri = 'mongodb://host/my_database'
17591760
>>> client = MongoClient(uri)
@@ -1765,15 +1766,41 @@ def get_default_database(self):
17651766
Useful in scripts where you want to choose which database to use
17661767
based only on the URI in a configuration file.
17671768
1769+
:Parameters:
1770+
- `default` (optional): the database name to use if no database name
1771+
was provided in the URI.
1772+
- `codec_options` (optional): An instance of
1773+
:class:`~bson.codec_options.CodecOptions`. If ``None`` (the
1774+
default) the :attr:`codec_options` of this :class:`MongoClient` is
1775+
used.
1776+
- `read_preference` (optional): The read preference to use. If
1777+
``None`` (the default) the :attr:`read_preference` of this
1778+
:class:`MongoClient` is used. See :mod:`~pymongo.read_preferences`
1779+
for options.
1780+
- `write_concern` (optional): An instance of
1781+
:class:`~pymongo.write_concern.WriteConcern`. If ``None`` (the
1782+
default) the :attr:`write_concern` of this :class:`MongoClient` is
1783+
used.
1784+
- `read_concern` (optional): An instance of
1785+
:class:`~pymongo.read_concern.ReadConcern`. If ``None`` (the
1786+
default) the :attr:`read_concern` of this :class:`MongoClient` is
1787+
used.
1788+
1789+
.. versionchanged:: 3.8
1790+
Undeprecated. Added the ``default``, ``codec_options``,
1791+
``read_preference``, ``write_concern`` and ``read_concern``
1792+
parameters.
1793+
17681794
.. versionchanged:: 3.5
17691795
Deprecated, use :meth:`get_database` instead.
17701796
"""
1771-
warnings.warn("get_default_database is deprecated. Use get_database "
1772-
"instead.", DeprecationWarning, stacklevel=2)
1773-
if self.__default_database_name is None:
1774-
raise ConfigurationError('No default database defined')
1797+
if self.__default_database_name is None and default is None:
1798+
raise ConfigurationError(
1799+
'No default database name defined or provided.')
17751800

1776-
return self[self.__default_database_name]
1801+
return database.Database(
1802+
self, self.__default_database_name or default, codec_options,
1803+
read_preference, write_concern, read_concern)
17771804

17781805
def get_database(self, name=None, codec_options=None, read_preference=None,
17791806
write_concern=None, read_concern=None):

test/test_client.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,42 @@ def iterate():
179179

180180
self.assertRaises(TypeError, iterate)
181181

182+
def test_get_default_database(self):
183+
c = rs_or_single_client("mongodb://%s:%d/foo" % (client_context.host,
184+
client_context.port),
185+
connect=False)
186+
self.assertEqual(Database(c, 'foo'), c.get_default_database())
187+
# Test that default doesn't override the URI value.
188+
self.assertEqual(Database(c, 'foo'), c.get_default_database('bar'))
189+
190+
codec_options = CodecOptions(tz_aware=True)
191+
write_concern = WriteConcern(w=2, j=True)
192+
db = c.get_default_database(
193+
None, codec_options, ReadPreference.SECONDARY, write_concern)
194+
self.assertEqual('foo', db.name)
195+
self.assertEqual(codec_options, db.codec_options)
196+
self.assertEqual(ReadPreference.SECONDARY, db.read_preference)
197+
self.assertEqual(write_concern, db.write_concern)
198+
199+
c = rs_or_single_client("mongodb://%s:%d/" % (client_context.host,
200+
client_context.port),
201+
connect=False)
202+
self.assertEqual(Database(c, 'foo'), c.get_default_database('foo'))
203+
204+
def test_get_default_database_error(self):
205+
# URI with no database.
206+
c = rs_or_single_client("mongodb://%s:%d/" % (client_context.host,
207+
client_context.port),
208+
connect=False)
209+
self.assertRaises(ConfigurationError, c.get_default_database)
210+
211+
def test_get_default_database_with_authsource(self):
212+
# Ensure we distinguish database name from authSource.
213+
uri = "mongodb://%s:%d/foo?authSource=src" % (
214+
client_context.host, client_context.port)
215+
c = rs_or_single_client(uri, connect=False)
216+
self.assertEqual(Database(c, 'foo'), c.get_default_database())
217+
182218
def test_get_database_default(self):
183219
c = rs_or_single_client("mongodb://%s:%d/foo" % (client_context.host,
184220
client_context.port),

test/test_legacy_api.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,26 +1403,6 @@ def raises_cursor_not_found():
14031403

14041404
wait_until(raises_cursor_not_found, 'close cursor')
14051405

1406-
def test_get_default_database(self):
1407-
c = rs_or_single_client("mongodb://%s:%d/foo" % (client_context.host,
1408-
client_context.port),
1409-
connect=False)
1410-
self.assertEqual(Database(c, 'foo'), c.get_default_database())
1411-
1412-
def test_get_default_database_error(self):
1413-
# URI with no database.
1414-
c = rs_or_single_client("mongodb://%s:%d/" % (client_context.host,
1415-
client_context.port),
1416-
connect=False)
1417-
self.assertRaises(ConfigurationError, c.get_default_database)
1418-
1419-
def test_get_default_database_with_authsource(self):
1420-
# Ensure we distinguish database name from authSource.
1421-
uri = "mongodb://%s:%d/foo?authSource=src" % (
1422-
client_context.host, client_context.port)
1423-
c = rs_or_single_client(uri, connect=False)
1424-
self.assertEqual(Database(c, 'foo'), c.get_default_database())
1425-
14261406

14271407
class TestLegacyBulk(BulkTestBase):
14281408

0 commit comments

Comments
 (0)