From 797f7f2c2ad3935417279e74f35b3fdd66ed3c83 Mon Sep 17 00:00:00 2001 From: hugues de keyzer Date: Fri, 14 Feb 2025 11:17:41 +0100 Subject: [PATCH 1/2] [FIX] website_sale: avoid memory error avoid memory error when converting website_sale images. --- .../migrations/13.0.1.0/post-migration.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/addons/website_sale/migrations/13.0.1.0/post-migration.py b/addons/website_sale/migrations/13.0.1.0/post-migration.py index c44889dce933..c21d16749232 100644 --- a/addons/website_sale/migrations/13.0.1.0/post-migration.py +++ b/addons/website_sale/migrations/13.0.1.0/post-migration.py @@ -20,14 +20,20 @@ def convert_image_attachments(env): 'product.image': "image", 'product.public.category': "image", } + attachment_model = env['ir.attachment'] for model, field in mapping.items(): Model = env[model] - attachments = env['ir.attachment'].search([ + attachment_ids = attachment_model.search([ ('res_model', '=', model), ('res_field', '=', field), ('res_id', '!=', False), - ]) - for attachment in attachments: + ]).ids + # a MemoryError can happen if too many attachments are loaded in + # memory. therefore, we only load one record at the time and flush the + # cache at each iteration. + for attachment_id in attachment_ids: + # load attachments one by one to avoid prefetching + attachment = attachment_model.browse(attachment_id) try: Model.browse(attachment.res_id).image_1920 = attachment.datas except Exception as e: @@ -38,6 +44,10 @@ def convert_image_attachments(env): attachment.res_id, repr(e), ) + # flush and clear cache to avoid to fill up memory and force the + # computation of the computed fields (smaller size images) + Model.flush() + env.cache.invalidate() def move_fields_from_invoice_to_moves(env): From 83b6c6225198acf796cbe1d9d5c7d76338543e1f Mon Sep 17 00:00:00 2001 From: hugues de keyzer Date: Wed, 19 Nov 2025 10:01:11 +0100 Subject: [PATCH 2/2] [FIX] product: avoid memory error avoid memory error when converting product images. --- .../migrations/13.0.1.2/post-migration.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/addons/product/migrations/13.0.1.2/post-migration.py b/addons/product/migrations/13.0.1.2/post-migration.py index c4bce191f1c3..2e2efc6ae838 100644 --- a/addons/product/migrations/13.0.1.2/post-migration.py +++ b/addons/product/migrations/13.0.1.2/post-migration.py @@ -14,14 +14,20 @@ def convert_image_attachments(env): 'product.product': "image_variant", 'product.template': "image", } + attachment_model = env['ir.attachment'] for model, field in mapping.items(): Model = env[model] - attachments = env['ir.attachment'].search([ + attachment_ids = attachment_model.search([ ('res_model', '=', model), ('res_field', '=', field), ('res_id', '!=', False), - ]) - for attachment in attachments: + ]).ids + # a MemoryError can happen if too many attachments are loaded in + # memory. therefore, we only load one record at the time and flush the + # cache at each iteration. + for attachment_id in attachment_ids: + # load attachments one by one to avoid prefetching + attachment = attachment_model.browse(attachment_id) try: Model.browse(attachment.res_id).image_1920 = attachment.datas except Exception as e: @@ -32,6 +38,10 @@ def convert_image_attachments(env): attachment.res_id, repr(e), ) + # flush and clear cache to avoid to fill up memory and force the + # computation of the computed fields (smaller size images) + Model.flush() + env.cache.invalidate() @openupgrade.logging()