Skip to content

Commit e10ebc2

Browse files
Merge pull request #23 from codewithme-py/fix/add-migrations-with-auditlogs
feat: add AuditLog model and create audit_logs database table.
2 parents 9634e9f + 4bafd78 commit e10ebc2

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

migrations/env.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from sqlalchemy.engine import Connection
77
from sqlalchemy.ext.asyncio import async_engine_from_config
88

9+
from app.core.audit_log.models import AuditLog
910
from app.core.config import settings
1011
from app.core.database import Base
1112
from app.services.inventory.models import Product, Reservation
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""Add audit log model
2+
3+
Revision ID: dd775d4f45bb
4+
Revises: 8e607d73b2b3
5+
Create Date: 2026-03-16 19:52:54.853052
6+
7+
"""
8+
9+
from collections.abc import Sequence
10+
11+
import sqlalchemy as sa
12+
from alembic import op
13+
14+
# revision identifiers, used by Alembic.
15+
revision: str = 'dd775d4f45bb'
16+
down_revision: str | Sequence[str] | None = '8e607d73b2b3'
17+
branch_labels: str | Sequence[str] | None = None
18+
depends_on: str | Sequence[str] | None = None
19+
20+
21+
def upgrade() -> None:
22+
"""Upgrade schema."""
23+
# ### commands auto generated by Alembic - please adjust! ###
24+
op.create_table(
25+
'audit_logs',
26+
sa.Column('id', sa.UUID(), nullable=False),
27+
sa.Column('target_type', sa.String(), nullable=False),
28+
sa.Column('target_id', sa.UUID(), nullable=False),
29+
sa.Column('actor_id', sa.UUID(), nullable=False),
30+
sa.Column('action', sa.String(), nullable=False),
31+
sa.Column('changes', sa.JSON(), nullable=False),
32+
sa.Column(
33+
'created_at',
34+
sa.DateTime(timezone=True),
35+
server_default=sa.text('now()'),
36+
nullable=False,
37+
),
38+
sa.Column('remote_ip', sa.String(length=45), nullable=True),
39+
sa.Column('request_id', sa.String(), nullable=True),
40+
sa.ForeignKeyConstraint(
41+
['actor_id'],
42+
['users.id'],
43+
),
44+
sa.PrimaryKeyConstraint('id'),
45+
)
46+
op.create_index(
47+
op.f('ix_audit_logs_action'), 'audit_logs', ['action'], unique=False
48+
)
49+
op.create_index(
50+
op.f('ix_audit_logs_actor_id'), 'audit_logs', ['actor_id'], unique=False
51+
)
52+
op.create_index(
53+
op.f('ix_audit_logs_created_at'), 'audit_logs', ['created_at'], unique=False
54+
)
55+
op.create_index(
56+
op.f('ix_audit_logs_remote_ip'), 'audit_logs', ['remote_ip'], unique=False
57+
)
58+
op.create_index(
59+
op.f('ix_audit_logs_request_id'), 'audit_logs', ['request_id'], unique=False
60+
)
61+
op.create_index(
62+
op.f('ix_audit_logs_target_id'), 'audit_logs', ['target_id'], unique=False
63+
)
64+
op.create_index(
65+
op.f('ix_audit_logs_target_type'), 'audit_logs', ['target_type'], unique=False
66+
)
67+
op.create_index(
68+
op.f('ix_products_owner_id'), 'products', ['owner_id'], unique=False
69+
)
70+
# ### end Alembic commands ###
71+
72+
73+
def downgrade() -> None:
74+
"""Downgrade schema."""
75+
# ### commands auto generated by Alembic - please adjust! ###
76+
op.drop_index(op.f('ix_products_owner_id'), table_name='products')
77+
op.drop_index(op.f('ix_audit_logs_target_type'), table_name='audit_logs')
78+
op.drop_index(op.f('ix_audit_logs_target_id'), table_name='audit_logs')
79+
op.drop_index(op.f('ix_audit_logs_request_id'), table_name='audit_logs')
80+
op.drop_index(op.f('ix_audit_logs_remote_ip'), table_name='audit_logs')
81+
op.drop_index(op.f('ix_audit_logs_created_at'), table_name='audit_logs')
82+
op.drop_index(op.f('ix_audit_logs_actor_id'), table_name='audit_logs')
83+
op.drop_index(op.f('ix_audit_logs_action'), table_name='audit_logs')
84+
op.drop_table('audit_logs')
85+
# ### end Alembic commands ###

0 commit comments

Comments
 (0)