Skip to content

Commit

Permalink
[FIX] delete_record_translations: adapt to Odoo v16
Browse files Browse the repository at this point in the history
  • Loading branch information
MiquelRForgeFlow committed Jul 4, 2023
1 parent c3016fc commit 49286dc
Showing 1 changed file with 60 additions and 14 deletions.
74 changes: 60 additions & 14 deletions openupgradelib/openupgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -2592,7 +2592,7 @@ def delete_record_translations(cr, module, xml_ids):
:param xml_ids: a tuple or list of xml record IDs
"""
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,))

cr.execute(
"""
Expand All @@ -2606,19 +2606,65 @@ 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:
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:
table = model.replace(".", "_")
# we use information_schema to assure the columns exist
cr.execute(
"""
SELECT isc.column_name
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 not list_columns:
continue
query = """
SELECT {checks}
FROM {table}
WHERE id = %s""".format(
table=table,
checks=", ".join(
f"({x} ? 'en_US') AND (SELECT count(*) FROM jsonb_object_keys({x})) > 1"
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(
f'\'{{"en_US": {x} -> "en_US"}}\'::jsonb' for x in list_columns
),
)
logged_query(cr, query, (record_id,))


# flake8: noqa: C901
Expand Down

0 comments on commit 49286dc

Please sign in to comment.