30 lines
753 B
Python
30 lines
753 B
Python
import os
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
|
|
# 데이터베이스 URL 가져오기
|
|
DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://asset_user:password@localhost/asset_pilot")
|
|
|
|
# SQLAlchemy 엔진 생성
|
|
engine = create_engine(
|
|
DATABASE_URL,
|
|
pool_size=10,
|
|
max_overflow=20,
|
|
pool_pre_ping=True, # 연결 유효성 자동 확인
|
|
echo=False # SQL 쿼리 로그 (디버깅 시 True로 변경)
|
|
)
|
|
|
|
# 세션 팩토리 생성
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
def get_db():
|
|
"""데이터베이스 세션 의존성"""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|