Skip to content

Commit

Permalink
[IMP] chunked: warn about untested versions
Browse files Browse the repository at this point in the history
  • Loading branch information
ivantodorovich committed Apr 11, 2024
1 parent 16f45d1 commit 1ba6a13
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
27 changes: 1 addition & 26 deletions openupgradelib/openupgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -3261,36 +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."""
# Before each iteration we must flush everything and clear the cache.
# This differs depending on the Odoo version.
if version_info[0] >= 16:

def reset():
records.env.invalidate_all(flush=True)

elif version_info[0] >= 13:

def reset():
records.env["base"].flush()
records.env.cache.invalidate()

elif version_info[0] >= 11:

def reset():
records.env.cache.invalidate()

elif version_info[0] >= 8:

def reset():
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):
reset()
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 1ba6a13

Please sign in to comment.