Table Renaming

From version 14.0 to version 15.0, in the module mail, the table of the model mail.notification has been renamed from mail_message_res_partner_needaction_rel to mail_notification.

Analysis

For the time being, this change is not automatically detected and reported in the upgrade_analysis.txt file.

However, if the table is used as part of a many2many field, the following lines will be present.

...
---Fields in module 'mail'---
...
mail         / mail.message             / notified_partner_ids (many2many):
    table is now 'mail_notification' ('mail_message_res_partner_needaction_rel')

See Full V15 Analysis File.

Source Code Differences

Version 14.0

class MailNotification(models.Model):
    _name = "mail.notification"
    _table = "mail_message_res_partner_needaction_rel"

See Full V14 Code Source.

Version 15.0

class MailNotification(models.Model):
    _name = "mail.notification"
    _table = "mail_notification"

See Full V15 Code Source.

Result without migration script / Expected Result

V14 table mail_message_res_partner_needaction_rel

id

mail_message_id

notification_type

1

92

email

2

94

sms

V15 table mail_notification (Without migration script)

id

mail_message_id

notification_type

Problem : the data is lost during the migration process.

V15 table mail_notification (With migration script)

id

mail_message_id

notification_type

1

92

email

2

94

sms

Contribution to OpenUpgrade

Write migration Script

in the pre-migration.py script add:

from openupgradelib import openupgrade

@openupgrade.migrate()
def migrate(env, version):
    openupgrade.rename_tables(
        env.cr, [("mail_message_res_partner_needaction_rel", "mail_notification")]
    )

See Full pre migration Script.

Notes

  • If you think the new name may already be used by an existing table, you can backup the table before the renaming as follows.

    if openupgrade.table_exists(env.cr, "mail_notification"):
        openupgrade.rename_tables(env.cr, [("mail_notification", None)])
    
  • If the table has SQL constraints, you should drop them before the renaming

    openupgrade.delete_sql_constraint_safely(
        env,
        "mail",                                     # module name
        "mail_message_res_partner_needaction_rel",  # old table name
        "notification_partner_required",            # constraint name
    )