- 复制来源: /home/yueli/track @ 192c8ee (feature/ai-audit-update) - 组织隔离目标: LICA - 端口规划: 前端 8030 / 后端 8031 / 数据库 8032 - 已排除 deploy.sh、deploy_full.sh、docker-compose.prod.yml(IRIS 生产发布脚本) - 已排除工作区未提交改动,取干净的 192c8ee 状态
82 lines
3.7 KiB
Python
82 lines
3.7 KiB
Python
"""add_audit_logs
|
||
|
||
Revision ID: j1k2l3m4n5o6
|
||
Revises: i1j2k3l4m5n6
|
||
Create Date: 2026-09-21
|
||
|
||
操作审计日志表(audit_logs)
|
||
--------------------------
|
||
新增一张独立的审计表,用于记录 task_logs 覆盖不到的操作:
|
||
登录、导出、产品增删改、收口、权限/配置变更等与单个任务无关的动作。
|
||
|
||
为什么另起一张表而不复用 task_logs:
|
||
task_logs.task_id 是 NOT NULL 外键,只能挂在任务上,无法表达「张三导出了
|
||
产品清单」这类动作;且缺少来源 IP / UA / 结果状态等审计必需字段。
|
||
|
||
存量数据无需回填(本表从上线时刻开始记录)。
|
||
"""
|
||
from typing import Sequence, Union
|
||
|
||
import sqlalchemy as sa
|
||
from alembic import op
|
||
from sqlalchemy.dialects import postgresql
|
||
|
||
revision: str = "j1k2l3m4n5o6"
|
||
down_revision: Union[str, None] = "i1j2k3l4m5n6"
|
||
branch_labels: Union[str, Sequence[str], None] = None
|
||
depends_on: Union[str, Sequence[str], None] = None
|
||
|
||
|
||
def upgrade() -> None:
|
||
op.create_table(
|
||
"audit_logs",
|
||
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True, nullable=False),
|
||
# 操作人
|
||
sa.Column("user_id", sa.String(64), nullable=True, comment="操作人账号(逻辑外键→MOM)"),
|
||
sa.Column("display_name", sa.String(100), nullable=True, comment="操作人显示名"),
|
||
sa.Column("role", sa.String(50), nullable=True, comment="操作时角色快照"),
|
||
# 业务语义
|
||
sa.Column("action", sa.String(50), nullable=False, comment="动作"),
|
||
sa.Column("module", sa.String(50), nullable=False, comment="业务模块"),
|
||
sa.Column("target_type", sa.String(50), nullable=True),
|
||
sa.Column("target_id", sa.String(100), nullable=True),
|
||
sa.Column("target_name", sa.String(200), nullable=True),
|
||
sa.Column("details", postgresql.JSONB(), nullable=True, comment="变更详情"),
|
||
# 请求上下文
|
||
sa.Column("ip_address", sa.String(50), nullable=True),
|
||
sa.Column("user_agent", sa.String(500), nullable=True),
|
||
sa.Column("method", sa.String(10), nullable=True),
|
||
sa.Column("url", sa.String(500), nullable=True),
|
||
sa.Column("status_code", sa.Integer(), nullable=True),
|
||
sa.Column("error_message", sa.Text(), nullable=True),
|
||
# 与结构化日志对账
|
||
sa.Column("request_id", sa.String(64), nullable=True),
|
||
sa.Column(
|
||
"created_at",
|
||
sa.DateTime(timezone=True),
|
||
nullable=False,
|
||
server_default=sa.func.now(),
|
||
),
|
||
)
|
||
|
||
# 索引:按审计页最常用的检索维度建
|
||
op.create_index("ix_audit_logs_created_at", "audit_logs", ["created_at"])
|
||
op.create_index("ix_audit_logs_user_id", "audit_logs", ["user_id"])
|
||
op.create_index("ix_audit_logs_module", "audit_logs", ["module"])
|
||
op.create_index("ix_audit_logs_action", "audit_logs", ["action"])
|
||
op.create_index("ix_audit_logs_target_id", "audit_logs", ["target_id"])
|
||
op.create_index("ix_audit_logs_request_id", "audit_logs", ["request_id"])
|
||
# 组合索引:审计页默认「按时间倒序 + 按模块/动作过滤」
|
||
op.create_index("ix_audit_logs_module_created", "audit_logs", ["module", "created_at"])
|
||
|
||
|
||
def downgrade() -> None:
|
||
op.drop_index("ix_audit_logs_module_created", table_name="audit_logs")
|
||
op.drop_index("ix_audit_logs_request_id", table_name="audit_logs")
|
||
op.drop_index("ix_audit_logs_target_id", table_name="audit_logs")
|
||
op.drop_index("ix_audit_logs_action", table_name="audit_logs")
|
||
op.drop_index("ix_audit_logs_module", table_name="audit_logs")
|
||
op.drop_index("ix_audit_logs_user_id", table_name="audit_logs")
|
||
op.drop_index("ix_audit_logs_created_at", table_name="audit_logs")
|
||
op.drop_table("audit_logs")
|