From 947d91236fcda02ef2c8f9a3a246b5b5beb1716c 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: Sat, 25 May 2024 10:37:58 +0700 Subject: [PATCH] [MIG] analytic: migration to 17.0 --- docsource/modules160-170.rst | 2 +- .../scripts/account/17.0.1.2/pre-migration.py | 8 ++ .../analytic/17.0.1.1/post-migration.py | 73 +++++++++++++++++++ .../analytic/17.0.1.1/pre-migration.py | 35 +++++++++ .../17.0.1.1/upgrade_analysis_work.txt | 26 +++++++ 5 files changed, 143 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 26055fa7003b..5718a40d11b5 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/account/17.0.1.2/pre-migration.py b/openupgrade_scripts/scripts/account/17.0.1.2/pre-migration.py index ec94f3972c87..02c221cf76d1 100644 --- a/openupgrade_scripts/scripts/account/17.0.1.2/pre-migration.py +++ b/openupgrade_scripts/scripts/account/17.0.1.2/pre-migration.py @@ -193,3 +193,11 @@ def migrate(env, version): _map_account_report_filter_account_type(env) _map_chart_template_id_to_chart_template(env, "res_company") _map_chart_template_id_to_chart_template(env, "account_report") + # Drop triagram index on name column of account.account + # to avoid error when loading registry, it will be recreated + openupgrade.logged_query( + env.cr, + """ + DROP INDEX IF EXISTS account_account_name_index; + """, + ) 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..ea2aee217f9e --- /dev/null +++ b/openupgrade_scripts/scripts/analytic/17.0.1.1/post-migration.py @@ -0,0 +1,73 @@ +# 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: + 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..a7946d504c81 --- /dev/null +++ b/openupgrade_scripts/scripts/analytic/17.0.1.1/pre-migration.py @@ -0,0 +1,35 @@ +# 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 _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): + _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