diff --git a/docs/operations/data_migration.md b/docs/operations/data_migration.md index 472362a79..bc1aa908d 100644 --- a/docs/operations/data_migration.md +++ b/docs/operations/data_migration.md @@ -697,3 +697,30 @@ Example: `cf ssh getgov-za` | | Parameter | Description | |:-:|:-------------------------- |:----------------------------------------------------------------------------| | 1 | **debug** | Increases logging detail. Defaults to False. | + +## Transfer federal agency script +The transfer federal agency script adds the "federal_type" field on each associated DomainRequest, and uses that to populate the "federal_type" field on each FederalAgency. + +**Important:** When running this script, note that data generated by our fixtures will be inaccurate (since we assign random data to them). Use real data on this script. +Do note that there is a check on record uniqueness. If two or more records do NOT have the same value for federal_type for any given federal agency, then the record is skipped. This protects against fixtures data when loaded with real data. + +### Running on sandboxes + +#### Step 1: Login to CloudFoundry +```cf login -a api.fr.cloud.gov --sso``` + +#### Step 2: SSH into your environment +```cf ssh getgov-{space}``` + +Example: `cf ssh getgov-za` + +#### Step 3: Create a shell instance +```/tmp/lifecycle/shell``` + +#### Step 4: Running the script +```./manage.py transfer_federal_agency_type``` + +### Running locally + +#### Step 1: Running the script +```docker-compose exec app ./manage.py transfer_federal_agency_type``` diff --git a/src/registrar/management/commands/transfer_federal_agency.py b/src/registrar/management/commands/transfer_federal_agency_type.py similarity index 78% rename from src/registrar/management/commands/transfer_federal_agency.py rename to src/registrar/management/commands/transfer_federal_agency_type.py index dd7b1e5db..e9ee6f3ce 100644 --- a/src/registrar/management/commands/transfer_federal_agency.py +++ b/src/registrar/management/commands/transfer_federal_agency_type.py @@ -18,7 +18,7 @@ class Command(BaseCommand, PopulateScriptTemplate): def handle(self, **kwargs): """Loops through each valid User object and updates its verification_type value""" - # Get all existing domain requests + # Get all existing domain requests. Select_related allows us to skip doing db queries. self.all_domain_requests = DomainRequest.objects.select_related("federal_agency").distinct() self.mass_update_records( FederalAgency, filter_conditions={"agency__isnull": False}, fields_to_update=["federal_type"] @@ -28,9 +28,11 @@ class Command(BaseCommand, PopulateScriptTemplate): """Defines how we update the federal_type field on each record.""" request = self.all_domain_requests.filter(federal_agency__agency=record.agency).first() record.federal_type = request.federal_type - + def should_skip_record(self, record) -> bool: # noqa """Defines the conditions in which we should skip updating a record.""" - request = self.all_domain_requests.filter(federal_agency__agency=record.agency).first() - return not request or not request.federal_agency + requests = self.all_domain_requests.filter(federal_agency__agency=record.agency, federal_type__isnull=False) + # Check if all federal_type values are the same. Skip the record otherwise. + distinct_federal_types = requests.values('federal_type').distinct() + return distinct_federal_types.count() != 1