# coding=utf-8 """同步日志表(仅总服务器 MySQL)。""" from datetime import datetime from sqlalchemy import Column, DateTime, ForeignKey, Index, Integer, String, Text from sqlalchemy.orm import relationship from sqlalchemy.sql import func from dao.db import Base class SyncLog(Base): __tablename__ = 'sync_logs' id = Column(Integer, primary_key=True, autoincrement=True, comment='主键ID') base_id = Column( Integer, ForeignKey('bases.id', ondelete='CASCADE'), nullable=False, comment='基地ID', ) # 使用 String 而非原生 Enum,兼容 MySQL / SQLite sync_type = Column(String(20), nullable=False, comment='同步类型:upload / download') record_count = Column(Integer, nullable=False, default=0, server_default='0', comment='同步记录数') start_time = Column( DateTime, nullable=False, default=datetime.now, server_default=func.now(), comment='开始时间', ) end_time = Column(DateTime, nullable=True, comment='结束时间') status = Column(String(20), nullable=False, default='success', server_default='success', comment='success / failed') error_msg = Column(Text, nullable=True, comment='错误信息') base = relationship('BaseInfo', back_populates='sync_logs') __table_args__ = ( Index('idx_sync_logs_base_time', 'base_id', 'start_time'), Index('idx_sync_logs_status', 'status'), ) def to_dict(self): return { 'id': self.id, 'base_id': self.base_id, 'sync_type': self.sync_type, 'record_count': self.record_count, 'status': self.status, 'error_msg': self.error_msg, 'start_time': self.start_time.strftime('%Y-%m-%d %H:%M:%S') if self.start_time else None, 'end_time': self.end_time.strftime('%Y-%m-%d %H:%M:%S') if self.end_time else None, }