Skip to content

Commit 5fd7c97

Browse files
authored
Merge pull request #441 from hbrunn/master-v19-PREFETCH_MAX
[IMP] openupgrade.chunked: support v19
2 parents 98abfac + 53e361f commit 5fd7c97

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

openupgradelib/openupgrade.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3587,7 +3587,10 @@ def chunked(records, single=True):
35873587
"""Memory and performance friendly method to iterate over a potentially
35883588
large number of records. Yields either a whole chunk or a single record
35893589
at the time. Don't nest calls to this method."""
3590-
size = core.models.PREFETCH_MAX
3590+
# PREFETCH_MAX lives in models in <v19, and tools.constants afterwards
3591+
size = (
3592+
getattr(core.models, "PREFETCH_MAX", None) or core.tools.constants.PREFETCH_MAX
3593+
)
35913594
model = records._name
35923595
ids = records.with_context(prefetch_fields=False).ids
35933596
for i in range(0, len(ids), size):

tests/test_openupgradelib.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@ def test_delete_translations(self):
6464
record.with_context(lang="fr_FR").description,
6565
)
6666

67+
def test_chunked(self):
68+
records = self.env["ir.module.module"].search([])
69+
70+
chunked_records = self.env["ir.module.module"]
71+
for chunk in openupgrade.chunked(records):
72+
chunked_records += chunk
73+
self.assertEqual(records, chunked_records)
74+
75+
chunked_records = self.env["ir.module.module"]
76+
for chunk in openupgrade.chunked(records, single=True):
77+
chunked_records += chunk
78+
self.assertEqual(records, chunked_records)
79+
6780
def tearDown(self):
6881
super().tearDown()
6982
self.cr.close()

0 commit comments

Comments
 (0)