Coverage for src/backend/InvenTree/build/migrations/0018_build_reference.py: 67%

14 statements  

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

1# Generated by Django 3.0.7 on 2020-10-19 11:25 

2 

3from django.db import migrations, models 

4 

5 

6def add_default_reference(apps, schema_editor): 

7 """ 

8 Add a "default" build-order reference for any existing build orders. 

9 Best we can do is use the PK of the build order itself. 

10 """ 

11 

12 Build = apps.get_model('build', 'build') 

13 

14 count = 0 

15 

16 for build in Build.objects.all(): 16 ↛ 18line 16 didn't jump to line 18 because the loop on line 16 never started

17 

18 build.reference = str(build.pk) 

19 build.save() 

20 count += 1 

21 

22 if count > 0: 22 ↛ 23line 22 didn't jump to line 23 because the condition on line 22 was never true

23 print(f"\nUpdated build reference for {count} existing BuildOrder objects") 

24 

25 

26class Migration(migrations.Migration): 

27 

28 atomic = False 

29 

30 dependencies = [ 

31 ('build', '0017_auto_20200426_0612'), 

32 ] 

33 

34 operations = [ 

35 # Initial operation - create a 'reference' field for the Build object: 

36 migrations.AddField( 

37 model_name='build', 

38 name='reference', 

39 field=models.CharField(help_text='Build Order Reference', blank=True, max_length=64, unique=False, verbose_name='Reference'), 

40 ), 

41 

42 # Auto-populate the new reference field for any existing build order objects 

43 migrations.RunPython( 

44 add_default_reference, 

45 reverse_code=migrations.RunPython.noop 

46 ), 

47 

48 # Now that each build has a non-empty, unique reference, update the field requirements! 

49 migrations.AlterField( 

50 model_name='build', 

51 name='reference', 

52 field=models.CharField( 

53 help_text='Build Order Reference', 

54 max_length=64, 

55 blank=False, 

56 unique=True, 

57 verbose_name='Reference' 

58 ) 

59 ) 

60 ]