refactor(Dockerfile): 使用requirements.txt替代硬编码依赖 将Dockerfile中的硬编码pip包列表替换为通过requirements.txt文件安装, 提高依赖管理的灵活性和可维护性。 feat(scheduling): 移除内置APScheduler,采用独立调度系统 移除app/core/background/scheduler.py中原来的APScheduler实现, 改为使用新的应用级调度系统app.application.scheduling。 refactor(task_queue): 调整任务队列模块结构和导入路径 将任务队列相关常量从app.core.background.task_queue.constants迁移至 app.tasks.constants,并更新所有相关导入路径和引用。 refactor(events): 将事件服务重构为独立的应用层组件 将事件分发逻辑从核心层迁移到应用层,使用app.application.events.EventDispatchService 替代原有的app.modules.events.services.EventService。 feat(ai_memory): 增强AI记忆自动写入的安全策略 新增ai_memory_blocked_content_terms配置项用于阻止敏感内容, 添加TTL过期机制控制自动写入条目的生命周期。 fix(security): 强化生产环境安全验证机制 增加model_validator确保生产环境中数据库连接、API密钥、CORS设置等 关键安全配置符合要求。 feat(risks): 优化风险事件操作动作的外键约束 为RiskEventAction模型的风险事件ID字段添加外键约束, 防止孤立记录并增强数据完整性。 refactor(audit): 优化审计服务方法命名和事务处理 将AuditService的log方法重命名为record以反映其阶段行为, 并调整事务提交时机以提高性能。 feat(events): 增强领域事件并发处理和响应模型 添加事件锁定机制防止重复处理,更新API响应模型以提供 更准确的数据类型定义。 ```
190 lines
6.9 KiB
Python
190 lines
6.9 KiB
Python
from typing import Any
|
|
|
|
from fastapi import HTTPException, status
|
|
from sqlalchemy import select
|
|
|
|
from app.core.constants import ActorValue
|
|
from app.core.security import ensure_business_mutations_enabled
|
|
from app.core.utils.time import utc_now
|
|
from app.modules.audit.constants import AuditAction, AuditRiskLevel, AuditSource, AuditStatus
|
|
from app.modules.audit.schemas import AuditLogCreate
|
|
from app.modules.audit.service import AuditService
|
|
from app.modules.business.constants import BusinessDomain, SourceSystem, StatusValue
|
|
from app.modules.business.models import LegacySyncRun, Project
|
|
from app.modules.business.service import serialize_model
|
|
from app.modules.events.constants import (
|
|
EventAggregateType,
|
|
EventPayloadKey,
|
|
EventSource,
|
|
EventType,
|
|
)
|
|
from app.modules.events.services import EventService
|
|
from app.modules.legacy_mysql.constants import (
|
|
LEGACY_PROJECT_QUERY_SOURCE,
|
|
LEGACY_PROJECT_SYNC_NOTE,
|
|
LEGACY_SYNC_MISSING_ID_REASON,
|
|
LEGACY_SYNC_RUN_CODE_PREFIX,
|
|
LegacyProjectField,
|
|
LegacyQueryError,
|
|
LegacyQueryName,
|
|
LegacyResponseKey,
|
|
LegacySyncAction,
|
|
)
|
|
|
|
from app.modules.legacy_mysql.services.common import _query_name_text
|
|
|
|
|
|
class LegacyProjectSyncMixin:
|
|
def sync_projects(
|
|
self,
|
|
source_query: str | None = None,
|
|
source_query_name: str | None = None,
|
|
field_map: dict[str, str] | None = None,
|
|
limit: int = 100,
|
|
dry_run: bool = True,
|
|
actor: str = ActorValue.API,
|
|
) -> dict[str, Any]:
|
|
ensure_business_mutations_enabled()
|
|
if self.db is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
detail=LegacyQueryError.APP_DB_UNAVAILABLE,
|
|
)
|
|
|
|
query_name = source_query_name or LegacyQueryName.PROJECTS
|
|
if source_query:
|
|
rows = self.execute_readonly(
|
|
source_query,
|
|
{LegacyResponseKey.LIMIT: limit},
|
|
limit=limit,
|
|
)[LegacyResponseKey.ROWS]
|
|
query_ref = LegacySyncAction.ALLOWLISTED_INLINE_SQL
|
|
else:
|
|
rows = self.execute_allowed_query(
|
|
query_name,
|
|
{LegacyResponseKey.LIMIT: limit},
|
|
limit=limit,
|
|
)[LegacyResponseKey.ROWS]
|
|
query_ref = _query_name_text(query_name)
|
|
field_map = field_map or {}
|
|
created = 0
|
|
updated = 0
|
|
skipped = 0
|
|
items: list[dict[str, Any]] = []
|
|
|
|
for row in rows:
|
|
payload = self._project_payload(row, field_map)
|
|
if not payload[LegacyProjectField.EXTERNAL_ID] and not payload[LegacyProjectField.CODE]:
|
|
skipped += 1
|
|
items.append(
|
|
{
|
|
LegacyResponseKey.ACTION: LegacySyncAction.SKIPPED,
|
|
LegacyResponseKey.REASON: LEGACY_SYNC_MISSING_ID_REASON,
|
|
LegacyResponseKey.SOURCE: row,
|
|
}
|
|
)
|
|
continue
|
|
|
|
stmt = select(Project).where(
|
|
Project.source_system == SourceSystem.LEGACY_MYSQL,
|
|
Project.external_id == payload[LegacyProjectField.EXTERNAL_ID],
|
|
)
|
|
record = self.db.execute(stmt).scalar_one_or_none()
|
|
if record is None:
|
|
record = self.db.execute(
|
|
select(Project).where(Project.code == payload[LegacyProjectField.CODE])
|
|
).scalar_one_or_none()
|
|
|
|
if record is None:
|
|
created += 1
|
|
action = LegacySyncAction.CREATE
|
|
result = payload
|
|
if not dry_run:
|
|
record = Project(**payload)
|
|
self.db.add(record)
|
|
self.db.flush()
|
|
result = serialize_model(record)
|
|
else:
|
|
updated += 1
|
|
action = LegacySyncAction.UPDATE
|
|
if not dry_run:
|
|
for key, value in payload.items():
|
|
setattr(record, key, value)
|
|
self.db.flush()
|
|
result = serialize_model(record)
|
|
else:
|
|
result = payload
|
|
items.append(
|
|
{
|
|
LegacyResponseKey.ACTION: action,
|
|
LegacyResponseKey.PROJECT: result,
|
|
LegacyResponseKey.SOURCE: row,
|
|
}
|
|
)
|
|
|
|
result = {
|
|
LegacyResponseKey.DRY_RUN: dry_run,
|
|
LegacyResponseKey.CREATED: created,
|
|
LegacyResponseKey.UPDATED: updated,
|
|
LegacyResponseKey.SKIPPED: skipped,
|
|
LegacyResponseKey.ITEMS: items,
|
|
}
|
|
sync_run = LegacySyncRun(
|
|
code=f"{LEGACY_SYNC_RUN_CODE_PREFIX}-{utc_now():%Y%m%d%H%M%S%f}",
|
|
domain=BusinessDomain.PROJECTS,
|
|
source_table=LEGACY_PROJECT_QUERY_SOURCE,
|
|
status=StatusValue.DRY_RUN if dry_run else AuditStatus.SUCCESS,
|
|
finished_at=utc_now(),
|
|
created_count=created,
|
|
updated_count=updated,
|
|
skipped_count=skipped,
|
|
note=LEGACY_PROJECT_SYNC_NOTE,
|
|
)
|
|
self.db.add(sync_run)
|
|
self.db.flush()
|
|
result[LegacyResponseKey.SYNC_RUN_CODE] = sync_run.code
|
|
|
|
AuditService(self.db).record(
|
|
AuditLogCreate(
|
|
actor=actor,
|
|
source=AuditSource.LEGACY_MYSQL,
|
|
action=AuditAction.LEGACY_SYNC_PROJECTS,
|
|
target_type=BusinessDomain.PROJECTS,
|
|
risk_level=AuditRiskLevel.MEDIUM,
|
|
request_payload={
|
|
LegacyResponseKey.SOURCE_QUERY: query_ref,
|
|
LegacyResponseKey.FIELD_MAP: field_map,
|
|
LegacyResponseKey.LIMIT: limit,
|
|
LegacyResponseKey.DRY_RUN: dry_run,
|
|
},
|
|
response_payload={
|
|
key: result[key]
|
|
for key in [
|
|
LegacyResponseKey.DRY_RUN,
|
|
LegacyResponseKey.CREATED,
|
|
LegacyResponseKey.UPDATED,
|
|
LegacyResponseKey.SKIPPED,
|
|
]
|
|
},
|
|
)
|
|
)
|
|
EventService(self.db).enqueue(
|
|
event_type=EventType.LEGACY_SYNC_COMPLETED,
|
|
source=EventSource.LEGACY_MYSQL,
|
|
aggregate_type=EventAggregateType.LEGACY_SYNC_RUN,
|
|
aggregate_id=sync_run.code,
|
|
actor=actor,
|
|
payload={
|
|
EventPayloadKey.CODE: sync_run.code,
|
|
EventPayloadKey.DOMAIN: BusinessDomain.PROJECTS,
|
|
EventPayloadKey.STATUS: sync_run.status,
|
|
EventPayloadKey.CREATED: created,
|
|
EventPayloadKey.UPDATED: updated,
|
|
EventPayloadKey.SKIPPED: skipped,
|
|
},
|
|
idempotency_key=f"legacy-sync:{sync_run.code}",
|
|
)
|
|
self.db.commit()
|
|
self.db.refresh(sync_run)
|
|
return result
|