-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: converted raw SQL script to Django migration. Haven't tested i…
…t yet
- Loading branch information
1 parent
1642aa7
commit 12b592d
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
bc_obps/registration/migrations/0078_make_contacts_from_industry_users.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] |