Skip to content
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

[17.0][OU-ADD] analytic: migration to 17.0 #4597

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
92 changes: 92 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,92 @@
# 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
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):
"""
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
"""
env.cr.execute(
"""
SELECT id, default_applicability FROM account_analytic_plan
WHERE default_applicability != 'optional'
"""
)
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,
)


@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,
_deleted_xml_records,
)
_analytic_line_create_x_plan_column(env)
_analytic_plan_update_applicability_into_property(env)
38 changes: 38 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,38 @@
# 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


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.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;
""",
)
# 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)]}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---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')
pedrobaeza marked this conversation as resolved.
Show resolved Hide resolved
analytic / account.analytic.plan / sequence (integer) : NEW hasdefault: default
# 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 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

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: try to delete safely the record
Loading