Skip to content

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

Merged
merged 8 commits into from
Jul 18, 2022

Conversation

juliusgeo
Copy link
Contributor

No description provided.

@@ -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.
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@@ -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.
Copy link
Member

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?

def test_check_exists(self):
client = rs_or_single_client()
db = client[self.db.name]
with mock.patch.object(db, "list_collections") as m:
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -421,6 +424,7 @@ def create_collection(
https://mongodb.com/docs/manual/reference/command/create
"""
encrypted_fields = kwargs.get("encryptedFields")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unneeded

@@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addCleanup(client.close)

@juliusgeo juliusgeo requested a review from ShaneHarvey July 18, 2022 17:07
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"]])
Copy link
Member

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().

@juliusgeo juliusgeo merged commit 484374e into mongodb:master Jul 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants