Skip to content

Commit

Permalink
[IMP] delete_records_safely_by_xml_id: Added functionality to also de…
Browse files Browse the repository at this point in the history
…lete child records
  • Loading branch information
Bastian Guenther authored and Bastian Guenther committed Feb 11, 2022
1 parent 0911623 commit 860a5b1
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 @@ -2559,7 +2559,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 @@ -2568,6 +2568,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 @@ -2583,7 +2585,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 860a5b1

Please sign in to comment.