Skip to content

Commit

Permalink
[MIG] mail: add mail_group migration
Browse files Browse the repository at this point in the history
  • Loading branch information
MiquelRForgeFlow committed Jul 21, 2022
1 parent 2959f59 commit 5d65bf2
Show file tree
Hide file tree
Showing 5 changed files with 352 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docsource/modules140-150.rst
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ Module coverage 14.0 -> 15.0
+-------------------------------------------------+----------------------+-------------------------------------------------+
| |del| mail_client_extension | |Renamed to mail_plugin. |
+-------------------------------------------------+----------------------+-------------------------------------------------+
| |new| mail_group | | |
| |new| mail_group | Done | |
+-------------------------------------------------+----------------------+-------------------------------------------------+
| |new| mail_plugin | |Renamed from mail_client_extension. |
+-------------------------------------------------+----------------------+-------------------------------------------------+
Expand Down
26 changes: 26 additions & 0 deletions openupgrade_scripts/scripts/mail/15.0.1.5/post-migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,31 @@ def _map_mail_notification_failure_type(env):
)


def finish_migration_to_mail_group(env):
env.cr.execute(
"""
SELECT 1 FROM ir_module_module
WHERE name = 'mail_group' AND state = 'to install'""",
)
will_have_mail_group = env.cr.rowcount
if not will_have_mail_group:
return
openupgrade.logged_query(
env.cr,
"""
DELETE FROM ir_attachment ia
USING mail_channel mc
WHERE ia.res_model = 'mail.channel' AND ia.res_id = mc.id
AND mc.email_send AND ia.res_field = 'image_128'""",
)
openupgrade.logged_query(
env.cr,
"""
DELETE FROM mail_channel
WHERE email_send""",
)


@openupgrade.migrate()
def migrate(env, version):
openupgrade.load_data(env.cr, "mail", "15.0.1.5/noupdate_changes.xml")
Expand All @@ -52,3 +77,4 @@ def migrate(env, version):
"mail.ir_cron_mail_notify_channel_moderators",
],
)
finish_migration_to_mail_group(env)
208 changes: 207 additions & 1 deletion openupgrade_scripts/scripts/mail/15.0.1.5/pre-migration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from openupgradelib import openupgrade

from odoo.tools import sql


def _copy_columns(env):
openupgrade.copy_columns(
Expand Down Expand Up @@ -68,7 +70,210 @@ def delete_obsolete_constraints(env):
]
for module, table, name in _contraints:
openupgrade.delete_sql_constraint_safely(env, module, table, name)
openupgrade.remove_tables_fks(env.cr, ["mail_moderation"])
openupgrade.remove_tables_fks(
env.cr,
[
"mail_moderation",
"mail_channel_moderator_rel",
"mail_message_mail_channel_rel",
],
)


def migration_to_mail_group(env):
env.cr.execute("SELECT 1 FROM mail_moderation LIMIT 1")
has_moderation = env.cr.rowcount
env.cr.execute("SELECT 1 FROM mail_channel WHERE email_send LIMIT 1")
has_old_mail_groups = env.cr.rowcount
env.cr.execute(
"""
SELECT 1 FROM ir_module_module
WHERE name = 'portal' AND state in ('installed', 'to upgrade', 'to install')""",
)
has_portal = env.cr.rowcount
if not (has_moderation or has_old_mail_groups) or not has_portal:
return
openupgrade.logged_query(
env.cr,
"""
UPDATE ir_module_module
SET state='to install'
WHERE name = 'mail_group' AND state='uninstalled'""",
)
openupgrade.rename_models(
env.cr,
[
("mail.moderation", "mail.group.moderation"),
],
)
openupgrade.rename_tables(
env.cr,
[
("mail_moderation", "mail_group_moderation"),
("mail_channel_moderator_rel", "mail_group_moderator_rel"),
],
)
openupgrade.rename_columns(
env.cr,
{
"mail_group_moderator_rel": [
("mail_channel_id", "mail_group_id"),
]
},
)
# fill mail_group table
sql.create_model_table(
env.cr,
"mail_group",
columns=[
("name", "varchar", ""),
("active", "bool", ""),
("description", "text", ""),
("moderation", "bool", ""),
("access_mode", "varchar", ""),
("access_group_id", "integer", ""),
("moderation_notify", "bool", ""),
("moderation_notify_msg", "text", ""),
("moderation_guidelines", "bool", ""),
("moderation_guidelines_msg", "text", ""),
("create_uid", "integer", ""),
("write_uid", "integer", ""),
("create_date", "timestamp", ""),
("write_date", "timestamp", ""),
("old_channel_id", "integer", ""),
],
)
openupgrade.logged_query(
env.cr,
"""
INSERT INTO mail_group (name, active, description, moderation,
access_group_id, access_mode, moderation_notify, moderation_notify_msg,
moderation_guidelines, moderation_guidelines_msg,
create_uid, write_uid, create_date, write_date, old_channel_id)
SELECT mc.name, mc.active, mc.description, mc.moderation, mc.group_public_id,
CASE WHEN mc.public = 'private' THEN 'members' ELSE mc.public END,
mc.moderation_notify, mc.moderation_notify_msg, mc.moderation_guidelines,
mc.moderation_guidelines_msg, mc.create_uid, mc.write_uid, mc.create_date,
mc.write_date, mc.id
FROM mail_channel mc
WHERE mc.email_send""",
)
# add image_128 attachments
env.cr.execute(
"""
SELECT ia.*, mg.id AS mail_group_id
FROM ir_attachment ia
JOIN mail_channel mc ON (ia.res_model = 'mail.channel' AND ia.res_id = mc.id)
JOIN mail_group mg ON mg.old_channel_id = mc.id
WHERE ia.res_field = 'image_128'
""",
)
columns = [
x.name for x in env.cr._obj.description if x.name not in ("id", "mail_group_id")
]
attachments = env.cr.dictfetchall()
values = []
for attachment in attachments:
attachment["res_model"] = "mail.group"
attachment["res_id"] = attachment["mail_group_id"]
vals = ["'%s'" % attachment[x] for x in columns]
vals = [x if x != "'None'" else "NULL" for x in vals]
values += ["(" + ", ".join(vals) + ")"]
if values:
openupgrade.logged_query(
env.cr,
"""
INSERT INTO ir_attachment ({})
VALUES {};""".format(
", ".join(columns), ", ".join(values)
),
)
# adapt m2m table
openupgrade.logged_query(
env.cr,
"""
UPDATE mail_group_moderator_rel rel
SET mail_group_id = mg.id
FROM mail_group mg
WHERE mg.old_channel_id = rel.mail_group_id""",
)
# fill mail_group_moderation.mail_group_id (field is required)
openupgrade.logged_query(
env.cr,
"""
ALTER TABLE mail_group_moderation
ADD COLUMN mail_group_id integer""",
)
openupgrade.logged_query(
env.cr,
"""
UPDATE mail_group_moderation mgm
SET mail_group_id = mg.id
FROM mail_group mg
WHERE mgm.channel_id = mg.old_channel_id""",
)
# fill mail_group_message table
sql.create_model_table(
env.cr,
"mail_group_message",
columns=[
("mail_group_id", "integer", ""),
("mail_message_id", "integer", ""),
("group_message_parent_id", "integer", ""),
("moderator_id", "integer", ""),
("moderation_status", "varchar", ""),
("create_uid", "integer", ""),
("write_uid", "integer", ""),
("create_date", "timestamp", ""),
("write_date", "timestamp", ""),
],
)
openupgrade.logged_query(
env.cr,
"""
INSERT INTO mail_group_message (mail_group_id, mail_message_id, moderator_id,
moderation_status, create_uid, write_uid, create_date, write_date)
SELECT mg.id, mm.id, mm.moderator_id, mm.moderation_status,
mm.create_uid, mm.write_uid, mm.create_date, mm.write_date
FROM mail_message mm
JOIN mail_message_mail_channel_rel rel ON rel.mail_message_id = mm.id
JOIN mail_group mg ON rel.mail_channel_id = mg.old_channel_id""",
)
openupgrade.logged_query(
env.cr,
"""
UPDATE mail_group_message mgm
SET group_message_parent_id = mgm2.id
FROM mail_message mm
JOIN mail_message mm2 ON mm.parent_id = mm2.id
JOIN mail_group_message mgm2 ON mgm2.mail_message_id = mm2.id
WHERE mgm.mail_message_id = mm.id""",
)
# fill mail_group_member table
sql.create_model_table(
env.cr,
"mail_group_member",
columns=[
("email", "varchar", ""),
("mail_group_id", "integer", ""),
("partner_id", "integer", ""),
("create_uid", "integer", ""),
("write_uid", "integer", ""),
("create_date", "timestamp", ""),
("write_date", "timestamp", ""),
],
)
openupgrade.logged_query(
env.cr,
"""
INSERT INTO mail_group_member (email, mail_group_id, partner_id,
create_uid, write_uid, create_date, write_date)
SELECT rp.email, mg.id, rp.id,
rel.create_uid, rel.write_uid, rel.create_date, rel.write_date
FROM res_partner rp
JOIN mail_channel_partner rel ON rel.partner_id = rp.id
JOIN mail_group mg ON rel.channel_id = mg.old_channel_id""",
)


@openupgrade.migrate()
Expand All @@ -78,3 +283,4 @@ def migrate(env, version):
_rename_fields(env)
_delete_channel_follower_records(env)
delete_obsolete_constraints(env)
migration_to_mail_group(env)
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
---Models in module 'mail'---
obsolete model mail.moderation (renamed to mail.group.moderation in module mail_group)
# DONE: pre-migration: renamed model and table

new model mail.channel.rtc.session
new model mail.composer.mixin [abstract]
new model mail.guest
new model mail.ice.server
new model mail.message.reaction
new model res.users.settings
new model res.users.settings.volumes
# NOTHING TO DO

---Fields in module 'mail'---
mail / bus.presence / guest_id (many2one) : NEW relation: mail.guest
# NOTHING TO DO: new feature
Expand Down Expand Up @@ -36,12 +40,12 @@ mail / mail.activity.type / triggered_next_type_id (many2one): NEW
# DONE: pre-migration, rename fields (default_next_type_id -> triggered_next_type_id, next_type_ids -> suggested_next_type_ids)

mail / mail.channel / channel_message_ids (many2many): DEL relation: mail.message
# NOTHING TO DO: feature removed
# DONE: pre-migration: used to create mail.group.message records

mail / mail.channel / channel_partner_ids (many2many): not stored anymore
mail / mail.channel / channel_partner_ids (many2many): now a function
mail / mail.channel / channel_partner_ids (many2many): table is now 'False' ('mail_channel_partner')
# NOTHING TO DO: table will be removed by ORM
# DONE: pre-migration: used to create mail.group.member records

mail / mail.channel / channel_type (selection) : selection_keys is now '['channel', 'chat', 'group']' ('['channel', 'chat']')
# NOTHING TO DO: new feature
Expand All @@ -51,14 +55,17 @@ mail / mail.channel / default_display_mode (selection): NEW
# NOTHING TO DO: new feature call audio and video

mail / mail.channel / email_send (boolean) : DEL
# DONE: pre-migration: created mail.group records from the ones with email_send = True
# DONE: post-migration: deleted records that have email_send = True (they are now mail.group records)

mail / mail.channel / moderation (boolean) : DEL
mail / mail.channel / moderation_guidelines (boolean): DEL
mail / mail.channel / moderation_guidelines_msg (text): DEL
mail / mail.channel / moderation_ids (one2many) : DEL relation: mail.moderation
mail / mail.channel / moderation_notify (boolean) : DEL
mail / mail.channel / moderation_notify_msg (text) : DEL
mail / mail.channel / moderator_ids (many2many) : DEL relation: res.users
# NOTHING TO DO: new feature
# DONE: pre-migration: used this fields to fill mail.group model

mail / mail.channel.partner / channel_id (many2one) : now required
# NOTHING DO TO: nocase in v14 that this field is null
Expand Down Expand Up @@ -147,7 +154,7 @@ mail / mail.moderation / channel_id (many2one) : DEL re
mail / mail.moderation / display_name (char) : module is now 'mail_group' ('mail')
mail / mail.moderation / email (char) : module is now 'mail_group' ('mail')
mail / mail.moderation / status (selection) : module is now 'mail_group' ('mail')
# NOTHING TO DO: renamed model and moved to mail_group
# DONE: pre-migration: renamed model and table

mail / mail.notification / failure_type (selection) : selection_keys is now '['mail_email_invalid', 'mail_email_missing', 'mail_smtp', 'unknown']' ('['BOUNCE', 'RECIPIENT', 'SMTP', 'UNKNOWN']')
# DONE: post-migration: map old values to new values
Expand Down
Loading

0 comments on commit 5d65bf2

Please sign in to comment.