From 5cd321c09ceaa22b6f2b70e011e285014367a37c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20=C4=90=E1=BA=A1i=20D=C6=B0=C6=A1ng?= Date: Thu, 13 Jun 2024 13:31:23 +0700 Subject: [PATCH 1/3] [OU-ADD] analytic: migration to 17.0 --- docsource/modules160-170.rst | 2 +- .../analytic/17.0.1.1/post-migration.py | 75 +++++++++++++++++++ .../analytic/17.0.1.1/pre-migration.py | 40 ++++++++++ .../17.0.1.1/upgrade_analysis_work.txt | 26 +++++++ 4 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 openupgrade_scripts/scripts/analytic/17.0.1.1/post-migration.py create mode 100644 openupgrade_scripts/scripts/analytic/17.0.1.1/pre-migration.py create mode 100644 openupgrade_scripts/scripts/analytic/17.0.1.1/upgrade_analysis_work.txt diff --git a/docsource/modules160-170.rst b/docsource/modules160-170.rst index 39b03c8be720..773334c81b32 100644 --- a/docsource/modules160-170.rst +++ b/docsource/modules160-170.rst @@ -40,7 +40,7 @@ Module coverage 16.0 -> 17.0 +---------------------------------------------------+----------------------+-------------------------------------------------+ | account_tax_python | | | +---------------------------------------------------+----------------------+-------------------------------------------------+ -| analytic | | | +| analytic | Done | | +---------------------------------------------------+----------------------+-------------------------------------------------+ | |del| association | |Merged into membership. | +---------------------------------------------------+----------------------+-------------------------------------------------+ diff --git a/openupgrade_scripts/scripts/analytic/17.0.1.1/post-migration.py b/openupgrade_scripts/scripts/analytic/17.0.1.1/post-migration.py new file mode 100644 index 000000000000..dd88553c7243 --- /dev/null +++ b/openupgrade_scripts/scripts/analytic/17.0.1.1/post-migration.py @@ -0,0 +1,75 @@ +# Copyright 2024 Viindoo Technology Joint Stock Company (Viindoo) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from openupgradelib import openupgrade + +_deleted_xml_records = ["analytic.analytic_plan_comp_rule"] + + +def _analytic_line_create_x_plan_column(env): + """ + This method set system parameter for project analytic plan + and create dynamic field on analytic items using + '_sync_plan_column' method + """ + project_plan = ( + env.ref("analytic.analytic_plan_projects", raise_if_not_found=False) + or env["account.analytic.plan"] + ) + if project_plan: + env["ir.config_parameter"].set_param( + "analytic.project_plan", str(project_plan.id) + ) + plans_to_create_fields = env["account.analytic.plan"].search([]) + (plans_to_create_fields - project_plan)._sync_plan_column() + for plan in plans_to_create_fields - project_plan: + if plan.parent_id: + continue + column = plan._strict_column_name() + openupgrade.logged_query( + env.cr, + f""" + UPDATE account_analytic_line + SET {column} = account_id, + account_id = NULL + WHERE plan_id = {plan.id}; + """, + ) + + +def _analytic_plan_update_applicability_into_property(env): + """ + Manually create ir.property for default_applicability of account.analytic.plan + """ + vals_list = [] + field_id = ( + env["ir.model.fields"]._get("account.analytic.plan", "default_applicability").id + ) + env.cr.execute( + """ + SELECT id, default_applicability, company_id FROM account_analytic_plan + WHERE default_applicability != 'optional' + """ + ) + for plan_id, default_applicability, company_id in env.cr.fetchall(): + vals_list.append( + { + "fields_id": field_id, + "company_id": company_id, + "res_id": "account.analytic.plan,%s" % plan_id, + "name": "default_applicability", + "value": default_applicability, + "type": "selection", + } + ) + if vals_list: + env["ir.property"].create(vals_list) + + +@openupgrade.migrate() +def migrate(env, version): + openupgrade.delete_records_safely_by_xml_id( + env, + _deleted_xml_records, + ) + _analytic_line_create_x_plan_column(env) + _analytic_plan_update_applicability_into_property(env) diff --git a/openupgrade_scripts/scripts/analytic/17.0.1.1/pre-migration.py b/openupgrade_scripts/scripts/analytic/17.0.1.1/pre-migration.py new file mode 100644 index 000000000000..f542950d4c43 --- /dev/null +++ b/openupgrade_scripts/scripts/analytic/17.0.1.1/pre-migration.py @@ -0,0 +1,40 @@ +# Copyright 2024 Viindoo Technology Joint Stock Company (Viindoo) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from openupgradelib import openupgrade + + +def _fill_config_parameter_analytic_project_plan(env): + env["ir.config_parameter"].set_param("analytic.project_plan", "1") + + +def _analytic_applicability_fill_company_id(env): + openupgrade.logged_query( + env.cr, + """ + ALTER TABLE account_analytic_applicability + ADD COLUMN IF NOT EXISTS company_id INTEGER; + """, + ) + openupgrade.logged_query( + env.cr, + """ + UPDATE account_analytic_applicability t1 + SET company_id = t2.company_id + FROM account_analytic_plan t2 + WHERE t1.analytic_plan_id = t2.id + """, + ) + + +@openupgrade.migrate() +def migrate(env, version): + _fill_config_parameter_analytic_project_plan(env) + _analytic_applicability_fill_company_id(env) + # Drop triagram index on name column of account.analytic.account + # to avoid error when loading registry, it will be recreated + openupgrade.logged_query( + env.cr, + """ + DROP INDEX IF EXISTS account_analytic_account_name_index; + """, + ) diff --git a/openupgrade_scripts/scripts/analytic/17.0.1.1/upgrade_analysis_work.txt b/openupgrade_scripts/scripts/analytic/17.0.1.1/upgrade_analysis_work.txt new file mode 100644 index 000000000000..32936487c9ca --- /dev/null +++ b/openupgrade_scripts/scripts/analytic/17.0.1.1/upgrade_analysis_work.txt @@ -0,0 +1,26 @@ +---Models in module 'analytic'--- +---Fields in module 'analytic'--- +analytic / account.analytic.account / message_main_attachment_id (many2one): DEL relation: ir.attachment +analytic / account.analytic.account / root_plan_id (many2one) : not a function anymore +analytic / account.analytic.account / root_plan_id (many2one) : now related +analytic / account.analytic.plan / _order : _order is now 'sequence asc, id' ('complete_name asc') +analytic / account.analytic.plan / sequence (integer) : NEW hasdefault: default +# NOTHING TO DO + +analytic / account.analytic.applicability / company_id (many2one) : NEW relation: res.company, hasdefault: default +analytic / account.analytic.plan / company_id (many2one) : DEL relation: res.company +# DONE pre-migration: create column and fill value using company in plan_id + +analytic / account.analytic.line / plan_id (many2one) : DEL relation: account.analytic.plan +# DONE post-migration: create dynamic x_plan_id column using '_sync_plan_column' method in analytic.plan, pr: https://github.com/odoo/odoo/pull/139225 + +analytic / account.analytic.plan / default_applicability (selection): not stored anymore +# DONE post-migration: create ir.property if default_applicability != 'optional' + +---XML records in module 'analytic'--- +NEW account.analytic.plan: analytic.analytic_plan_projects (noupdate) +NEW ir.rule: analytic.analytic_applicability_comp_rule (noupdate) +# NOTHING TO DO + +DEL ir.rule: analytic.analytic_plan_comp_rule (noupdate) +# DONE post-migration: delete xml-id From 9073f86a0e62b90af90e9e9bb3fc886b9086c6e1 Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Mon, 21 Oct 2024 13:50:26 +0200 Subject: [PATCH 2/3] [OU-IMP] analytic: use ir.property api to set properties and set them on all companies also save account.analytic.plan#company_id for posterity and apply noupdate changes --- .../analytic/17.0.1.1/post-migration.py | 29 +++++++------------ .../analytic/17.0.1.1/pre-migration.py | 5 ++++ 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/openupgrade_scripts/scripts/analytic/17.0.1.1/post-migration.py b/openupgrade_scripts/scripts/analytic/17.0.1.1/post-migration.py index dd88553c7243..59e6298a7f0f 100644 --- a/openupgrade_scripts/scripts/analytic/17.0.1.1/post-migration.py +++ b/openupgrade_scripts/scripts/analytic/17.0.1.1/post-migration.py @@ -38,35 +38,28 @@ def _analytic_line_create_x_plan_column(env): def _analytic_plan_update_applicability_into_property(env): """ - Manually create ir.property for default_applicability of account.analytic.plan + Create ir.property for default_applicability of account.analytic.plan + in all companies as the company_id field was shifted from account.analytic.plan + to account.analytic.applicability """ - vals_list = [] - field_id = ( - env["ir.model.fields"]._get("account.analytic.plan", "default_applicability").id - ) env.cr.execute( """ - SELECT id, default_applicability, company_id FROM account_analytic_plan + SELECT id, default_applicability FROM account_analytic_plan WHERE default_applicability != 'optional' """ ) - for plan_id, default_applicability, company_id in env.cr.fetchall(): - vals_list.append( - { - "fields_id": field_id, - "company_id": company_id, - "res_id": "account.analytic.plan,%s" % plan_id, - "name": "default_applicability", - "value": default_applicability, - "type": "selection", - } + values = dict(env.cr.fetchall()) + for company in env["res.company"].search([]): + env["ir.property"].with_company(company)._set_multi( + "default_applicability", + "account.analytic.plan", + values, ) - if vals_list: - env["ir.property"].create(vals_list) @openupgrade.migrate() def migrate(env, version): + openupgrade.load_data(env, "analytic", "17.0.1.1/noupdate_changes.xml") openupgrade.delete_records_safely_by_xml_id( env, _deleted_xml_records, diff --git a/openupgrade_scripts/scripts/analytic/17.0.1.1/pre-migration.py b/openupgrade_scripts/scripts/analytic/17.0.1.1/pre-migration.py index f542950d4c43..513fc404e63e 100644 --- a/openupgrade_scripts/scripts/analytic/17.0.1.1/pre-migration.py +++ b/openupgrade_scripts/scripts/analytic/17.0.1.1/pre-migration.py @@ -38,3 +38,8 @@ def migrate(env, version): DROP INDEX IF EXISTS account_analytic_account_name_index; """, ) + # Save company_id field of analytic plans for modules reinstating this + # to pick up + openupgrade.copy_columns( + env.cr, {"account_analytic_plan": [("company_id", None, None)]} + ) From 14fa9dd206f4d6ac47c82fd5ec40a00e5c5b2049 Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Thu, 7 Nov 2024 17:02:03 +0100 Subject: [PATCH 3/3] [OU-FIX] analytic: Finish migration scripts - Set account.analytic.plan sequence according previous order (by id) - Don't put default value for account.analytic.plan.applicability~company_id for preserving previous behavior. - Better work file explanations. --- .../analytic/17.0.1.1/post-migration.py | 24 +++++++++++++++++++ .../analytic/17.0.1.1/pre-migration.py | 11 ++------- .../17.0.1.1/upgrade_analysis_work.txt | 14 +++++++---- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/openupgrade_scripts/scripts/analytic/17.0.1.1/post-migration.py b/openupgrade_scripts/scripts/analytic/17.0.1.1/post-migration.py index 59e6298a7f0f..516c6950b600 100644 --- a/openupgrade_scripts/scripts/analytic/17.0.1.1/post-migration.py +++ b/openupgrade_scripts/scripts/analytic/17.0.1.1/post-migration.py @@ -1,10 +1,33 @@ # Copyright 2024 Viindoo Technology Joint Stock Company (Viindoo) +# Copyright 2024 Hunki enterprises - Holger Brunn +# Copyright 2024 Tecnativa - Pedro M. Baeza # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from openupgradelib import openupgrade _deleted_xml_records = ["analytic.analytic_plan_comp_rule"] +def _adjust_analytic_plan_sequence(env): + """As now the order is by sequence, and on v16 it was the id, we have to assign the + sequence numbers to preserve the previous order by default, for not having any + undesired modification. Once in v17, you can redefine the order to your + choice if desired. + """ + openupgrade.logged_query( + env.cr, + """ + UPDATE account_analytic_plan aap + SET sequence = sub.row_number + FROM ( + SELECT id, row_number() + OVER (PARTITION BY True order by id) + FROM account_analytic_plan + ) as sub + WHERE sub.id = aap.id + """, + ) + + def _analytic_line_create_x_plan_column(env): """ This method set system parameter for project analytic plan @@ -59,6 +82,7 @@ def _analytic_plan_update_applicability_into_property(env): @openupgrade.migrate() def migrate(env, version): + _adjust_analytic_plan_sequence(env) openupgrade.load_data(env, "analytic", "17.0.1.1/noupdate_changes.xml") openupgrade.delete_records_safely_by_xml_id( env, diff --git a/openupgrade_scripts/scripts/analytic/17.0.1.1/pre-migration.py b/openupgrade_scripts/scripts/analytic/17.0.1.1/pre-migration.py index 513fc404e63e..c5587709d993 100644 --- a/openupgrade_scripts/scripts/analytic/17.0.1.1/pre-migration.py +++ b/openupgrade_scripts/scripts/analytic/17.0.1.1/pre-migration.py @@ -1,4 +1,6 @@ # Copyright 2024 Viindoo Technology Joint Stock Company (Viindoo) +# Copyright 2024 Hunki enterprises - Holger Brunn +# Copyright 2024 Tecnativa - Pedro M. Baeza # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from openupgradelib import openupgrade @@ -15,15 +17,6 @@ def _analytic_applicability_fill_company_id(env): ADD COLUMN IF NOT EXISTS company_id INTEGER; """, ) - openupgrade.logged_query( - env.cr, - """ - UPDATE account_analytic_applicability t1 - SET company_id = t2.company_id - FROM account_analytic_plan t2 - WHERE t1.analytic_plan_id = t2.id - """, - ) @openupgrade.migrate() diff --git a/openupgrade_scripts/scripts/analytic/17.0.1.1/upgrade_analysis_work.txt b/openupgrade_scripts/scripts/analytic/17.0.1.1/upgrade_analysis_work.txt index 32936487c9ca..098e371ba9a8 100644 --- a/openupgrade_scripts/scripts/analytic/17.0.1.1/upgrade_analysis_work.txt +++ b/openupgrade_scripts/scripts/analytic/17.0.1.1/upgrade_analysis_work.txt @@ -1,21 +1,25 @@ ---Models in module 'analytic'--- ---Fields in module 'analytic'--- analytic / account.analytic.account / message_main_attachment_id (many2one): DEL relation: ir.attachment +# NOTHING TO DO: Feature mainly not used + analytic / account.analytic.account / root_plan_id (many2one) : not a function anymore analytic / account.analytic.account / root_plan_id (many2one) : now related +# NOTHING TO DO: The related is equivalent to the previous compute + analytic / account.analytic.plan / _order : _order is now 'sequence asc, id' ('complete_name asc') analytic / account.analytic.plan / sequence (integer) : NEW hasdefault: default -# NOTHING TO DO +# DONE: post-migration: Set sequence for following the same order than in previous version analytic / account.analytic.applicability / company_id (many2one) : NEW relation: res.company, hasdefault: default analytic / account.analytic.plan / company_id (many2one) : DEL relation: res.company -# DONE pre-migration: create column and fill value using company in plan_id +# DONE: pre-migration: create column and let empty value, as it's what preserves the previous behavior analytic / account.analytic.line / plan_id (many2one) : DEL relation: account.analytic.plan -# DONE post-migration: create dynamic x_plan_id column using '_sync_plan_column' method in analytic.plan, pr: https://github.com/odoo/odoo/pull/139225 +# DONE: post-migration: create dynamic x_plan_id column using '_sync_plan_column' method in analytic.plan, pr: https://github.com/odoo/odoo/pull/139225 analytic / account.analytic.plan / default_applicability (selection): not stored anymore -# DONE post-migration: create ir.property if default_applicability != 'optional' +# DONE: post-migration: create ir.property if default_applicability != 'optional' ---XML records in module 'analytic'--- NEW account.analytic.plan: analytic.analytic_plan_projects (noupdate) @@ -23,4 +27,4 @@ NEW ir.rule: analytic.analytic_applicability_comp_rule (noupdate) # NOTHING TO DO DEL ir.rule: analytic.analytic_plan_comp_rule (noupdate) -# DONE post-migration: delete xml-id +# DONE: post-migration: try to delete safely the record