Data migration for PurchaseOrder and SalesOrder models

This commit is contained in:
Oliver 2021-10-14 17:58:09 +11:00
parent 5c6a7b489c
commit 068b54f666

View File

@ -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
)
]