-
-
Notifications
You must be signed in to change notification settings - Fork 815
[19.0][OU-ADD] crm: Migration scripts #5706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eduezerouali-tecnativa
wants to merge
1
commit into
OCA:19.0
Choose a base branch
from
Tecnativa:19.0-ou-add-crm
base: 19.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+141
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
openupgrade_scripts/scripts/crm/19.0.1.9/post-migration.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # Copyright 2026 Tecnativa - Eduardo Ezerouali | ||
| # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
| from openupgradelib import openupgrade | ||
|
|
||
|
|
||
| def _migrate_lead_mobile(env): | ||
| # Case 1: phone is empty → fill it with the old mobile value | ||
| openupgrade.logged_query( | ||
| env.cr, | ||
| """ | ||
| UPDATE crm_lead | ||
| SET phone = mobile | ||
| WHERE (phone IS NULL OR phone = '') | ||
| AND (mobile IS NOT NULL AND mobile != '') | ||
| """, | ||
| ) | ||
| # Case 2: both phone and mobile exist and differ → concatenate them | ||
| openupgrade.logged_query( | ||
| env.cr, | ||
| """ | ||
| UPDATE crm_lead | ||
| SET phone = phone || ' / ' || mobile | ||
| WHERE phone IS NOT NULL AND phone != '' | ||
| AND mobile IS NOT NULL AND mobile != '' | ||
| AND btrim(phone) != btrim(mobile) | ||
| """, | ||
| ) | ||
|
|
||
|
|
||
| def _load_data_pls_fields_param(env): | ||
| # New PLS fields added in v19. Merge them into the existing value instead of | ||
| # overwriting it, so a customized/removed `crm.pls_fields` param is preserved | ||
| new_fields = {"state_id", "country_id", "source_id", "lang_id", "tag_ids"} | ||
| param = env["ir.config_parameter"].sudo().get_param("crm.pls_fields") | ||
| if param: | ||
| current = set(param.split(",")) | ||
| merged = current | new_fields | ||
| env["ir.config_parameter"].sudo().set_param( | ||
| "crm.pls_fields", ",".join(sorted(merged)) | ||
| ) | ||
|
|
||
|
|
||
| def _load_data_stage_colors(env): | ||
| # Only color the default stages that still exist, | ||
| # skip any the user deleted in v18. | ||
| colors = { | ||
| "stage_lead1": 11, | ||
| "stage_lead2": 5, | ||
| "stage_lead3": 8, | ||
| "stage_lead4": 10, | ||
| } | ||
| for xml_id, color in colors.items(): | ||
| record = env.ref(f"crm.{xml_id}", raise_if_not_found=False) | ||
| if record: | ||
| record.color = color | ||
|
|
||
|
|
||
| @openupgrade.migrate() | ||
| def migrate(env, version): | ||
|
eduezerouali-tecnativa marked this conversation as resolved.
|
||
| # Do not load noupdate_changes.xml: every entry is problematic (deleted stages, | ||
| # customized pls param). the necessary changes are done manually in | ||
| # the _load_data_* functions below | ||
|
|
||
| openupgrade.m2o_to_x2m(env.cr, env["crm.stage"], "crm_stage", "team_ids", "team_id") | ||
| _load_data_stage_colors(env) | ||
| _load_data_pls_fields_param(env) | ||
| _migrate_lead_mobile(env) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,36 @@ | ||||||||||
| # Copyright 2026 Tecnativa - Eduardo Ezerouali | ||||||||||
| # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||||||||||
| from openupgradelib import openupgrade | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def _won_status_set(env): | ||||||||||
| # Precreate won_status and set with sql | ||||||||||
| openupgrade.add_columns( | ||||||||||
| env, | ||||||||||
| [ | ||||||||||
| ("crm.lead", "won_status", "selection", None, "crm_lead"), | ||||||||||
| ], | ||||||||||
|
Comment on lines
+10
to
+12
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| ) | ||||||||||
| openupgrade.logged_query( | ||||||||||
| env.cr, | ||||||||||
| """ | ||||||||||
| UPDATE crm_lead cl | ||||||||||
| SET won_status = CASE | ||||||||||
| WHEN cl.probability = 100 | ||||||||||
| AND cs.is_won = TRUE | ||||||||||
| THEN 'won' | ||||||||||
| WHEN cl.active = FALSE | ||||||||||
| AND cl.probability = 0 | ||||||||||
| THEN 'lost' | ||||||||||
| ELSE 'pending' | ||||||||||
| END | ||||||||||
| FROM crm_lead cl2 | ||||||||||
| LEFT JOIN crm_stage cs ON cs.id = cl2.stage_id | ||||||||||
| WHERE cl2.id = cl.id | ||||||||||
|
Comment on lines
+17
to
+29
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As we are already initializing all of them to "pending", just do 2 updates for the rest (note the changes in the boolean conditions): |
||||||||||
| """, | ||||||||||
| ) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| @openupgrade.migrate() | ||||||||||
| def migrate(env, version): | ||||||||||
| _won_status_set(env) | ||||||||||
37 changes: 37 additions & 0 deletions
37
openupgrade_scripts/scripts/crm/19.0.1.9/upgrade_analysis_work.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| ---Models in module 'crm'--- | ||
| ---Fields in module 'crm'--- | ||
| crm / crm.lead / commercial_partner_id (many2one): NEW relation: res.partner, hasdefault: compute, stored: False | ||
| # NOTHING TO DO | ||
|
|
||
| crm / crm.lead / mobile (char) : DEL | ||
| # 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 | ||
|
|
||
| crm / crm.lead / title (many2one) : DEL relation: res.partner.title | ||
| # NOTHING TO DO | ||
|
|
||
| crm / crm.lead / won_status (selection) : NEW selection_keys: ['lost', 'pending', 'won'], isfunction: function, stored | ||
| # DONE: pre-migration: pre-created column and set it to prevent massive computation | ||
|
|
||
| crm / crm.lead.scoring.frequency.field / color (integer) : NEW hasdefault: default | ||
| crm / crm.stage / color (integer) : NEW | ||
| # NOTHING TO DO | ||
|
|
||
| crm / crm.stage / rotting_threshold_days (integer): NEW hasdefault: default | ||
| # NOTHING TO DO: default is set to 0 to keep old behavoir | ||
|
|
||
| crm / crm.stage / team_id (many2one) : DEL relation: crm.team | ||
| crm / crm.stage / team_ids (many2many) : NEW relation: crm.team | ||
| # Done: post-migration: Transform m2o to m2m | ||
|
|
||
| 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 | ||
| # NOTHING TO DO | ||
|
|
||
| ---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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can be done in one shot: