64 lines
2.1 KiB
Python
64 lines
2.1 KiB
Python
# Generated by Django 5.2.12 on 2026-05-14
|
|
|
|
from django.db import migrations, models
|
|
from django.db.models import Q
|
|
from django.db.models.functions import Lower
|
|
|
|
|
|
def _normalize_email(value):
|
|
if value is None:
|
|
return None
|
|
normalized = value.strip().lower()
|
|
return normalized or None
|
|
|
|
|
|
def normalize_user_and_social_emails(apps, schema_editor):
|
|
User = apps.get_model("users", "User")
|
|
UserSocialAccount = apps.get_model("users", "UserSocialAccount")
|
|
|
|
seen_emails = set()
|
|
for user in User.objects.all().order_by("created_at", "id"):
|
|
normalized_email = _normalize_email(user.email)
|
|
if normalized_email in seen_emails:
|
|
normalized_email = None
|
|
elif normalized_email is not None:
|
|
seen_emails.add(normalized_email)
|
|
|
|
if user.email != normalized_email:
|
|
user.email = normalized_email
|
|
user.save(update_fields=["email"])
|
|
|
|
for social_account in UserSocialAccount.objects.all().order_by("created_at", "id"):
|
|
normalized_email = _normalize_email(social_account.email)
|
|
if social_account.email != normalized_email:
|
|
social_account.email = normalized_email
|
|
social_account.save(update_fields=["email"])
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
dependencies = [
|
|
("users", "0002_usersocialaccount"),
|
|
]
|
|
|
|
operations = [
|
|
migrations.AlterField(
|
|
model_name="user",
|
|
name="email",
|
|
field=models.EmailField(blank=True, default=None, max_length=254, null=True),
|
|
),
|
|
migrations.AlterField(
|
|
model_name="usersocialaccount",
|
|
name="email",
|
|
field=models.EmailField(blank=True, default=None, max_length=254, null=True),
|
|
),
|
|
migrations.RunPython(normalize_user_and_social_emails, migrations.RunPython.noop),
|
|
migrations.AddConstraint(
|
|
model_name="user",
|
|
constraint=models.UniqueConstraint(
|
|
Lower("email"),
|
|
condition=Q(email__isnull=False),
|
|
name="user_email_ci_uniq",
|
|
),
|
|
),
|
|
]
|