Skip to content

Commit a11d862

Browse files
committed
[FIX] models.remove_model: MemoryError
``` Traceback (most recent call last): [...] File "/tmp/tmpipxrg2eq/migrations/util/models.py", line 563, in merge_model remove_model(cr, source, drop_table=drop_table, ignore_m2m=ignore_m2m) File "/tmp/tmpipxrg2eq/migrations/util/models.py", line 138, in remove_model it = chunks([id for (id,) in cr.fetchall()], chunk_size, fmt=tuple) MemoryError ``` Some IR tables can be large. Avoid `cr.fetchall()` when getting ids by use of pg.query_ids()
1 parent 5c804b9 commit a11d862

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/util/models.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
get_m2m_tables,
2828
get_value_or_en_translation,
2929
parallel_execute,
30+
query_ids,
3031
table_exists,
3132
update_m2m_tables,
3233
view_exists,
@@ -128,17 +129,17 @@ def remove_model(cr, model, drop_table=True, ignore_m2m=()):
128129
'SELECT id FROM "{}" r WHERE {}'.format(ir.table, ir.model_filter(prefix="r.")), [model]
129130
).decode()
130131

131-
cr.execute(query)
132-
if ir.table == "ir_ui_view":
133-
for (view_id,) in cr.fetchall():
134-
remove_view(cr, view_id=view_id, silent=True)
135-
else:
136-
# remove in batch
137-
size = (cr.rowcount + chunk_size - 1) / chunk_size
138-
it = chunks([id for (id,) in cr.fetchall()], chunk_size, fmt=tuple)
139-
for sub_ids in log_progress(it, _logger, qualifier=ir.table, size=size):
140-
remove_records(cr, ref_model, sub_ids)
141-
_rm_refs(cr, ref_model, sub_ids)
132+
with query_ids(cr, query, itersize=chunk_size) as ids_:
133+
if ir.table == "ir_ui_view":
134+
for view_id in ids_:
135+
remove_view(cr, view_id=view_id, silent=True)
136+
else:
137+
# remove in batch
138+
size = (len(ids_) + chunk_size - 1) / chunk_size
139+
it = chunks(ids_, chunk_size, fmt=tuple)
140+
for sub_ids in log_progress(it, _logger, qualifier=ir.table, size=size):
141+
remove_records(cr, ref_model, sub_ids)
142+
_rm_refs(cr, ref_model, sub_ids)
142143

143144
if ir.set_unknown:
144145
# Link remaining records not linked to a XMLID

0 commit comments

Comments
 (0)