From bc172678a4f7776fcdb75ee9aa0fe203038756dc Mon Sep 17 00:00:00 2001 From: Don Kendall Date: Tue, 19 May 2026 09:28:13 -0400 Subject: [PATCH] =?UTF-8?q?[OU-ADD]=20crm:=20stage.team=5Fid=20m2o?= =?UTF-8?q?=E2=86=92m2m=20migration=20scripts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Promote crm.stage.team_id (m2o crm.team) to team_ids (m2m). pre-migration preserves the legacy FK column via rename_columns; post-migration inserts rows into crm_stage_crm_team_rel from it. Legacy column left for database_cleanup. --- docsource/modules180-190.rst | 2 +- .../scripts/crm/19.0.1.9/post-migration.py | 17 +++++++++++ .../scripts/crm/19.0.1.9/pre-migration.py | 10 +++++++ .../crm/19.0.1.9/tests/test_crm_migration.py | 29 ++++++++++++++++++ .../crm/19.0.1.9/upgrade_analysis_work.txt | 30 +++++++++++++++++++ .../scripts/crm/tests/data_crm_migration.py | 7 +++++ 6 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 openupgrade_scripts/scripts/crm/19.0.1.9/post-migration.py create mode 100644 openupgrade_scripts/scripts/crm/19.0.1.9/pre-migration.py create mode 100644 openupgrade_scripts/scripts/crm/19.0.1.9/tests/test_crm_migration.py create mode 100644 openupgrade_scripts/scripts/crm/19.0.1.9/upgrade_analysis_work.txt create mode 100644 openupgrade_scripts/scripts/crm/tests/data_crm_migration.py diff --git a/docsource/modules180-190.rst b/docsource/modules180-190.rst index e7f797ce0766..00e128586b47 100644 --- a/docsource/modules180-190.rst +++ b/docsource/modules180-190.rst @@ -112,7 +112,7 @@ Module coverage 18.0 -> 19.0 +---------------------------------------------------+----------------------+-------------------------------------------------+ | contacts | | | +---------------------------------------------------+----------------------+-------------------------------------------------+ -| crm | | | +| crm |Done | | +---------------------------------------------------+----------------------+-------------------------------------------------+ | crm_iap_enrich | |No DB layout changes. | +---------------------------------------------------+----------------------+-------------------------------------------------+ diff --git a/openupgrade_scripts/scripts/crm/19.0.1.9/post-migration.py b/openupgrade_scripts/scripts/crm/19.0.1.9/post-migration.py new file mode 100644 index 000000000000..f94b2328da9d --- /dev/null +++ b/openupgrade_scripts/scripts/crm/19.0.1.9/post-migration.py @@ -0,0 +1,17 @@ +from openupgradelib import openupgrade + + +@openupgrade.migrate() +def migrate(env, version): + """Populate crm.stage.team_ids m2m from legacy team_id (m2o -> m2m promotion).""" + legacy = openupgrade.get_legacy_name("team_id") + openupgrade.logged_query( + env.cr, + f""" + INSERT INTO crm_stage_crm_team_rel (crm_stage_id, crm_team_id) + SELECT id, {legacy} + FROM crm_stage + WHERE {legacy} IS NOT NULL + ON CONFLICT DO NOTHING + """, + ) diff --git a/openupgrade_scripts/scripts/crm/19.0.1.9/pre-migration.py b/openupgrade_scripts/scripts/crm/19.0.1.9/pre-migration.py new file mode 100644 index 000000000000..aac889a1fc30 --- /dev/null +++ b/openupgrade_scripts/scripts/crm/19.0.1.9/pre-migration.py @@ -0,0 +1,10 @@ +from openupgradelib import openupgrade + +_renamed_columns = { + "crm_stage": [("team_id", None)], +} + + +@openupgrade.migrate() +def migrate(env, version): + openupgrade.rename_columns(env.cr, _renamed_columns) diff --git a/openupgrade_scripts/scripts/crm/19.0.1.9/tests/test_crm_migration.py b/openupgrade_scripts/scripts/crm/19.0.1.9/tests/test_crm_migration.py new file mode 100644 index 000000000000..2bb54e884b43 --- /dev/null +++ b/openupgrade_scripts/scripts/crm/19.0.1.9/tests/test_crm_migration.py @@ -0,0 +1,29 @@ +from odoo.tests import TransactionCase + +from odoo.addons.openupgrade_framework import openupgrade_test + + +@openupgrade_test +class TestCrmMigration(TransactionCase): + def test_stage_team_m2m_populated(self): + """The m2o→m2m promotion on crm.stage.team_id must preserve + relationships in the new team_ids m2m field. + """ + self.assertTrue( + self.env["crm.stage"].search([("team_ids", "!=", False)], limit=1), + "Migration should have populated crm.stage.team_ids from the " + "legacy team_id column.", + ) + + def test_legacy_column_remains(self): + """Pre-migration renames the legacy FK column; it should still + be on disk so a later database_cleanup pass can drop it. + """ + self.env.cr.execute( + """ + SELECT column_name FROM information_schema.columns + WHERE table_name = 'crm_stage' + AND column_name = 'openupgrade_legacy_19_0_team_id' + """ + ) + self.assertEqual(len(self.env.cr.fetchall()), 1) diff --git a/openupgrade_scripts/scripts/crm/19.0.1.9/upgrade_analysis_work.txt b/openupgrade_scripts/scripts/crm/19.0.1.9/upgrade_analysis_work.txt new file mode 100644 index 000000000000..d674d473b2f6 --- /dev/null +++ b/openupgrade_scripts/scripts/crm/19.0.1.9/upgrade_analysis_work.txt @@ -0,0 +1,30 @@ +---Models in module 'crm'--- +---Fields in module 'crm'--- +crm / crm.lead / commercial_partner_id (many2one): NEW relation: res.partner, hasdefault: compute, stored: False +crm / crm.lead / mobile (char) : DEL +crm / crm.lead / title (many2one) : DEL relation: res.partner.title +crm / crm.lead / won_status (selection) : NEW selection_keys: ['lost', 'pending', 'won'], isfunction: function, stored +crm / crm.lead.scoring.frequency.field / color (integer) : NEW hasdefault: default +crm / crm.stage / color (integer) : NEW +crm / crm.stage / rotting_threshold_days (integer): NEW hasdefault: default +crm / crm.stage / team_id (many2one) : DEL relation: crm.team +crm / crm.stage / team_ids (many2many) : NEW relation: crm.team +crm / crm.team.member / assignment_domain_preferred (char): NEW +crm / res.users / target_sales_done (integer) : DEL +crm / res.users / target_sales_won (integer) : DEL + +# DONE: crm.stage.team_id (m2o) -> team_ids (m2m) promotion handled by +# pre-migration rename_columns (preserve as legacy) + post-migration +# INSERT into crm_stage_crm_team_rel. All NEW fields are additive +# (default / compute / function-stored). DEL fields (crm.lead.mobile, +# crm.lead.title, res.users.target_sales_done/won) are left for +# database_cleanup per pedrobaeza policy. + +---XML records in module 'crm'--- +NEW ir.actions.act_window: crm.mail_followers_edit_action_from_lead +NEW ir.model.constraint: crm.constraint_crm_lead_create_date_team_id_idx +NEW ir.model.constraint: crm.constraint_crm_lead_default_order_idx +NEW ir.model.constraint: crm.constraint_crm_lead_user_id_team_id_type_index +DEL ir.ui.view: crm.crm_lead_partner_kanban_view + +# NOTHING TO DO diff --git a/openupgrade_scripts/scripts/crm/tests/data_crm_migration.py b/openupgrade_scripts/scripts/crm/tests/data_crm_migration.py new file mode 100644 index 000000000000..369208d3da73 --- /dev/null +++ b/openupgrade_scripts/scripts/crm/tests/data_crm_migration.py @@ -0,0 +1,7 @@ +env = locals().get("env") +team = env.ref("sales_team.team_sales_department", raise_if_not_found=False) +if team: + stage = env["crm.stage"].search([("team_id", "=", False)], limit=1) + if stage: + stage.team_id = team +env.cr.commit()