Skip to content

PYTHON-1587 Remove MongoClient.database_names and Database.collection_names #551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion doc/api/pymongo/mongo_client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
.. automethod:: start_session
.. automethod:: list_databases
.. automethod:: list_database_names
.. automethod:: database_names
.. automethod:: drop_database
.. automethod:: get_default_database
.. automethod:: get_database
Expand Down
1 change: 0 additions & 1 deletion doc/api/pymongo/mongo_replica_set_client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
.. autoattribute:: codec_options
.. autoattribute:: read_preference
.. autoattribute:: write_concern
.. automethod:: database_names
.. automethod:: drop_database
.. automethod:: get_database
.. automethod:: close_cursor
Expand Down
2 changes: 2 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Breaking Changes in 4.0
- Removed :meth:`pymongo.mongo_client.MongoClient.fsync`,
:meth:`pymongo.mongo_client.MongoClient.unlock`, and
:attr:`pymongo.mongo_client.MongoClient.is_locked`.
- Removed :meth:`pymongo.mongo_client.MongoClient.database_names`.
- Removed :meth:`pymongo.database.Database.collection_names`.
- Removed :meth:`pymongo.collection.Collection.parallel_scan`.
- Removed :mod:`pymongo.thread_util`.

Expand Down
31 changes: 31 additions & 0 deletions doc/migrate-to-pymongo4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,37 @@ collection::

.. _currentOp command: https://docs.mongodb.com/manual/reference/command/currentOp/

MongoClient.database_names is removed
.....................................

Removed :meth:`pymongo.mongo_client.MongoClient.database_names`. Use
:meth:`~pymongo.mongo_client.MongoClient.list_database_names` instead. Code like
this::

names = client.database_names()

can be changed to this::

names = client.list_database_names()

Database
--------

Database.collection_names is removed
....................................

Removed :meth:`pymongo.database.Database.collection_names`. Use
:meth:`~pymongo.database.Database.list_collection_names` instead. Code like
this::

names = client.collection_names()
non_system_names = client.collection_names(include_system_collections=False)

can be changed to this::

names = client.list_collection_names()
non_system_names = client.list_collection_names(filter={"name": {"$regex": r"^(?!system\\.)"}})

Removed features with no migration path
---------------------------------------

Expand Down
26 changes: 0 additions & 26 deletions pymongo/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@


_INDEX_REGEX = {"name": {"$regex": r"^(?!.*\$)"}}
_SYSTEM_FILTER = {"filter": {"name": {"$regex": r"^(?!system\.)"}}}


def _check_name(name):
Expand Down Expand Up @@ -863,31 +862,6 @@ def list_collection_names(self, session=None, filter=None, **kwargs):
return [result["name"]
for result in self.list_collections(session=session, **kwargs)]

def collection_names(self, include_system_collections=True,
session=None):
"""**DEPRECATED**: Get a list of all the collection names in this
database.

:Parameters:
- `include_system_collections` (optional): if ``False`` list
will not include system collections (e.g ``system.indexes``)
- `session` (optional): a
:class:`~pymongo.client_session.ClientSession`.

.. versionchanged:: 3.7
Deprecated. Use :meth:`list_collection_names` instead.

.. versionchanged:: 3.6
Added ``session`` parameter.
"""
warnings.warn("collection_names is deprecated. Use "
"list_collection_names instead.",
DeprecationWarning, stacklevel=2)
kws = {} if include_system_collections else _SYSTEM_FILTER
return [result["name"]
for result in self.list_collections(session=session,
nameOnly=True, **kws)]

def drop_collection(self, name_or_collection, session=None):
"""Drop a collection.

Expand Down
18 changes: 0 additions & 18 deletions pymongo/mongo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1931,24 +1931,6 @@ def list_database_names(self, session=None):
return [doc["name"]
for doc in self.list_databases(session, nameOnly=True)]

def database_names(self, session=None):
"""**DEPRECATED**: Get a list of the names of all databases on the
connected server.

:Parameters:
- `session` (optional): a
:class:`~pymongo.client_session.ClientSession`.

.. versionchanged:: 3.7
Deprecated. Use :meth:`list_database_names` instead.

.. versionchanged:: 3.6
Added ``session`` parameter.
"""
warnings.warn("database_names is deprecated. Use list_database_names "
"instead.", DeprecationWarning, stacklevel=2)
return self.list_database_names(session)

def drop_database(self, name_or_database, session=None):
"""Drop a database.

Expand Down
10 changes: 2 additions & 8 deletions test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,23 +712,17 @@ def test_list_databases(self):
for doc in cursor:
self.assertEqual(["name"], list(doc))

def _test_list_names(self, meth):
def test_list_database_names(self):
self.client.pymongo_test.test.insert_one({"dummy": u"object"})
self.client.pymongo_test_mike.test.insert_one({"dummy": u"object"})
cmd_docs = self.client.admin.command("listDatabases")["databases"]
cmd_names = [doc["name"] for doc in cmd_docs]

db_names = meth()
db_names = self.client.list_database_names()
self.assertTrue("pymongo_test" in db_names)
self.assertTrue("pymongo_test_mike" in db_names)
self.assertEqual(db_names, cmd_names)

def test_list_database_names(self):
self._test_list_names(self.client.list_database_names)

def test_database_names(self):
self._test_list_names(self.client.database_names)

def test_drop_database(self):
self.assertRaises(TypeError, self.client.drop_database, 5)
self.assertRaises(TypeError, self.client.drop_database, None)
Expand Down
21 changes: 6 additions & 15 deletions test/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,20 @@ def test_create_collection(self):
self.assertTrue(u"test.foo" in db.list_collection_names())
self.assertRaises(CollectionInvalid, db.create_collection, "test.foo")

def _test_collection_names(self, meth, **no_system_kwargs):
def test_list_collection_names(self):
db = Database(self.client, "pymongo_test")
db.test.insert_one({"dummy": u"object"})
db.test.mike.insert_one({"dummy": u"object"})

colls = getattr(db, meth)()
colls = db.list_collection_names()
self.assertTrue("test" in colls)
self.assertTrue("test.mike" in colls)
for coll in colls:
self.assertTrue("$" not in coll)

db.systemcoll.test.insert_one({})
no_system_collections = getattr(db, meth)(**no_system_kwargs)
no_system_collections = db.list_collection_names(
filter={"name": {"$regex": r"^(?!system\.)"}})
for coll in no_system_collections:
self.assertTrue(not coll.startswith("system."))
self.assertIn("systemcoll.test", no_system_collections)
Expand All @@ -186,19 +187,10 @@ def _test_collection_names(self, meth, **no_system_kwargs):
db["coll" + str(i)].insert_one({})
# No Error
try:
getattr(db, meth)()
db.list_collection_names()
finally:
self.client.drop_database("many_collections")

def test_collection_names(self):
self._test_collection_names(
'collection_names', include_system_collections=False)

def test_list_collection_names(self):
self._test_collection_names(
'list_collection_names', filter={
"name": {"$regex": r"^(?!system\.)"}})

def test_list_collection_names_filter(self):
listener = OvertCommandListener()
results = listener.results
Expand Down Expand Up @@ -306,8 +298,7 @@ def test_list_collections(self):

self.client.drop_database("pymongo_test")

def test_collection_names_single_socket(self):
# Test that Database.collection_names only requires one socket.
def test_list_collection_names_single_socket(self):
client = rs_or_single_client(maxPoolSize=1)
client.drop_database('test_collection_names_single_socket')
db = client.test_collection_names_single_socket
Expand Down
1 change: 0 additions & 1 deletion test/test_read_preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ def test_reads_from_secondary(self):
self.assertEqual(10, len(list(coll.find())))

# Test some database helpers.
self.assertIsNotNone(db.collection_names())
self.assertIsNotNone(db.list_collection_names())
self.assertIsNotNone(db.validate_collection("test"))
self.assertIsNotNone(db.command("ping"))
Expand Down
8 changes: 3 additions & 5 deletions test/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,11 @@ def test_end_sessions(self):
client.close()
self.assertEqual(len(listener.results['started']), 0)

@ignore_deprecations # fsync and unlock
def test_client(self):
client = self.client
ops = [
(client.server_info, [], {}),
(client.database_names, [], {}),
(client.list_database_names, [], {}),
(client.drop_database, ['pymongo_test'], {}),
]

Expand All @@ -244,7 +243,6 @@ def test_database(self):
ops = [
(db.command, ['ping'], {}),
(db.create_collection, ['collection'], {}),
(db.collection_names, [], {}),
(db.list_collection_names, [], {}),
(db.validate_collection, ['collection'], {}),
(db.drop_collection, ['collection'], {}),
Expand Down Expand Up @@ -1129,7 +1127,7 @@ def test_explicit_session_logout(self):
db.collection.bulk_write([InsertOne({})], session=s)

with self.assertRaisesRegex(InvalidOperation, err):
db.collection_names(session=s)
db.list_collection_names(session=s)

with self.assertRaisesRegex(InvalidOperation, err):
db.collection.find_one(session=s)
Expand All @@ -1147,7 +1145,7 @@ def test_implicit_session_logout(self):

for name, f in [
('bulk_write', lambda: db.collection.bulk_write([InsertOne({})])),
('collection_names', db.collection_names),
('list_collection_names', db.list_collection_names),
('find_one', db.collection.find_one),
('aggregate', lambda: list(db.collection.aggregate([])))
]:
Expand Down