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响应模型以提供 更准确的数据类型定义。 ```
127 lines
3.6 KiB
Python
127 lines
3.6 KiB
Python
"""Harden event, workflow, and owned-ledger constraints.
|
|
|
|
Revision ID: 202607150001
|
|
Revises: 202607120004
|
|
Create Date: 2026-07-15
|
|
"""
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
|
|
revision = "202607150001"
|
|
down_revision = "202607120004"
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def _scalar_count(sql: str) -> int:
|
|
return int(op.get_bind().execute(sa.text(sql)).scalar() or 0)
|
|
|
|
|
|
def _assert_owned_relations_are_consistent() -> None:
|
|
orphan_risk_actions = _scalar_count(
|
|
"""
|
|
SELECT COUNT(*)
|
|
FROM risk_event_actions action
|
|
LEFT JOIN risk_events event ON event.id = action.risk_event_id
|
|
WHERE event.id IS NULL
|
|
"""
|
|
)
|
|
if orphan_risk_actions:
|
|
raise RuntimeError(
|
|
"Cannot add risk-event foreign key: orphan risk_event_actions rows exist"
|
|
)
|
|
|
|
orphan_workflow_actions = _scalar_count(
|
|
"""
|
|
SELECT COUNT(*)
|
|
FROM workflow_actions action
|
|
LEFT JOIN workflow_instances workflow ON workflow.code = action.workflow_code
|
|
WHERE workflow.code IS NULL
|
|
"""
|
|
)
|
|
if orphan_workflow_actions:
|
|
raise RuntimeError(
|
|
"Cannot add workflow foreign key: orphan workflow_actions rows exist"
|
|
)
|
|
|
|
duplicate_workflows = _scalar_count(
|
|
"""
|
|
SELECT COUNT(*)
|
|
FROM (
|
|
SELECT workflow_type, aggregate_type, aggregate_id
|
|
FROM workflow_instances
|
|
WHERE aggregate_id IS NOT NULL
|
|
GROUP BY workflow_type, aggregate_type, aggregate_id
|
|
HAVING COUNT(*) > 1
|
|
) duplicates
|
|
"""
|
|
)
|
|
if duplicate_workflows:
|
|
raise RuntimeError(
|
|
"Cannot add workflow uniqueness constraint: duplicate aggregate workflows exist"
|
|
)
|
|
|
|
|
|
def upgrade() -> None:
|
|
_assert_owned_relations_are_consistent()
|
|
op.execute("UPDATE domain_events SET max_attempts = 3 WHERE max_attempts IS NULL")
|
|
|
|
with op.batch_alter_table("domain_events") as batch_op:
|
|
batch_op.alter_column(
|
|
"max_attempts",
|
|
existing_type=sa.Integer(),
|
|
nullable=False,
|
|
server_default=sa.text("3"),
|
|
)
|
|
|
|
with op.batch_alter_table("risk_event_actions") as batch_op:
|
|
batch_op.create_foreign_key(
|
|
"fk_risk_event_actions_risk_event_id",
|
|
"risk_events",
|
|
["risk_event_id"],
|
|
["id"],
|
|
ondelete="RESTRICT",
|
|
)
|
|
|
|
with op.batch_alter_table("workflow_actions") as batch_op:
|
|
batch_op.create_foreign_key(
|
|
"fk_workflow_actions_workflow_code",
|
|
"workflow_instances",
|
|
["workflow_code"],
|
|
["code"],
|
|
ondelete="RESTRICT",
|
|
)
|
|
|
|
with op.batch_alter_table("workflow_instances") as batch_op:
|
|
batch_op.create_unique_constraint(
|
|
"uq_workflow_aggregate",
|
|
["workflow_type", "aggregate_type", "aggregate_id"],
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
with op.batch_alter_table("workflow_instances") as batch_op:
|
|
batch_op.drop_constraint("uq_workflow_aggregate", type_="unique")
|
|
|
|
with op.batch_alter_table("workflow_actions") as batch_op:
|
|
batch_op.drop_constraint(
|
|
"fk_workflow_actions_workflow_code",
|
|
type_="foreignkey",
|
|
)
|
|
|
|
with op.batch_alter_table("risk_event_actions") as batch_op:
|
|
batch_op.drop_constraint(
|
|
"fk_risk_event_actions_risk_event_id",
|
|
type_="foreignkey",
|
|
)
|
|
|
|
with op.batch_alter_table("domain_events") as batch_op:
|
|
batch_op.alter_column(
|
|
"max_attempts",
|
|
existing_type=sa.Integer(),
|
|
nullable=True,
|
|
server_default=None,
|
|
)
|