2025-12-19 02:07:25 +07:00
|
|
|
"""Make admin_id nullable in admin_logs for system actions
|
|
|
|
|
|
|
|
|
|
Revision ID: 017_admin_logs_nullable_admin_id
|
|
|
|
|
Revises: 016_add_banned_until
|
|
|
|
|
Create Date: 2024-12-19
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
from typing import Sequence, Union
|
|
|
|
|
|
|
|
|
|
from alembic import op
|
|
|
|
|
import sqlalchemy as sa
|
2025-12-19 02:23:50 +07:00
|
|
|
from sqlalchemy import inspect
|
2025-12-19 02:07:25 +07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
|
|
|
|
revision: str = '017_admin_logs_nullable_admin_id'
|
|
|
|
|
down_revision: Union[str, None] = '016_add_banned_until'
|
|
|
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
|
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|
|
|
|
|
|
|
|
|
|
2025-12-19 02:23:50 +07:00
|
|
|
def is_column_nullable(table_name: str, column_name: str) -> bool:
|
|
|
|
|
"""Check if a column is nullable."""
|
|
|
|
|
bind = op.get_bind()
|
|
|
|
|
inspector = inspect(bind)
|
|
|
|
|
columns = inspector.get_columns(table_name)
|
|
|
|
|
for col in columns:
|
|
|
|
|
if col['name'] == column_name:
|
|
|
|
|
return col.get('nullable', True)
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
2025-12-19 02:07:25 +07:00
|
|
|
def upgrade() -> None:
|
|
|
|
|
# Make admin_id nullable for system actions (like auto-unban)
|
2025-12-19 02:23:50 +07:00
|
|
|
# Only alter if currently not nullable
|
|
|
|
|
if not is_column_nullable('admin_logs', 'admin_id'):
|
|
|
|
|
op.alter_column('admin_logs', 'admin_id',
|
|
|
|
|
existing_type=sa.Integer(),
|
|
|
|
|
nullable=True)
|
2025-12-19 02:07:25 +07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def downgrade() -> None:
|
|
|
|
|
# Revert to not nullable (will fail if there are NULL values)
|
2025-12-19 02:23:50 +07:00
|
|
|
if is_column_nullable('admin_logs', 'admin_id'):
|
|
|
|
|
op.alter_column('admin_logs', 'admin_id',
|
|
|
|
|
existing_type=sa.Integer(),
|
|
|
|
|
nullable=False)
|