diff --git a/InvenTree/order/migrations/0052_auto_20211014_0631.py b/InvenTree/order/migrations/0052_auto_20211014_0631.py new file mode 100644 index 0000000000..93d3f86038 --- /dev/null +++ b/InvenTree/order/migrations/0052_auto_20211014_0631.py @@ -0,0 +1,80 @@ +# Generated by Django 3.2.5 on 2021-10-14 06:31 + +import re + +from django.db import migrations + +def build_refs(apps, schema_editor): + """ + Rebuild the integer "reference fields" for existing Build objects + """ + + print("\n - Rebuilding reference field for PurchaseOrder model...") + + PurchaseOrder = apps.get_model('order', 'purchaseorder') + + n = PurchaseOrder.objects.count() + + for order in PurchaseOrder.objects.all(): + + ref = 0 + + result = re.match(r"^(\d+)", order.reference) + + if result and len(result.groups()) == 1: + try: + ref = int(result.groups()[0]) + except: + ref = 0 + + order.reference_int = ref + order.save() + + print(f" - Updated {n} PurchaseOrder objects") + + print("\n - Rebuilding reference field for SalesOrder model...") + + SalesOrder = apps.get_model('order', 'salesorder') + + n = SalesOrder.objects.count() + + for order in SalesOrder.objects.all(): + + ref = 0 + + result = re.match(r"^(\d+)", order.reference) + + if result and len(result.groups()) == 1: + try: + ref = int(result.groups()[0]) + except: + ref = 0 + + order.reference_int = ref + order.save() + + print(f" - Updated {n} SalesOrder objects") + + print(f" - COMPLETE! -") + + +def unbuild_refs(apps, schema_editor): + """ + Provided only for reverse migration compatibility + """ + pass + + +class Migration(migrations.Migration): + + dependencies = [ + ('order', '0051_auto_20211014_0623'), + ] + + + operations = [ + migrations.RunPython( + build_refs, + reverse_code=unbuild_refs + ) + ]