Update drop_tables.py

This commit is contained in:
zandercymatics 2024-07-22 14:31:39 -06:00
parent a4c06b9cdf
commit 9b079811c8
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7

View file

@ -19,21 +19,20 @@ class Command(BaseCommand):
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])
try:
logger.info(self.style.NOTICE('Dropping tables in the database:'))
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:
logger.info(self.style.WARNING('No tables found.'))
logger.info(self.style.WARNING('No tables found.'))