Coverage for src/backend/InvenTree/company/migrations/0024_unique_name_email_constraint.py: 40%
17 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
1from django.db import migrations, models
4def reverse_empty_email(apps, schema_editor): # pragma: no cover
5 Company = apps.get_model('company', 'Company')
6 for company in Company.objects.all():
7 if company.email == None:
8 company.email = ''
9 company.save()
12def make_empty_email_field_null(apps, schema_editor):
13 Company = apps.get_model('company', 'Company')
14 for company in Company.objects.all(): 14 ↛ 15line 14 didn't jump to line 15 because the loop on line 14 never started
15 if company.email == '':
16 company.email = None
17 company.save()
20class Migration(migrations.Migration):
22 atomic = False
24 dependencies = [
25 ('company', '0023_auto_20200808_0715'),
26 ]
28 operations = [
29 # Allow email field to be NULL
30 migrations.AlterField(
31 model_name='company',
32 name='email',
33 field=models.EmailField(blank=True, help_text='Contact email address', max_length=254, null=True, unique=False, verbose_name='Email'),
34 ),
35 # Convert empty email string to NULL
36 migrations.RunPython(make_empty_email_field_null, reverse_code=reverse_empty_email),
37 # Remove unique constraint on name field
38 migrations.AlterField(
39 model_name='company',
40 name='name',
41 field=models.CharField(help_text='Company name', max_length=100, verbose_name='Company name'),
42 ),
43 # Add unique constraint on name/email pair
44 migrations.AddConstraint(
45 model_name='company',
46 constraint=models.UniqueConstraint(fields=('name', 'email'), name='unique_name_email_pair'),
47 ),
48 ]