Skip to content

Commit

Permalink
Merge pull request #426 from Tecnativa/15.0-imp-commission-migration-…
Browse files Browse the repository at this point in the history
…scripts

[15.0][OU-ADD] commission: Migration scripts
  • Loading branch information
pedrobaeza authored Aug 10, 2023
2 parents a5db7ef + 2619d22 commit 74d1760
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
22 changes: 22 additions & 0 deletions commission/migrations/15.0.2.0.0/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2023 Tecnativa - David Vidal
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from openupgradelib import openupgrade


@openupgrade.migrate()
def migrate(env, version):
"""Convert the former `agent_line` m2m relation in `commission.line.mixin` into
the new `settlement_line_ids` o2m relation."""
openupgrade.logged_query(
env.cr,
"""
UPDATE commission_settlement_line
SET invoice_agent_line_id = sal_rel.agent_line_id
FROM (
SELECT DISTINCT ON (agent_line_id) agent_line_id, settlement_id
FROM settlement_agent_line_rel
ORDER BY agent_line_id
) AS sal_rel
WHERE id = sal_rel.settlement_id
""",
)
49 changes: 49 additions & 0 deletions commission/migrations/15.0.2.0.0/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright 2023 Tecnativa - David Vidal
# Copyright 2023 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from openupgradelib import openupgrade

table_renames = [
("sale_commission", "commission"),
("sale_commission_settlement", "commission_settlement"),
("sale_commission_make_invoice", "commission_make_invoice"),
("sale_commission_settlement_line", "commission_settlement_line"),
("sale_commission_make_settle", "commission_make_settle"),
]
model_renames = [
("sale.commission", "commission"),
("sale.commission.mixin", "commission.mixin"),
("sale.commission.line.mixin", "commission.line.mixin"),
("sale.commission.settlement", "commission.settlement"),
("sale.commission.make.invoice", "commission.make.invoice"),
("sale.commission.settlement.line", "commission.settlement.line"),
("sale.commission.make.settle", "commission.make.settle"),
]


def _handle_settlement_line_commission_id(env):
"""On the new version, this field is computed stored, but the compute method
doesn't resolve correctly the link here (as it's handled in `account_commission`),
so we pre-create the column and fill it properly according old expected data.
"""
openupgrade.logged_query(
env.cr, "ALTER TABLE commission_settlement_line ADD commission_id int4"
)
openupgrade.logged_query(
env.cr,
"""
UPDATE commission_settlement_line csl
SET commission_id = aila.commission_id
FROM settlement_agent_line_rel rel
JOIN account_invoice_line_agent aila ON aila.id = rel.agent_line_id
WHERE rel.settlement_id = csl.id
AND csl.commission_id IS NULL
""",
)


@openupgrade.migrate(no_version=True)
def migrate(env, version):
openupgrade.rename_tables(env.cr, table_renames)
openupgrade.rename_models(env.cr, model_renames)
_handle_settlement_line_commission_id(env)

0 comments on commit 74d1760

Please sign in to comment.