Skip to content

Commit c55a662

Browse files
committed
PYTHON-1685 - Renovate get_default_database
1 parent 6bea39d commit c55a662

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
@@ -70,6 +70,8 @@ Changes in Version 3.8.0.dev0
7070
``Database``'s :attr:`~pymongo.database.Database.codec_options`
7171
when decoding the command response. Previously the codec_options
7272
was only used when the MongoDB server version was <= 3.0.
73+
- Undeprecated :meth:`~pymongo.mongo_client.MongoClient.get_default_database`
74+
and added the ``default`` parameter.
7375
- TLS Renegotiation is now disabled when possible.
7476
- Custom types can now be directly encoded to, and decoded from MongoDB using
7577
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
@@ -1817,8 +1817,9 @@ def drop_database(self, name_or_database, session=None):
18171817
parse_write_concern_error=True,
18181818
session=session)
18191819

1820-
def get_default_database(self):
1821-
"""DEPRECATED - Get the database named in the MongoDB connection URI.
1820+
def get_default_database(self, default=None, codec_options=None,
1821+
read_preference=None, write_concern=None, read_concern=None):
1822+
"""Get the database named in the MongoDB connection URI.
18221823
18231824
>>> uri = 'mongodb://host/my_database'
18241825
>>> client = MongoClient(uri)
@@ -1830,15 +1831,41 @@ def get_default_database(self):
18301831
Useful in scripts where you want to choose which database to use
18311832
based only on the URI in a configuration file.
18321833
1834+
:Parameters:
1835+
- `default` (optional): the database name to use if no database name
1836+
was provided in the URI.
1837+
- `codec_options` (optional): An instance of
1838+
:class:`~bson.codec_options.CodecOptions`. If ``None`` (the
1839+
default) the :attr:`codec_options` of this :class:`MongoClient` is
1840+
used.
1841+
- `read_preference` (optional): The read preference to use. If
1842+
``None`` (the default) the :attr:`read_preference` of this
1843+
:class:`MongoClient` is used. See :mod:`~pymongo.read_preferences`
1844+
for options.
1845+
- `write_concern` (optional): An instance of
1846+
:class:`~pymongo.write_concern.WriteConcern`. If ``None`` (the
1847+
default) the :attr:`write_concern` of this :class:`MongoClient` is
1848+
used.
1849+
- `read_concern` (optional): An instance of
1850+
:class:`~pymongo.read_concern.ReadConcern`. If ``None`` (the
1851+
default) the :attr:`read_concern` of this :class:`MongoClient` is
1852+
used.
1853+
1854+
.. versionchanged:: 3.8
1855+
Undeprecated. Added the ``default``, ``codec_options``,
1856+
``read_preference``, ``write_concern`` and ``read_concern``
1857+
parameters.
1858+
18331859
.. versionchanged:: 3.5
18341860
Deprecated, use :meth:`get_database` instead.
18351861
"""
1836-
warnings.warn("get_default_database is deprecated. Use get_database "
1837-
"instead.", DeprecationWarning, stacklevel=2)
1838-
if self.__default_database_name is None:
1839-
raise ConfigurationError('No default database defined')
1862+
if self.__default_database_name is None and default is None:
1863+
raise ConfigurationError(
1864+
'No default database name defined or provided.')
18401865

1841-
return self[self.__default_database_name]
1866+
return database.Database(
1867+
self, self.__default_database_name or default, codec_options,
1868+
read_preference, write_concern, read_concern)
18421869

18431870
def get_database(self, name=None, codec_options=None, read_preference=None,
18441871
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)