From cca1eea84988f1563d416fef17c23d73fdfdb106 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miquel=20Ra=C3=AFch?= Date: Tue, 6 Jun 2023 16:34:14 +0200 Subject: [PATCH 1/2] [FIX] base: upgrade interaction with l10n_multilang Consider https://github.com/odoo/odoo/commit/5d46cdee878d752b9a61a74762a8710be0890e9d. --- .../scripts/base/16.0.1.3/pre-migration.py | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/openupgrade_scripts/scripts/base/16.0.1.3/pre-migration.py b/openupgrade_scripts/scripts/base/16.0.1.3/pre-migration.py index 5e5860e8258c..933ab01f0e27 100644 --- a/openupgrade_scripts/scripts/base/16.0.1.3/pre-migration.py +++ b/openupgrade_scripts/scripts/base/16.0.1.3/pre-migration.py @@ -83,17 +83,25 @@ def migrate(cr, version): cr, """ WITH t AS ( - SELECT res_id, jsonb_object_agg(lang, value) AS value - FROM ir_translation - WHERE type = 'model' AND name = %(name)s AND state = 'translated' - GROUP BY res_id + 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)s" m - SET "%(field_name)s" = t.value || m."%(field_name)s" - FROM t - WHERE t.res_id = m.id + SET "%(field_name)s" = CASE WHEN t.noupdate THEN m.%(field_name)s || t.value + ELSE t.value || m.%(field_name)s END + FROM t + WHERE t.res_id = m.id """, - {"table": AsIs(table), "name": translation_name, "field_name": AsIs(field)}, + { + "table": AsIs(table), + "model": model, + "name": translation_name, + "field_name": AsIs(field), + }, ) openupgrade.logged_query( cr, From 1893c276fabda85ab553e086ee7bc0d8e22c499c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miquel=20Ra=C3=AFch?= Date: Tue, 6 Jun 2023 16:39:14 +0200 Subject: [PATCH 2/2] [IMP] base: group all translate content in one method --- .../scripts/base/16.0.1.3/pre-migration.py | 56 ++++++++++--------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/openupgrade_scripts/scripts/base/16.0.1.3/pre-migration.py b/openupgrade_scripts/scripts/base/16.0.1.3/pre-migration.py index 933ab01f0e27..6ddddff6c510 100644 --- a/openupgrade_scripts/scripts/base/16.0.1.3/pre-migration.py +++ b/openupgrade_scripts/scripts/base/16.0.1.3/pre-migration.py @@ -13,32 +13,7 @@ _logger = logging.getLogger(__name__) -@openupgrade.migrate(use_env=False) -def migrate(cr, version): - """ - Don't request an env for the base pre-migration as flushing the env in - odoo/modules/registry.py will break on the 'base' module not yet having - been instantiated. - """ - if "openupgrade_framework" not in tools.config["server_wide_modules"]: - _logger.error( - "openupgrade_framework is not preloaded. You are highly " - "recommended to run the Odoo with --load=openupgrade_framework " - "when migrating your database." - ) - openupgrade.update_module_names(cr, renamed_modules.items()) - openupgrade.update_module_names(cr, merged_modules.items(), merge_modules=True) - # restricting inherited views to groups isn't allowed any more - cr.execute( - "DELETE FROM ir_ui_view_group_rel r " - "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 - 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" - ) +def update_translatable_fields(cr): # map of nonstandard table names model2table = { "ir.actions.actions": "ir_actions", @@ -59,6 +34,10 @@ def migrate(cr, version): "ir.actions.client": ["name"], "ir.actions.report": ["name"], } + 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 @@ -108,3 +87,28 @@ def migrate(cr, version): "DELETE FROM ir_translation WHERE type = 'model' AND name = %s", [translation_name], ) + + +@openupgrade.migrate(use_env=False) +def migrate(cr, version): + """ + Don't request an env for the base pre-migration as flushing the env in + odoo/modules/registry.py will break on the 'base' module not yet having + been instantiated. + """ + if "openupgrade_framework" not in tools.config["server_wide_modules"]: + _logger.error( + "openupgrade_framework is not preloaded. You are highly " + "recommended to run the Odoo with --load=openupgrade_framework " + "when migrating your database." + ) + openupgrade.update_module_names(cr, renamed_modules.items()) + openupgrade.update_module_names(cr, merged_modules.items(), merge_modules=True) + # restricting inherited views to groups isn't allowed any more + cr.execute( + "DELETE FROM ir_ui_view_group_rel r " + "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)