Skip to content

Commit

Permalink
[OU-ADD] mrp: Migration scripts (continuation)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobaeza committed Mar 27, 2022
1 parent 283a6a7 commit a09e412
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 19 deletions.
22 changes: 11 additions & 11 deletions openupgrade_scripts/scripts/mrp/14.0.2.0/noupdate_changes.xml
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
<?xml version='1.0' encoding='utf-8'?>
<odoo>
<record id="mrp_bom_byproduct_rule" model="ir.rule">
<!-- <field name="global"/>-->
<!-- <record id="mrp_bom_byproduct_rule" model="ir.rule">
<field name="global"/>
</record>
<record id="mrp_bom_line_rule" model="ir.rule">
<!-- <field name="global"/>-->
<field name="global"/>
</record>
<record id="mrp_bom_rule" model="ir.rule">
<!-- <field name="global"/>-->
<field name="global"/>
</record>
<record id="mrp_production_rule" model="ir.rule">
<!-- <field name="global"/>-->
<field name="global"/>
</record>
<record id="mrp_routing_workcenter_rule" model="ir.rule">
<!-- <field name="global"/>-->
<field name="global"/>
</record>
<record id="mrp_unbuild_rule" model="ir.rule">
<!-- <field name="global"/>-->
<field name="global"/>
</record>
<record id="mrp_workcenter_productivity" model="ir.rule">
<!-- <field name="global"/>-->
<field name="global"/>
</record>
<record id="mrp_workcenter_rule" model="ir.rule">
<!-- <field name="global"/>-->
<field name="global"/>
</record>
<record id="mrp_workorder_rule" model="ir.rule">
<!-- <field name="global"/>-->
</record>
<field name="global"/>
</record> -->
</odoo>
38 changes: 38 additions & 0 deletions openupgrade_scripts/scripts/mrp/14.0.2.0/post-migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,49 @@ def map_mrp_production_state_planned(env):
env["mrp.production"].search([("state", "=", "planned")])._compute_state()


def generate_workorders_for_draft_orders(env):
"""Recreate workorders for draft production orders, as in v13, this is only done
later when planning production.
"""
env.cr.execute(
"SELECT id, routing_id FROM mrp_production "
"WHERE state = 'draft' AND routing_id IS NOT NULL"
)
for production_id, routing_id in env.cr.fetchall():
production = env["mrp.production"].browse(production_id)
workorders_values = []
env.cr.execute(
"""
SELECT mrw.id, mrw.name, mrw.workcenter_id
FROM mrp_routing_workcenter mrw
WHERE mrw.routing_id = %s AND mrw.bom_id = %s
""",
(routing_id, production.bom_id.id),
)
# TODO: If there are phantom BoMs with other routings, it won't be covered
for operation_id, operation_name, workcenter_id in env.cr.fetchall():
workorders_values += [
{
"name": operation_name,
"production_id": production_id,
"workcenter_id": workcenter_id,
"product_uom_id": production.product_uom_id.id,
"operation_id": operation_id,
"state": "pending",
"consumption": production.consumption,
}
]
workorders = production.workorder_ids.create(workorders_values)
for workorder in workorders:
workorder.duration_expected = workorder._get_duration_expected()


@openupgrade.migrate()
def migrate(env, version):
merge_priorities(env)
map_stock_move_line_lot_produced_ids(env)
map_mrp_production_state_planned(env)
generate_workorders_for_draft_orders(env)
openupgrade.load_data(env.cr, "mrp", "14.0.2.0/noupdate_changes.xml")
openupgrade.delete_records_safely_by_xml_id(
env, ["mrp.mrp_routing_rule", "mrp.sequence_mrp_route"]
Expand Down
28 changes: 28 additions & 0 deletions openupgrade_scripts/scripts/mrp/14.0.2.0/pre-migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,32 @@
}


def switch_flexible_consumption_to_warning(env):
"""For benefiting from this new feature."""
openupgrade.logged_query(
env.cr,
"UPDATE mrp_bom SET consumption = 'warning' WHERE consumption='flexible'",
)
openupgrade.logged_query(
env.cr,
"UPDATE mrp_workorder SET consumption = 'warning' WHERE consumption='flexible'",
)


def fill_mrp_production__consumption(env):
openupgrade.logged_query(
env.cr, "ALTER TABLE mrp_production ADD consumption VARCHAR"
)
openupgrade.logged_query(
env.cr,
"""UPDATE mrp_production mp
SET consumption = mb.consumption
FROM mrp_bom mb
WHERE mb.id = mp.bom_id
""",
)


def fill_mrp_routing_workcenter_bom_id(env):
openupgrade.logged_query(
env.cr,
Expand Down Expand Up @@ -104,4 +130,6 @@ def fill_mrp_routing_workcenter_bom_id(env):
@openupgrade.migrate()
def migrate(env, version):
openupgrade.copy_columns(env.cr, _column_copies)
switch_flexible_consumption_to_warning(env)
fill_mrp_production__consumption(env)
fill_mrp_routing_workcenter_bom_id(env)
19 changes: 11 additions & 8 deletions openupgrade_scripts/scripts/mrp/14.0.2.0/upgrade_analysis_work.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ new model mrp.production.backorder [transient]
new model mrp.production.backorder.line [transient]
# NOTHING TO DO: abstract + transient models

obsolete model mrp.routing
obsolete model mrp.workorder.line
# NOTHING TO DO: obsolete models

Expand All @@ -24,24 +23,28 @@ mrp / mrp.document / website_url (char) : previo

mrp / mrp.bom / consumption (selection) : now required, req_default: function
mrp / mrp.bom / consumption (selection) : selection_keys is now '['flexible', 'strict', 'warning']' ('['flexible', 'strict']')
# NOTHING TO DO: new default value 'warning'
mrp / mrp.workorder / consumption (selection) : selection_keys is now '['flexible', 'strict', 'warning']' ('['flexible', 'strict']')
# DONE: pre-migration: Switch 'flexible' to 'warning' for benefiting from this new feature

mrp / mrp.production / consumption (selection) : NEW required, selection_keys: ['flexible', 'strict', 'warning'], req_default: function, hasdefault
mrp / mrp.workorder / consumption (selection) : selection_keys is now '['flexible', 'strict', 'warning']' ('['flexible', 'strict']')
# NOTHING TO DO: new features
# DONE: pre-migration: Init the field with its related BoM value

obsolete model mrp.routing
mrp / mrp.bom / operation_ids (one2many) : NEW relation: mrp.routing.workcenter
mrp / mrp.routing / operation_ids (one2many) : DEL relation: mrp.routing.workcenter
mrp / mrp.routing.workcenter / bom_id (many2one) : NEW relation: mrp.bom
mrp / mrp.routing.workcenter / routing_id (many2one) : DEL relation: mrp.routing, required
# DONE: pre-migration: duplicate mrp.routing.workcenter records and fill bom_id

mrp / mrp.bom / routing_id (many2one) : DEL relation: mrp.routing
# DONE: pre-migration: duplicate mrp.routing.workcenter records per assigned BoM, filling bom_id
# TODO: There can be mrp.routing without assigned BoMs that will be in a limbo after this migration. This will be highlighted through mrp.routing.workcenter having nulls in `bom_id` field. One possible strategy is to create fake BoMs for that routings with a wildcard product.

mrp / mrp.bom.byproduct / routing_id (many2one) : DEL relation: mrp.routing
mrp / mrp.bom.line / routing_id (many2one) : DEL relation: mrp.routing
mrp / mrp.production / routing_id (many2one) : DEL relation: mrp.routing
# NOTHING TO DO: mrp.routing model is obsolete

mrp / mrp.production / routing_id (many2one) : DEL relation: mrp.routing
# DONE: post-migration: Populate workorders for draft production orders from old routing

mrp / mrp.production / _order : _order is now 'priority desc, date_planned_start asc,id' ('date_planned_start asc,id')
# NOTHING TO DO

Expand Down Expand Up @@ -117,7 +120,7 @@ mrp / mrp.workorder.line / qty_reserved (float) : DEL
mrp / mrp.workorder.line / qty_to_consume (float) : DEL
mrp / mrp.workorder.line / raw_workorder_id (many2one) : DEL relation: mrp.workorder
mrp / mrp.workorder / finished_workorder_line_ids (one2many): DEL relation: mrp.workorder.line
# NOTHING TO DO: obsolete model
# TODO: Not clear if there should be any conversion

mrp / procurement.group / mrp_production_ids (one2many) : NEW relation: mrp.production
# NOTHING TO DO: one2many
Expand Down

0 comments on commit a09e412

Please sign in to comment.