Files
wecom_it_smart_desk/backend/app/models/config_change_log.py
T

96 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# =============================================================================
# 企微IT智能服务台 — 配置变更日志模型
# =============================================================================
# 说明:对应数据库 config_change_logs 表,记录每次配置项的变更历史
# 包含变更前后的值、操作人和时间,用于配置审计和回滚
# =============================================================================
import uuid
from datetime import datetime
from sqlalchemy import DateTime, Index, String, Text
from sqlalchemy.orm import Mapped, mapped_column
from app.database import Base
class ConfigChangeLog(Base):
"""配置变更日志模型 — 对应 config_change_logs 表。
记录每次配置项的变更历史,包含变更前后的值、操作人和时间。
Attributes:
id: 日志唯一标识(UUID)
config_key: 变更的配置键
old_value: 变更前的值
new_value: 变更后的值
changed_by: 变更操作人(agent_id
changed_at: 变更时间
"""
# 表名(必须和架构文档 DDL 一致)
__tablename__ = "config_change_logs"
# --------------------------------------------------------------------------
# 字段定义
# --------------------------------------------------------------------------
# 主键:UUIDPython端生成(兼容PostgreSQL和SQLite
id: Mapped[str] = mapped_column(
String(36),
primary_key=True,
default=lambda: str(uuid.uuid4()),
)
# 配置键(关联 system_configs 表的 config_key
config_key: Mapped[str] = mapped_column(
String(128),
nullable=False,
comment="配置键",
)
# 变更前的值(空字符串表示新增配置项)
old_value: Mapped[str] = mapped_column(
Text,
nullable=False,
default="",
comment="变更前的值",
)
# 变更后的值
new_value: Mapped[str] = mapped_column(
Text,
nullable=False,
default="",
comment="变更后的值",
)
# 变更操作人(关联 agents 表的 id)
changed_by: Mapped[str] = mapped_column(
String(36),
nullable=False,
comment="变更操作人 agent_id",
)
# 变更时间
changed_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
nullable=False,
default=datetime.now,
comment="变更时间",
)
# --------------------------------------------------------------------------
# 索引定义(和架构文档 DDL 严格一致)
# --------------------------------------------------------------------------
__table_args__ = (
# 按配置键查询(如查询某配置项的所有变更历史)
Index("idx_ccl_config_key", "config_key"),
# 按变更时间查询(如查询最近的变更记录)
Index("idx_ccl_changed_at", "changed_at"),
)
def __repr__(self) -> str:
"""变更日志对象的字符串表示,方便调试。"""
return f"<ConfigChangeLog(key={self.config_key}, by={self.changed_by})>"