-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-4946 - Add GridFSBucket.rename_by_name #2219
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
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cd4b1db
PYTHON-4946 - GridFS spec: Add performant 'rename all revisions by fi…
NoahStapp 2103d67
Address review
NoahStapp f7da415
Merge branch 'master' into PYTHON-4946
NoahStapp 91dbda3
Merge branch 'master' into PYTHON-4946
NoahStapp 1218176
Adjust changelog wording
NoahStapp 9c0bc9f
Address review
NoahStapp c57e6c8
Merge branch 'master' into PYTHON-4946
NoahStapp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1015,6 +1015,35 @@ def rename( | |
"matched file_id %i" % (new_filename, file_id) | ||
) | ||
|
||
def rename_by_name( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add some hand written tests for this too like test_rename in test_gridfs_bucket. That way we get some signal on typing and the UX. |
||
self, filename: str, new_filename: str, session: Optional[ClientSession] = None | ||
) -> None: | ||
"""Renames the stored file with the specified filename. | ||
|
||
For example:: | ||
|
||
my_db = MongoClient().test | ||
fs = GridFSBucket(my_db) | ||
fs.upload_from_stream("test_file", "data I want to store!") | ||
fs.rename_by_name("test_file", "new_test_name") | ||
|
||
Raises :exc:`~gridfs.errors.NoFile` if no file with the given filename exists. | ||
|
||
:param filename: The filename of the file to be renamed. | ||
:param new_filename: The new name of the file. | ||
:param session: a :class:`~pymongo.client_session.ClientSession` | ||
|
||
.. versionadded:: 4.12 | ||
""" | ||
_disallow_transactions(session) | ||
result = self._files.update_many( | ||
{"filename": filename}, {"$set": {"filename": new_filename}}, session=session | ||
) | ||
if not result.matched_count: | ||
raise NoFile( | ||
f"no files could be renamed {new_filename!r} because none matched filename {filename!r}" | ||
) | ||
|
||
|
||
class GridIn: | ||
"""Class to write data to GridFS.""" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Instead of adding a new method for this, should we consider overloading the existing rename method? For example:
The pro is we have less apis to maintain. I guess a con is that the user could accidentally mix up the file_id and filename params.
Note I'm not advocating for either, I just think we should consider it and maybe ask the spec author about it.
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.
I like this suggestion--both methods have nearly identical bodies, so combining them makes sense. For #2218, the two methods have slightly different bodies, but still similar enough that we could do the same thing there. Thoughts?
Uh oh!
There was an error while loading. Please reload this page.
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.
What would the method signatures looks like (including type hints)?
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.
After trying this out a bit, I think it's a little too confusing/unclear to combine them.
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.
Could you share the signatures for posterity?
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.
async def rename(file_id: Optional[Any], new_filename: str, filename: Optional[str] = None,...)