Skip to content

Commit fe07ea0

Browse files
[OU-ADD] crm: Migration scripts
1 parent 750b5c1 commit fe07ea0

4 files changed

Lines changed: 141 additions & 1 deletion

File tree

docsource/modules180-190.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Module coverage 18.0 -> 19.0
112112
+---------------------------------------------------+----------------------+-------------------------------------------------+
113113
| contacts |Nothing to do | |
114114
+---------------------------------------------------+----------------------+-------------------------------------------------+
115-
| crm | | |
115+
| crm |Done | |
116116
+---------------------------------------------------+----------------------+-------------------------------------------------+
117117
| crm_iap_enrich | |No DB layout changes. |
118118
+---------------------------------------------------+----------------------+-------------------------------------------------+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Copyright 2026 Tecnativa - Eduardo Ezerouali
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3+
from openupgradelib import openupgrade
4+
5+
6+
def _migrate_lead_mobile(env):
7+
# Case 1: phone is empty → fill it with the old mobile value
8+
openupgrade.logged_query(
9+
env.cr,
10+
"""
11+
UPDATE crm_lead
12+
SET phone = mobile
13+
WHERE (phone IS NULL OR phone = '')
14+
AND (mobile IS NOT NULL AND mobile != '')
15+
""",
16+
)
17+
# Case 2: both phone and mobile exist and differ → concatenate them
18+
openupgrade.logged_query(
19+
env.cr,
20+
"""
21+
UPDATE crm_lead
22+
SET phone = phone || ' / ' || mobile
23+
WHERE phone IS NOT NULL AND phone != ''
24+
AND mobile IS NOT NULL AND mobile != ''
25+
AND btrim(phone) != btrim(mobile)
26+
""",
27+
)
28+
29+
30+
def _load_data_pls_fields_param(env):
31+
# New PLS fields added in v19. Merge them into the existing value instead of
32+
# overwriting it, so a customized/removed `crm.pls_fields` param is preserved
33+
new_fields = {"state_id", "country_id", "source_id", "lang_id", "tag_ids"}
34+
param = env["ir.config_parameter"].sudo().get_param("crm.pls_fields")
35+
if param:
36+
current = set(param.split(","))
37+
merged = current | new_fields
38+
env["ir.config_parameter"].sudo().set_param(
39+
"crm.pls_fields", ",".join(sorted(merged))
40+
)
41+
42+
43+
def _load_data_stage_colors(env):
44+
# Only color the default stages that still exist,
45+
# skip any the user deleted in v18.
46+
colors = {
47+
"stage_lead1": 11,
48+
"stage_lead2": 5,
49+
"stage_lead3": 8,
50+
"stage_lead4": 10,
51+
}
52+
for xml_id, color in colors.items():
53+
record = env.ref(f"crm.{xml_id}", raise_if_not_found=False)
54+
if record:
55+
record.color = color
56+
57+
58+
@openupgrade.migrate()
59+
def migrate(env, version):
60+
# Do not load noupdate_changes.xml: every entry is problematic (deleted stages,
61+
# customized pls param). the necessary changes are done manually in
62+
# the _load_data_* functions below
63+
64+
openupgrade.m2o_to_x2m(env.cr, env["crm.stage"], "crm_stage", "team_ids", "team_id")
65+
_load_data_stage_colors(env)
66+
_load_data_pls_fields_param(env)
67+
_migrate_lead_mobile(env)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2026 Tecnativa - Eduardo Ezerouali
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3+
from openupgradelib import openupgrade
4+
5+
6+
def _won_status_set(env):
7+
# Precreate won_status and set with sql
8+
openupgrade.add_columns(
9+
env,
10+
[
11+
("crm.lead", "won_status", "selection", None, "crm_lead"),
12+
],
13+
)
14+
openupgrade.logged_query(
15+
env.cr,
16+
"""
17+
UPDATE crm_lead cl
18+
SET won_status = CASE
19+
WHEN cl.probability = 100
20+
AND cs.is_won = TRUE
21+
THEN 'won'
22+
WHEN cl.active = FALSE
23+
AND cl.probability = 0
24+
THEN 'lost'
25+
ELSE 'pending'
26+
END
27+
FROM crm_lead cl2
28+
LEFT JOIN crm_stage cs ON cs.id = cl2.stage_id
29+
WHERE cl2.id = cl.id
30+
""",
31+
)
32+
33+
34+
@openupgrade.migrate()
35+
def migrate(env, version):
36+
_won_status_set(env)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---Models in module 'crm'---
2+
---Fields in module 'crm'---
3+
crm / crm.lead / commercial_partner_id (many2one): NEW relation: res.partner, hasdefault: compute, stored: False
4+
# NOTHING TO DO
5+
6+
crm / crm.lead / mobile (char) : DEL
7+
# DONE: pos-migration: if phone is empty and mobile not set phone with mobile and concatenate the mobile in the pone field in case both exist
8+
9+
crm / crm.lead / title (many2one) : DEL relation: res.partner.title
10+
# NOTHING TO DO
11+
12+
crm / crm.lead / won_status (selection) : NEW selection_keys: ['lost', 'pending', 'won'], isfunction: function, stored
13+
# DONE: pre-migration: pre-created column and set it to prevent massive computation
14+
15+
crm / crm.lead.scoring.frequency.field / color (integer) : NEW hasdefault: default
16+
crm / crm.stage / color (integer) : NEW
17+
# NOTHING TO DO
18+
19+
crm / crm.stage / rotting_threshold_days (integer): NEW hasdefault: default
20+
# NOTHING TO DO: default is set to 0 to keep old behavoir
21+
22+
crm / crm.stage / team_id (many2one) : DEL relation: crm.team
23+
crm / crm.stage / team_ids (many2many) : NEW relation: crm.team
24+
# Done: post-migration: Transform m2o to m2m
25+
26+
crm / crm.team.member / assignment_domain_preferred (char): NEW
27+
crm / res.users / target_sales_done (integer) : DEL
28+
crm / res.users / target_sales_won (integer) : DEL
29+
# NOTHING TO DO
30+
31+
---XML records in module 'crm'---
32+
NEW ir.actions.act_window: crm.mail_followers_edit_action_from_lead
33+
NEW ir.model.constraint: crm.constraint_crm_lead_create_date_team_id_idx
34+
NEW ir.model.constraint: crm.constraint_crm_lead_default_order_idx
35+
NEW ir.model.constraint: crm.constraint_crm_lead_user_id_team_id_type_index
36+
DEL ir.ui.view: crm.crm_lead_partner_kanban_view
37+
# NOTHING TO DO

0 commit comments

Comments
 (0)