diff --git a/docsource/modules180-190.rst b/docsource/modules180-190.rst index fd4b86e2175e..f5f098bf583a 100644 --- a/docsource/modules180-190.rst +++ b/docsource/modules180-190.rst @@ -698,7 +698,7 @@ Module coverage 18.0 -> 19.0 +---------------------------------------------------+----------------------+-------------------------------------------------+ | link_tracker |Nothing to do |No DB layout changes. | +---------------------------------------------------+----------------------+-------------------------------------------------+ -| loyalty | | | +| loyalty |Done | | +---------------------------------------------------+----------------------+-------------------------------------------------+ | lunch | | | +---------------------------------------------------+----------------------+-------------------------------------------------+ diff --git a/openupgrade_scripts/scripts/loyalty/19.0.1.0/pre-migration.py b/openupgrade_scripts/scripts/loyalty/19.0.1.0/pre-migration.py new file mode 100644 index 000000000000..b29087957954 --- /dev/null +++ b/openupgrade_scripts/scripts/loyalty/19.0.1.0/pre-migration.py @@ -0,0 +1,37 @@ +from openupgradelib import openupgrade + + +@openupgrade.migrate() +def migrate(env, version): + """Rewrite the removed ``_get_mail_partner`` helper (which returned + ``partner_id``, the recipient) to its exact 18.0 equivalent in stored + template fields, as the 19.0 data-load render aborts before the existing + post-migration could clear the field. ``_get_mail_author`` would swap in + the company/internal user instead — wrong partner for custom templates + that are never reset by the module update. + """ + for column in ("lang", "partner_to", "email_to", "body_html"): + if not openupgrade.column_exists(env.cr, "mail_template", column): + continue + env.cr.execute( + """ + SELECT data_type FROM information_schema.columns + WHERE table_name = 'mail_template' AND column_name = %s + """, + (column,), + ) + # translated columns (body_html) are jsonb; cast the rewrite back + cast = "::jsonb" if env.cr.fetchone()[0] == "jsonb" else "" + openupgrade.logged_query( + env.cr, + f""" + UPDATE mail_template + SET {column} = REPLACE( + {column}::text, + 'object._get_mail_partner()', + 'object.partner_id' + ){cast} + WHERE model = 'loyalty.card' + AND {column}::text LIKE '%%_get_mail_partner%%' + """, + )