-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
50 lines (40 loc) · 977 Bytes
/
database.py
File metadata and controls
50 lines (40 loc) · 977 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Database connection and session management.
"""
from sqlalchemy import create_engine
from sqlalchemy.orm import declarative_base, sessionmaker
from typing import Generator
from config import settings
# Check if database URL is configured
if settings.database_url is None:
raise ValueError(
"DATABASE_URL environment variable is required. "
"Please set it in your .env file or environment."
)
# Create sync engine
engine = create_engine(
settings.database_url,
echo=settings.debug,
pool_pre_ping=True,
pool_size=10,
max_overflow=20,
)
# Create session factory
SessionLocal = sessionmaker(
autocommit=False,
autoflush=False,
bind=engine
)
# Base class for models
Base = declarative_base()
def get_db() -> Generator:
"""
Dependency for getting database session.
Yields:
Session: Database session
"""
db = SessionLocal()
try:
yield db
finally:
db.close()