Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion openupgradelib/openupgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -3587,7 +3587,10 @@ 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."""
size = core.models.PREFETCH_MAX
# PREFETCH_MAX lives in models in <v19, and tools.constants afterwards
size = (
getattr(core.models, "PREFETCH_MAX", None) or core.tools.constants.PREFETCH_MAX
)
model = records._name
ids = records.with_context(prefetch_fields=False).ids
for i in range(0, len(ids), size):
Expand Down
13 changes: 13 additions & 0 deletions tests/test_openupgradelib.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ def test_delete_translations(self):
record.with_context(lang="fr_FR").description,
)

def test_chunked(self):
records = self.env["ir.module.module"].search([])

chunked_records = self.env["ir.module.module"]
for chunk in openupgrade.chunked(records):
chunked_records += chunk
self.assertEqual(records, chunked_records)

chunked_records = self.env["ir.module.module"]
for chunk in openupgrade.chunked(records, single=True):
chunked_records += chunk
self.assertEqual(records, chunked_records)

def tearDown(self):
super().tearDown()
self.cr.close()