Skip to content

Commit

Permalink
SalesOrder Data Migration (#8585)
Browse files Browse the repository at this point in the history
* Add data migration to remedy bug

* Add migration tests

* Remove faulty migration test

* Tweak filter

* Update migration file

---------

Co-authored-by: Matthias Mair <[email protected]>
  • Loading branch information
SchrodingersGat and matmair authored Dec 10, 2024
1 parent 5aae93e commit 3554429
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
52 changes: 52 additions & 0 deletions src/backend/InvenTree/order/migrations/0105_auto_20241128_0431.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Generated by Django 4.2.16 on 2024-11-28 04:31

from django.db import migrations


def update_shipment_date(apps, schema_editor):
"""
Update the shipment date for existing SalesOrderAllocation objects
"""

from order.status_codes import SalesOrderStatusGroups

SalesOrder = apps.get_model('order', 'SalesOrder')

# Find any orders which are "complete" but missing a shipment date
orders = SalesOrder.objects.filter(
status__in=SalesOrderStatusGroups.COMPLETE,
shipment_date__isnull=True
)

updated_orders = 0

for order in orders:
# Find the latest shipment date for any associated allocations
shipments = order.shipments.filter(shipment_date__isnull=False)
latest_shipment = shipments.order_by('-shipment_date').first()

if not latest_shipment:
continue

# Update the order with the new shipment date
order.shipment_date = latest_shipment.shipment_date
order.save()

updated_orders += 1

if updated_orders > 0:
print(f"Updated {updated_orders} SalesOrder objects with missing shipment_date")


class Migration(migrations.Migration):

dependencies = [
('order', '0104_alter_returnorderlineitem_quantity'),
]

operations = [
migrations.RunPython(
update_shipment_date,
reverse_code=migrations.RunPython.noop
)
]
2 changes: 1 addition & 1 deletion src/backend/InvenTree/order/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def prepare(self):

customer = Company.objects.create(
name='My customer',
description='A customer we sell stuff too',
description='A customer we sell stuff to',
is_customer=True,
)

Expand Down

0 comments on commit 3554429

Please sign in to comment.