mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-21 10:16:13 +02:00
added drop table script
This commit is contained in:
parent
9dc7b06e08
commit
a4c06b9cdf
1 changed files with 39 additions and 0 deletions
39
src/registrar/management/commands/drop_tables.py
Normal file
39
src/registrar/management/commands/drop_tables.py
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
import logging
|
||||||
|
from django.conf import settings
|
||||||
|
from django.core.management import BaseCommand
|
||||||
|
from django.apps import apps
|
||||||
|
from django.db import connection, transaction
|
||||||
|
|
||||||
|
from registrar.management.commands.utility.terminal_helper import TerminalHelper
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
help = 'Drops all tables in the database'
|
||||||
|
|
||||||
|
def handle(self, **options):
|
||||||
|
"""Delete all rows from a list of tables"""
|
||||||
|
|
||||||
|
if settings.IS_PRODUCTION:
|
||||||
|
logger.error("drop_tables cannot be run in production")
|
||||||
|
return
|
||||||
|
|
||||||
|
self.print_tables()
|
||||||
|
logger.info(self.style.WARNING('Dropping all tables...'))
|
||||||
|
with connection.cursor() as cursor:
|
||||||
|
cursor.execute("DROP SCHEMA public CASCADE;")
|
||||||
|
cursor.execute("CREATE SCHEMA public;")
|
||||||
|
logger.info(self.style.SUCCESS('All tables dropped.'))
|
||||||
|
|
||||||
|
def print_tables(self):
|
||||||
|
logger.info(self.style.WARNING('Fetching table names...'))
|
||||||
|
with connection.cursor() as cursor:
|
||||||
|
cursor.execute("SELECT table_name FROM information_schema.tables WHERE table_schema='public'")
|
||||||
|
table_names = cursor.fetchall()
|
||||||
|
if table_names:
|
||||||
|
logger.info(self.style.NOTICE('Tables in the database:'))
|
||||||
|
for name in table_names:
|
||||||
|
logger.info(name[0])
|
||||||
|
else:
|
||||||
|
logger.info(self.style.WARNING('No tables found.'))
|
Loading…
Add table
Add a link
Reference in a new issue