Compare commits
2 Commits
develop
...
13f484e726
| Author | SHA1 | Date | |
|---|---|---|---|
| 13f484e726 | |||
| ebaf6d39ea |
@@ -9,6 +9,7 @@ from typing import Sequence, Union
|
|||||||
|
|
||||||
from alembic import op
|
from alembic import op
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy import inspect
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
# revision identifiers, used by Alembic.
|
||||||
revision: str = '001_add_roles'
|
revision: str = '001_add_roles'
|
||||||
@@ -17,17 +18,35 @@ branch_labels: Union[str, Sequence[str], None] = None
|
|||||||
depends_on: Union[str, Sequence[str], None] = None
|
depends_on: Union[str, Sequence[str], None] = None
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
def upgrade() -> None:
|
def upgrade() -> None:
|
||||||
# Add role column to users table
|
# Add role column to users table
|
||||||
op.add_column('users', sa.Column('role', sa.String(20), nullable=False, server_default='user'))
|
if not column_exists('users', 'role'):
|
||||||
|
op.add_column('users', sa.Column('role', sa.String(20), nullable=False, server_default='user'))
|
||||||
|
|
||||||
# Add role column to participants table
|
# Add role column to participants table
|
||||||
op.add_column('participants', sa.Column('role', sa.String(20), nullable=False, server_default='participant'))
|
if not column_exists('participants', 'role'):
|
||||||
|
op.add_column('participants', sa.Column('role', sa.String(20), nullable=False, server_default='participant'))
|
||||||
|
|
||||||
# Rename organizer_id to creator_id in marathons table
|
# Rename organizer_id to creator_id in marathons table
|
||||||
op.alter_column('marathons', 'organizer_id', new_column_name='creator_id')
|
if column_exists('marathons', 'organizer_id') and not column_exists('marathons', 'creator_id'):
|
||||||
|
op.alter_column('marathons', 'organizer_id', new_column_name='creator_id')
|
||||||
|
|
||||||
# Update existing participants: set role='organizer' for marathon creators
|
# Update existing participants: set role='organizer' for marathon creators
|
||||||
|
# This is idempotent - running multiple times is safe
|
||||||
op.execute("""
|
op.execute("""
|
||||||
UPDATE participants p
|
UPDATE participants p
|
||||||
SET role = 'organizer'
|
SET role = 'organizer'
|
||||||
@@ -36,37 +55,48 @@ def upgrade() -> None:
|
|||||||
""")
|
""")
|
||||||
|
|
||||||
# Add status column to games table
|
# Add status column to games table
|
||||||
op.add_column('games', sa.Column('status', sa.String(20), nullable=False, server_default='approved'))
|
if not column_exists('games', 'status'):
|
||||||
|
op.add_column('games', sa.Column('status', sa.String(20), nullable=False, server_default='approved'))
|
||||||
|
|
||||||
# Rename added_by_id to proposed_by_id in games table
|
# Rename added_by_id to proposed_by_id in games table
|
||||||
op.alter_column('games', 'added_by_id', new_column_name='proposed_by_id')
|
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')
|
||||||
|
|
||||||
# Add approved_by_id column to games table
|
# Add approved_by_id column to games table
|
||||||
op.add_column('games', sa.Column('approved_by_id', sa.Integer(), nullable=True))
|
if not column_exists('games', 'approved_by_id'):
|
||||||
op.create_foreign_key(
|
op.add_column('games', sa.Column('approved_by_id', sa.Integer(), nullable=True))
|
||||||
'fk_games_approved_by_id',
|
if not constraint_exists('games', 'fk_games_approved_by_id'):
|
||||||
'games', 'users',
|
op.create_foreign_key(
|
||||||
['approved_by_id'], ['id'],
|
'fk_games_approved_by_id',
|
||||||
ondelete='SET NULL'
|
'games', 'users',
|
||||||
)
|
['approved_by_id'], ['id'],
|
||||||
|
ondelete='SET NULL'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def downgrade() -> None:
|
def downgrade() -> None:
|
||||||
# Remove approved_by_id from games
|
# Remove approved_by_id from games
|
||||||
op.drop_constraint('fk_games_approved_by_id', 'games', type_='foreignkey')
|
if constraint_exists('games', 'fk_games_approved_by_id'):
|
||||||
op.drop_column('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')
|
||||||
|
|
||||||
# Rename proposed_by_id back to added_by_id
|
# Rename proposed_by_id back to added_by_id
|
||||||
op.alter_column('games', 'proposed_by_id', new_column_name='added_by_id')
|
if column_exists('games', 'proposed_by_id'):
|
||||||
|
op.alter_column('games', 'proposed_by_id', new_column_name='added_by_id')
|
||||||
|
|
||||||
# Remove status from games
|
# Remove status from games
|
||||||
op.drop_column('games', 'status')
|
if column_exists('games', 'status'):
|
||||||
|
op.drop_column('games', 'status')
|
||||||
|
|
||||||
# Rename creator_id back to organizer_id
|
# Rename creator_id back to organizer_id
|
||||||
op.alter_column('marathons', 'creator_id', new_column_name='organizer_id')
|
if column_exists('marathons', 'creator_id'):
|
||||||
|
op.alter_column('marathons', 'creator_id', new_column_name='organizer_id')
|
||||||
|
|
||||||
# Remove role from participants
|
# Remove role from participants
|
||||||
op.drop_column('participants', 'role')
|
if column_exists('participants', 'role'):
|
||||||
|
op.drop_column('participants', 'role')
|
||||||
|
|
||||||
# Remove role from users
|
# Remove role from users
|
||||||
op.drop_column('users', 'role')
|
if column_exists('users', 'role'):
|
||||||
|
op.drop_column('users', 'role')
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ from typing import Sequence, Union
|
|||||||
|
|
||||||
from alembic import op
|
from alembic import op
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy import inspect
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
# revision identifiers, used by Alembic.
|
||||||
revision: str = '002_marathon_settings'
|
revision: str = '002_marathon_settings'
|
||||||
@@ -17,16 +18,27 @@ branch_labels: Union[str, Sequence[str], None] = None
|
|||||||
depends_on: Union[str, Sequence[str], None] = None
|
depends_on: Union[str, Sequence[str], None] = None
|
||||||
|
|
||||||
|
|
||||||
|
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 upgrade() -> None:
|
def upgrade() -> None:
|
||||||
# Add is_public column to marathons table (default False = private)
|
# Add is_public column to marathons table (default False = private)
|
||||||
op.add_column('marathons', sa.Column('is_public', sa.Boolean(), nullable=False, server_default='false'))
|
if not column_exists('marathons', 'is_public'):
|
||||||
|
op.add_column('marathons', sa.Column('is_public', sa.Boolean(), nullable=False, server_default='false'))
|
||||||
|
|
||||||
# Add game_proposal_mode column to marathons table
|
# Add game_proposal_mode column to marathons table
|
||||||
# 'all_participants' - anyone can propose games (with moderation)
|
# 'all_participants' - anyone can propose games (with moderation)
|
||||||
# 'organizer_only' - only organizers can add games
|
# 'organizer_only' - only organizers can add games
|
||||||
op.add_column('marathons', sa.Column('game_proposal_mode', sa.String(20), nullable=False, server_default='all_participants'))
|
if not column_exists('marathons', 'game_proposal_mode'):
|
||||||
|
op.add_column('marathons', sa.Column('game_proposal_mode', sa.String(20), nullable=False, server_default='all_participants'))
|
||||||
|
|
||||||
|
|
||||||
def downgrade() -> None:
|
def downgrade() -> None:
|
||||||
op.drop_column('marathons', 'game_proposal_mode')
|
if column_exists('marathons', 'game_proposal_mode'):
|
||||||
op.drop_column('marathons', 'is_public')
|
op.drop_column('marathons', 'game_proposal_mode')
|
||||||
|
if column_exists('marathons', 'is_public'):
|
||||||
|
op.drop_column('marathons', 'is_public')
|
||||||
|
|||||||
@@ -17,15 +17,17 @@ depends_on = None
|
|||||||
|
|
||||||
def upgrade() -> None:
|
def upgrade() -> None:
|
||||||
# Update event type from 'rematch' to 'game_choice' in events table
|
# Update event type from 'rematch' to 'game_choice' in events table
|
||||||
|
# These UPDATE statements are idempotent - safe to run multiple times
|
||||||
op.execute("UPDATE events SET type = 'game_choice' WHERE type = 'rematch'")
|
op.execute("UPDATE events SET type = 'game_choice' WHERE type = 'rematch'")
|
||||||
|
|
||||||
# Update event_type in assignments table
|
# Update event_type in assignments table
|
||||||
op.execute("UPDATE assignments SET event_type = 'game_choice' WHERE event_type = 'rematch'")
|
op.execute("UPDATE assignments SET event_type = 'game_choice' WHERE event_type = 'rematch'")
|
||||||
|
|
||||||
# Update activity data that references rematch event
|
# Update activity data that references rematch event
|
||||||
|
# Cast JSON to JSONB, apply jsonb_set, then cast back to JSON
|
||||||
op.execute("""
|
op.execute("""
|
||||||
UPDATE activities
|
UPDATE activities
|
||||||
SET data = jsonb_set(data, '{event_type}', '"game_choice"')
|
SET data = jsonb_set(data::jsonb, '{event_type}', '"game_choice"')::json
|
||||||
WHERE data->>'event_type' = 'rematch'
|
WHERE data->>'event_type' = 'rematch'
|
||||||
""")
|
""")
|
||||||
|
|
||||||
@@ -36,6 +38,6 @@ def downgrade() -> None:
|
|||||||
op.execute("UPDATE assignments SET event_type = 'rematch' WHERE event_type = 'game_choice'")
|
op.execute("UPDATE assignments SET event_type = 'rematch' WHERE event_type = 'game_choice'")
|
||||||
op.execute("""
|
op.execute("""
|
||||||
UPDATE activities
|
UPDATE activities
|
||||||
SET data = jsonb_set(data, '{event_type}', '"rematch"')
|
SET data = jsonb_set(data::jsonb, '{event_type}', '"rematch"')::json
|
||||||
WHERE data->>'event_type' = 'game_choice'
|
WHERE data->>'event_type' = 'game_choice'
|
||||||
""")
|
""")
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ from typing import Sequence, Union
|
|||||||
|
|
||||||
from alembic import op
|
from alembic import op
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy import inspect
|
||||||
|
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
# revision identifiers, used by Alembic.
|
||||||
@@ -18,13 +19,26 @@ branch_labels: Union[str, Sequence[str], None] = None
|
|||||||
depends_on: Union[str, Sequence[str], None] = None
|
depends_on: Union[str, Sequence[str], None] = None
|
||||||
|
|
||||||
|
|
||||||
|
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 upgrade() -> None:
|
def upgrade() -> None:
|
||||||
op.add_column('users', sa.Column('telegram_first_name', sa.String(100), nullable=True))
|
if not column_exists('users', 'telegram_first_name'):
|
||||||
op.add_column('users', sa.Column('telegram_last_name', sa.String(100), nullable=True))
|
op.add_column('users', sa.Column('telegram_first_name', sa.String(100), nullable=True))
|
||||||
op.add_column('users', sa.Column('telegram_avatar_url', sa.String(500), nullable=True))
|
if not column_exists('users', 'telegram_last_name'):
|
||||||
|
op.add_column('users', sa.Column('telegram_last_name', sa.String(100), nullable=True))
|
||||||
|
if not column_exists('users', 'telegram_avatar_url'):
|
||||||
|
op.add_column('users', sa.Column('telegram_avatar_url', sa.String(500), nullable=True))
|
||||||
|
|
||||||
|
|
||||||
def downgrade() -> None:
|
def downgrade() -> None:
|
||||||
op.drop_column('users', 'telegram_avatar_url')
|
if column_exists('users', 'telegram_avatar_url'):
|
||||||
op.drop_column('users', 'telegram_last_name')
|
op.drop_column('users', 'telegram_avatar_url')
|
||||||
op.drop_column('users', 'telegram_first_name')
|
if column_exists('users', 'telegram_last_name'):
|
||||||
|
op.drop_column('users', 'telegram_last_name')
|
||||||
|
if column_exists('users', 'telegram_first_name'):
|
||||||
|
op.drop_column('users', 'telegram_first_name')
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ from typing import Sequence, Union
|
|||||||
|
|
||||||
from alembic import op
|
from alembic import op
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy import inspect
|
||||||
|
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
# revision identifiers, used by Alembic.
|
||||||
@@ -18,11 +19,22 @@ branch_labels: Union[str, Sequence[str], None] = None
|
|||||||
depends_on: Union[str, Sequence[str], None] = None
|
depends_on: Union[str, Sequence[str], None] = None
|
||||||
|
|
||||||
|
|
||||||
|
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 upgrade() -> None:
|
def upgrade() -> None:
|
||||||
op.add_column('challenges', sa.Column('proposed_by_id', sa.Integer(), sa.ForeignKey('users.id'), nullable=True))
|
if not column_exists('challenges', 'proposed_by_id'):
|
||||||
op.add_column('challenges', sa.Column('status', sa.String(20), server_default='approved', nullable=False))
|
op.add_column('challenges', sa.Column('proposed_by_id', sa.Integer(), sa.ForeignKey('users.id'), nullable=True))
|
||||||
|
if not column_exists('challenges', 'status'):
|
||||||
|
op.add_column('challenges', sa.Column('status', sa.String(20), server_default='approved', nullable=False))
|
||||||
|
|
||||||
|
|
||||||
def downgrade() -> None:
|
def downgrade() -> None:
|
||||||
op.drop_column('challenges', 'status')
|
if column_exists('challenges', 'status'):
|
||||||
op.drop_column('challenges', 'proposed_by_id')
|
op.drop_column('challenges', 'status')
|
||||||
|
if column_exists('challenges', 'proposed_by_id'):
|
||||||
|
op.drop_column('challenges', 'proposed_by_id')
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ from typing import Sequence, Union
|
|||||||
|
|
||||||
from alembic import op
|
from alembic import op
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy import inspect
|
||||||
|
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
# revision identifiers, used by Alembic.
|
||||||
@@ -18,15 +19,30 @@ branch_labels: Union[str, Sequence[str], None] = None
|
|||||||
depends_on: Union[str, Sequence[str], None] = None
|
depends_on: Union[str, Sequence[str], None] = None
|
||||||
|
|
||||||
|
|
||||||
|
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 upgrade() -> None:
|
def upgrade() -> None:
|
||||||
op.add_column('users', sa.Column('is_banned', sa.Boolean(), server_default='false', nullable=False))
|
if not column_exists('users', 'is_banned'):
|
||||||
op.add_column('users', sa.Column('banned_at', sa.DateTime(), nullable=True))
|
op.add_column('users', sa.Column('is_banned', sa.Boolean(), server_default='false', nullable=False))
|
||||||
op.add_column('users', sa.Column('banned_by_id', sa.Integer(), sa.ForeignKey('users.id'), nullable=True))
|
if not column_exists('users', 'banned_at'):
|
||||||
op.add_column('users', sa.Column('ban_reason', sa.String(500), nullable=True))
|
op.add_column('users', sa.Column('banned_at', sa.DateTime(), nullable=True))
|
||||||
|
if not column_exists('users', 'banned_by_id'):
|
||||||
|
op.add_column('users', sa.Column('banned_by_id', sa.Integer(), sa.ForeignKey('users.id'), nullable=True))
|
||||||
|
if not column_exists('users', 'ban_reason'):
|
||||||
|
op.add_column('users', sa.Column('ban_reason', sa.String(500), nullable=True))
|
||||||
|
|
||||||
|
|
||||||
def downgrade() -> None:
|
def downgrade() -> None:
|
||||||
op.drop_column('users', 'ban_reason')
|
if column_exists('users', 'ban_reason'):
|
||||||
op.drop_column('users', 'banned_by_id')
|
op.drop_column('users', 'ban_reason')
|
||||||
op.drop_column('users', 'banned_at')
|
if column_exists('users', 'banned_by_id'):
|
||||||
op.drop_column('users', 'is_banned')
|
op.drop_column('users', 'banned_by_id')
|
||||||
|
if column_exists('users', 'banned_at'):
|
||||||
|
op.drop_column('users', 'banned_at')
|
||||||
|
if column_exists('users', 'is_banned'):
|
||||||
|
op.drop_column('users', 'is_banned')
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ from typing import Sequence, Union
|
|||||||
|
|
||||||
from alembic import op
|
from alembic import op
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy import inspect
|
||||||
|
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
# 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
|
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:
|
def upgrade() -> None:
|
||||||
# Make admin_id nullable for system actions (like auto-unban)
|
# Make admin_id nullable for system actions (like auto-unban)
|
||||||
op.alter_column('admin_logs', 'admin_id',
|
# Only alter if currently not nullable
|
||||||
existing_type=sa.Integer(),
|
if not is_column_nullable('admin_logs', 'admin_id'):
|
||||||
nullable=True)
|
op.alter_column('admin_logs', 'admin_id',
|
||||||
|
existing_type=sa.Integer(),
|
||||||
|
nullable=True)
|
||||||
|
|
||||||
|
|
||||||
def downgrade() -> None:
|
def downgrade() -> None:
|
||||||
# Revert to not nullable (will fail if there are NULL values)
|
# Revert to not nullable (will fail if there are NULL values)
|
||||||
op.alter_column('admin_logs', 'admin_id',
|
if is_column_nullable('admin_logs', 'admin_id'):
|
||||||
existing_type=sa.Integer(),
|
op.alter_column('admin_logs', 'admin_id',
|
||||||
nullable=False)
|
existing_type=sa.Integer(),
|
||||||
|
nullable=False)
|
||||||
|
|||||||
Reference in New Issue
Block a user