Skip to content

Commit f207e52

Browse files
yajoaj-fuentes
andcommitted
[IMP] Helper to preserve all translated fields on v16 migration
Currently, the upgrade process preserves translations only for Odoo CE+EE fields. However, databases usually have more modules (or even custom fields) and those translations get lost when upgrading to Odoo 16. Running this script in a migrated database, all translated fields will inherit their translations in all languages. @moduon MT-7120 Co-authored-by: Alvaro Fuentes <[email protected]>
1 parent 41abe70 commit f207e52

File tree

1 file changed

+88
-2
lines changed

1 file changed

+88
-2
lines changed

src/util/specific.py

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
# -*- coding: utf-8 -*-
22
import logging
33

4+
from .exceptions import UpgradeError
45
from .helpers import _validate_table
5-
from .misc import _cached
6+
from .misc import _cached, version_gte
67
from .models import rename_model
78
from .modules import rename_module
8-
from .pg import column_exists, rename_table, table_exists
9+
from .orm import env
10+
from .pg import column_exists, format_query, parallel_execute, rename_table, table_exists
911
from .report import add_to_migration_reports
1012

13+
try:
14+
from odoo.tools.translate import _get_translation_upgrade_queries
15+
except ImportError:
16+
if version_gte("16.0"):
17+
raise
18+
1119
_logger = logging.getLogger(__name__)
1220

1321

@@ -156,3 +164,81 @@ def reset_cowed_views(cr, xmlid, key=None):
156164
[key, module, name],
157165
)
158166
return set(sum(cr.fetchall(), ()))
167+
168+
169+
def translation2jsonb_all_missing(cr):
170+
"""Convert all missing translated fields to jsonb.
171+
172+
This helper function is designed to run only in an on-premise Odoo instance
173+
where any unofficial modules you use are installed, after going through the
174+
upgrade to version 16.0.
175+
176+
It will find all unofficial fields and convert translations from the
177+
``_ir_translation`` table into the new JSONB format.
178+
179+
Modules must be available in the environment to be loaded.
180+
181+
:return: Converted fields.
182+
"""
183+
if not version_gte("16.0"):
184+
raise UpgradeError("JSONB translations are only available from Odoo 16")
185+
if not table_exists(cr, "_ir_translation"):
186+
_logger.getChild("translation2jsonb_all_missing").info("Missing `_ir_translation` table; skip.")
187+
return None
188+
_env = env(cr)
189+
fields = []
190+
cr.execute(
191+
"""
192+
SELECT STRING_TO_ARRAY(name, ','),
193+
ARRAY_AGG(DISTINCT lang)
194+
FROM _ir_translation
195+
WHERE type IN ('model', 'model_terms')
196+
AND name LIKE '%,%'
197+
AND lang != 'en_US'
198+
GROUP BY name
199+
"""
200+
)
201+
for (mname, fname), langs in cr.fetchall():
202+
try:
203+
model = _env[mname]
204+
field = model._fields[fname]
205+
except KeyError:
206+
continue
207+
if (
208+
not model._auto
209+
or model._abstract
210+
or model._transient
211+
or not field.store
212+
or not field.translate
213+
or field.manual
214+
):
215+
continue
216+
# Check if the field is already converted
217+
cr.execute(
218+
format_query(cr, "SELECT 1 FROM {} WHERE {} ?| %s LIMIT 1", model._table, fname),
219+
[langs],
220+
)
221+
if not cr.rowcount:
222+
fields.append(field)
223+
return translation2jsonb(cr, *fields)
224+
225+
226+
def translation2jsonb(cr, *fields):
227+
"""Convert selected translated fields to jsonb.
228+
229+
Migrates translations for chosen fields, from the ``_ir_translation`` table
230+
into the new JSONB format.
231+
232+
:param odoo.fields.Field fields: Fields to convert.
233+
:return: Converted fields.
234+
"""
235+
if not version_gte("16.0"):
236+
raise UpgradeError("JSONB translations are only available from Odoo 16")
237+
all_cleanup_queries = []
238+
_logger.info("Migrating translations for fields %r", fields)
239+
for field in fields:
240+
migrate_queries, cleanup_queries = _get_translation_upgrade_queries(cr, field)
241+
all_cleanup_queries.extend(cleanup_queries)
242+
parallel_execute(cr, migrate_queries)
243+
parallel_execute(cr, all_cleanup_queries)
244+
return fields

0 commit comments

Comments
 (0)