Skip to content

Commit

Permalink
[MIG] analytic: migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
duong77476-viindoo authored and royle-vietnam committed May 29, 2024
1 parent 4c44562 commit 947d912
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docsource/modules160-170.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Module coverage 16.0 -> 17.0
+---------------------------------------------------+----------------------+-------------------------------------------------+
| account_tax_python | | |
+---------------------------------------------------+----------------------+-------------------------------------------------+
| analytic | | |
| analytic |Done | |
+---------------------------------------------------+----------------------+-------------------------------------------------+
| |del| association | |Merged into membership. |
+---------------------------------------------------+----------------------+-------------------------------------------------+
Expand Down
8 changes: 8 additions & 0 deletions openupgrade_scripts/scripts/account/17.0.1.2/pre-migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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;
""",
)
73 changes: 73 additions & 0 deletions openupgrade_scripts/scripts/analytic/17.0.1.1/post-migration.py
Original file line number Diff line number Diff line change
@@ -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)
35 changes: 35 additions & 0 deletions openupgrade_scripts/scripts/analytic/17.0.1.1/pre-migration.py
Original file line number Diff line number Diff line change
@@ -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;
""",
)
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 947d912

Please sign in to comment.