-
-
Notifications
You must be signed in to change notification settings - Fork 801
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
5aae93e
commit 3554429
Showing
2 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
src/backend/InvenTree/order/migrations/0105_auto_20241128_0431.py
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,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 | ||
) | ||
] |
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