from datetime import datetime from sqlalchemy import ( Column, BigInteger, String, JSON, SmallInteger, DateTime, UniqueConstraint, Index, ) from app.core.database import Base class NodeConfiguration(Base): """ 节点角色配置主表 """ __tablename__ = "node_configurations" id = Column(BigInteger, primary_key=True, autoincrement=True) role = Column(String(32), nullable=False, comment="角色类型: sentinel / booker / standalone") node_id = Column(String(64), nullable=False, comment="节点唯一标识,如: node01") config_content = Column(JSON, nullable=False, comment="完整配置内容 (JSON 数组或 JSON 对象)") config_hash = Column(String(32), nullable=False, index=True, comment="配置内容 MD5 哈希,用于节点秒级比对") is_enabled = Column(SmallInteger, nullable=False, default=1, index=True, comment="是否启用: 1-启用, 0-禁用") description = Column(String(255), nullable=True, default="", comment="配置用途/备注说明") created_by = Column(String(64), nullable=True, default="system", comment="创建人") updated_by = Column(String(64), nullable=True, default="system", comment="最后更新人") created_at = Column(DateTime, default=datetime.utcnow, nullable=False, comment="创建时间") updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False, comment="更新时间") __table_args__ = ( UniqueConstraint("role", "node_id", name="uk_role_node"), Index("idx_node_config_updated_at", "updated_at"), ) class NodeConfigAuditLog(Base): """ 配置修改审计日志表 """ __tablename__ = "node_config_audit_logs" id = Column(BigInteger, primary_key=True, autoincrement=True) role = Column(String(32), nullable=False, comment="角色类型") node_id = Column(String(64), nullable=False, comment="节点标识") action = Column(String(32), nullable=False, comment="操作类型: CREATE / UPDATE / TOGGLE / DELETE / CLONE") before_hash = Column(String(32), nullable=True, comment="修改前配置 Hash") after_hash = Column(String(32), nullable=True, comment="修改后配置 Hash") operator = Column(String(64), nullable=False, default="system", comment="操作人") remark = Column(String(500), nullable=True, default="", comment="修改原因或备注说明") created_at = Column(DateTime, default=datetime.utcnow, nullable=False, comment="操作时间") __table_args__ = ( Index("idx_role_node_time", "role", "node_id", "created_at"), )