Skip to content

Commit

Permalink
Merge pull request #306 from acsone/16.0-migrate_translations-mle
Browse files Browse the repository at this point in the history
[16.0] [ADD] Add helper to migrate translations from ir_translation table to model table
  • Loading branch information
pedrobaeza authored Sep 26, 2022
2 parents f97bf37 + cf92e08 commit dad04c4
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions openupgradelib/openupgrade_160.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2022 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

"""This module provides simple tools for OpenUpgrade migration, specific for
the >=16.0 migration.
"""
import itertools
from .openupgrade import logged_query, table_exists
from odoo.tools.translate import _get_translation_upgrade_queries


def migrate_translations_to_jsonb(env, fields_spec):
"""
In Odoo 16, translated fields no longer use the model ir.translation.
Instead they store all their values into jsonb columns
in the model's table.
See https://github.com/odoo/odoo/pull/97692 for more details.
Odoo provides a method _get_translation_upgrade_queries returning queries
to execute to migrate all the translations of a particular field.
The present openupgrade method executes the provided queries
on table _ir_translation if exists (when ir_translation table was renamed
by Odoo's migration scripts) or on table ir_translation (if module was
migrated by OCA).
This should be called in a post-migration script of the module
that contains the definition of the translatable field.
:param fields_spec: list of tuples of (model name, field name)
"""
initial_translation_tables = None
if table_exists(env.cr, "_ir_translation"):
initial_translation_tables = "_ir_translation"
elif table_exists(env.cr, "ir_translation"):
initial_translation_tables = "ir_translation"
if initial_translation_tables:
for model, field_name in fields_spec:
field = env[model]._fields[field_name]
for query in itertools.chain.from_iterable(
_get_translation_upgrade_queries(env.cr, field)
):
if initial_translation_tables == "ir_translation":
query = query.replace("_ir_translation", "ir_translation")
logged_query(env.cr, query)

0 comments on commit dad04c4

Please sign in to comment.