Skip to content

Commit

Permalink
[FIX] base: keep translations for null src while upgrade
Browse files Browse the repository at this point in the history
The method ``update_translatable_fields`` is inspired in the core
``_get_translation_upgrade_queries`` method, which had a bug that has
been recently addressed in odoo/odoo#168038

Let's use that core method with openupgradelib so we can be up to date
with any further bugfix.

TT49615
  • Loading branch information
chienandalu committed Jun 18, 2024
1 parent 2217dfd commit fb50daa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 66 deletions.
35 changes: 34 additions & 1 deletion openupgrade_scripts/scripts/base/16.0.1.3/post-migration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
# Copyright 2023 Hunki Enterprises BV (https://hunki-enterprises.com)
# Copyright 2024 Tecnativa - David Vidal
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openupgradelib import openupgrade
from openupgradelib import openupgrade, openupgrade_160

from odoo.tools import column_exists


def update_translatable_fields(env):
# exclude fields from translation update
exclusions = [
# ir.actions.* inherits the name and help columns from ir.actions.actions
("ir.actions.act_window", "name"),
("ir.actions.act_window", "help"),
("ir.actions.act_url", "name"),
("ir.actions.act_url", "help"),
("ir.actions.server", "name"),
("ir.actions.server", "help"),
("ir.actions.client", "name"),
("ir.actions.client", "help"),
("ir.actions.report", "name"),
("ir.actions.report", "help"),
]
fields = env["ir.model.fields"].search_read(
[("translate", "=", True)], ["model", "name"]
)
fields_spec = [
(f["model"], f["name"])
for f in fields
if (
(f["model"], f["name"]) not in exclusions
and column_exists(env.cr, env[f["model"]]._table, f["name"])
)
]
openupgrade_160.migrate_translations_to_jsonb(env, fields_spec)


@openupgrade.migrate()
Expand All @@ -11,3 +43,4 @@ def migrate(env, version):
"UPDATE res_partner p SET company_registry=c.company_registry "
"FROM res_company c WHERE c.partner_id=p.id"
)
update_translatable_fields(env)
65 changes: 0 additions & 65 deletions openupgrade_scripts/scripts/base/16.0.1.3/pre-migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,69 +52,6 @@ def login_or_registration_required_at_checkout(cr):
)


def update_translatable_fields(cr):
# exclude fields from translation update
exclusions = {
# ir.actions.* inherits the name and help columns from ir.actions.actions
"ir.actions.act_window": ["name", "help"],
"ir.actions.act_url": ["name", "help"],
"ir.actions.server": ["name", "help"],
"ir.actions.client": ["name", "help"],
"ir.actions.report": ["name", "help"],
}
cr.execute(
"SELECT f.name, m.model FROM ir_model_fields f "
"JOIN ir_model m ON f.model_id=m.id WHERE f.translate"
)
for field, model in cr.fetchall():
if field in exclusions.get(model, []):
continue
table = openupgrade.get_model2table(model)
if not openupgrade.table_exists(cr, table):
_logger.warning(
"Couldn't find table for model %s - not updating translations", model
)
continue
columns = tools.sql.table_columns(cr, table)
if field in columns:
if columns[field]["udt_name"] in ["varchar", "text"]:
tools.sql.convert_column_translatable(cr, table, field, "jsonb")
else:
_logger.warning(
"Couldn't find column for field %s - not updating translations", field
)
continue
# borrowed from odoo/tools/translate.py#_get_translation_upgrade_queries
translation_name = "%s,%s" % (model, field)
openupgrade.logged_query(
cr,
f"""
WITH t AS (
SELECT it.res_id as res_id, jsonb_object_agg(it.lang, it.value) AS value,
bool_or(imd.noupdate) AS noupdate
FROM ir_translation it
LEFT JOIN ir_model_data imd ON imd.model = %(model)s AND imd.res_id = it.res_id
WHERE it.type = 'model' AND it.name = %(name)s AND it.state = 'translated'
GROUP BY it.res_id
)
UPDATE {table} m
SET "{field}" = CASE WHEN t.noupdate IS FALSE THEN t.value || m."{field}"
ELSE m."{field}" || t.value END
FROM t
WHERE t.res_id = m.id
""",
{
"model": model,
"name": translation_name,
},
)
openupgrade.logged_query(
cr,
"DELETE FROM ir_translation WHERE type = 'model' AND name = %s",
[translation_name],
)


@openupgrade.migrate(use_env=False)
def migrate(cr, version):
"""
Expand All @@ -139,5 +76,3 @@ def migrate(cr, version):
"USING ir_ui_view v "
"WHERE r.view_id=v.id AND v.inherit_id IS NOT NULL AND v.mode != 'primary'"
)
# update all translatable fields
update_translatable_fields(cr)

0 comments on commit fb50daa

Please sign in to comment.