61 lines
2.1 KiB
Python
61 lines
2.1 KiB
Python
"""Create audit_log table
|
|
|
|
Revision ID: create_audit_log
|
|
Revises: add_soft_delete_admin
|
|
Create Date: 2024-11-11 00:00:00.000000
|
|
|
|
"""
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
from sqlalchemy.dialects import postgresql
|
|
from sqlalchemy.engine import reflection
|
|
|
|
# revision identifiers
|
|
revision = 'create_audit_log'
|
|
down_revision = 'add_soft_delete_admin'
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade():
|
|
"""创建审计日志表"""
|
|
# 获取数据库连接和表信息
|
|
bind = op.get_bind()
|
|
inspector = reflection.Inspector.from_engine(bind)
|
|
|
|
# 获取现有表
|
|
existing_tables = inspector.get_table_names()
|
|
|
|
# 只有当表不存在时才创建
|
|
if 'audit_log' not in existing_tables:
|
|
# 创建 audit_log 表
|
|
op.create_table('audit_log',
|
|
sa.Column('log_id', sa.Integer(), nullable=False),
|
|
sa.Column('admin_id', sa.Integer(), nullable=False),
|
|
sa.Column('action', sa.String(length=32), nullable=False),
|
|
sa.Column('target_type', sa.String(length=32), nullable=False),
|
|
sa.Column('target_id', sa.Integer(), nullable=True),
|
|
sa.Column('details', sa.Text(), nullable=True),
|
|
sa.Column('ip_address', sa.String(length=32), nullable=True),
|
|
sa.Column('user_agent', sa.String(length=256), nullable=True),
|
|
sa.Column('create_time', sa.DateTime(), nullable=False),
|
|
sa.ForeignKeyConstraint(['admin_id'], ['admin.admin_id'], ),
|
|
sa.PrimaryKeyConstraint('log_id')
|
|
)
|
|
|
|
# 创建索引
|
|
op.create_index('ix_audit_log_admin_id', 'audit_log', ['admin_id'])
|
|
op.create_index('ix_audit_log_action', 'audit_log', ['action'])
|
|
op.create_index('ix_audit_log_create_time', 'audit_log', ['create_time'])
|
|
|
|
|
|
def downgrade():
|
|
"""删除审计日志表"""
|
|
# 删除索引
|
|
op.drop_index('ix_audit_log_create_time', table_name='audit_log')
|
|
op.drop_index('ix_audit_log_action', table_name='audit_log')
|
|
op.drop_index('ix_audit_log_admin_id', table_name='audit_log')
|
|
|
|
# 删除表
|
|
op.drop_table('audit_log')
|