From 5c6a7b489cdcb2fa2ea7e759e24db40b3e640c17 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 14 Oct 2021 17:54:46 +1100 Subject: [PATCH] Data migration for the Build model --- .../migrations/0032_auto_20211014_0632.py | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 InvenTree/build/migrations/0032_auto_20211014_0632.py diff --git a/InvenTree/build/migrations/0032_auto_20211014_0632.py b/InvenTree/build/migrations/0032_auto_20211014_0632.py new file mode 100644 index 0000000000..6c84c24526 --- /dev/null +++ b/InvenTree/build/migrations/0032_auto_20211014_0632.py @@ -0,0 +1,57 @@ +# Generated by Django 3.2.5 on 2021-10-14 06:32 + +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 BuildOrder model...") + + BuildOrder = apps.get_model('build', 'build') + + n = BuildOrder.objects.count() + + for build in BuildOrder.objects.all(): + + ref = 0 + + result = re.match(r"^(\d+)", build.reference) + + if result and len(result.groups()) == 1: + try: + ref = int(result.groups()[0]) + except: + ref = 0 + + build.reference_int = ref + build.save() + + print(f" - Updated {n} BuildOrder objects") + print(f" - COMPLETE! -") + +def unbuild_refs(apps, schema_editor): + """ + Provided only for reverse migration compatibility + """ + pass + + +class Migration(migrations.Migration): + + atomic = False + + dependencies = [ + ('build', '0031_build_reference_int'), + ] + + operations = [ + migrations.RunPython( + build_refs, + reverse_code=unbuild_refs + ) + ]