148 lines
5.2 KiB
Python
148 lines
5.2 KiB
Python
|
#!/usr/bin/env python
|
|||
|
# -*- coding: utf-8 -*-
|
|||
|
|
|||
|
"""
|
|||
|
任务备份模型模块
|
|||
|
包含任务备份相关的数据模型
|
|||
|
"""
|
|||
|
|
|||
|
import enum
|
|||
|
from sqlalchemy import Column, Integer, String, Text, DateTime, Boolean, Enum, ForeignKey, JSON
|
|||
|
from sqlalchemy.orm import relationship
|
|||
|
from data.models.base import BaseModel
|
|||
|
|
|||
|
class BackupType(enum.Enum):
|
|||
|
"""
|
|||
|
备份类型枚举
|
|||
|
"""
|
|||
|
MANUAL = 'manual' # 手动备份
|
|||
|
AUTO = 'auto' # 自动备份
|
|||
|
SYSTEM = 'system' # 系统备份(如发布前自动备份)
|
|||
|
|
|||
|
class TaskBackup(BaseModel):
|
|||
|
"""
|
|||
|
任务备份模型
|
|||
|
表示任务配置的备份版本
|
|||
|
"""
|
|||
|
__tablename__ = 'task_backups'
|
|||
|
|
|||
|
task_id = Column(Integer, ForeignKey('tasks.id'), nullable=False, comment='任务ID')
|
|||
|
version_id = Column(Integer, ForeignKey('task_versions.id'), nullable=True, comment='任务版本ID')
|
|||
|
backup_type = Column(Enum(BackupType), nullable=False, default=BackupType.MANUAL, comment='备份类型')
|
|||
|
name = Column(String(100), nullable=True, comment='备份名称')
|
|||
|
description = Column(String(500), nullable=True, comment='备份描述')
|
|||
|
backup_time = Column(DateTime, nullable=False, comment='备份时间')
|
|||
|
created_by = Column(String(100), nullable=True, comment='创建用户ID')
|
|||
|
content = Column(JSON, nullable=False, comment='备份内容(JSON格式)')
|
|||
|
flow_data = Column(JSON, nullable=True, comment='流程数据(JSON格式)')
|
|||
|
is_restorable = Column(Boolean, default=True, comment='是否可恢复')
|
|||
|
|
|||
|
def __repr__(self):
|
|||
|
return f"<TaskBackup(id={self.id}, task_id={self.task_id}, backup_type='{self.backup_type}')>"
|
|||
|
|
|||
|
@classmethod
|
|||
|
def get_by_task(cls, task_id, page=1, per_page=20):
|
|||
|
"""
|
|||
|
获取任务的所有备份
|
|||
|
"""
|
|||
|
return cls.query.filter(
|
|||
|
cls.task_id == task_id,
|
|||
|
cls.is_deleted == False
|
|||
|
).order_by(cls.backup_time.desc()).paginate(page=page, per_page=per_page)
|
|||
|
|
|||
|
@classmethod
|
|||
|
def create_backup(cls, task_id, version_id, content, flow_data=None, backup_type=BackupType.MANUAL,
|
|||
|
name=None, description=None, created_by=None):
|
|||
|
"""
|
|||
|
创建任务备份
|
|||
|
"""
|
|||
|
import datetime
|
|||
|
from config.database import db_session
|
|||
|
|
|||
|
backup = cls(
|
|||
|
task_id=task_id,
|
|||
|
version_id=version_id,
|
|||
|
backup_type=backup_type,
|
|||
|
name=name,
|
|||
|
description=description,
|
|||
|
backup_time=datetime.datetime.now(),
|
|||
|
created_by=created_by,
|
|||
|
content=content,
|
|||
|
flow_data=flow_data,
|
|||
|
is_restorable=True
|
|||
|
)
|
|||
|
|
|||
|
db_session.add(backup)
|
|||
|
db_session.commit()
|
|||
|
|
|||
|
# 记录用户操作
|
|||
|
from data.models.user_operation import UserOperation, OperationType, TargetType
|
|||
|
if created_by:
|
|||
|
UserOperation.log_operation(
|
|||
|
user_id=created_by,
|
|||
|
user_name=None, # 用户名称需要从外部系统获取
|
|||
|
operation_type=OperationType.CREATE,
|
|||
|
target_type=TargetType.TASK,
|
|||
|
target_id=task_id,
|
|||
|
target_name=name or f"Backup {backup.id}",
|
|||
|
operation_details={
|
|||
|
'backup_id': backup.id,
|
|||
|
'backup_type': backup_type.value,
|
|||
|
'version_id': version_id
|
|||
|
}
|
|||
|
)
|
|||
|
|
|||
|
return backup
|
|||
|
|
|||
|
@classmethod
|
|||
|
def restore_backup(cls, backup_id, restored_by=None):
|
|||
|
"""
|
|||
|
恢复任务备份
|
|||
|
"""
|
|||
|
from config.database import db_session
|
|||
|
|
|||
|
backup = cls.query.get(backup_id)
|
|||
|
if not backup or backup.is_deleted or not backup.is_restorable:
|
|||
|
return None
|
|||
|
|
|||
|
# 获取任务和任务版本
|
|||
|
from data.models.task import Task, TaskVersion
|
|||
|
task = Task.query.get(backup.task_id)
|
|||
|
if not task or task.is_deleted:
|
|||
|
return None
|
|||
|
|
|||
|
# 创建新版本
|
|||
|
version = TaskVersion(
|
|||
|
task_id=task.id,
|
|||
|
version=f"{float(task.current_version.version) + 0.1:.1f}" if task.current_version else "1.0",
|
|||
|
content=backup.content,
|
|||
|
flow_data=backup.flow_data,
|
|||
|
remark=f"Restored from backup {backup.id}"
|
|||
|
)
|
|||
|
|
|||
|
db_session.add(version)
|
|||
|
db_session.commit()
|
|||
|
|
|||
|
# 更新任务的当前版本
|
|||
|
task.current_version_id = version.id
|
|||
|
task.updated_by = restored_by
|
|||
|
db_session.commit()
|
|||
|
|
|||
|
# 记录用户操作
|
|||
|
from data.models.user_operation import UserOperation, OperationType, TargetType
|
|||
|
if restored_by:
|
|||
|
UserOperation.log_operation(
|
|||
|
user_id=restored_by,
|
|||
|
user_name=None, # 用户名称需要从外部系统获取
|
|||
|
operation_type=OperationType.UPDATE,
|
|||
|
target_type=TargetType.TASK,
|
|||
|
target_id=task.id,
|
|||
|
target_name=task.name,
|
|||
|
operation_details={
|
|||
|
'backup_id': backup.id,
|
|||
|
'version_id': version.id,
|
|||
|
'action': 'restore'
|
|||
|
}
|
|||
|
)
|
|||
|
|
|||
|
return version
|