Skip to content

Commit f5871e7

Browse files
committed
[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
1 parent c8c1a6e commit f5871e7

File tree

1 file changed

+65
-1
lines changed

1 file changed

+65
-1
lines changed

src/util/specific.py

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
from .helpers import _validate_table
55
from .misc import _cached
66
from .models import rename_model
7-
from .modules import rename_module
7+
from .modules import odoo, rename_module
8+
from .orm import env, iter_browse
89
from .pg import column_exists, rename_table, table_exists
910
from .report import add_to_migration_reports
1011

@@ -137,3 +138,66 @@ def reset_cowed_views(cr, xmlid, key=None):
137138
[key, module, name],
138139
)
139140
return set(sum(cr.fetchall(), ()))
141+
142+
143+
def translation2jsonb_unofficial_fields(cr):
144+
"""Convert all translated unofficial fields to jsonb.
145+
146+
This helper function is designed to run only in an on-premise Odoo instance
147+
where any unofficial modules you use are installed, after going through the
148+
upgrade to version 16.0.
149+
150+
It will find all those fields and convert translations from the
151+
``_ir_translation`` table into the new JSONB format.
152+
153+
Any fields not loaded will be skipped.
154+
"""
155+
cr.execute(
156+
"""
157+
SELECT ARRAY_AGG(name)
158+
FROM ir_module_module
159+
WHERE author LIKE 'Odoo S.A.'
160+
"""
161+
)
162+
official_modules = set(cr.fetchone()[0])
163+
cr.execute(
164+
"""
165+
SELECT ARRAY_AGG(id)
166+
FROM ir_model_fields
167+
WHERE store = TRUE AND translate = TRUE
168+
"""
169+
)
170+
env = env(cr)
171+
all_migrate_queries, all_cleanup_queries = [], []
172+
for ir_field in iter_browse(env["ir.model.fields"], cr.fetchall()[0][0]):
173+
# Skip official fields, which were translated already
174+
if ir_field.modules and official_modules.intersection(ir_field.modules.split(", ")):
175+
continue
176+
try:
177+
model = env[ir_field.model_id.model]
178+
field = model._fields[ir_field.name]
179+
except KeyError:
180+
_logger.warning(
181+
"Field %s in model %s is not loaded; skipping",
182+
ir_field.name,
183+
ir_field.model_id.model,
184+
)
185+
continue
186+
# Skip DB views or other kind of non-table fields
187+
if not table_exists(cr, model._table):
188+
continue
189+
_logger.info(
190+
"Migrating translations for field %s in model %s",
191+
ir_field.name,
192+
ir_field.model_id.model,
193+
)
194+
(
195+
migrate_queries,
196+
cleanup_queries,
197+
) = odoo.tools.translate._get_translation_upgrade_queries(cr, field)
198+
all_migrate_queries.extend(migrate_queries)
199+
all_cleanup_queries.extend(cleanup_queries)
200+
for query in all_migrate_queries:
201+
cr.execute(query)
202+
for query in all_cleanup_queries:
203+
cr.execute(query)

0 commit comments

Comments
 (0)