-
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #426 from Tecnativa/15.0-imp-commission-migration-…
…scripts [15.0][OU-ADD] commission: Migration scripts
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
""", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |