# Generated by Django 5.0.8 on 2024-11-18 14:51

from django.db import migrations, models

def check_duplicate_email(apps, schema_editor):
    # sets an email to null if there are duplicates
    CustomUser = apps.get_model('users', 'CustomUser')
    duplicates = CustomUser.objects.values('email').annotate(email_count=models.Count('email')).filter(email_count__gt=1)
    for duplicate in duplicates:
        CustomUser.objects.filter(email=duplicate['email']).update(email=None)
        print(f"Duplicate email: {duplicate['email']}")


class Migration(migrations.Migration):

    dependencies = [
        ('users', '0002_customuser_public_profile'),
    ]

    operations = [
        migrations.RunPython(check_duplicate_email),
        migrations.AlterField(
            model_name='customuser',
            name='email',
            field=models.EmailField(max_length=254, unique=True),
        ),
    ]
