"""Changes to support groups

Revision ID: 24c31d11173d
Revises: 8eb8a3ac7bf7
Create Date: 2019-09-08 06:07:07.896551

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '24c31d11173d'
down_revision = '8eb8a3ac7bf7'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('groups_users',
    sa.Column('group_id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['group_id'], ['groups.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('group_id', 'user_id')
    )
    op.add_column('groups', sa.Column('creator_id', sa.Integer(), nullable=False))
    op.create_foreign_key('groups_creator_id_fkey', 'groups', 'users', ['creator_id'], ['id'])
    op.alter_column('users', 'email',
               existing_type=sa.VARCHAR(length=80),
               nullable=False)
    op.create_unique_constraint('users_email_key', 'users', ['email'])
    op.drop_constraint('users_group_id_fkey', 'users', type_='foreignkey')
    op.drop_column('users', 'group_id')
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('users', sa.Column('group_id', sa.INTEGER(), autoincrement=False, nullable=False))
    op.create_foreign_key('users_group_id_fkey', 'users', 'groups', ['group_id'], ['id'])
    op.drop_constraint('users_email_key', 'users', type_='unique')
    op.alter_column('users', 'email',
               existing_type=sa.VARCHAR(length=80),
               nullable=True)
    op.drop_constraint('groups_creator_id_fkey', 'groups', type_='foreignkey')
    op.drop_column('groups', 'creator_id')
    op.drop_table('groups_users')
    # ### end Alembic commands ###
