"""Add leaderboard table add leaderboardscores

Revision ID: 6b45cfe1799c
Revises: 3f2ec6253b7e
Create Date: 2022-05-19 21:09:02.690868

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "6b45cfe1799c"
down_revision = "3f2ec6253b7e"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "leaderboards",
        sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("title", sa.VARCHAR(length=128), nullable=False),
        sa.Column("description", sa.String(), nullable=True),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("TIMEZONE('utc', statement_timestamp())"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("TIMEZONE('utc', statement_timestamp())"),
            nullable=False,
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_leaderboards")),
        sa.UniqueConstraint("id", name=op.f("uq_leaderboards_id")),
    )
    op.create_table(
        "leaderboard_scores",
        sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("leaderboard_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("address", sa.VARCHAR(length=256), nullable=False),
        sa.Column("score", sa.BigInteger(), nullable=False),
        sa.Column(
            "points_data", postgresql.JSONB(astext_type=sa.Text()), nullable=True
        ),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("TIMEZONE('utc', statement_timestamp())"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("TIMEZONE('utc', statement_timestamp())"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["leaderboard_id"],
            ["leaderboards.id"],
            name=op.f("fk_leaderboard_scores_leaderboard_id_leaderboards"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_leaderboard_scores")),
        sa.UniqueConstraint("id", name=op.f("uq_leaderboard_scores_id")),
        sa.UniqueConstraint(
            "leaderboard_id",
            "address",
            name=op.f("uq_leaderboard_scores_leaderboard_id"),
        ),
    )
    op.create_index(
        op.f("ix_leaderboard_scores_address"),
        "leaderboard_scores",
        ["address"],
        unique=False,
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(
        op.f("ix_leaderboard_scores_address"), table_name="leaderboard_scores"
    )
    op.drop_table("leaderboard_scores")
    op.drop_table("leaderboards")
    # ### end Alembic commands ###
