""" 資料庫連線設定 (MySQL) """ from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker, declarative_base from app.core.config import settings # 建立資料庫引擎 (MySQL) engine = create_engine( settings.DATABASE_URL, pool_pre_ping=True, pool_size=10, max_overflow=20, pool_recycle=3600, # MySQL 連線回收 ) # 建立 Session 工廠 SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) # 建立 Base 類別 Base = declarative_base() def get_db(): """ 取得資料庫 Session 用於 FastAPI 依賴注入 """ db = SessionLocal() try: yield db finally: db.close() def init_db(): """初始化資料庫(建立所有表)""" Base.metadata.create_all(bind=engine)