"""Add clinician_id to user, and missing index on email field

Revision ID: 6395e14d5ee5
Revises: 974b82f86387
Create Date: 2020-09-23 16:55:19.534624

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '6395e14d5ee5'
down_revision = '974b82f86387'


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        'users', sa.Column('clinician_id', sa.Integer(), nullable=True))
    op.create_index(
        op.f('ix_users_clinician_id'),
        'users',
        ['clinician_id'],
        unique=False)
    op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
    op.drop_constraint('users_email_key', 'users', type_='unique')
    op.create_foreign_key(None, 'users', 'users', ['clinician_id'], ['id'])
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'users', type_='foreignkey')
    op.create_unique_constraint('users_email_key', 'users', ['email'])
    op.drop_index(op.f('ix_users_email'), table_name='users')
    op.drop_index(op.f('ix_users_clinician_id'), table_name='users')
    op.drop_column('users', 'clinician_id')
    # ### end Alembic commands ###

"""Add clinician_id to user, and missing index on email field

Revision ID: 6395e14d5ee5
Revises: 974b82f86387
Create Date: 2020-09-23 16:55:19.534624

"""
from alembic import op
from sqlalchemy.sql import text
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '6395e14d5ee5'
down_revision = '974b82f86387'


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    conn = op.get_bind()
    op.add_column(
        'users', sa.Column('clinician_id', sa.Integer(), nullable=True))
    op.create_index(
        op.f('ix_users_clinician_id'),
        'users',
        ['clinician_id'],
        unique=False)
    op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
    conn.execute(text("ALTER TABLE users DROP CONSTRAINT IF EXISTS user_email_key"))
    conn.execute(text("ALTER TABLE users DROP CONSTRAINT IF EXISTS users_email_key"))
    op.create_foreign_key(None, 'users', 'users', ['clinician_id'], ['id'])
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'users', type_='foreignkey')
    op.create_unique_constraint('users_email_key', 'users', ['email'])
    op.drop_index(op.f('ix_users_email'), table_name='users')
    op.drop_index(op.f('ix_users_clinician_id'), table_name='users')
    op.drop_column('users', 'clinician_id')
    # ### end Alembic commands ###

Drop both versions of unique email constraint (#3807)