2
0
mirror of https://github.com/inventree/InvenTree.git synced 2026-04-04 18:40:55 +00:00

Improve checks for data migrations

This commit is contained in:
Oliver Walters
2025-12-03 13:13:15 +00:00
parent 447e18c1cb
commit f5839fd302
2 changed files with 20 additions and 10 deletions

View File

@@ -20,22 +20,32 @@ def update_parameter(apps, schema_editor):
model="partparameter", model="partparameter",
) )
parmeters_to_update = [] parameters_to_update = []
N = PartParameter.objects.count()
# Update any existing PartParameter object, setting the new fields
for parameter in PartParameter.objects.all(): for parameter in PartParameter.objects.all():
parameter.model_type = part_content_type parameter.model_type = part_content_type
parameter.model_id = parameter.part_id parameter.model_id = parameter.part_id
parmeters_to_update.append(parameter) parameters_to_update.append(parameter)
if len(parmeters_to_update) > 0: if len(parameters_to_update) > 0:
print(f"Updating {len(parmeters_to_update)} PartParameter records.") print(f"Updating {len(parameters_to_update)} PartParameter records.")
PartParameter.objects.bulk_update( PartParameter.objects.bulk_update(
parmeters_to_update, parameters_to_update,
fields=["model_type", "model_id"], fields=["model_type", "model_id"],
) )
# Ensure that the number of updated records matches the total number of records
assert PartParameter.objects.count() == N
# Ensure that no PartParameter records have null model_type or model_id
assert PartParameter.objects.filter(model_type=None).count() == 0
assert PartParameter.objects.filter(model_id=None).count() == 0
def reverse_update_parameter(apps, schema_editor): def reverse_update_parameter(apps, schema_editor):
"""Reverse data migration to restore existing PartParameter records. """Reverse data migration to restore existing PartParameter records.
@@ -149,9 +159,4 @@ class Migration(migrations.Migration):
name="partparameter", name="partparameter",
unique_together={('model_type', 'model_id', 'template')}, unique_together={('model_type', 'model_id', 'template')},
), ),
# Remove the obsolete "part" field from the PartParameter model
migrations.RemoveField(
model_name="partparameter",
name="part",
),
] ]

View File

@@ -58,6 +58,11 @@ class Migration(migrations.Migration):
] ]
operations = [ operations = [
# Remove the obsolete "part" field from the PartParameter model
migrations.RemoveField(
model_name="partparameter",
name="part",
),
# Add a new "template" field to the PartCategoryParameterTemplate model # Add a new "template" field to the PartCategoryParameterTemplate model
migrations.AddField( migrations.AddField(
model_name="partcategoryparametertemplate", model_name="partcategoryparametertemplate",