mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-08-20 00:14:16 +02:00
added command scripts clean_tables, export_tables and import_tables
This commit is contained in:
parent
cb1c0dece0
commit
1e540335d6
3 changed files with 174 additions and 0 deletions
33
src/registrar/management/commands/clean_tables.py
Normal file
33
src/registrar/management/commands/clean_tables.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
import logging
|
||||
from django.core.management import BaseCommand
|
||||
from django.apps import apps
|
||||
from django.db import transaction
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Clean tables in database to prepare for import."
|
||||
|
||||
def handle(self, **options):
|
||||
"""Delete all rows from a list of tables"""
|
||||
table_names = [
|
||||
"DomainInformation", "DomainRequest", "Domain", "User", "Contact",
|
||||
"Website", "DraftDomain", "HostIp", "Host"
|
||||
]
|
||||
|
||||
for table_name in table_names:
|
||||
self.clean_table(table_name)
|
||||
|
||||
def clean_table(self, table_name):
|
||||
"""Delete all rows in the given table"""
|
||||
try:
|
||||
# Get the model class dynamically
|
||||
model = apps.get_model('registrar', table_name)
|
||||
# Use a transaction to ensure database integrity
|
||||
with transaction.atomic():
|
||||
model.objects.all().delete()
|
||||
logger.info(f"Successfully cleaned table {table_name}")
|
||||
except LookupError:
|
||||
logger.error(f"Model for table {table_name} not found.")
|
||||
except Exception as e:
|
||||
logger.error(f"Error cleaning table {table_name}: {e}")
|
Loading…
Add table
Add a link
Reference in a new issue