Skip to content

Commit a460725

Browse files
committed
PYTHON-2019 Add support for validate command "background" option
1 parent d7128c1 commit a460725

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

doc/changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ Version 3.11 adds support for MongoDB 4.4. Highlights include:
1111
alternative TLS implementation. PyOpenSSL is required for :ref:`OCSP`
1212
support. It will also be installed when using the "tls" extra if the
1313
version of Python in use is older than 2.7.9.
14+
- Added the ``background`` parameter to
15+
:meth:`pymongo.database.Database.validate_collection`. For a description
16+
of this parameter see the MongoDB documentation for the `validate command`_.
17+
18+
.. _validate command: https://docs.mongodb.com/manual/reference/command/validate/
1419

1520
Issues Resolved
1621
...............

pymongo/database.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -921,12 +921,15 @@ def drop_collection(self, name_or_collection, session=None):
921921
session=session)
922922

923923
def validate_collection(self, name_or_collection,
924-
scandata=False, full=False, session=None):
924+
scandata=False, full=False, session=None,
925+
background=None):
925926
"""Validate a collection.
926927
927928
Returns a dict of validation info. Raises CollectionInvalid if
928929
validation fails.
929930
931+
See also the MongoDB documentation on the `validate command`_.
932+
930933
:Parameters:
931934
- `name_or_collection`: A Collection object or the name of a
932935
collection to validate.
@@ -938,9 +941,16 @@ def validate_collection(self, name_or_collection,
938941
documents.
939942
- `session` (optional): a
940943
:class:`~pymongo.client_session.ClientSession`.
944+
- `background` (optional): A boolean flag that determines whether
945+
the command runs in the background. Requires MongoDB 4.4+.
946+
947+
.. versionchanged:: 3.11
948+
Added ``background`` parameter.
941949
942950
.. versionchanged:: 3.6
943951
Added ``session`` parameter.
952+
953+
.. _validate command: https://docs.mongodb.com/manual/reference/command/validate/
944954
"""
945955
name = name_or_collection
946956
if isinstance(name, Collection):
@@ -950,8 +960,13 @@ def validate_collection(self, name_or_collection,
950960
raise TypeError("name_or_collection must be an instance of "
951961
"%s or Collection" % (string_type.__name__,))
952962

953-
result = self.command("validate", _unicode(name),
954-
scandata=scandata, full=full, session=session)
963+
cmd = SON([("validate", _unicode(name)),
964+
("scandata", scandata),
965+
("full", full)])
966+
if background is not None:
967+
cmd["background"] = background
968+
969+
result = self.command(cmd, session=session)
955970

956971
valid = True
957972
# Pre 1.9 results

test/test_database.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,16 @@ def test_validate_collection(self):
367367
self.assertTrue(db.validate_collection(db.test, scandata=True))
368368
self.assertTrue(db.validate_collection(db.test, scandata=True, full=True))
369369
self.assertTrue(db.validate_collection(db.test, True, True))
370+
if client_context.version.at_least(4, 3, 3):
371+
self.assertTrue(db.validate_collection(db.test, background=True))
372+
self.assertTrue(db.validate_collection(db.test, background=False))
373+
self.assertTrue(
374+
db.validate_collection(db.test, scandata=True, background=True))
375+
# The server does not support background=True with full=True.
376+
# Assert that we actually send the background option by checking
377+
# that this combination fails.
378+
with self.assertRaises(OperationFailure):
379+
db.validate_collection(db.test, full=True, background=True)
370380

371381
@client_context.require_no_mongos
372382
def test_profiling_levels(self):

0 commit comments

Comments
 (0)