"""Auto generated users

Revision ID: d244ea9f09df
Revises: ec8724ff64dc
Create Date: 2020-11-16 06:39:57.017040

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

# revision identifiers, used by Alembic.
revision = 'd244ea9f09df'
down_revision = 'ec8724ff64dc'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint('fk_group_users_group_id', 'group_users', type_='foreignkey')
    op.drop_constraint('fk_group_users_user_id', 'group_users', type_='foreignkey')
    op.create_foreign_key('fk_group_users_user_id', 'group_users', 'users', ['user_id'], ['id'], ondelete='CASCADE')
    op.create_foreign_key('fk_group_users_group_id', 'group_users', 'groups', ['group_id'], ['id'], ondelete='CASCADE')
    op.create_unique_constraint(op.f('uq_groups_id'), 'groups', ['id'])
    op.create_unique_constraint(op.f('uq_reset_passwords_id'), 'reset_passwords', ['id'])
    op.drop_constraint('fk_tokens_user_id', 'tokens', type_='foreignkey')
    op.create_foreign_key('fk_tokens_user_id', 'tokens', 'users', ['user_id'], ['id'], ondelete='CASCADE')
    op.add_column('users', sa.Column('autogenerated', sa.Boolean(), nullable=True))
    op.drop_constraint('fk_verification_emails_user_id', 'verification_emails', type_='foreignkey')
    op.create_foreign_key('fk_verification_emails_user_id', 'verification_emails', 'users', ['user_id'], ['id'], ondelete='CASCADE')
    
    # Manual
    op.execute("UPDATE users SET autogenerated = false")
    op.alter_column('users', 'autogenerated', nullable=False)
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint('fk_verification_emails_user_id', 'verification_emails', type_='foreignkey')
    op.create_foreign_key('fk_verification_emails_user_id', 'verification_emails', 'users', ['user_id'], ['id'])
    op.drop_column('users', 'autogenerated')
    op.drop_constraint('fk_tokens_user_id', 'tokens', type_='foreignkey')
    op.create_foreign_key('fk_tokens_user_id', 'tokens', 'users', ['user_id'], ['id'])
    op.drop_constraint(op.f('uq_reset_passwords_id'), 'reset_passwords', type_='unique')
    op.drop_constraint(op.f('uq_groups_id'), 'groups', type_='unique')
    op.drop_constraint('fk_group_users_group_id', 'group_users', type_='foreignkey')
    op.drop_constraint('fk_group_users_user_id', 'group_users', type_='foreignkey')
    op.create_foreign_key('fk_group_users_user_id', 'group_users', 'users', ['user_id'], ['id'])
    op.create_foreign_key('fk_group_users_group_id', 'group_users', 'groups', ['group_id'], ['id'])
    # ### end Alembic commands ###
