mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-23 03:06:01 +02:00
Update drop_tables.py
This commit is contained in:
parent
a4c06b9cdf
commit
9b079811c8
1 changed files with 11 additions and 12 deletions
|
@ -19,21 +19,20 @@ class Command(BaseCommand):
|
||||||
logger.error("drop_tables cannot be run in production")
|
logger.error("drop_tables cannot be run in production")
|
||||||
return
|
return
|
||||||
|
|
||||||
self.print_tables()
|
|
||||||
logger.info(self.style.WARNING('Dropping all 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:
|
with connection.cursor() as cursor:
|
||||||
cursor.execute("SELECT table_name FROM information_schema.tables WHERE table_schema='public'")
|
cursor.execute("SELECT table_name FROM information_schema.tables WHERE table_schema='public'")
|
||||||
table_names = cursor.fetchall()
|
table_names = cursor.fetchall()
|
||||||
if table_names:
|
if table_names:
|
||||||
logger.info(self.style.NOTICE('Tables in the database:'))
|
try:
|
||||||
for name in table_names:
|
logger.info(self.style.NOTICE('Dropping tables in the database:'))
|
||||||
logger.info(name[0])
|
for name in table_names:
|
||||||
|
name_as_str = name[0]
|
||||||
|
logger.info(f"Dropping {name_as_str}")
|
||||||
|
cursor.execute(f"DROP TABLE {name_as_str} CASCADE;")
|
||||||
|
except Exception as err:
|
||||||
|
logger.error(f"Could not drop tables from DB: {err}")
|
||||||
|
else:
|
||||||
|
logger.info(self.style.SUCCESS('All tables dropped.'))
|
||||||
else:
|
else:
|
||||||
logger.info(self.style.WARNING('No tables found.'))
|
logger.info(self.style.WARNING('No tables found.'))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue