Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] delete_records_safely_by_xml_id: Added functionality to also delete child records #279

Merged
merged 1 commit into from
Oct 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's sad this improvement only applies to views, but ok.

[("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