update doc and require
This commit is contained in:
parent
99b3270de6
commit
7bbd115bf2
59
app/api/endpoints/config_history.py
Normal file
59
app/api/endpoints/config_history.py
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from app.models.database import get_db
|
||||||
|
from app.models.config import Config
|
||||||
|
from app.models.config_history import ConfigHistory
|
||||||
|
from app.schemas.config import ConfigCreate, ConfigUpdate
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.get("/config/{config_id}/history", response_model=List[dict])
|
||||||
|
def get_config_history(config_id: int, db: Session = Depends(get_db)):
|
||||||
|
"""获取配置的修改历史"""
|
||||||
|
history = db.query(ConfigHistory).filter(ConfigHistory.config_id == config_id).order_by(ConfigHistory.created_at.desc()).all()
|
||||||
|
return [{
|
||||||
|
"history_id": h.history_id,
|
||||||
|
"config_id": h.config_id,
|
||||||
|
"type_id": h.type_id,
|
||||||
|
"key": h.key,
|
||||||
|
"old_value": h.old_value,
|
||||||
|
"new_value": h.new_value,
|
||||||
|
"operator": h.operator,
|
||||||
|
"operation_type": h.operation_type,
|
||||||
|
"created_at": h.created_at
|
||||||
|
} for h in history]
|
||||||
|
|
||||||
|
@router.post("/config/{config_id}/rollback/{history_id}")
|
||||||
|
def rollback_config(config_id: int, history_id: int, db: Session = Depends(get_db)):
|
||||||
|
"""回滚配置到指定的历史版本"""
|
||||||
|
# 获取历史记录
|
||||||
|
history = db.query(ConfigHistory).filter(ConfigHistory.history_id == history_id).first()
|
||||||
|
if not history:
|
||||||
|
raise HTTPException(status_code=404, detail="History not found")
|
||||||
|
|
||||||
|
# 获取当前配置
|
||||||
|
config = db.query(Config).filter(Config.config_id == config_id).first()
|
||||||
|
if not config:
|
||||||
|
raise HTTPException(status_code=404, detail="Config not found")
|
||||||
|
|
||||||
|
# 创建新的历史记录
|
||||||
|
new_history = ConfigHistory(
|
||||||
|
config_id=config_id,
|
||||||
|
type_id=config.type_id,
|
||||||
|
key=config.key,
|
||||||
|
old_value=config.value,
|
||||||
|
new_value=history.old_value, # 回滚到历史版本的值
|
||||||
|
operator="system",
|
||||||
|
operation_type="rollback"
|
||||||
|
)
|
||||||
|
|
||||||
|
# 更新配置值
|
||||||
|
config.value = history.old_value
|
||||||
|
|
||||||
|
# 保存更改
|
||||||
|
db.add(new_history)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
return {"message": "Config rolled back successfully"}
|
||||||
24
app/models/config_history.py
Normal file
24
app/models/config_history.py
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, func
|
||||||
|
from sqlalchemy.orm import relationship
|
||||||
|
from app.models.database import Base
|
||||||
|
|
||||||
|
class ConfigHistory(Base):
|
||||||
|
__tablename__ = "config_history"
|
||||||
|
|
||||||
|
history_id = Column(Integer, primary_key=True, index=True, autoincrement=True)
|
||||||
|
config_id = Column(Integer, ForeignKey("configs.config_id"), nullable=False)
|
||||||
|
type_id = Column(Integer, ForeignKey("types.type_id"), nullable=False)
|
||||||
|
key = Column(String, nullable=False)
|
||||||
|
old_value = Column(String, nullable=False)
|
||||||
|
new_value = Column(String, nullable=False)
|
||||||
|
operator = Column(String, nullable=True) # 操作人
|
||||||
|
operation_type = Column(String, nullable=False) # 操作类型:create/update/delete
|
||||||
|
created_at = Column(DateTime, default=func.now())
|
||||||
|
|
||||||
|
# 关联配置
|
||||||
|
config = relationship("Config")
|
||||||
|
# 关联类型
|
||||||
|
type = relationship("Type")
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"<ConfigHistory(history_id={self.history_id}, config_id={self.config_id}, operation_type='{self.operation_type}')>"
|
||||||
Loading…
Reference in New Issue
Block a user