From 10f0e775500934661a0add2e3c996759ceb218da Mon Sep 17 00:00:00 2001 From: Calum Mackervoy Date: Wed, 20 Nov 2024 17:05:40 +0100 Subject: [PATCH] Add a migration to ensure EmailAddress model populated Many users on production have User.email set with no corresponding email address model Fix migration should assume emails are not verified Fix migration typo --- .../0015_ensure_emailaddress_model_use.py | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 itou/users/migrations/0015_ensure_emailaddress_model_use.py diff --git a/itou/users/migrations/0015_ensure_emailaddress_model_use.py b/itou/users/migrations/0015_ensure_emailaddress_model_use.py new file mode 100644 index 0000000000..887f88c541 --- /dev/null +++ b/itou/users/migrations/0015_ensure_emailaddress_model_use.py @@ -0,0 +1,38 @@ +from django.db import migrations +from django.db.models import OuterRef, Subquery + + +def create_email_addresses_for_users(apps, schema_editor): + User = apps.get_model("users", "User") + EmailAddress = apps.get_model("account", "EmailAddress") + + # Get all those values of User.email where there is no corresponding EmailAddresses instance + users_missing_addresses = ( + User.objects.prefetch_related("emailaddress_set") + .annotate(email_exists=Subquery(EmailAddress.objects.filter(email=OuterRef("email")).values("id")[:1])) + .filter(email_exists__isnull=True) + .values("id", "email") + ) + + EmailAddress.objects.bulk_create( + EmailAddress(user_id=x["id"], email=x["email"], primary=True, verified=False) for x in users_missing_addresses + ) + + +class Migration(migrations.Migration): + """ + This migration was created at a time when not all User email addresses had an associated EmailAddress. + It ensures that EmailAddress instances are created where they are not existing. + Of course this means that the migration can be squashed later. + """ + + dependencies = [ + ("users", "0014_alter_jobseekerprofile_birthdate__add_index"), + ] + + operations = [ + migrations.RunPython( + create_email_addresses_for_users, + reverse_code=migrations.RunPython.noop, + ), + ]