diff --git a/openupgrade_scripts/scripts/hr_expense/19.0.2.1/post-migration.py b/openupgrade_scripts/scripts/hr_expense/19.0.2.1/post-migration.py index 3ed6bbc3c2b..b92dce6875f 100644 --- a/openupgrade_scripts/scripts/hr_expense/19.0.2.1/post-migration.py +++ b/openupgrade_scripts/scripts/hr_expense/19.0.2.1/post-migration.py @@ -47,6 +47,81 @@ def hr_expense_approval_fields(env): ) +def hr_expense_sheet_chatter(env): + """ + Move the messages, attachments, followers and activities of each + hr.expense.sheet to its first expense, and drop the activities of sheets + without expenses, as the model no longer exists + """ + sheet_expense = """ + sheet_expense AS ( + SELECT DISTINCT ON (former_sheet_id) + former_sheet_id AS sheet_id, id AS expense_id, name + FROM hr_expense + WHERE former_sheet_id IS NOT NULL + ORDER BY former_sheet_id, id + ) + """ + openupgrade.logged_query( + env.cr, + f""" + WITH {sheet_expense} + UPDATE mail_message mm + SET model = 'hr.expense', res_id = se.expense_id + FROM sheet_expense se + WHERE mm.model = 'hr.expense.sheet' AND mm.res_id = se.sheet_id + """, + ) + openupgrade.logged_query( + env.cr, + f""" + WITH {sheet_expense} + UPDATE ir_attachment ia + SET res_model = 'hr.expense', res_id = se.expense_id + FROM sheet_expense se + WHERE ia.res_model = 'hr.expense.sheet' AND ia.res_id = se.sheet_id + """, + ) + openupgrade.logged_query( + env.cr, + f""" + WITH {sheet_expense} + UPDATE mail_followers mf + SET res_model = 'hr.expense', res_id = se.expense_id + FROM sheet_expense se + WHERE mf.res_model = 'hr.expense.sheet' AND mf.res_id = se.sheet_id + AND NOT EXISTS ( + SELECT 1 FROM mail_followers mf2 + WHERE mf2.res_model = 'hr.expense' + AND mf2.res_id = se.expense_id + AND mf2.partner_id = mf.partner_id + ) + """, + ) + openupgrade.logged_query( + env.cr, + "DELETE FROM mail_followers WHERE res_model = 'hr.expense.sheet'", + ) + openupgrade.logged_query( + env.cr, + f""" + WITH {sheet_expense} + UPDATE mail_activity ma + SET res_model = 'hr.expense', + res_model_id = im.id, + res_id = se.expense_id, + res_name = se.name + FROM sheet_expense se, ir_model im + WHERE ma.res_model = 'hr.expense.sheet' AND ma.res_id = se.sheet_id + AND im.model = 'hr.expense' + """, + ) + openupgrade.logged_query( + env.cr, + "DELETE FROM mail_activity WHERE res_model = 'hr.expense.sheet'", + ) + + def update_product_uoms(env): """ Set updated product uoms if possible @@ -80,5 +155,6 @@ def migrate(env, version): ) hr_expense_account_move_id(env) hr_expense_approval_fields(env) + hr_expense_sheet_chatter(env) update_product_uoms(env) openupgrade.delete_records_safely_by_xml_id(env, deleted_xmlids) diff --git a/openupgrade_scripts/scripts/hr_expense/tests/data_hr_expense_migration.py b/openupgrade_scripts/scripts/hr_expense/tests/data_hr_expense_migration.py index 93eddfad75c..7033a891799 100644 --- a/openupgrade_scripts/scripts/hr_expense/tests/data_hr_expense_migration.py +++ b/openupgrade_scripts/scripts/hr_expense/tests/data_hr_expense_migration.py @@ -11,4 +11,7 @@ action = env.ref("hr_expense.customer_meeting_sheet").action_register_payment() wizard = env[action["res_model"]].with_context(**action["context"]).create({}) wizard.action_create_payments() +sheet = env.ref("hr_expense.customer_meeting_sheet") +sheet.message_post(body="Sheet chatter to migrate") +sheet.activity_schedule("mail.mail_activity_data_todo", summary="Sheet activity") env.cr.commit() diff --git a/openupgrade_scripts/scripts/hr_expense/tests/test_hr_expense_migration.py b/openupgrade_scripts/scripts/hr_expense/tests/test_hr_expense_migration.py index e8999b0c9c8..f4f622ffc60 100644 --- a/openupgrade_scripts/scripts/hr_expense/tests/test_hr_expense_migration.py +++ b/openupgrade_scripts/scripts/hr_expense/tests/test_hr_expense_migration.py @@ -41,3 +41,22 @@ def test_product_uoms(self): self.env.ref("uom.product_uom_unit"), "Communication UOM should have been updated to unit", ) + + def test_sheet_chatter(self): + """ + Test that the chatter of expense sheets moved to their first expense + """ + message = self.env["mail.message"].search( + [("body", "ilike", "Sheet chatter to migrate")] + ) + self.assertEqual(message.model, "hr.expense") + activity = self.env["mail.activity"].search( + [("summary", "=", "Sheet activity")] + ) + self.assertEqual(activity.res_model, "hr.expense") + self.assertEqual(activity.res_id, message.res_id) + self.assertFalse( + self.env["mail.activity"].search_count( + [("res_model", "=", "hr.expense.sheet")] + ) + )