@@ -14,14 +14,20 @@ def convert_image_attachments(env):
1414 'product.product' : "image_variant" ,
1515 'product.template' : "image" ,
1616 }
17+ attachment_model = env ['ir.attachment' ]
1718 for model , field in mapping .items ():
1819 Model = env [model ]
19- attachments = env [ 'ir.attachment' ] .search ([
20+ attachment_ids = attachment_model .search ([
2021 ('res_model' , '=' , model ),
2122 ('res_field' , '=' , field ),
2223 ('res_id' , '!=' , False ),
23- ])
24- for attachment in attachments :
24+ ]).ids
25+ # a MemoryError can happen if too many attachments are loaded in
26+ # memory. therefore, we only load one record at the time and flush the
27+ # cache at each iteration.
28+ for attachment_id in attachment_ids :
29+ # load attachments one by one to avoid prefetching
30+ attachment = attachment_model .browse (attachment_id )
2531 try :
2632 Model .browse (attachment .res_id ).image_1920 = attachment .datas
2733 except Exception as e :
@@ -32,6 +38,10 @@ def convert_image_attachments(env):
3238 attachment .res_id ,
3339 repr (e ),
3440 )
41+ # flush and clear cache to avoid to fill up memory and force the
42+ # computation of the computed fields (smaller size images)
43+ Model .flush ()
44+ env .cache .invalidate ()
3545
3646
3747@openupgrade .logging ()
0 commit comments