Skip to content

Commit

Permalink
chore: converted raw SQL script to Django migration. Haven't tested i…
Browse files Browse the repository at this point in the history
…t yet
  • Loading branch information
andrea-williams committed Feb 14, 2025
1 parent a0ef5f0 commit 1c45ca2
Showing 1 changed file with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Generated by Django 5.0.11 on 2025-02-14 01:29

from django.db import migrations


"""
One-time forward-only migration to be applied to prod data.
Purpose: create a contact record for each approved industry_user who doesn't already appear in the contact table.
"""


def create_contacts_from_prod_industry_users(apps, schema_monitor):
# import the required django models
User = apps.get_model('registration', 'User')
Contact = apps.get_model('registration', 'Contact')
UserOperator = apps.get_model('registration', 'UserOperator')
BusinessRole = apps.get_model('registration', 'BusinessRole')

# filter on Users to fetch only those with app_role_id == "industry_user"
# and status == "Approved", and whose email doesn't appear in the Contact table
approved_industry_users = (
User.objects.filter(app_role__role_name="industry_user", user_operators__status=UserOperator.Statuses.APPROVED)
.exclude(email__in=Contact.objects.values_list("email", flat=True))
.distinct()
)

operation_representative_role = BusinessRole.objects.get(role_name="Operation Representative")

# for each user in approved_industry_users, create a new Contact record for them
new_contacts = [
Contact(
first_name=user.first_name,
last_name=user.last_name,
email=user.email,
phone_number=user.phone_number,
business_role=operation_representative_role,
address=None,
operator=user.user_operator.operator,
)
for user in approved_industry_users
]
Contact.objects.bulk_create(new_contacts)


class Migration(migrations.Migration):
dependencies = [
('registration', '0077_historicalfacility_well_authorization_numbers_and_more'),
]

operations = [migrations.RunPython(create_contacts_from_prod_industry_users)]

0 comments on commit 1c45ca2

Please sign in to comment.