node_configuration.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from datetime import datetime
  2. from sqlalchemy import (
  3. Column,
  4. BigInteger,
  5. String,
  6. JSON,
  7. SmallInteger,
  8. DateTime,
  9. UniqueConstraint,
  10. Index,
  11. )
  12. from app.core.database import Base
  13. class NodeConfiguration(Base):
  14. """
  15. 节点角色配置主表
  16. """
  17. __tablename__ = "node_configurations"
  18. id = Column(BigInteger, primary_key=True, autoincrement=True)
  19. role = Column(String(32), nullable=False, comment="角色类型: sentinel / booker / standalone")
  20. node_id = Column(String(64), nullable=False, comment="节点唯一标识,如: node01")
  21. config_content = Column(JSON, nullable=False, comment="完整配置内容 (JSON 数组或 JSON 对象)")
  22. config_hash = Column(String(32), nullable=False, index=True, comment="配置内容 MD5 哈希,用于节点秒级比对")
  23. is_enabled = Column(SmallInteger, nullable=False, default=1, index=True, comment="是否启用: 1-启用, 0-禁用")
  24. description = Column(String(255), nullable=True, default="", comment="配置用途/备注说明")
  25. created_by = Column(String(64), nullable=True, default="system", comment="创建人")
  26. updated_by = Column(String(64), nullable=True, default="system", comment="最后更新人")
  27. created_at = Column(DateTime, default=datetime.utcnow, nullable=False, comment="创建时间")
  28. updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False, comment="更新时间")
  29. __table_args__ = (
  30. UniqueConstraint("role", "node_id", name="uk_role_node"),
  31. Index("idx_node_config_updated_at", "updated_at"),
  32. )
  33. class NodeConfigAuditLog(Base):
  34. """
  35. 配置修改审计日志表
  36. """
  37. __tablename__ = "node_config_audit_logs"
  38. id = Column(BigInteger, primary_key=True, autoincrement=True)
  39. role = Column(String(32), nullable=False, comment="角色类型")
  40. node_id = Column(String(64), nullable=False, comment="节点标识")
  41. action = Column(String(32), nullable=False, comment="操作类型: CREATE / UPDATE / TOGGLE / DELETE / CLONE")
  42. before_hash = Column(String(32), nullable=True, comment="修改前配置 Hash")
  43. after_hash = Column(String(32), nullable=True, comment="修改后配置 Hash")
  44. operator = Column(String(64), nullable=False, default="system", comment="操作人")
  45. remark = Column(String(500), nullable=True, default="", comment="修改原因或备注说明")
  46. created_at = Column(DateTime, default=datetime.utcnow, nullable=False, comment="操作时间")
  47. __table_args__ = (
  48. Index("idx_role_node_time", "role", "node_id", "created_at"),
  49. )