-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean_db.py
More file actions
32 lines (22 loc) · 822 Bytes
/
clean_db.py
File metadata and controls
32 lines (22 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from sqlalchemy import text
from db import engine
def quote_identifier(name: str) -> str:
return '"' + name.replace('"', '""') + '"'
def drop_all_tables() -> None:
with engine.begin() as conn:
table_names = conn.execute(
text(
"SELECT tablename "
"FROM pg_tables "
"WHERE schemaname = 'public' "
"ORDER BY tablename"
)
).scalars().all()
if not table_names:
print("No tables found in schema 'public'.")
return
for table_name in table_names:
conn.execute(text(f"DROP TABLE IF EXISTS {quote_identifier(table_name)} CASCADE"))
print(f"Dropped {len(table_names)} table(s): {', '.join(table_names)}")
if __name__ == "__main__":
drop_all_tables()