From 4ff60817da00982730734d337f87fc09b4395dd7 Mon Sep 17 00:00:00 2001 From: David Kennedy Date: Mon, 3 Jun 2024 16:53:02 -0400 Subject: [PATCH] comments and code adjustments --- docs/operations/import_export.md | 40 +++++++++++++++---- src/registrar/admin.py | 9 ++--- .../management/commands/clean_tables.py | 20 ++++++++++ .../management/commands/import_tables.py | 3 ++ 4 files changed, 60 insertions(+), 12 deletions(-) diff --git a/docs/operations/import_export.md b/docs/operations/import_export.md index 9f29b153f..7ddfd5d3b 100644 --- a/docs/operations/import_export.md +++ b/docs/operations/import_export.md @@ -11,7 +11,12 @@ Simple scripts are provided as detailed below. To export from the source environment, run the following command from src directory: manage.py export_tables -On a sandbox, connect to the sandbox (getgov-stable in ex below) and run the command: +Connect to the source sandbox and run the command: +cf ssh {source-app} +/tmp/lifecycle/shell +./manage.py export_tables + +example exporting from getgov-stable: cf ssh getgov-stable /tmp/lifecycle/shell ./manage.py export_tables @@ -37,10 +42,13 @@ file from the target environment to local. Run the below commands from local. Get passcode by running: cf ssh-code -scp file from app (app is getgov-stable in example below) to local tmp: -scp -P 2222 -o User=cf:$(cf curl /v3/apps/$(cf app getgov-stable --guid)/processes | jq -r '.resources[] | select(.type=="web") | .guid')/0 ssh.fr.cloud.gov:app/tmp/exported_tables.zip . +scp file from source app to local file: +scp -P 2222 -o User=cf:$(cf curl /v3/apps/$(cf app {source-app} --guid)/processes | jq -r '.resources[] | select(.type=="web") | .guid')/0 ssh.fr.cloud.gov:app/tmp/exported_tables.zip {local_file_path} when prompted, supply the passcode retrieved in the 'cf ssh-code' command +example copying from stable to local cwd: +scp -P 2222 -o User=cf:$(cf curl /v3/apps/$(cf app getgov-stable --guid)/processes | jq -r '.resources[] | select(.type=="web") | .guid')/0 ssh.fr.cloud.gov:app/tmp/exported_tables.zip . + ### Import @@ -53,7 +61,14 @@ that there are no database conflicts on import. In order to delete all rows from the appropriate tables, run the following command: -manage.py clean_tables +cf ssh {target-app} +/tmp/lifecycle/shell +./manage.py clean_tables + +example cleaning getgov-backup: +cf ssh getgov-backup +/tmp/lifecycle/backup +./manage.py clean_tables For reference, this deletes all rows from the following tables: @@ -76,12 +91,23 @@ To scp the exported_tables.zip file from local to the sandbox, run the following Get passcode by running: cf ssh-code -scp file from app (app is getgov-stable in example below) to local cwd: -scp -P 2222 -o User=cf:$(cf curl /v3/apps/$(cf app getgov-stable --guid)/processes | jq -r '.resources[] | select(.type=="web") | .guid')/0 tmp/exported_tables.zip ssh.fr.cloud.gov:app/tmp/exported_tables.zip +scp file from local to target app: +scp -P 2222 -o User=cf:$(cf curl /v3/apps/$(cf app {target-app} --guid)/processes | jq -r '.resources[] | select(.type=="web") | .guid')/0 {local_file_path} ssh.fr.cloud.gov:app/tmp/exported_tables.zip when prompted, supply the passcode retrieved in the 'cf ssh-code' command +example copy of local file in tmp to getgov-backup: +scp -P 2222 -o User=cf:$(cf curl /v3/apps/$(cf app getgov-backup --guid)/processes | jq -r '.resources[] | select(.type=="web") | .guid')/0 tmp/exported_tables.zip ssh.fr.cloud.gov:app/tmp/exported_tables.zip + + Then connect to a shell in the target environment, and run the following import command: -manage.py import_tables +cf ssh {target-app} +/tmp/lifecycle/shell +./manage.py import_tables + +example cleaning getgov-backup: +cf ssh getgov-backup +/tmp/lifecycle/backup +./manage.py import_tables For reference, this imports tables in the following order: diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 2df56a4c0..ce00bbc88 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -2272,12 +2272,11 @@ class PublicContactResource(resources.ModelResource): self.create_instances.append(instance) else: self.update_instances.append(instance) + elif not using_transactions and dry_run: + # we don't have transactions and we want to do a dry_run + pass else: - if not using_transactions and dry_run: - # we don't have transactions and we want to do a dry_run - pass - else: - instance.save(skip_epp_save=True) + instance.save(skip_epp_save=True) self.after_save_instance(instance, using_transactions, dry_run) diff --git a/src/registrar/management/commands/clean_tables.py b/src/registrar/management/commands/clean_tables.py index 14fb17142..4342c208a 100644 --- a/src/registrar/management/commands/clean_tables.py +++ b/src/registrar/management/commands/clean_tables.py @@ -4,6 +4,8 @@ from django.core.management import BaseCommand from django.apps import apps from django.db import transaction +from registrar.management.commands.utility.terminal_helper import TerminalHelper + logger = logging.getLogger(__name__) @@ -16,6 +18,24 @@ class Command(BaseCommand): if settings.IS_PRODUCTION: logger.error("clean_tables cannot be run in production") return + + TerminalHelper.prompt_for_execution( + system_exit_on_terminate=True, + info_to_inspect=f""" + This script will delete all rows from the following tables: + * Contact + * Domain + * DomainInformation + * DomainRequest + * DraftDomain + * Host + * HostIp + * PublicContact + * User + * Website + """, + prompt_title="Do you wish to proceed with these changes?", + ) table_names = [ "DomainInformation", diff --git a/src/registrar/management/commands/import_tables.py b/src/registrar/management/commands/import_tables.py index fd241a48b..3594d3215 100644 --- a/src/registrar/management/commands/import_tables.py +++ b/src/registrar/management/commands/import_tables.py @@ -63,6 +63,9 @@ class Command(BaseCommand): return # if table_name is Contact, clean the table first + # User table is loaded before Contact, and signals create + # rows in Contact table which break the import, so need + # to be cleaned again before running import on Contact table if table_name == "Contact": self.clean_table(table_name)