-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreset_db.py
More file actions
28 lines (19 loc) · 721 Bytes
/
reset_db.py
File metadata and controls
28 lines (19 loc) · 721 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
import os
from sqlalchemy import create_engine, text
from app.database import SQLALCHEMY_DATABASE_URL
from dotenv import load_dotenv
load_dotenv()
def reset_database():
engine = create_engine(SQLALCHEMY_DATABASE_URL)
host = os.getenv("DB_HOST", "localhost")
query = text("TRUNCATE posts, votes RESTART IDENTITY CASCADE;")
try:
with engine.connect() as conn:
print(f"Connecting to host: {host}...")
conn.execute(query)
conn.commit()
print("Successfully cleared posts and votes. Counters reset to 1.")
except Exception as e:
print(f"Error resetting database: {e}")
if __name__ == "__main__":
reset_database()