"""Add partner tables

Revision ID: e866ad0e78e1
Revises: 0aaad1740797
Create Date: 2022-05-05 12:10:01.229457

"""
import sqlalchemy_utils
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'e866ad0e78e1'
down_revision = '0aaad1740797'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('partner',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('created_at', sqlalchemy_utils.types.arrow.ArrowType(), nullable=False),
    sa.Column('updated_at', sqlalchemy_utils.types.arrow.ArrowType(), nullable=True),
    sa.Column('name', sa.String(length=128), nullable=False),
    sa.Column('contact_email', sa.String(length=128), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('contact_email'),
    sa.UniqueConstraint('name')
    )
    op.create_table('partner_api_token',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('created_at', sqlalchemy_utils.types.arrow.ArrowType(), nullable=False),
    sa.Column('updated_at', sqlalchemy_utils.types.arrow.ArrowType(), nullable=True),
    sa.Column('token', sa.String(length=32), nullable=False),
    sa.Column('partner_id', sa.Integer(), nullable=False),
    sa.Column('expiration_time', sqlalchemy_utils.types.arrow.ArrowType(), nullable=True),
    sa.ForeignKeyConstraint(['partner_id'], ['partner.id'], ondelete='cascade'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_partner_api_token_partner_id'), 'partner_api_token', ['partner_id'], unique=False)
    op.create_index(op.f('ix_partner_api_token_token'), 'partner_api_token', ['token'], unique=True)
    op.create_table('partner_user',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('created_at', sqlalchemy_utils.types.arrow.ArrowType(), nullable=False),
    sa.Column('updated_at', sqlalchemy_utils.types.arrow.ArrowType(), nullable=True),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('partner_id', sa.Integer(), nullable=False),
    sa.Column('partner_email', sa.String(length=255), nullable=True),
    sa.ForeignKeyConstraint(['partner_id'], ['partner.id'], ondelete='cascade'),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='cascade'),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('user_id', 'partner_id', name='uq_user_id_partner_id')
    )
    op.create_index(op.f('ix_partner_user_partner_id'), 'partner_user', ['partner_id'], unique=False)
    op.create_index(op.f('ix_partner_user_user_id'), 'partner_user', ['user_id'], unique=False)
    op.add_column('users', sa.Column('partner_id', sa.BigInteger(), nullable=True))
    op.add_column('users', sa.Column('partner_user_id', sa.String(length=128), nullable=True))
    op.create_unique_constraint('uq_partner_id_partner_user_id', 'users', ['partner_id', 'partner_user_id'])
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint('uq_partner_id_partner_user_id', 'users', type_='unique')
    op.drop_column('users', 'partner_user_id')
    op.drop_column('users', 'partner_id')
    op.drop_index(op.f('ix_partner_user_user_id'), table_name='partner_user')
    op.drop_index(op.f('ix_partner_user_partner_id'), table_name='partner_user')
    op.drop_table('partner_user')
    op.drop_index(op.f('ix_partner_api_token_token'), table_name='partner_api_token')
    op.drop_index(op.f('ix_partner_api_token_partner_id'), table_name='partner_api_token')
    op.drop_table('partner_api_token')
    op.drop_table('partner')
    # ### end Alembic commands ###
