Skip to content

Commit

Permalink
Merge pull request OCA#3909 from ForgeFlow/16.0-fix-base
Browse files Browse the repository at this point in the history
[16.0][FIX] base: upgrade interaction with l10n_multilang
  • Loading branch information
pedrobaeza authored Jul 11, 2023
2 parents 39ee0f0 + 1893c27 commit fc4a888
Showing 1 changed file with 46 additions and 34 deletions.
80 changes: 46 additions & 34 deletions openupgrade_scripts/scripts/base/16.0.1.3/pre-migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand All @@ -83,20 +62,53 @@ 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,
"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)

0 comments on commit fc4a888

Please sign in to comment.