Skip to content

Commit

Permalink
[MIG] stock: migration script
Browse files Browse the repository at this point in the history
  • Loading branch information
sang250399 committed Jun 27, 2022
1 parent 2aa22ab commit 0c7b945
Show file tree
Hide file tree
Showing 4 changed files with 445 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docsource/modules140-150.rst
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ Module coverage 14.0 -> 15.0
+-------------------------------------------------+----------------------+-------------------------------------------------+
| social_media | |No DB layout changes. |
+-------------------------------------------------+----------------------+-------------------------------------------------+
| stock | | |
| stock |Done | |
+-------------------------------------------------+----------------------+-------------------------------------------------+
| stock_account | | |
+-------------------------------------------------+----------------------+-------------------------------------------------+
Expand Down
128 changes: 128 additions & 0 deletions openupgrade_scripts/scripts/stock/15.0.1.1/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
from openupgradelib import openupgrade


def _fill_product_template_detailed_type(env):
openupgrade.logged_query(
env.cr,
"""
UPDATE product_template
SET detailed_type = type
WHERE type = 'product'
""",
)


def _fill_stock_move_is_inventory(env):
openupgrade.logged_query(
env.cr,
"""
UPDATE stock_move
SET is_inventory = True
WHERE inventory_id IS NOT NULL;
""",
)


def _fill_stock_picking_type_print_label(env):
openupgrade.logged_query(
env.cr,
"""
UPDATE stock_picking_type
SET print_label = True
WHERE code = 'outgoing';
""",
)


def _create_default_return_type_for_all_warehouses(env):
all_warehouses = env["stock.warehouse"].with_context(active_test=False).search([])
for wh in all_warehouses:
# choose the next available color for the operation types of this warehouse
all_used_colors = [
res["color"]
for res in env["stock.picking.type"].search_read(
[("warehouse_id", "!=", False), ("color", "!=", False)],
["color"],
order="color",
)
]
available_colors = [zef for zef in range(0, 12) if zef not in all_used_colors]
color = available_colors[0] if available_colors else 0

sequence_data = wh._get_sequence_values()
# suit for each warehouse: reception, internal, pick, pack, ship
max_sequence = env["stock.picking.type"].search_read(
[("sequence", "!=", False)],
["sequence"],
limit=1,
order="sequence desc",
)
max_sequence = max_sequence and max_sequence[0]["sequence"] or 0

values = wh._get_picking_type_update_values()["return_type_id"]
create_data, _ = wh._get_picking_type_create_values(max_sequence)

values.update(create_data["return_type_id"])
sequence = env["ir.sequence"].create(sequence_data["return_type_id"])
values.update(
warehouse_id=wh.id,
color=color,
sequence_id=sequence.id,
sequence=max_sequence + 1,
)
# create return picking type
return_type_id = env["stock.picking.type"].create(values).id
# update return pikcing type for warehouse
wh.write({"return_type_id": return_type_id})
wh.out_type_id.write({"return_picking_type_id": return_type_id})
wh.in_type_id.write({"return_picking_type_id": wh.out_type_id.id})


def _fill_stock_quant_last_inventory_date(env):
openupgrade.logged_query(
env.cr,
"""
WITH sub_tmpl AS (
SELECT sl.id as sl_id, max(sml.date) as sml_date
FROM stock_location sl
JOIN stock_move_line sml ON (
sml.company_id = sl.company_id
AND sml.state = 'done'
AND (sml.location_id = sl.id
OR sml.location_dest_id = sl.id))
JOIN stock_move sm ON (sml.move_id = sm.id AND sm.is_inventory = true)
WHERE sl.usage in ('internal', 'transit')
GROUP BY sl.id
)
UPDATE stock_location sl
SET last_inventory_date = sub_tmpl.sml_date
FROM sub_tmpl
WHERE sub_tmpl.sl_id = sl.id
""",
)


@openupgrade.migrate()
def migrate(env, version):
openupgrade.load_data(env.cr, "stock", "15.0.1.1/noupdate_changes.xml")
openupgrade.delete_record_translations(
env.cr,
"stock",
["mail_template_data_delivery_confirmation"],
)
# try delete noupdate records
openupgrade.delete_records_safely_by_xml_id(
env,
[
"stock.stock_inventory_comp_rule",
"stock.stock_inventory_line_comp_rule",
"stock.sequence_tracking",
],
)

_fill_product_template_detailed_type(env)

_fill_stock_move_is_inventory(env)
_fill_stock_picking_type_print_label(env)
_create_default_return_type_for_all_warehouses(env)
_fill_stock_quant_last_inventory_date(env)
42 changes: 42 additions & 0 deletions openupgrade_scripts/scripts/stock/15.0.1.1/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from openupgradelib import openupgrade


def _create_column_for_avoiding_automatic_computing(env):
openupgrade.logged_query(
env.cr,
"""
ALTER TABLE stock_move
ADD COLUMN IF NOT EXISTS reservation_date date;
""",
)


def _fill_stock_quant_package_name_if_null(env):
openupgrade.logged_query(
env.cr,
"""
UPDATE stock_quant_package
SET name = 'Unknown Pack'
WHERE name IS NULL;
""",
)


def _fill_stock_quant_in_date(env):
openupgrade.logged_query(
env.cr,
"""
UPDATE stock_quant
SET in_date = create_date
WHERE in_date IS NULL;
""",
)


@openupgrade.migrate()
def migrate(env, version):
openupgrade.convert_field_to_html(env.cr, "stock_location", "comment", "comment")
openupgrade.convert_field_to_html(env.cr, "stock_picking", "note", "note")
_create_column_for_avoiding_automatic_computing(env)
_fill_stock_quant_package_name_if_null(env)
_fill_stock_quant_in_date(env)
Loading

0 comments on commit 0c7b945

Please sign in to comment.