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

Django 1.9, 1.10, and 1.11 upgrades #125

Merged
merged 27 commits into from Oct 13, 2017

Conversation

vkurup
Copy link
Contributor

@vkurup vkurup commented Oct 7, 2017

Starting with #119, aim to get this working on Django 1.11. It currently only works in 1.8

@coveralls
Copy link

Coverage Status

Coverage decreased (-3.7%) to 89.961% when pulling 77d21e8 on vkurup:django-upgrades into b94ec75 on django-cache-machine:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.04%) to 93.668% when pulling 36589e9 on vkurup:django-upgrades into b94ec75 on django-cache-machine:master.

@coveralls
Copy link

coveralls commented Oct 7, 2017

Coverage Status

Coverage increased (+0.1%) to 93.75% when pulling f6839e4 on vkurup:django-upgrades into b94ec75 on django-cache-machine:master.

@coveralls
Copy link

coveralls commented Oct 7, 2017

Coverage Status

Coverage increased (+0.1%) to 93.75% when pulling 3b562d4 on vkurup:django-upgrades into b94ec75 on django-cache-machine:master.

@coveralls
Copy link

coveralls commented Oct 7, 2017

Coverage Status

Coverage increased (+0.1%) to 93.75% when pulling b7295ff on vkurup:django-upgrades into b94ec75 on django-cache-machine:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.1%) to 93.75% when pulling 7bd4f77 on vkurup:django-upgrades into b94ec75 on django-cache-machine:master.

2 similar comments
@coveralls
Copy link

Coverage Status

Coverage increased (+0.1%) to 93.75% when pulling 7bd4f77 on vkurup:django-upgrades into b94ec75 on django-cache-machine:master.

@coveralls
Copy link

coveralls commented Oct 7, 2017

Coverage Status

Coverage increased (+0.1%) to 93.75% when pulling 7bd4f77 on vkurup:django-upgrades into b94ec75 on django-cache-machine:master.

@vkurup vkurup changed the title WIP: Django upgrades Django 1.9, 1.10, and 1.11 upgrades Oct 7, 2017
@coveralls
Copy link

coveralls commented Oct 8, 2017

Coverage Status

Coverage increased (+0.1%) to 93.75% when pulling 24c9779 on vkurup:django-upgrades into b94ec75 on django-cache-machine:master.

@coveralls
Copy link

coveralls commented Oct 9, 2017

Coverage Status

Coverage increased (+0.1%) to 93.75% when pulling f98e32f on vkurup:django-upgrades into b94ec75 on django-cache-machine:master.

caching/base.py Outdated
https://github.com/django/django/commit/f3b7c059367a4e82bbfc7e4f0d42b10975e79f0c#diff-5b0dda5eb9a242c15879dc9cd2121379
"""
if self._result_cache is None:
self._result_cache = list(self.iterator())
Copy link
Member

Choose a reason for hiding this comment

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

I believe this change will cause a server-side cursor to be used for _fetch_all (if not globally disabled via DISABLE_SERVER_SIDE_CURSORS) on Django 1.11.1+ due two these lines:

I think we need to make sure _fetch_all() still uses self._iterable_class() directly rather than self.iterator() on Django 1.11+, perhaps via something along the lines of the following:

  • Separate our iterator() logic into a separate method, e.g., _iterate_on(iterable)
  • In _fetch_all(), call list(self._iterate_on(iter(self._iterable_class(self)))) for Django 1.11+ or list(self._iterate_on(self.iterator())) for older Djangos (_iterable_class unfortunately does not exist in Django 1.8).
  • In iterator(), simply return self._iterate_on(super(CachingQuerySet, self).iterator())

Does that seem crazy? What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Or better yet, rather than check the version directly, use duck typing as you already did here: 24c9779#diff-76150a51cd21d8d6b30aaaab22c7f114R179

_fetch_all() and iterator() are unchanged between Django 1.9-1.10, so it should be safe to use self._iterable_class directly in those versions (in addition to Django 1.11)

_fetch_all():

iterator():

@vkurup
Copy link
Contributor Author

vkurup commented Oct 11, 2017

@tobiasmcnulty I made an update, trying to avoid overriding _fetch_all. This involved the following changes:

  • Creating CachingModelIterable as a subclass of django's ModelIterable, which allows us to use the superclass's __iter__ definition easily
  • Changed the constructor of CacheMachine to allow it to share most of its code with CachingModelIterable
  • Some 'conditional' hacks to make sure we load the correct iterator for Django 1.8 vs Django 1.9+

Let me know what you think!

@coveralls
Copy link

Coverage Status

Coverage increased (+0.6%) to 94.194% when pulling 4f1fb8e on vkurup:django-upgrades into b94ec75 on django-cache-machine:master.

3 similar comments
@coveralls
Copy link

Coverage Status

Coverage increased (+0.6%) to 94.194% when pulling 4f1fb8e on vkurup:django-upgrades into b94ec75 on django-cache-machine:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.6%) to 94.194% when pulling 4f1fb8e on vkurup:django-upgrades into b94ec75 on django-cache-machine:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.6%) to 94.194% when pulling 4f1fb8e on vkurup:django-upgrades into b94ec75 on django-cache-machine:master.

except StopIteration:
if to_cache or config.CACHE_EMPTY_QUERYSETS:
self.cache_objects(to_cache, query_key)
raise
Copy link
Member

Choose a reason for hiding this comment

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

This change loses the StopIteration re-raised here -- is that intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh... I think I missed that (I thought I was just simplifying an iteration through a iterator). I'll have to see if I can find a way to test that, or maybe just revert back to the old version of the code.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, this might be a part of Python that I just don't understand well. I'd think that I could just fix this by adding a raise StopIteration on line 128 (at the same indentation level as the if on line 126). But that seems superfluous, since it's the last line of the method and the iteration will end anyways. Am I missing something?

Copy link
Member

Choose a reason for hiding this comment

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

caching/base.py Outdated
def __iter__(self):
if hasattr(super(CacheInternalCommonMixin, self), '__iter__'):
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps try:/except AttributeError: to avoid hasattr() usage? The Django 1.8 bit can go away eventually, and there will be no performance hit for Django 1.9+

Copy link
Contributor Author

Choose a reason for hiding this comment

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

+1

caching/base.py Outdated
cache.add(query_key, objects, timeout=self.timeout)
invalidator.cache_objects(self.model, objects, query_key, query_flush)

class CacheMachine(CacheInternalCommonMixin):
Copy link
Member

Choose a reason for hiding this comment

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

Not entirely sure this will work, but I wonder if CacheInternalCommonMixin, CacheMachine, and CachingModelIterable could all be collapsed into a single class (e.g., by setting ModelIterable = object when it fails to import). Definitely ignore this comment if that would complicate rather than simplify things!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried doing that, but gave up early because of issues that I can't remember. But some of those problems might have been 2 small issues in the tests which I since identified, so let me try again.

caching/base.py Outdated
yield obj
return

# Try to fetch from the cache.
try:
query_key = self.query_key()
except query.EmptyResultSet:
raise StopIteration
Copy link
Member

Choose a reason for hiding this comment

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

Given our findings about StopIteration elsewhere, I think this should be converted to a simple return

caching/base.py Outdated
@@ -309,11 +332,13 @@ def __iter__(self):
while True:
yield next(iterator)
else:
sql = self.raw_query % tuple(self.params)
for obj in CacheMachine(self.model, sql, iterator, timeout=self.timeout):
for obj in CacheMachine(self, iterator, timeout=self.timeout):
yield obj
raise StopIteration
Copy link
Member

Choose a reason for hiding this comment

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

I think this line can be deleted (so we get the implicit return None instead)

@coveralls
Copy link

coveralls commented Oct 13, 2017

Coverage Status

Coverage increased (+0.5%) to 94.092% when pulling c220d61 on vkurup:django-upgrades into b94ec75 on django-cache-machine:master.

@coveralls
Copy link

coveralls commented Oct 13, 2017

Coverage Status

Coverage increased (+0.5%) to 94.092% when pulling 096f2b2 on vkurup:django-upgrades into b94ec75 on django-cache-machine:master.

@tobiasmcnulty
Copy link
Member

⛵️

Thanks again for taking this on!

@coveralls
Copy link

Coverage Status

Coverage increased (+0.5%) to 94.092% when pulling 251e5e6 on vkurup:django-upgrades into b94ec75 on django-cache-machine:master.

1 similar comment
@coveralls
Copy link

coveralls commented Oct 13, 2017

Coverage Status

Coverage increased (+0.5%) to 94.092% when pulling 251e5e6 on vkurup:django-upgrades into b94ec75 on django-cache-machine:master.

@vkurup vkurup merged commit 7f05b92 into django-cache-machine:master Oct 13, 2017
@vkurup vkurup deleted the django-upgrades branch October 13, 2017 16:49
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

3 participants