"""Add default roles and UserTeam model

Revision ID: 99242b3e4f37
Revises: c659f9e3539a
Create Date: 2020-04-11 20:12:54.420574

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.sql import table, column
from sqlalchemy import String, Integer

# revision identifiers, used by Alembic.
revision = "99242b3e4f37"
down_revision = "c659f9e3539a"
branch_labels = None
depends_on = None

role_table = table(
    "role", column("id", Integer), column("name", String), column("description", String)
)


def upgrade():
    op.execute(role_table.delete())
    op.bulk_insert(
        role_table,
        [
            {"name": "superadmin", "description": "Super Admin"},
            {"name": "dataprovider", "description": "Data Provider"},
        ],
    )

    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "user_team",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=False),
        sa.Column("team_id", sa.Integer(), nullable=False),
        sa.Column("role", sa.String(), nullable=False),
        sa.ForeignKeyConstraint(
            ["team_id"],
            ["teams.team_id"],
            name=op.f("fk_user_team_team_id_teams"),
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["user.id"],
            name=op.f("fk_user_team_user_id_user"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_user_team")),
        sa.UniqueConstraint("user_id", "team_id", name=op.f("uq_user_team_user_id")),
    )
    op.create_index(
        op.f("ix_user_team_team_id"), "user_team", ["team_id"], unique=False
    )
    op.create_index(
        op.f("ix_user_team_user_id"), "user_team", ["user_id"], unique=False
    )
    # ### end Alembic commands ###


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