From 59dfce495192888e66161dfbc251ed86318a8d74 Mon Sep 17 00:00:00 2001
From: dyturralde <dyturralde@sisinfo.com.uy>
Date: Tue, 5 Nov 2024 10:26:13 -0300
Subject: [PATCH 1/2] [17.0][OU-ADD] im_livechat: Migrate to 17.0

---
 docsource/modules160-170.rst                  |  2 +-
 .../im_livechat/17.0.1.0/end-migration.py     | 14 ++++
 .../im_livechat/17.0.1.0/post-migration.py    |  8 ++
 .../im_livechat/17.0.1.0/pre-migration.py     | 17 ++++
 .../17.0.1.0/upgrade_analysis_work.txt        | 79 +++++++++++++++++++
 5 files changed, 119 insertions(+), 1 deletion(-)
 create mode 100644 openupgrade_scripts/scripts/im_livechat/17.0.1.0/end-migration.py
 create mode 100644 openupgrade_scripts/scripts/im_livechat/17.0.1.0/post-migration.py
 create mode 100644 openupgrade_scripts/scripts/im_livechat/17.0.1.0/pre-migration.py
 create mode 100644 openupgrade_scripts/scripts/im_livechat/17.0.1.0/upgrade_analysis_work.txt

diff --git a/docsource/modules160-170.rst b/docsource/modules160-170.rst
index 24e7aa0f0c3a..4e7874b79dcb 100644
--- a/docsource/modules160-170.rst
+++ b/docsource/modules160-170.rst
@@ -214,7 +214,7 @@ Module coverage 16.0 -> 17.0
 +---------------------------------------------------+----------------------+-------------------------------------------------+
 | iap_mail                                          | Nothing to do        |                                                 |
 +---------------------------------------------------+----------------------+-------------------------------------------------+
-| im_livechat                                       |                      |                                                 |
+| im_livechat                                       | Done                 |                                                 |
 +---------------------------------------------------+----------------------+-------------------------------------------------+
 | im_livechat_mail_bot                              |                      |No DB layout changes.                            |
 +---------------------------------------------------+----------------------+-------------------------------------------------+
diff --git a/openupgrade_scripts/scripts/im_livechat/17.0.1.0/end-migration.py b/openupgrade_scripts/scripts/im_livechat/17.0.1.0/end-migration.py
new file mode 100644
index 000000000000..3c942bd9506a
--- /dev/null
+++ b/openupgrade_scripts/scripts/im_livechat/17.0.1.0/end-migration.py
@@ -0,0 +1,14 @@
+# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
+from openupgradelib import openupgrade
+
+
+def _discuss_channel_computation(env):
+    rating_last_discuss = (
+        env["discuss.channel"].with_context(active_test=False).search([])
+    )
+    rating_last_discuss._compute_rating_last_value()
+
+
+@openupgrade.migrate()
+def migrate(env, version):
+    _discuss_channel_computation(env)
diff --git a/openupgrade_scripts/scripts/im_livechat/17.0.1.0/post-migration.py b/openupgrade_scripts/scripts/im_livechat/17.0.1.0/post-migration.py
new file mode 100644
index 000000000000..02c89ef93e9e
--- /dev/null
+++ b/openupgrade_scripts/scripts/im_livechat/17.0.1.0/post-migration.py
@@ -0,0 +1,8 @@
+# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
+
+from openupgradelib import openupgrade
+
+
+@openupgrade.migrate()
+def migrate(env, version):
+    openupgrade.load_data(env, "im_livechat", "17.0.1.0/noupdate_changes.xml")
diff --git a/openupgrade_scripts/scripts/im_livechat/17.0.1.0/pre-migration.py b/openupgrade_scripts/scripts/im_livechat/17.0.1.0/pre-migration.py
new file mode 100644
index 000000000000..d8f3a6202094
--- /dev/null
+++ b/openupgrade_scripts/scripts/im_livechat/17.0.1.0/pre-migration.py
@@ -0,0 +1,17 @@
+# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
+from openupgradelib import openupgrade
+
+
+def _discuss_channel_create_column(env):
+    openupgrade.logged_query(
+        env.cr,
+        """
+        ALTER TABLE discuss_channel
+            ADD COLUMN IF NOT EXISTS rating_last_value FLOAT;
+        """,
+    )
+
+
+@openupgrade.migrate()
+def migrate(env, version):
+    _discuss_channel_create_column(env)
diff --git a/openupgrade_scripts/scripts/im_livechat/17.0.1.0/upgrade_analysis_work.txt b/openupgrade_scripts/scripts/im_livechat/17.0.1.0/upgrade_analysis_work.txt
new file mode 100644
index 000000000000..c4026f64c698
--- /dev/null
+++ b/openupgrade_scripts/scripts/im_livechat/17.0.1.0/upgrade_analysis_work.txt
@@ -0,0 +1,79 @@
+---Models in module 'im_livechat'---
+# NOTHING TO DO
+
+---Fields in module 'im_livechat'---
+im_livechat  / chatbot.message          / discuss_channel_id (many2one) : NEW relation: discuss.channel, required
+im_livechat  / chatbot.message          / mail_channel_id (many2one)    : DEL relation: mail.channel, required
+im_livechat  / discuss.channel          / anonymous_name (char)         : NEW
+im_livechat  / discuss.channel          / channel_type (False)          : NEW selection_keys: ['channel', 'chat', 'group', 'livechat'], mode: modify
+im_livechat  / discuss.channel          / chatbot_current_step_id (many2one): NEW relation: chatbot.script.step
+im_livechat  / discuss.channel          / chatbot_message_ids (one2many): NEW relation: chatbot.message
+im_livechat  / discuss.channel          / country_id (many2one)         : NEW relation: res.country
+im_livechat  / discuss.channel          / livechat_active (boolean)     : NEW
+im_livechat  / discuss.channel          / livechat_channel_id (many2one): NEW relation: im_livechat.channel
+im_livechat  / discuss.channel          / livechat_operator_id (many2one): NEW relation: res.partner
+# NOTHING TO DO
+
+im_livechat  / discuss.channel          / rating_last_value (float)     : NEW isfunction: function, stored
+# DONE create column in pre-migration and compute using orm in end-migration
+
+im_livechat  / im_livechat.channel      / channel_ids (one2many)        : relation is now 'discuss.channel' ('mail.channel') [nothing to do]
+im_livechat  / mail.channel             / anonymous_name (char)         : DEL
+im_livechat  / mail.channel             / channel_type (False)          : DEL selection_keys: ['channel', 'chat', 'group', 'livechat'], mode: modify
+im_livechat  / mail.channel             / chatbot_current_step_id (many2one): DEL relation: chatbot.script.step
+im_livechat  / mail.channel             / chatbot_message_ids (one2many): DEL relation: chatbot.message
+im_livechat  / mail.channel             / country_id (many2one)         : DEL relation: res.country
+im_livechat  / mail.channel             / livechat_active (boolean)     : DEL
+im_livechat  / mail.channel             / livechat_channel_id (many2one): DEL relation: im_livechat.channel
+im_livechat  / mail.channel             / livechat_operator_id (many2one): DEL relation: res.partner
+im_livechat  / mail.channel             / rating_ids (one2many)         : DEL relation: rating.rating
+im_livechat  / mail.channel             / rating_last_value (float)     : DEL
+im_livechat  / res.users                / livechat_username (char)      : not stored anymore
+im_livechat  / res.users                / livechat_username (char)      : now a function
+im_livechat  / res.users.settings       / livechat_lang_ids (many2many) : NEW relation: res.lang
+im_livechat  / res.users.settings       / livechat_username (char)      : NEW
+# NOTHING TO DO
+
+
+---XML records in module 'im_livechat'---
+NEW ir.actions.act_window: im_livechat.discuss_channel_action
+NEW ir.actions.act_window: im_livechat.discuss_channel_action_from_livechat_channel
+DEL ir.actions.act_window: im_livechat.im_livechat_canned_response_action
+DEL ir.actions.act_window: im_livechat.mail_channel_action
+DEL ir.actions.act_window: im_livechat.mail_channel_action_from_livechat_channel
+NEW ir.actions.act_window.view: im_livechat.discuss_channel_action_form
+NEW ir.actions.act_window.view: im_livechat.discuss_channel_action_livechat_form
+NEW ir.actions.act_window.view: im_livechat.discuss_channel_action_livechat_tree
+NEW ir.actions.act_window.view: im_livechat.discuss_channel_action_tree
+NEW ir.actions.act_window.view: im_livechat.rating_rating_action_livechat_view_tree
+DEL ir.actions.act_window.view: im_livechat.mail_channel_action_form
+DEL ir.actions.act_window.view: im_livechat.mail_channel_action_livechat_form
+DEL ir.actions.act_window.view: im_livechat.mail_channel_action_livechat_tree
+DEL ir.actions.act_window.view: im_livechat.mail_channel_action_tree
+NEW ir.model.access: im_livechat.access_livechat_channel_employee
+NEW ir.model.access: im_livechat.access_livechat_channel_portal
+NEW ir.model.access: im_livechat.access_livechat_channel_public
+NEW ir.model.access: im_livechat.access_livechat_channel_rule_employee
+NEW ir.model.access: im_livechat.access_livechat_channel_rule_portal
+NEW ir.model.access: im_livechat.access_livechat_channel_rule_public
+DEL ir.model.access: im_livechat.access_chatbot_message_all
+DEL ir.model.access: im_livechat.access_chatbot_script
+DEL ir.model.access: im_livechat.access_chatbot_script_step
+DEL ir.model.access: im_livechat.access_livechat_channel
+DEL ir.model.access: im_livechat.access_livechat_channel_rule
+NEW ir.model.constraint: im_livechat.constraint_discuss_channel_livechat_operator_id
+DEL ir.model.constraint: im_livechat.constraint_mail_channel_livechat_operator_id
+NEW ir.rule: im_livechat.ir_rule_discuss_channel_group_im_livechat_group_manager (noupdate)
+NEW ir.rule: im_livechat.ir_rule_discuss_channel_member_group_im_livechat_group_manager (noupdate)
+DEL ir.rule: im_livechat.im_livechat_rule_manager_read_all_mail_channel (noupdate)
+NEW ir.ui.view: im_livechat.discuss_channel_view_form
+NEW ir.ui.view: im_livechat.discuss_channel_view_search
+NEW ir.ui.view: im_livechat.discuss_channel_view_tree
+NEW ir.ui.view: im_livechat.qunit_embed_suite
+NEW ir.ui.view: im_livechat.rating_rating_view_kanban
+NEW ir.ui.view: im_livechat.rating_rating_view_tree
+DEL ir.ui.view: im_livechat.im_livechat_canned_response_view_tree
+DEL ir.ui.view: im_livechat.mail_channel_view_form
+DEL ir.ui.view: im_livechat.mail_channel_view_search
+DEL ir.ui.view: im_livechat.mail_channel_view_tree
+# NOTHING TO DO

From 426d6a36cfe5d53839ca7ef18a9935dc046bddee Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Miquel=20Ra=C3=AFch?= <miquel.raich@forgeflow.com>
Date: Wed, 22 Jan 2025 18:07:37 +0100
Subject: [PATCH 2/2] [OU-IMP] im_livechat: finish migration

---
 .../scripts/im_livechat/17.0.1.0/post-migration.py    | 10 ++++++++++
 .../scripts/im_livechat/17.0.1.0/pre-migration.py     | 11 ++++++++++-
 .../im_livechat/17.0.1.0/upgrade_analysis_work.txt    |  9 ++++++++-
 3 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/openupgrade_scripts/scripts/im_livechat/17.0.1.0/post-migration.py b/openupgrade_scripts/scripts/im_livechat/17.0.1.0/post-migration.py
index 02c89ef93e9e..de336d86d865 100644
--- a/openupgrade_scripts/scripts/im_livechat/17.0.1.0/post-migration.py
+++ b/openupgrade_scripts/scripts/im_livechat/17.0.1.0/post-migration.py
@@ -6,3 +6,13 @@
 @openupgrade.migrate()
 def migrate(env, version):
     openupgrade.load_data(env, "im_livechat", "17.0.1.0/noupdate_changes.xml")
+    openupgrade.delete_record_translations(
+        env.cr,
+        "im_livechat",
+        [
+            "livechat_email_template",
+        ],
+    )
+    openupgrade.delete_records_safely_by_xml_id(
+        env, ["im_livechat.im_livechat_rule_manager_read_all_mail_channel"]
+    )
diff --git a/openupgrade_scripts/scripts/im_livechat/17.0.1.0/pre-migration.py b/openupgrade_scripts/scripts/im_livechat/17.0.1.0/pre-migration.py
index d8f3a6202094..ec139833deaa 100644
--- a/openupgrade_scripts/scripts/im_livechat/17.0.1.0/pre-migration.py
+++ b/openupgrade_scripts/scripts/im_livechat/17.0.1.0/pre-migration.py
@@ -7,7 +7,7 @@ def _discuss_channel_create_column(env):
         env.cr,
         """
         ALTER TABLE discuss_channel
-            ADD COLUMN IF NOT EXISTS rating_last_value FLOAT;
+            ADD COLUMN IF NOT EXISTS rating_last_value NUMERIC;
         """,
     )
 
@@ -15,3 +15,12 @@ def _discuss_channel_create_column(env):
 @openupgrade.migrate()
 def migrate(env, version):
     _discuss_channel_create_column(env)
+    # cannot use openupgrade.delete_sql_constraint_safely
+    openupgrade.logged_query(
+        env.cr,
+        """ALTER TABLE discuss_channel
+           DROP CONSTRAINT IF EXISTS mail_channel_livechat_operator_id""",
+    )
+    openupgrade.delete_records_safely_by_xml_id(
+        env, ["im_livechat.constraint_mail_channel_livechat_operator_id"]
+    )
diff --git a/openupgrade_scripts/scripts/im_livechat/17.0.1.0/upgrade_analysis_work.txt b/openupgrade_scripts/scripts/im_livechat/17.0.1.0/upgrade_analysis_work.txt
index c4026f64c698..c3290539312f 100644
--- a/openupgrade_scripts/scripts/im_livechat/17.0.1.0/upgrade_analysis_work.txt
+++ b/openupgrade_scripts/scripts/im_livechat/17.0.1.0/upgrade_analysis_work.txt
@@ -34,7 +34,6 @@ im_livechat  / res.users.settings       / livechat_lang_ids (many2many) : NEW re
 im_livechat  / res.users.settings       / livechat_username (char)      : NEW
 # NOTHING TO DO
 
-
 ---XML records in module 'im_livechat'---
 NEW ir.actions.act_window: im_livechat.discuss_channel_action
 NEW ir.actions.act_window: im_livechat.discuss_channel_action_from_livechat_channel
@@ -61,11 +60,19 @@ DEL ir.model.access: im_livechat.access_chatbot_script
 DEL ir.model.access: im_livechat.access_chatbot_script_step
 DEL ir.model.access: im_livechat.access_livechat_channel
 DEL ir.model.access: im_livechat.access_livechat_channel_rule
+# NOTHING TO DO
+
 NEW ir.model.constraint: im_livechat.constraint_discuss_channel_livechat_operator_id
 DEL ir.model.constraint: im_livechat.constraint_mail_channel_livechat_operator_id
+# DONE: pre-migration: deleted safely old constraint (we don't have renaming mechanism)
+
 NEW ir.rule: im_livechat.ir_rule_discuss_channel_group_im_livechat_group_manager (noupdate)
 NEW ir.rule: im_livechat.ir_rule_discuss_channel_member_group_im_livechat_group_manager (noupdate)
+# NOTHING TO DO
+
 DEL ir.rule: im_livechat.im_livechat_rule_manager_read_all_mail_channel (noupdate)
+# DONE: post-migration: deleted safely
+
 NEW ir.ui.view: im_livechat.discuss_channel_view_form
 NEW ir.ui.view: im_livechat.discuss_channel_view_search
 NEW ir.ui.view: im_livechat.discuss_channel_view_tree
