Fix migrations
This commit is contained in:
@@ -9,6 +9,7 @@ from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import inspect
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
@@ -18,15 +19,29 @@ branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# Make admin_id nullable for system actions (like auto-unban)
|
||||
op.alter_column('admin_logs', 'admin_id',
|
||||
existing_type=sa.Integer(),
|
||||
nullable=True)
|
||||
# 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)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# Revert to not nullable (will fail if there are NULL values)
|
||||
op.alter_column('admin_logs', 'admin_id',
|
||||
existing_type=sa.Integer(),
|
||||
nullable=False)
|
||||
if is_column_nullable('admin_logs', 'admin_id'):
|
||||
op.alter_column('admin_logs', 'admin_id',
|
||||
existing_type=sa.Integer(),
|
||||
nullable=False)
|
||||
|
||||
Reference in New Issue
Block a user