Skip to content

Commit 44d0fa1

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

4 files changed

Lines changed: 130 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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
openupgrade.logged_query(
8+
env.cr,
9+
"""
10+
UPDATE crm_lead
11+
SET phone = CONCAT(COALESCE(phone, ''), mobile)
12+
WHERE COELESCE(mobile, '') != ''
13+
""",
14+
)
15+
16+
17+
def _load_data_pls_fields_param(env):
18+
# New PLS fields added in v19. Merge them into the existing value instead of
19+
# overwriting it, so a customized/removed `crm.pls_fields` param is preserved
20+
new_fields = {"state_id", "country_id", "source_id", "lang_id", "tag_ids"}
21+
param = env["ir.config_parameter"].sudo().get_param("crm.pls_fields")
22+
if param:
23+
current = set(param.split(","))
24+
merged = current | new_fields
25+
env["ir.config_parameter"].sudo().set_param(
26+
"crm.pls_fields", ",".join(sorted(merged))
27+
)
28+
29+
30+
def _load_data_stage_colors(env):
31+
# Only color the default stages that still exist,
32+
# skip any the user deleted in v18.
33+
colors = {
34+
"stage_lead1": 11,
35+
"stage_lead2": 5,
36+
"stage_lead3": 8,
37+
"stage_lead4": 10,
38+
}
39+
for xml_id, color in colors.items():
40+
record = env.ref(f"crm.{xml_id}", raise_if_not_found=False)
41+
if record:
42+
record.color = color
43+
44+
45+
@openupgrade.migrate()
46+
def migrate(env, version):
47+
# Do not load noupdate_changes.xml: every entry is problematic (deleted stages,
48+
# customized pls param). the necessary changes are done manually in
49+
# the _load_data_* functions below
50+
51+
openupgrade.m2o_to_x2m(env.cr, env["crm.stage"], "crm_stage", "team_ids", "team_id")
52+
_load_data_stage_colors(env)
53+
_load_data_pls_fields_param(env)
54+
_migrate_lead_mobile(env)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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", "pending", "crm_lead"),
12+
],
13+
)
14+
openupgrade.logged_query(
15+
env.cr,
16+
"""
17+
UPDATE crm_lead cl
18+
SET won_status = 'won'
19+
FROM crm_stage cs
20+
WHERE cs.id = cl.stage_id
21+
AND cs.is_won
22+
AND cl.probability = 100
23+
""",
24+
)
25+
openupgrade.logged_query(
26+
env.cr,
27+
"""
28+
UPDATE crm_lead cl
29+
SET won_status = 'lost'
30+
WHERE NOT cl.active
31+
AND cl.probability = 0
32+
""",
33+
)
34+
35+
36+
@openupgrade.migrate()
37+
def migrate(env, version):
38+
_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)