Add events

This commit is contained in:
2025-12-15 03:22:29 +07:00
parent 1a882fb2e0
commit 4239ea8516
31 changed files with 7288 additions and 75 deletions

View File

@@ -0,0 +1,60 @@
"""Add events table and auto_events_enabled to marathons
Revision ID: 004_add_events
Revises: 003_create_admin
Create Date: 2024-12-14
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '004_add_events'
down_revision: Union[str, None] = '003_create_admin'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# Create events table if it doesn't exist
conn = op.get_bind()
inspector = sa.inspect(conn)
tables = inspector.get_table_names()
if 'events' not in tables:
op.create_table(
'events',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('marathon_id', sa.Integer(), nullable=False),
sa.Column('type', sa.String(30), nullable=False),
sa.Column('start_time', sa.DateTime(), nullable=False),
sa.Column('end_time', sa.DateTime(), nullable=True),
sa.Column('is_active', sa.Boolean(), nullable=False, server_default='true'),
sa.Column('created_by_id', sa.Integer(), nullable=True),
sa.Column('data', sa.JSON(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=False, server_default=sa.func.now()),
sa.PrimaryKeyConstraint('id'),
sa.ForeignKeyConstraint(['marathon_id'], ['marathons.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['created_by_id'], ['users.id'], ondelete='SET NULL'),
)
# Create index if it doesn't exist
indexes = [idx['name'] for idx in inspector.get_indexes('events')]
if 'ix_events_marathon_id' not in indexes:
op.create_index('ix_events_marathon_id', 'events', ['marathon_id'])
# Add auto_events_enabled to marathons if it doesn't exist
columns = [col['name'] for col in inspector.get_columns('marathons')]
if 'auto_events_enabled' not in columns:
op.add_column(
'marathons',
sa.Column('auto_events_enabled', sa.Boolean(), nullable=False, server_default='true')
)
def downgrade() -> None:
op.drop_column('marathons', 'auto_events_enabled')
op.drop_index('ix_events_marathon_id', table_name='events')
op.drop_table('events')

View File

@@ -0,0 +1,34 @@
"""Add event_type to assignments
Revision ID: 005_add_assignment_event_type
Revises: 004_add_events
Create Date: 2024-12-14
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '005_add_assignment_event_type'
down_revision: Union[str, None] = '004_add_events'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# Add event_type column to assignments if it doesn't exist
conn = op.get_bind()
inspector = sa.inspect(conn)
columns = [col['name'] for col in inspector.get_columns('assignments')]
if 'event_type' not in columns:
op.add_column(
'assignments',
sa.Column('event_type', sa.String(30), nullable=True)
)
def downgrade() -> None:
op.drop_column('assignments', 'event_type')

View File

@@ -0,0 +1,54 @@
"""Add swap_requests table for two-sided swap confirmation
Revision ID: 006_add_swap_requests
Revises: 005_assignment_event
Create Date: 2024-12-15
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '006_add_swap_requests'
down_revision: Union[str, None] = '005_add_assignment_event_type'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# Create swap_requests table if it doesn't exist
conn = op.get_bind()
inspector = sa.inspect(conn)
tables = inspector.get_table_names()
if 'swap_requests' not in tables:
op.create_table(
'swap_requests',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('event_id', sa.Integer(), nullable=False),
sa.Column('from_participant_id', sa.Integer(), nullable=False),
sa.Column('to_participant_id', sa.Integer(), nullable=False),
sa.Column('from_assignment_id', sa.Integer(), nullable=False),
sa.Column('to_assignment_id', sa.Integer(), nullable=False),
sa.Column('status', sa.String(20), nullable=False, server_default='pending'),
sa.Column('created_at', sa.DateTime(), nullable=False, server_default=sa.func.now()),
sa.Column('responded_at', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.ForeignKeyConstraint(['event_id'], ['events.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['from_participant_id'], ['participants.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['to_participant_id'], ['participants.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['from_assignment_id'], ['assignments.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['to_assignment_id'], ['assignments.id'], ondelete='CASCADE'),
)
op.create_index('ix_swap_requests_event_id', 'swap_requests', ['event_id'])
op.create_index('ix_swap_requests_from_participant_id', 'swap_requests', ['from_participant_id'])
op.create_index('ix_swap_requests_to_participant_id', 'swap_requests', ['to_participant_id'])
def downgrade() -> None:
op.drop_index('ix_swap_requests_to_participant_id', table_name='swap_requests')
op.drop_index('ix_swap_requests_from_participant_id', table_name='swap_requests')
op.drop_index('ix_swap_requests_event_id', table_name='swap_requests')
op.drop_table('swap_requests')