/ migrations / versions / 014_add_audit_log.py
014_add_audit_log.py
 1  from alembic import op
 2  import sqlalchemy as sa
 3  
 4  revision = '014'
 5  down_revision = '013'
 6  branch_labels = None
 7  depends_on = None
 8  
 9  def upgrade():
10      try:
11          op.create_table(
12              'audit_log',
13              sa.Column('id', sa.Integer(), primary_key=True, index=True),
14              sa.Column('user_id', sa.Integer(), nullable=True),
15              sa.Column('username', sa.String(255), nullable=True),
16              sa.Column('action', sa.String(10), nullable=False),
17              sa.Column('resource', sa.String(500), nullable=False),
18              sa.Column('status_code', sa.Integer(), nullable=False),
19              sa.Column('date', sa.DateTime(), nullable=False, index=True),
20          )
21      except Exception as e:
22          print(f"Error creating audit_log: {e}")
23  
24  def downgrade():
25      try:
26          op.drop_table('audit_log')
27      except Exception as e:
28          print(f"Error dropping audit_log: {e}")