Coverage for src/backend/InvenTree/order/migrations/0105_auto_20241128_0431.py: 47%
24 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
1# Generated by Django 4.2.16 on 2024-11-28 04:31
3from django.db import migrations, connection
6def update_shipment_date(apps, schema_editor):
7 """
8 Update the shipment date for existing SalesOrderAllocation objects
9 """
11 from order.status_codes import SalesOrderStatusGroups
13 SalesOrder = apps.get_model('order', 'SalesOrder')
15 # Find any orders which are "complete" but missing a shipment date
16 orders = SalesOrder.objects.filter(
17 status__in=SalesOrderStatusGroups.COMPLETE,
18 shipment_date__isnull=True
19 )
21 update_count = 0
23 cursor = connection.cursor()
25 for order in orders: 25 ↛ 28line 25 didn't jump to line 28 because the loop on line 25 never started
27 # Check that the shipment date is actually null here
28 assert order.shipment_date is None, f"SalesOrder {order.pk} has non-null shipment_date"
30 # Find the latest shipment date for any associated allocations
31 shipments = order.shipments.filter(shipment_date__isnull=False)
32 latest_shipment = shipments.order_by('-shipment_date').first()
34 if not latest_shipment:
35 continue
37 # Update the order with the new shipment date
38 shipment_date = latest_shipment.shipment_date
40 # Raw SQL to prevent some weird migration "order of operations" issues
41 # Reference: https://github.com/inventree/InvenTree/pull/8814
42 query = f"UPDATE order_salesorder SET shipment_date = '{shipment_date}' WHERE id = {order.pk}"
43 cursor.execute(query)
45 # Fetch the updated object, check that the shipment date has been updated
46 order.refresh_from_db()
47 assert order.shipment_date is not None, f"SalesOrder {order.pk} still has missing shipment_date"
49 update_count += 1
51 if update_count > 0: 51 ↛ 52line 51 didn't jump to line 52 because the condition on line 51 was never true
52 print(f"Updated {update_count} SalesOrder shipment dates")
55class Migration(migrations.Migration):
57 dependencies = [
58 ('order', '0104_alter_returnorderlineitem_quantity'),
59 ]
61 operations = [
62 migrations.RunPython(
63 update_shipment_date,
64 reverse_code=migrations.RunPython.noop
65 )
66 ]