Skip to content
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

Prepare for pymongo 4 upgrade #60

Open
wants to merge 28 commits into
base: master
Choose a base branch
from

Conversation

dill0wn
Copy link
Contributor

@dill0wn dill0wn commented Jun 26, 2024

This was motivated by this issue #48 requesting pymongo v4 support. This PR lays the groundwork for the upgrade to pymongo4, but it stops short of actually bumping the package.

I followed pymongo's migration guide to see what was removed or deprecated then updated everything relevant in Ming. pymongo has backported many of their API changes to pymongo3 therefore I think this work should be representative.

That said, I'm sure more issues will crop up once we actually update to pymongo4, but that can be a separate chunk of work.

Copy link

codecov bot commented Jul 1, 2024

Codecov Report

Attention: Patch coverage is 96.27660% with 14 lines in your changes missing coverage. Please review.

Project coverage is 91.11%. Comparing base (637a033) to head (281069f).
Report is 8 commits behind head on master.

Files Patch % Lines
ming/odm/odmsession.py 54.54% 5 Missing ⚠️
ming/session.py 83.33% 5 Missing ⚠️
ming/mim.py 94.73% 3 Missing ⚠️
ming/odm/mapper.py 90.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #60      +/-   ##
==========================================
+ Coverage   90.19%   91.11%   +0.91%     
==========================================
  Files          43       43              
  Lines        6724     6682      -42     
==========================================
+ Hits         6065     6088      +23     
+ Misses        659      594      -65     
Flag Coverage Δ
tests-3.10 91.30% <96.27%> (+0.91%) ⬆️
tests-3.11 91.30% <96.27%> (+0.91%) ⬆️
tests-3.12 91.30% <96.27%> (?)
tests-3.7 ?
tests-3.8 91.10% <96.27%> (+0.91%) ⬆️
tests-3.9 91.10% <96.27%> (+0.91%) ⬆️
tests-pypy3.9 91.22% <96.27%> (+0.89%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@brondsem brondsem left a comment

Choose a reason for hiding this comment

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

Wow what a great amount of work. It seems real good. I've posted some specific feedback, and then lets also do additional testing before we merge and release. E.g. testing with codebases that use Ming, and with pymongo actually upgraded to v4

docs/src/ming_odm_tutorial.py Outdated Show resolved Hide resolved
ming/mim.py Outdated Show resolved Hide resolved
@@ -358,7 +359,7 @@ def _ensure_indexes(self):
try:
with self._lock:
for idx in self.manager.indexes:
collection.ensure_index(idx.index_spec, background=True,
collection.create_index(idx.index_spec, background=True,
Copy link
Collaborator

Choose a reason for hiding this comment

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

https://pymongo.readthedocs.io/en/stable/migrate-to-pymongo4.html#collection-ensure-index-is-removed recommends we should avoid frequent calls to ensure_index Are we okay because of the self.indexes_ensured flag? Seems like it

def replace_one(self, filter, replacement, upsert=False):
return self._update(filter, replacement, upsert)
return self.__update(filter, replacement, upsert)

def __update(self, spec, updates, upsert=False, multi=False):
Copy link
Collaborator

Choose a reason for hiding this comment

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

def __update returns a dict with some values like err n nModified etc. That is old. Now pymongo returns a UpdateResult https://pymongo.readthedocs.io/en/stable/api/pymongo/results.html#pymongo.results.UpdateResult

MIM should be updated to match

Ming could be an abstraction layer and return the old format, but I think there's probably not that many places that use the old result dict, and we could require them to be updated.

ming/odm/mapper.py Outdated Show resolved Hide resolved
ming/session.py Outdated
data = {arg: data[arg] for arg in args}
result = self._impl(doc).update_one(
dict(_id=doc._id), {'$set': data},
upsert=True, **fix_write_concern(kwargs)
Copy link
Collaborator

Choose a reason for hiding this comment

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

the old update() call didn't pass upsert=True, do we need it here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My thinking was that we probably don't ever want to "fail" on a save. We probably always want to just... do the save. An upsert guarantees that. At least that was my thinking. I wasn't necessarily thinking about how we're currently using this fn, more about conceptually how I would expect it to work as a user.

ming/tests/test_mim.py Outdated Show resolved Hide resolved
matched_count=0,
modified_count=0,
raw_result=None,
upsert_id=None,
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

seems like this should be an UpdateResult instance rather than a dict? Although you have some comments about backwards-compatibillity with ['n'] Maybe it won't be possible to be backwards compatible if we want to match the new return type.

self._deindex(doc)
else:
new_data[id] = doc
self._data = new_data
# TODO: this is needed for backcompat, but can drop 'n' in SF-9544
result['n'] = result['deleted_count']
return result
Copy link
Collaborator

Choose a reason for hiding this comment

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

should this be DeleteResult rather than a dict?

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.

None yet

2 participants