-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-3298 Add flag to create_collection to skip listCollections pre-check #1006
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
Conversation
pymongo/database.py
Outdated
@@ -342,7 +342,8 @@ def create_collection( | |||
All optional `create collection command`_ parameters should be passed | |||
as keyword arguments to this method. Valid options include, but are not | |||
limited to: | |||
|
|||
- ``checkExists`` (bool): if True (the default) , send a listCollections command to check | |||
if the collection already exists before creation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
checkExists is not a create command option so it should be an explicit check_exists argument.
@@ -202,23 +202,14 @@ class OvertCommandListener(EventListener): | |||
ignore_list_collections = False | |||
|
|||
def started(self, event): | |||
if self.ignore_list_collections and event.command_name.lower() == "listcollections": | |||
self.ignore_list_collections = False | |||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
pymongo/database.py
Outdated
@@ -402,7 +403,7 @@ def create_collection( | |||
enabling pre- and post-images. | |||
|
|||
.. versionchanged:: 4.2 | |||
Added the ``clusteredIndex`` and ``encryptedFields`` parameters. | |||
Added the ``checkExists``, ``clusteredIndex``, and ``encryptedFields`` parameters. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a note in the changelog too?
test/test_database.py
Outdated
def test_check_exists(self): | ||
client = rs_or_single_client() | ||
db = client[self.db.name] | ||
with mock.patch.object(db, "list_collections") as m: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use a command listener instead of mock patching. mocking makes the driver harder to maintain since it ties the test to internal details of the driver implementation. For example, if we wanted to refactor create_collection() to call db._list_collections instead of db.list_collections we would need to update this test as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
pymongo/database.py
Outdated
@@ -421,6 +424,7 @@ def create_collection( | |||
https://mongodb.com/docs/manual/reference/command/create | |||
""" | |||
encrypted_fields = kwargs.get("encryptedFields") | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unneeded
test/test_database.py
Outdated
@@ -220,6 +221,18 @@ def test_list_collection_names_filter(self): | |||
self.assertIn("nameOnly", command) | |||
self.assertTrue(command["nameOnly"]) | |||
|
|||
def test_check_exists(self): | |||
client = rs_or_single_client() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addCleanup(client.close)
test/test_database.py
Outdated
db = client[self.db.name] | ||
db.drop_collection("unique") | ||
db.create_collection("unique", check_exists=True) | ||
self.assertIn("listCollections", [next(iter(x.command)) for x in results["started"]]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use listeners.started_command_names() and listener.reset().
No description provided.