Compare commits

...
@@ -0,0 +1,95 @@
"""Add wecom notify/callback audit tables
Revision ID: b71e5c9d4a2f
Revises: eff84b4f6171
Create Date: 2026-10-30 11:30:00.000000
用途:补齐 WecomNotifyAudit / WecomCallbackAudit 两个模型的建表迁移。
背景:这两张表历史上由 db.create_all() 顺手创建,迁移链中缺失,
导致重建/新建的数据库缺少 wecom_notify_audits,管理端删除用户时
清理企微触达审计报 UndefinedTable(HTTP 500)。本迁移对已存在该表
的环境(老库)不做任何变更,仅补建缺失的表。
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'b71e5c9d4a2f'
down_revision = 'eff84b4f6171'
branch_labels = None
depends_on = None
def _create_notify_audits() -> None:
op.create_table(
'wecom_notify_audits',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('event_type', sa.String(length=64), nullable=False),
sa.Column('status', sa.String(length=20), nullable=False, server_default='sent'),
sa.Column('trace_id', sa.String(length=64), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('username', sa.String(length=50), nullable=True),
sa.Column('wecom_userid', sa.String(length=128), nullable=True),
sa.Column('title', sa.String(length=120), nullable=True),
sa.Column('content', sa.Text(), nullable=True),
sa.Column('jump_url', sa.String(length=512), nullable=True),
sa.Column('error_code', sa.String(length=64), nullable=True),
sa.Column('error_message', sa.String(length=255), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('opened_at', sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], name='fk_wecom_notify_audits_user_id_users'),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('trace_id', name='uq_wecom_notify_audits_trace_id'),
)
op.create_index('ix_wecom_notify_audits_event_type', 'wecom_notify_audits', ['event_type'])
op.create_index('ix_wecom_notify_audits_status', 'wecom_notify_audits', ['status'])
op.create_index('ix_wecom_notify_audits_trace_id', 'wecom_notify_audits', ['trace_id'], unique=True)
op.create_index('ix_wecom_notify_audits_user_id', 'wecom_notify_audits', ['user_id'])
op.create_index('ix_wecom_notify_audits_created_at', 'wecom_notify_audits', ['created_at'])
op.create_index('ix_wecom_notify_audits_opened_at', 'wecom_notify_audits', ['opened_at'])
def _create_callback_audits() -> None:
op.create_table(
'wecom_callback_audits',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('msg_signature', sa.String(length=128), nullable=True),
sa.Column('timestamp', sa.String(length=32), nullable=True),
sa.Column('nonce', sa.String(length=64), nullable=True),
sa.Column('encrypt_raw', sa.Text(), nullable=True),
sa.Column('decrypted_xml', sa.Text(), nullable=True),
sa.Column('msg_type', sa.String(length=32), nullable=True),
sa.Column('event', sa.String(length=64), nullable=True),
sa.Column('from_user', sa.String(length=128), nullable=True),
sa.Column('agent_id', sa.String(length=32), nullable=True),
sa.Column('msg_id', sa.String(length=64), nullable=True),
sa.Column('error_message', sa.String(length=255), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint('id'),
)
op.create_index('ix_wecom_callback_audits_msg_type', 'wecom_callback_audits', ['msg_type'])
op.create_index('ix_wecom_callback_audits_event', 'wecom_callback_audits', ['event'])
op.create_index('ix_wecom_callback_audits_from_user', 'wecom_callback_audits', ['from_user'])
op.create_index('ix_wecom_callback_audits_msg_id', 'wecom_callback_audits', ['msg_id'])
op.create_index('ix_wecom_callback_audits_created_at', 'wecom_callback_audits', ['created_at'])
def upgrade():
conn = op.get_bind()
insp = sa.inspect(conn)
tables = set(insp.get_table_names())
if 'wecom_notify_audits' not in tables:
_create_notify_audits()
if 'wecom_callback_audits' not in tables:
_create_callback_audits()
def downgrade():
conn = op.get_bind()
insp = sa.inspect(conn)
tables = set(insp.get_table_names())
if 'wecom_callback_audits' in tables:
op.drop_table('wecom_callback_audits')
if 'wecom_notify_audits' in tables:
op.drop_table('wecom_notify_audits')