From 1a7a4cfd3a9eefa2ecf36c0392b47f4032e0e59e Mon Sep 17 00:00:00 2001 From: Jordi Ballester Alomar Date: Mon, 23 Nov 2026 18:29:25 +0100 Subject: [PATCH 1/2] [IMP] hr_expense: test that the sheet chatter is kept hr.expense.sheet is removed in 19.0. Post a message and schedule an activity on a sheet, and check that both are found on its expense after the migration. --- .../tests/data_hr_expense_migration.py | 3 +++ .../tests/test_hr_expense_migration.py | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) 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 93eddfad75ca..7033a891799c 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 e8999b0c9c87..f4f622ffc603 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")] + ) + ) From 52fab54b19b0c90fa220c571a9d3b5f5e395d2e0 Mon Sep 17 00:00:00 2001 From: Jordi Ballester Alomar Date: Mon, 23 Nov 2026 07:58:14 +0100 Subject: [PATCH 2/2] [FIX] hr_expense: move the sheet chatter to the expenses hr.expense.sheet is removed in 19.0, but its messages, attachments, followers and activities kept pointing to it. The history of the sheets was no longer visible anywhere, and the activities of the removed model broke the activity overview of every user. Move them to the first expense of each sheet, and drop the activities of sheets without expenses. --- .../hr_expense/19.0.2.1/post-migration.py | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) 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 3ed6bbc3c2bb..b92dce6875f1 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)