From 45a2859a61a7dfe06ee398dc9b3daf553feb94f3 Mon Sep 17 00:00:00 2001 From: duongnguyen Date: Mon, 17 Jul 2023 13:05:23 +0700 Subject: [PATCH] [FIX] mail: create constrain to avoid migration error when install 'to_mail_notif_and_email' module So in the mail module v16, it has a constrain call unique_mail_message_id_res_partner_id_if_set, but in the module 'to_mail_notif_and_email' we drop that constains and create a new with additional condition so when migrating from 15 to 16 it will get error when registry try to create the constaint. To avoid that, we should check if the module 'to_mail_notif_and_email' is installed then instanly create constrain index specify in that module --- .../scripts/mail/16.0.1.10/pre-migration.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/openupgrade_scripts/scripts/mail/16.0.1.10/pre-migration.py b/openupgrade_scripts/scripts/mail/16.0.1.10/pre-migration.py index 9fe3634313b1..afe9fff2f8e4 100644 --- a/openupgrade_scripts/scripts/mail/16.0.1.10/pre-migration.py +++ b/openupgrade_scripts/scripts/mail/16.0.1.10/pre-migration.py @@ -86,6 +86,26 @@ def scheduled_date_set_empty_strings_to_null(env): ) +def _to_mail_notif_and_email_create_mail_notification_index(env): + """ + Only execute this method when module 'to_mail_notif_and_email' + is found and installed to avoid error when init mail module + because it has a constrain that we have overide in 'to_mail_notif_and_email' + if not then we will get error when running migration of mail module + """ + module_to_mail_notif_and_email = env["ir.module.module"].search( + [("name", "=", "to_mail_notif_and_email"), ("state", "=", "to upgrade")] + ) + if module_to_mail_notif_and_email: + env.cr.execute( + """ + CREATE UNIQUE INDEX IF NOT EXISTS unique_mail_message_id_res_partner_id_if_set + ON mail_notification (mail_message_id, res_partner_id, notification_type) + WHERE res_partner_id IS NOT NULL + """ + ) + + @openupgrade.migrate() def migrate(env, version): delete_obsolete_constraints(env) @@ -98,3 +118,6 @@ def migrate(env, version): ir_act_server_rename_state_email(env) mail_channel_channel_type_required(env) scheduled_date_set_empty_strings_to_null(env) + # This method is only exist in Viindoo/Openupgrade for some + # Technical reason + _to_mail_notif_and_email_create_mail_notification_index(env)