-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.py
More file actions
44 lines (37 loc) · 1.08 KB
/
database.py
File metadata and controls
44 lines (37 loc) · 1.08 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session
from config import get_settings
# Load settings from .env file
settings = get_settings()
DATABASE_URL = settings.database_url
print(f"Database URL: {DATABASE_URL}") # Debug print
# Force SQLite for now
DATABASE_URL = "sqlite:///./disaster.db"
print(f"Using SQLite: {DATABASE_URL}")
# Configure engine based on database type
if DATABASE_URL.startswith("sqlite"):
engine = create_engine(
DATABASE_URL,
connect_args={"check_same_thread": False} # SQLite specific
)
else:
# PostgreSQL settings
engine = create_engine(
DATABASE_URL,
pool_pre_ping=True,
pool_size=10,
max_overflow=20
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def get_db():
"""Dependency for FastAPI routes"""
db = SessionLocal()
try:
yield db
finally:
db.close()
def init_db():
"""Initialize database tables"""
from models.base import Base
# Create all tables
Base.metadata.create_all(bind=engine)