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

[FIX] delete_record_translations: adapt to Odoo v16 #328

Merged
merged 2 commits into from
Jul 4, 2023
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
107 changes: 91 additions & 16 deletions openupgradelib/openupgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -2585,15 +2585,17 @@ def rename_property(cr, model, old_name, new_name):
)


def delete_record_translations(cr, module, xml_ids):
def delete_record_translations(cr, module, xml_ids, field_list=None):
"""Cleanup translations of specific records in a module.

:param module: module name
:param xml_ids: a tuple or list of xml record IDs
:param field_list: optional list of field names whose translations will be deleted
"""
if not isinstance(xml_ids, (list, tuple)):
do_raise("XML IDs %s must be a tuple or list!" % (xml_ids))

do_raise("XML IDs %s must be a tuple or list!" % (xml_ids,))
if not field_list:
field_list = []
cr.execute(
"""
SELECT model, res_id
Expand All @@ -2606,19 +2608,92 @@ def delete_record_translations(cr, module, xml_ids):
),
)
for row in cr.fetchall():
query = """
DELETE FROM ir_translation
WHERE module = %s AND name LIKE %s AND res_id = %s;
"""
logged_query(
cr,
query,
(
module,
row[0] + ",%",
row[1],
),
)
model = row[0]
record_id = row[1]
if version_info[0] < 16:
if not field_list:
query = """
DELETE FROM ir_translation
WHERE module = %s AND name LIKE %s AND res_id = %s;
"""
logged_query(
cr,
query,
(
module,
model + ",%",
record_id,
),
)
else:
name_comparisons = " OR ".join(
["name = %s" % (model + "," + field) for field in field_list]
)
query = """
DELETE FROM ir_translation
WHERE module = %s AND ({name_comparison}) AND res_id = %s;
""".format(
name_comparison=name_comparisons
)
logged_query(
cr,
query,
(
module,
record_id,
),
)
else:
table = model.replace(".", "_")
# we use information_schema to assure the columns exist
cr.execute(
"""
SELECT isc.column_name
MiquelRForgeFlow marked this conversation as resolved.
Show resolved Hide resolved
FROM information_schema.columns isc
JOIN ir_model_fields imf ON (
imf.name = isc.column_name AND imf.model = %s)
WHERE isc.table_name = %s AND imf.translate""",
(model, table),
)
list_columns = [x[0] for x in cr.fetchall()]
if field_list:
list_columns = [x for x in list_columns if x in field_list]
if not list_columns:
continue
query = """
SELECT {checks}
FROM {table}
WHERE id = %s""".format(
table=table,
checks=", ".join(
[
"{x} IS NOT NULL AND ({x} ? 'en_US') AND "
"(SELECT count(*) FROM jsonb_object_keys({x})) > 1".format(x=x)
for x in list_columns
]
),
)
cr.execute(query, (record_id,))
checks = cr.fetchall()[0]
for i, (column, check) in enumerate(zip(list_columns, checks)):
if not check:
list_columns.pop(i)
if not list_columns:
continue
query = """
UPDATE {table}
SET ({columns}) = ({values})
WHERE id = %s""".format(
table=table,
columns=", ".join(list_columns),
values=", ".join(
[
'\'{{"en_US": {x} -> "en_US"}}\'::jsonb'.format(x=x)
for x in list_columns
]
),
)
logged_query(cr, query, (record_id,))


# flake8: noqa: C901
Expand Down