Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][FIX] base: keep translations for null src while upgrade #4477

Merged
merged 1 commit into from
Jun 21, 2024

Conversation

chienandalu
Copy link
Member

@chienandalu chienandalu commented Jun 13, 2024

The method update_translatable_fields is inspired in the core _get_translation_upgrade_queries method, which had a bug that has been recently addressed in odoo/odoo#168038

Also, we're avoiding removing any original ir_translation record so we can address any further issue easly without needing to redump the table from the former DB.

Let's use that core method with openupgradelib so we can be up to date with any further bugfix.

Depends on

cc @Tecnativa TT49615

please review @pedrobaeza

fyi @hbrunn

Copy link
Member

@pedrobaeza pedrobaeza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this is not using existing Odoo method for building the query? There's https://github.com/OCA/openupgradelib/blob/5646c07e598c411ae77b630e19122efc410415ae/openupgradelib/openupgrade_160.py#L29 to do it in theory.

@pedrobaeza pedrobaeza added this to the 16.0 milestone Jun 13, 2024
@chienandalu
Copy link
Member Author

Mmm, yes, that's a good question 😅 I'll check it tomorrow

@chienandalu chienandalu force-pushed the 16.0-fix-update-null-src-translation branch from f06b170 to c36a86b Compare June 14, 2024 10:49
@pedrobaeza
Copy link
Member

Did you see why the Odoo core method can't be used?

Copy link
Member

@hbrunn hbrunn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can't use Odoo function directly because it needs a registry, while this function might be called before some field exists there

@pedrobaeza
Copy link
Member

And why not doing it in post-migration?

@chienandalu
Copy link
Member Author

I will make a performance comparison and see

@chienandalu chienandalu force-pushed the 16.0-fix-update-null-src-translation branch 2 times, most recently from fb50daa to 1ff6415 Compare June 18, 2024 11:41
@pedrobaeza
Copy link
Member

 psycopg2.errors.DatatypeMismatch: column "title" is of type jsonb but expression is of type character varying
LINE 4:                 UPDATE utm_campaign SET title=name;
                                                      ^
HINT:  You will need to rewrite or cast the expression.

@chienandalu
Copy link
Member Author

Yes, looks like it will be needed to fix some scripts

@pedrobaeza
Copy link
Member

Why do it at end-migration? That way, if the translatable fields are involved in any computed operation, this will fail.

@chienandalu
Copy link
Member Author

Yes I see. The issue here is that only the base models are populated in the environment env at the moment of post-migrate... so a lower level approach will be needed...

@pedrobaeza
Copy link
Member

Yeah, that's true... so the core method is requiring that the model is loaded? It seems a bit restricting... Then we should turn back to the pure SQL approach that it was before. I think we have the answer to why this was done this way originally. Please document it for not going again this way in the future.

@chienandalu
Copy link
Member Author

I think we can work with just the field, that we can access in post but adapt openupgradelib to avoid reliying on env

@pedrobaeza
Copy link
Member

Seeing the core method, it relies on the registry model to get the table, so that's a non-go one...

@chienandalu
Copy link
Member Author

Ok, but this would leave behind html translatable fields, which weren't covered by OU: https://github.com/odoo/odoo/blob/3f9eab47809da86eacf0e5a89914aea7531ab44f/odoo/tools/translate.py#L1593

My propose would be:

  • Let's do in pre an initial pass
  • And then in end do an additional pass on html translatable records to let them right

@chienandalu chienandalu force-pushed the 16.0-fix-update-null-src-translation branch 3 times, most recently from 0fad432 to 38a0bf6 Compare June 19, 2024 14:14
The method ``update_translatable_fields`` is inspired in the core
``_get_translation_upgrade_queries`` method, which had a bug that has
been recently addressed in odoo/odoo#168038

Also, we're avoiding removing any original ir_translation record so we
can address any further issue easly without needing to redump the table
from the former DB.

For html/xml tranlatable fields we'll use
``openupgrade_160.migrate_translations_to_jsonb`` which calls to the
proper core method in ``end-migration``.

Ideally we'd like to do everything with that method, but it relies on
having the models already in the registry, and that doesn't happen when
we're in `base`.

Rationale from Odoo's fix:

Before Odoo 16.0, it is possible to have database column whose value is ``NULL``
but still has translations in the ir_translation table. And these translations
can be displayed correctly in the UI.

How to reproduce before Odoo 16.0:
1. Open the form view of a record with non-required translated field.
   E.g. product.template.sale_description
2. Create a new record without touching the translated field for test
3. Directly click the translation button and fill all translations
4. click save
The column for the record's translated field has ``NULL`` value, and the
ir_translation table has new translation records with ``NULL`` in the src column

During upgrade, when the column value is converted to jsonb for the translated
field by the ORM, it will still be ``NULL``. And in the upgrade script when
update the value with all translations, the result will still be ``NULL``. E.g.
``NULL || '{"fr_FR": "french"}'::jsonb``
``NULL || '{"en_US": "english", "fr_FR": "french"}'::jsonb``

In this commit, for the above corner case, we assume the src was empty string
instead of NULL. In the above example, the result would be
``'{"en_US": ""}'::jsonb || '{"fr_FR": "french"}'::jsonb``
``'{"en_US": ""}'::jsonb || '{"en_US": "english", "fr_FR": "french"}'::jsonb``

TT49615
@chienandalu chienandalu force-pushed the 16.0-fix-update-null-src-translation branch from 38a0bf6 to 2ca4497 Compare June 19, 2024 14:52
@pedrobaeza
Copy link
Member

I still think that this is not correct, as if any computed field requires a translatable field, it will do the computation with empty strings.

@chienandalu
Copy link
Member Author

Only translatable fields with a callable (html_translate and xml_translate) are left for end-migration. Anyway, are you thinking in some case in particular?

In the other hand, Odoo themselves must use this in end-migration if they rely in the registry...

Copy link
Member

@pedrobaeza pedrobaeza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I see

@hbrunn can you give your final blessing?

@OCA-git-bot
Copy link
Contributor

This PR has the approved label and has been created more than 5 days ago. It should therefore be ready to merge by a maintainer (or a PSC member if the concerned addon has no declared maintainer). 🤖

@hbrunn
Copy link
Member

hbrunn commented Jun 21, 2024

looks good to me. What do you mean in your comment?

@pedrobaeza
Copy link
Member

pedrobaeza commented Jun 21, 2024

Nevermind, they were just wrong thoughts: first of all, a computed stored field depending on translations is incorrect by definition. Second, the end-migration script is only for callable translations (translate=html_translate), so they don't have also computed fields depending on them. Merging then.

@pedrobaeza pedrobaeza merged commit e12d179 into OCA:16.0 Jun 21, 2024
2 checks passed
@pedrobaeza pedrobaeza deleted the 16.0-fix-update-null-src-translation branch June 21, 2024 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants