Skip to content

Commit

Permalink
Merge pull request #279 from AmetrasIntelligence/delete_records_safel…
Browse files Browse the repository at this point in the history
…y_by_xml_id_with_childs

[IMP] delete_records_safely_by_xml_id: Added functionality to also delete child records
  • Loading branch information
pedrobaeza authored Oct 14, 2022
2 parents dad04c4 + 860a5b1 commit ed16a02
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions openupgradelib/openupgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -2796,7 +2796,7 @@ def safe_unlink(records, do_raise=False):
record._name, record.id, repr(e))


def delete_records_safely_by_xml_id(env, xml_ids):
def delete_records_safely_by_xml_id(env, xml_ids, delete_childs=False):
"""This removes in the safest possible way the records whose XML-IDs are
passed as argument.
Expand All @@ -2805,6 +2805,8 @@ def delete_records_safely_by_xml_id(env, xml_ids):
Odoo performs the regular update cleanup and trying to remove it as well.
:param xml_ids: List of XML-ID string identifiers of the records to remove.
:param delete_childs: If true, also child ids of the given xml_ids will
be deleted.
"""
errors = (KeyError, IntegrityError)
if version_info[0] > 6 or version_info[0:2] == (6, 1):
Expand All @@ -2820,7 +2822,12 @@ def delete_records_safely_by_xml_id(env, xml_ids):
record = env.ref(xml_id, raise_if_not_found=False)
if not record:
continue
safe_unlink(record, do_raise=True)
if delete_childs:
child_and_parent_records = env["ir.ui.view"].search(
[("inherit_id", "child_of", record.id)], order="id desc")
safe_unlink(child_and_parent_records, do_raise=True)
else:
safe_unlink(record, do_raise=True)
except errors as e:
logger.info('Error deleting XML-ID %s: %s', xml_id, repr(e))
module, name = xml_id.split('.')
Expand Down

0 comments on commit ed16a02

Please sign in to comment.