Skip to content

Commit

Permalink
[FIX] chunked: explicitly flush on every iteration
Browse files Browse the repository at this point in the history
Fixes #362
  • Loading branch information
ivantodorovich committed Apr 11, 2024
1 parent d341a6a commit e882280
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
8 changes: 1 addition & 7 deletions openupgradelib/openupgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -3261,17 +3261,11 @@ def chunked(records, single=True):
"""Memory and performance friendly method to iterate over a potentially
large number of records. Yields either a whole chunk or a single record
at the time. Don't nest calls to this method."""
if version_info[0] > 10:
invalidate = records.env.cache.invalidate
elif version_info[0] > 7:
invalidate = records.env.invalidate_all
else:
raise Exception("Not supported Odoo version for this method.")
size = core.models.PREFETCH_MAX
model = records._name
ids = records.with_context(prefetch_fields=False).ids
for i in range(0, len(ids), size):
invalidate()
openupgrade_tools.invalidate_cache(records.env, flush=True)
chunk = records.env[model].browse(ids[i : i + size])
if single:
for record in chunk:
Expand Down
41 changes: 41 additions & 0 deletions openupgradelib/openupgrade_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
from lxml.etree import tostring
from lxml.html import fromstring

logger = logging.getLogger(__name__)


def table_exists(cr, table):
"""Check whether a certain table or view exists"""
Expand Down Expand Up @@ -348,3 +350,42 @@ def replace_html_replacement_attr_shortcut(attr_rp="", **kwargs):
}
)
return kwargs


def invalidate_cache(env, flush=True):
"""Version-independent cache invalidation.
:param flush: whether pending updates should be flushed before invalidation.
It is ``True`` by default, which ensures cache consistency.
Do not use this parameter unless you know what you are doing.
"""

# It needs to be loaded after odoo is imported
from .openupgrade import version_info

version = version_info[0]

# Warning on possibly untested versions where chunked might not work as expected
if version > 17: # unreleased version at this time
logger.warning(
f"openupgradelib.invalidate_cache hasn't been tested on Odoo {version}. "
"Please report any issue you may find and consider bumping this warning "
"to the next version otherwise."
)

# 16.0: invalidate_all is re-introduced (with flush_all)
if version >= 16:
env.invalidate_all(flush=flush)
# 13.0: the write cache and flush is introduced
elif version >= 13:
if flush:
env["base"].flush()
env.cache.invalidate()
# 11.0: the invalidate_all method is deprecated
elif version >= 11:
env.cache.invalidate()
# 8.0: new api
elif version >= 8:
env.invalidate_all()
else:
raise Exception("Not supported Odoo version for this method.")

0 comments on commit e882280

Please sign in to comment.