Files
KPI-management/app/core/database.py
DonaldFang 方士碩 f810ddc2ea Initial commit: KPI Management System Backend
Features:
- FastAPI backend with JWT authentication
- MySQL database with SQLAlchemy ORM
- KPI workflow: draft → pending → approved → evaluation → completed
- Ollama LLM API integration for AI features
- Gitea API integration for version control
- Complete API endpoints for KPI, dashboard, notifications

Tables: KPI_D_* prefix naming convention

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-11 16:20:57 +08:00

40 lines
797 B
Python

"""
資料庫連線設定 (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)