Coverage for src/backend/InvenTree/stock/migrations/0069_auto_20211109_2347.py: 31%

21 statements  

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

1# Generated by Django 3.2.5 on 2021-11-09 23:47 

2 

3import re 

4 

5from django.db import migrations 

6 

7 

8def update_serials(apps, schema_editor): 

9 """ 

10 Rebuild the integer serial number field for existing StockItem objects 

11 """ 

12 

13 StockItem = apps.get_model('stock', 'stockitem') 

14 

15 for item in StockItem.objects.all(): # pragma: no cover 15 ↛ 17line 15 didn't jump to line 17 because the loop on line 15 never started

16 

17 if item.serial is None: 

18 # Skip items without existing serial numbers 

19 continue 

20 

21 serial = 0 

22 

23 result = re.match(r"^(\d+)", str(item.serial)) 

24 

25 if result and len(result.groups()) == 1: 

26 try: 

27 serial = int(result.groups()[0]) 

28 except Exception: 

29 serial = 0 

30 

31 # Ensure the integer value is not too large for the database field 

32 if serial > 0x7fffffff: 

33 serial = 0x7fffffff 

34 

35 item.serial_int = serial 

36 item.save() 

37 

38 

39class Migration(migrations.Migration): 

40 

41 dependencies = [ 

42 ('stock', '0068_stockitem_serial_int'), 

43 ] 

44 

45 operations = [ 

46 migrations.RunPython( 

47 update_serials, 

48 reverse_code=migrations.RunPython.noop, 

49 ) 

50 ]