Coverage for src/backend/InvenTree/part/migrations/0145_auto_20251203_1238.py: 40%

27 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 17:47 +0000

1# Generated by Django 5.2.8 on 2025-12-03 12:38 

2 

3from django.db import migrations, models 

4from django.db.models import deletion 

5 

6 

7def update_category_parameters(apps, schema_editor): 

8 """Copy the 'parameter_template' field to the new 'template' field.""" 

9 

10 PartCategoryParameterTemplate = apps.get_model("part", "PartCategoryParameterTemplate") 

11 ParameterTemplate = apps.get_model("common", "ParameterTemplate") 

12 

13 category_parameters_to_update = [] 

14 

15 for cat_param in PartCategoryParameterTemplate.objects.all(): 15 ↛ 16line 15 didn't jump to line 16 because the loop on line 15 never started

16 template = ParameterTemplate.objects.get(pk=cat_param.parameter_template_id) 

17 cat_param.template = template 

18 category_parameters_to_update.append(cat_param) 

19 

20 if len(category_parameters_to_update) > 0: 20 ↛ 22line 20 didn't jump to line 22 because the condition on line 20 was never true

21 

22 print(f"Updating {len(category_parameters_to_update)} PartCategoryParameterTemplate records.") 

23 

24 PartCategoryParameterTemplate.objects.bulk_update( 

25 category_parameters_to_update, 

26 fields=["template"], 

27 batch_size=250, 

28 ) 

29 

30 

31def reverse_update_category_parameters(apps, schema_editor): 

32 """Copy the 'template' field back to the 'parameter_template' field.""" 

33 

34 PartParameterTemplate = apps.get_model("part", "PartParameterTemplate") 

35 PartCategoryParameterTemplate = apps.get_model("part", "PartCategoryParameterTemplate") 

36 

37 category_parameters_to_update = [] 

38 

39 for cat_param in PartCategoryParameterTemplate.objects.all(): 

40 template = PartParameterTemplate.objects.get(pk=cat_param.template_id) 

41 cat_param.parameter_template = template 

42 category_parameters_to_update.append(cat_param) 

43 

44 if len(category_parameters_to_update) > 0: 

45 

46 print(f"Reversing update of {len(category_parameters_to_update)} PartCategoryParameterTemplate records.") 

47 

48 PartCategoryParameterTemplate.objects.bulk_update( 

49 category_parameters_to_update, 

50 fields=["parameter_template"], 

51 batch_size=250, 

52 ) 

53 

54 

55class Migration(migrations.Migration): 

56 

57 dependencies = [ 

58 ("part", "0144_auto_20251203_1045"), 

59 ("common", "0040_parametertemplate_parameter") 

60 ] 

61 

62 operations = [ 

63 # Remove the obsolete "part" field from the PartParameter model 

64 migrations.RemoveField( 

65 model_name="partparameter", 

66 name="part", 

67 ), 

68 # Add a new "template" field to the PartCategoryParameterTemplate model 

69 migrations.AddField( 

70 model_name="partcategoryparametertemplate", 

71 name="template", 

72 field=models.ForeignKey( 

73 blank=True, 

74 null=True, 

75 on_delete=deletion.CASCADE, 

76 related_name="part_categories", 

77 to="common.parametertemplate", 

78 ), 

79 ), 

80 # Remove unique constraint on PartCategoryParameterTemplate model 

81 migrations.RemoveConstraint( 

82 model_name="partcategoryparametertemplate", 

83 name="unique_category_parameter_template_pair", 

84 ), 

85 # Perform data migration for the PartCategoryParameterTemplate model 

86 migrations.RunPython( 

87 update_category_parameters, 

88 reverse_code=reverse_update_category_parameters, 

89 ), 

90 # Remove the obsolete "part_template" field from the PartCategoryParameterTemplate model 

91 migrations.RemoveField( 

92 model_name="partcategoryparametertemplate", 

93 name="parameter_template", 

94 ), 

95 # Remove nullable attribute from the new 'template' field 

96 migrations.AlterField( 

97 model_name="partcategoryparametertemplate", 

98 name="template", 

99 field=models.ForeignKey( 

100 on_delete=deletion.CASCADE, 

101 related_name="part_categories", 

102 to="common.parametertemplate", 

103 ), 

104 ), 

105 # Update uniqueness constraint on PartCategoryParameterTemplate model 

106 migrations.AddConstraint( 

107 model_name="partcategoryparametertemplate", 

108 constraint=models.UniqueConstraint( 

109 fields=("category", "template"), 

110 name="unique_category_parameter_pair", 

111 ), 

112 ), 

113 ]