Skip to content

Commit

Permalink
Merge pull request #832 from Tecnativa/13.0-stock_picking_invoice_lin…
Browse files Browse the repository at this point in the history
…k-mig_scripts

[13.0][OU-ADD] stock_picking_invoice_link: Migration scripts [ci skip]
  • Loading branch information
pedrobaeza committed Apr 15, 2021
2 parents 00761ea + e748e3f commit d75e026
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
39 changes: 39 additions & 0 deletions stock_picking_invoice_link/migrations/13.0.1.0.0/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2021 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
from openupgradelib import openupgrade


@openupgrade.logging()
def insert_invoice_line_stock_move_rel(env):
openupgrade.logged_query(
env.cr,
"""
INSERT INTO stock_move_invoice_line_rel
(invoice_line_id, move_id)
SELECT aml.id, rel.move_id
FROM old_stock_move_invoice_line_rel rel
JOIN account_move_line aml ON aml.old_invoice_line_id = rel.invoice_line_id""",
)


def insert_invoice_picking_rel(env):
# Remove temp table and re-create m2m table through ORM method
openupgrade.logged_query(env.cr, "DROP TABLE account_move_stock_picking_rel")
Move = env["account.move"]
Move._fields["picking_ids"].update_db(Move, False)
# Fill values from existing table
openupgrade.logged_query(
env.cr,
"""
INSERT INTO account_move_stock_picking_rel
(account_move_id, stock_picking_id)
SELECT am.id, rel.stock_picking_id
FROM account_invoice_stock_picking_rel rel
JOIN account_move am ON am.old_invoice_id = rel.account_invoice_id""",
)


@openupgrade.migrate()
def migrate(env, version):
insert_invoice_line_stock_move_rel(env)
insert_invoice_picking_rel(env)
19 changes: 19 additions & 0 deletions stock_picking_invoice_link/migrations/13.0.1.0.0/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2021 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
from openupgradelib import openupgrade

_table_renames = [("stock_move_invoice_line_rel", "old_stock_move_invoice_line_rel")]


@openupgrade.migrate()
def migrate(env, version):
openupgrade.rename_tables(env.cr, _table_renames)
# Remove FKs on old relations for avoiding further problems
openupgrade.lift_constraints(env.cr, _table_renames[0][1], "move_id")
openupgrade.lift_constraints(
env.cr, "account_invoice_stock_picking_rel", "picking_id"
)
# Add temporary table for avoiding the automatic launch of the compute method
openupgrade.logged_query(
env.cr, "CREATE TABLE account_move_stock_picking_rel (temp INTEGER)",
)

0 comments on commit d75e026

Please sign in to comment.