- Create organizations table with Alembic migration (3-phase: create table, migrate data, drop old column) - Add org_id FK on championships linking to organizations - Refactor all schemas into one-class-per-file packages (auth, championship, organization, participant, registration, user) - Update CRUD layer with selectinload for organization relationships - Update frontend types and components to use nested organization object - Remove phantom Championship fields (subtitle, venue, accent_color) from frontend Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
87 lines
3.5 KiB
Python
87 lines
3.5 KiB
Python
"""add organizations table and championship ownership
|
|
|
|
Revision ID: a1b2c3d4e5f6
|
|
Revises: 43d947192af5
|
|
Create Date: 2026-03-01 18:00:00.000000
|
|
|
|
"""
|
|
import uuid
|
|
from typing import Sequence, Union
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision: str = 'a1b2c3d4e5f6'
|
|
down_revision: Union[str, None] = '43d947192af5'
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
# Phase 1: Create organizations table
|
|
op.create_table('organizations',
|
|
sa.Column('id', sa.Uuid(), nullable=False),
|
|
sa.Column('user_id', sa.Uuid(), nullable=False),
|
|
sa.Column('name', sa.String(length=255), nullable=False),
|
|
sa.Column('instagram', sa.String(length=100), nullable=True),
|
|
sa.Column('email', sa.String(length=255), nullable=True),
|
|
sa.Column('city', sa.String(length=100), nullable=True),
|
|
sa.Column('logo_url', sa.String(length=500), nullable=True),
|
|
sa.Column('verified', sa.Boolean(), nullable=False),
|
|
sa.Column('status', sa.String(length=20), nullable=False),
|
|
sa.Column('block_reason', sa.String(length=500), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
|
|
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
|
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
sa.UniqueConstraint('user_id'),
|
|
)
|
|
|
|
# Phase 1b: Add org_id to championships
|
|
with op.batch_alter_table('championships') as batch_op:
|
|
batch_op.add_column(sa.Column('org_id', sa.Uuid(), nullable=True))
|
|
batch_op.create_foreign_key('fk_championships_org_id', 'organizations', ['org_id'], ['id'], ondelete='SET NULL')
|
|
|
|
# Phase 2: Migrate existing organizer data to organizations table
|
|
conn = op.get_bind()
|
|
rows = conn.execute(
|
|
sa.text("SELECT id, organization_name FROM users WHERE role = 'organizer' AND organization_name IS NOT NULL")
|
|
).fetchall()
|
|
for row in rows:
|
|
org_id = str(uuid.uuid4())
|
|
conn.execute(
|
|
sa.text(
|
|
"INSERT INTO organizations (id, user_id, name, verified, status, created_at, updated_at) "
|
|
"VALUES (:id, :user_id, :name, 0, 'active', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)"
|
|
),
|
|
{"id": org_id, "user_id": str(row[0]), "name": row[1]}
|
|
)
|
|
|
|
# Phase 3: Remove organization_name from users (keep instagram_handle)
|
|
with op.batch_alter_table('users') as batch_op:
|
|
batch_op.drop_column('organization_name')
|
|
|
|
|
|
def downgrade() -> None:
|
|
# Re-add organization_name to users
|
|
with op.batch_alter_table('users') as batch_op:
|
|
batch_op.add_column(sa.Column('organization_name', sa.String(length=255), nullable=True))
|
|
|
|
# Migrate data back from organizations to users
|
|
conn = op.get_bind()
|
|
orgs = conn.execute(sa.text("SELECT user_id, name FROM organizations")).fetchall()
|
|
for org in orgs:
|
|
conn.execute(
|
|
sa.text("UPDATE users SET organization_name = :name WHERE id = :user_id"),
|
|
{"name": org[1], "user_id": str(org[0])}
|
|
)
|
|
|
|
# Remove org_id from championships
|
|
with op.batch_alter_table('championships') as batch_op:
|
|
batch_op.drop_constraint('fk_championships_org_id', type_='foreignkey')
|
|
batch_op.drop_column('org_id')
|
|
|
|
op.drop_table('organizations')
|