|
| 1 | +"""add security audit and login attempts |
| 2 | +
|
| 3 | +Revision ID: b2f4c7d9a1e0 |
| 4 | +Revises: aa90557ef712 |
| 5 | +Create Date: 2026-06-19 00:00:00.000000 |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +from typing import Sequence, Union |
| 10 | + |
| 11 | +from alembic import op |
| 12 | +import sqlalchemy as sa |
| 13 | + |
| 14 | + |
| 15 | +revision: str = "b2f4c7d9a1e0" |
| 16 | +down_revision: Union[str, Sequence[str], None] = "aa90557ef712" |
| 17 | +branch_labels: Union[str, Sequence[str], None] = None |
| 18 | +depends_on: Union[str, Sequence[str], None] = None |
| 19 | + |
| 20 | + |
| 21 | +def upgrade() -> None: |
| 22 | + op.create_table( |
| 23 | + "audit_logs", |
| 24 | + sa.Column("action", sa.String(length=120), nullable=False), |
| 25 | + sa.Column("actor_id", sa.String(length=64), nullable=True), |
| 26 | + sa.Column("resource_type", sa.String(length=80), nullable=True), |
| 27 | + sa.Column("resource_id", sa.String(length=64), nullable=True), |
| 28 | + sa.Column("request_id", sa.String(length=120), nullable=True), |
| 29 | + sa.Column("meta", sa.JSON(), nullable=False), |
| 30 | + sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), |
| 31 | + sa.Column("id", sa.Uuid(), nullable=False), |
| 32 | + sa.PrimaryKeyConstraint("id"), |
| 33 | + ) |
| 34 | + op.create_index("ix_audit_logs_action", "audit_logs", ["action"], unique=False) |
| 35 | + op.create_index("ix_audit_logs_actor_id", "audit_logs", ["actor_id"], unique=False) |
| 36 | + op.create_index( |
| 37 | + "ix_audit_logs_created_at", "audit_logs", ["created_at"], unique=False |
| 38 | + ) |
| 39 | + op.create_index( |
| 40 | + "ix_audit_logs_request_id", "audit_logs", ["request_id"], unique=False |
| 41 | + ) |
| 42 | + |
| 43 | + op.create_table( |
| 44 | + "login_attempts", |
| 45 | + sa.Column("email", sa.String(length=255), nullable=False), |
| 46 | + sa.Column("occurred_at", sa.DateTime(timezone=True), nullable=False), |
| 47 | + sa.Column("locked_until", sa.DateTime(timezone=True), nullable=True), |
| 48 | + sa.Column("id", sa.Uuid(), nullable=False), |
| 49 | + sa.PrimaryKeyConstraint("id"), |
| 50 | + ) |
| 51 | + op.create_index("ix_login_attempts_email", "login_attempts", ["email"], unique=False) |
| 52 | + op.create_index( |
| 53 | + "ix_login_attempts_locked_until", |
| 54 | + "login_attempts", |
| 55 | + ["locked_until"], |
| 56 | + unique=False, |
| 57 | + ) |
| 58 | + op.create_index( |
| 59 | + "ix_login_attempts_occurred_at", |
| 60 | + "login_attempts", |
| 61 | + ["occurred_at"], |
| 62 | + unique=False, |
| 63 | + ) |
| 64 | + |
| 65 | + op.create_table( |
| 66 | + "error_traces", |
| 67 | + sa.Column("error_type", sa.String(length=120), nullable=False), |
| 68 | + sa.Column("message", sa.Text(), nullable=False), |
| 69 | + sa.Column("traceback", sa.Text(), nullable=False), |
| 70 | + sa.Column("method", sa.String(length=12), nullable=False), |
| 71 | + sa.Column("path", sa.String(length=500), nullable=False), |
| 72 | + sa.Column("actor_id", sa.String(length=64), nullable=True), |
| 73 | + sa.Column("request_id", sa.String(length=120), nullable=True), |
| 74 | + sa.Column("meta", sa.JSON(), nullable=False), |
| 75 | + sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), |
| 76 | + sa.Column("id", sa.Uuid(), nullable=False), |
| 77 | + sa.PrimaryKeyConstraint("id"), |
| 78 | + ) |
| 79 | + op.create_index( |
| 80 | + "ix_error_traces_actor_id", "error_traces", ["actor_id"], unique=False |
| 81 | + ) |
| 82 | + op.create_index( |
| 83 | + "ix_error_traces_created_at", "error_traces", ["created_at"], unique=False |
| 84 | + ) |
| 85 | + op.create_index( |
| 86 | + "ix_error_traces_error_type", "error_traces", ["error_type"], unique=False |
| 87 | + ) |
| 88 | + op.create_index("ix_error_traces_path", "error_traces", ["path"], unique=False) |
| 89 | + op.create_index( |
| 90 | + "ix_error_traces_request_id", "error_traces", ["request_id"], unique=False |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +def downgrade() -> None: |
| 95 | + op.drop_index("ix_error_traces_request_id", table_name="error_traces") |
| 96 | + op.drop_index("ix_error_traces_path", table_name="error_traces") |
| 97 | + op.drop_index("ix_error_traces_error_type", table_name="error_traces") |
| 98 | + op.drop_index("ix_error_traces_created_at", table_name="error_traces") |
| 99 | + op.drop_index("ix_error_traces_actor_id", table_name="error_traces") |
| 100 | + op.drop_table("error_traces") |
| 101 | + |
| 102 | + op.drop_index("ix_login_attempts_occurred_at", table_name="login_attempts") |
| 103 | + op.drop_index("ix_login_attempts_locked_until", table_name="login_attempts") |
| 104 | + op.drop_index("ix_login_attempts_email", table_name="login_attempts") |
| 105 | + op.drop_table("login_attempts") |
| 106 | + |
| 107 | + op.drop_index("ix_audit_logs_request_id", table_name="audit_logs") |
| 108 | + op.drop_index("ix_audit_logs_created_at", table_name="audit_logs") |
| 109 | + op.drop_index("ix_audit_logs_actor_id", table_name="audit_logs") |
| 110 | + op.drop_index("ix_audit_logs_action", table_name="audit_logs") |
| 111 | + op.drop_table("audit_logs") |
0 commit comments