Files
game-marathon/backend/alembic/versions/001_add_roles_system.py

103 lines
3.7 KiB
Python
Raw Normal View History

2025-12-14 20:21:56 +07:00
"""Add roles system
Revision ID: 001_add_roles
Revises:
Create Date: 2024-12-14
"""
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-14 20:21:56 +07:00
# revision identifiers, used by Alembic.
revision: str = '001_add_roles'
down_revision: Union[str, None] = None
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 column_exists(table_name: str, column_name: str) -> bool:
bind = op.get_bind()
inspector = inspect(bind)
columns = [col['name'] for col in inspector.get_columns(table_name)]
return column_name in columns
def constraint_exists(table_name: str, constraint_name: str) -> bool:
bind = op.get_bind()
inspector = inspect(bind)
fks = inspector.get_foreign_keys(table_name)
return any(fk['name'] == constraint_name for fk in fks)
2025-12-14 20:21:56 +07:00
def upgrade() -> None:
# Add role column to users table
2025-12-19 02:23:50 +07:00
if not column_exists('users', 'role'):
op.add_column('users', sa.Column('role', sa.String(20), nullable=False, server_default='user'))
2025-12-14 20:21:56 +07:00
# Add role column to participants table
2025-12-19 02:23:50 +07:00
if not column_exists('participants', 'role'):
op.add_column('participants', sa.Column('role', sa.String(20), nullable=False, server_default='participant'))
2025-12-14 20:21:56 +07:00
# Rename organizer_id to creator_id in marathons table
2025-12-19 02:23:50 +07:00
if column_exists('marathons', 'organizer_id') and not column_exists('marathons', 'creator_id'):
op.alter_column('marathons', 'organizer_id', new_column_name='creator_id')
2025-12-14 20:21:56 +07:00
# Update existing participants: set role='organizer' for marathon creators
2025-12-19 02:23:50 +07:00
# This is idempotent - running multiple times is safe
2025-12-14 20:21:56 +07:00
op.execute("""
UPDATE participants p
SET role = 'organizer'
FROM marathons m
WHERE p.marathon_id = m.id AND p.user_id = m.creator_id
""")
# Add status column to games table
2025-12-19 02:23:50 +07:00
if not column_exists('games', 'status'):
op.add_column('games', sa.Column('status', sa.String(20), nullable=False, server_default='approved'))
2025-12-14 20:21:56 +07:00
# Rename added_by_id to proposed_by_id in games table
2025-12-19 02:23:50 +07:00
if column_exists('games', 'added_by_id') and not column_exists('games', 'proposed_by_id'):
op.alter_column('games', 'added_by_id', new_column_name='proposed_by_id')
2025-12-14 20:21:56 +07:00
# Add approved_by_id column to games table
2025-12-19 02:23:50 +07:00
if not column_exists('games', 'approved_by_id'):
op.add_column('games', sa.Column('approved_by_id', sa.Integer(), nullable=True))
if not constraint_exists('games', 'fk_games_approved_by_id'):
op.create_foreign_key(
'fk_games_approved_by_id',
'games', 'users',
['approved_by_id'], ['id'],
ondelete='SET NULL'
)
2025-12-14 20:21:56 +07:00
def downgrade() -> None:
# Remove approved_by_id from games
2025-12-19 02:23:50 +07:00
if constraint_exists('games', 'fk_games_approved_by_id'):
op.drop_constraint('fk_games_approved_by_id', 'games', type_='foreignkey')
if column_exists('games', 'approved_by_id'):
op.drop_column('games', 'approved_by_id')
2025-12-14 20:21:56 +07:00
# Rename proposed_by_id back to added_by_id
2025-12-19 02:23:50 +07:00
if column_exists('games', 'proposed_by_id'):
op.alter_column('games', 'proposed_by_id', new_column_name='added_by_id')
2025-12-14 20:21:56 +07:00
# Remove status from games
2025-12-19 02:23:50 +07:00
if column_exists('games', 'status'):
op.drop_column('games', 'status')
2025-12-14 20:21:56 +07:00
# Rename creator_id back to organizer_id
2025-12-19 02:23:50 +07:00
if column_exists('marathons', 'creator_id'):
op.alter_column('marathons', 'creator_id', new_column_name='organizer_id')
2025-12-14 20:21:56 +07:00
# Remove role from participants
2025-12-19 02:23:50 +07:00
if column_exists('participants', 'role'):
op.drop_column('participants', 'role')
2025-12-14 20:21:56 +07:00
# Remove role from users
2025-12-19 02:23:50 +07:00
if column_exists('users', 'role'):
op.drop_column('users', 'role')