Coverage for src/backend/InvenTree/part/migrations/0112_auto_20230525_1606.py: 78%

32 statements  

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

1# Generated by Django 3.2.19 on 2023-05-31 12:05 

2 

3""" 

4Note: This is a total hack method to delete columns (if they already exist). 

5 

6Due to an improper set of migrations merges, 

7there may exist a situation where the columns (defined in the migrations below) already exist. 

8 

9In this case, we want to delete the columns, and then re-add them. 

10 

11Original error: https://github.com/inventree/InvenTree/pull/4898 

121st fix: https://github.com/inventree/InvenTree/pull/4961 

132nd fix: https://github.com/inventree/InvenTree/pull/4977 

143rd fix: https://github.com/inventree/InvenTree/pull/4987 

15""" 

16 

17from django.db import migrations, models 

18 

19 

20class RemoveFieldOrSkip(migrations.RemoveField): 

21 """Custom RemoveField operation which will fail gracefully if the field does not exist 

22 

23 Ref: https://stackoverflow.com/questions/58518726/how-to-ignore-a-specific-migration 

24 """ 

25 

26 def database_backwards(self, app_label, schema_editor, from_state, to_state) -> None: 

27 # Backwards migration should not do anything 

28 pass 

29 

30 def database_forwards(self, app_label, schema_editor, from_state, to_state) -> None: 

31 """Forwards migration *attempts* to remove existing fields, but will fail gracefully if they do not exist""" 

32 

33 try: 

34 super().database_forwards(app_label, schema_editor, from_state, to_state) 

35 print(f'Removed field {self.name} from model {self.model_name}') 

36 except Exception as exc: 

37 pass 

38 

39 def state_forwards(self, app_label, state) -> None: 

40 try: 

41 super().state_forwards(app_label, state) 

42 except Exception: 

43 pass 

44 

45class AddFieldOrSkip(migrations.AddField): 

46 """Custom AddField operation which will fail gracefully if the field already exists 

47 

48 Ref: https://stackoverflow.com/questions/58518726/how-to-ignore-a-specific-migration 

49 """ 

50 

51 def database_backwards(self, app_label, schema_editor, from_state, to_state) -> None: 

52 # Backwards migration should not do anything 

53 pass 

54 

55 def database_forwards(self, app_label, schema_editor, from_state, to_state) -> None: 

56 """Forwards migration *attempts* to remove existing fields, but will fail gracefully if they do not exist""" 

57 

58 try: 

59 super().database_forwards(app_label, schema_editor, from_state, to_state) 

60 except Exception as exc: 

61 pass 

62 

63 def state_forwards(self, app_label, state) -> None: 

64 try: 

65 super().state_forwards(app_label, state) 

66 except Exception: 

67 pass 

68 

69 

70class Migration(migrations.Migration): 

71 

72 atomic = False 

73 

74 dependencies = [ 

75 ('part', '0111_auto_20230521_1350'), 

76 ] 

77 

78 operations = [ 

79 RemoveFieldOrSkip( 

80 model_name='partparametertemplate', 

81 name='checkbox', 

82 ), 

83 RemoveFieldOrSkip( 

84 model_name='partparametertemplate', 

85 name='choices', 

86 ), 

87 AddFieldOrSkip( 

88 model_name='partparametertemplate', 

89 name='checkbox', 

90 field=models.BooleanField(default=False, help_text='Is this parameter a checkbox?', verbose_name='Checkbox'), 

91 ), 

92 AddFieldOrSkip( 

93 model_name='partparametertemplate', 

94 name='choices', 

95 field=models.CharField(blank=True, help_text='Valid choices for this parameter (comma-separated)', max_length=5000, verbose_name='Choices'), 

96 ), 

97 ]