52 lines
1.8 KiB
Python
52 lines
1.8 KiB
Python
|
|
"""audit_log: append-only admin/scheduler audit trail (who/what/when/before->after)
|
||
|
|
|
||
|
|
Revision ID: 0054_audit_log
|
||
|
|
Revises: 0053_user_session_epoch
|
||
|
|
Create Date: 2026-07-12
|
||
|
|
"""
|
||
|
|
from typing import Sequence, Union
|
||
|
|
|
||
|
|
from alembic import op
|
||
|
|
import sqlalchemy as sa
|
||
|
|
|
||
|
|
revision: str = "0054_audit_log"
|
||
|
|
down_revision: Union[str, None] = "0053_user_session_epoch"
|
||
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
||
|
|
depends_on: Union[str, Sequence[str], None] = None
|
||
|
|
|
||
|
|
|
||
|
|
def upgrade() -> None:
|
||
|
|
op.create_table(
|
||
|
|
"audit_log",
|
||
|
|
sa.Column("id", sa.Integer(), primary_key=True),
|
||
|
|
# SET NULL on user delete: keep the trail (it just becomes system-attributed).
|
||
|
|
sa.Column(
|
||
|
|
"actor_id",
|
||
|
|
sa.Integer(),
|
||
|
|
sa.ForeignKey("users.id", ondelete="SET NULL"),
|
||
|
|
nullable=True,
|
||
|
|
),
|
||
|
|
sa.Column("action", sa.String(length=48), nullable=False),
|
||
|
|
sa.Column("target_type", sa.String(length=32), nullable=True),
|
||
|
|
sa.Column("target_id", sa.String(length=128), nullable=True),
|
||
|
|
sa.Column("summary", sa.String(length=255), nullable=True),
|
||
|
|
sa.Column("before", sa.JSON(), nullable=True),
|
||
|
|
sa.Column("after", sa.JSON(), nullable=True),
|
||
|
|
sa.Column(
|
||
|
|
"created_at",
|
||
|
|
sa.DateTime(timezone=True),
|
||
|
|
server_default=sa.func.now(),
|
||
|
|
nullable=False,
|
||
|
|
),
|
||
|
|
)
|
||
|
|
op.create_index("ix_audit_log_actor_id", "audit_log", ["actor_id"])
|
||
|
|
op.create_index("ix_audit_log_action", "audit_log", ["action"])
|
||
|
|
op.create_index("ix_audit_log_created_at", "audit_log", ["created_at"])
|
||
|
|
|
||
|
|
|
||
|
|
def downgrade() -> None:
|
||
|
|
op.drop_index("ix_audit_log_created_at", table_name="audit_log")
|
||
|
|
op.drop_index("ix_audit_log_action", table_name="audit_log")
|
||
|
|
op.drop_index("ix_audit_log_actor_id", table_name="audit_log")
|
||
|
|
op.drop_table("audit_log")
|