diff --git a/src/registrar/management/commands/drop_tables.py b/src/registrar/management/commands/drop_tables.py index 8ef1e9584..1a5a40ee0 100644 --- a/src/registrar/management/commands/drop_tables.py +++ b/src/registrar/management/commands/drop_tables.py @@ -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.')) \ No newline at end of file + logger.info(self.style.WARNING('No tables found.'))