"""empty message

Revision ID: 55b9bd0159eb
Revises: 450cdb8ae33e
Create Date: 2022-06-08 15:07:23.233872

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

# revision identifiers, used by Alembic.
revision = '55b9bd0159eb'
down_revision = '450cdb8ae33e'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    new_table = op.create_table('leader',
    sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=True),
    sa.Column('project_id', postgresql.UUID(as_uuid=True), nullable=True),
    sa.ForeignKeyConstraint(['project_id'], ['project.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['user.id'], )
    )

    conn = op.get_bind()
    res = conn.execute("select id, project_leader_id from project")
    results = res.fetchall()

    old_info = [{'project_id': r[0], 'user_id': r[1]} for r in results]

    op.bulk_insert(new_table, old_info)

    op.drop_constraint('project_project_leader_id_fkey', 'project', type_='foreignkey')
    op.drop_column('project', 'project_leader_id')
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('project', sa.Column('project_leader_id', postgresql.UUID(), autoincrement=False, nullable=True))
    op.create_foreign_key('project_project_leader_id_fkey', 'project', 'user', ['project_leader_id'], ['id'])
    op.drop_table('leader')
    # ### end Alembic commands ###
