"""Added group to custom form.

Revision ID: c0687b086ee3
Revises: b364a538f0db
Create Date: 2018-07-09 20:09:05.368186

"""
from alembic import op
import sqlalchemy as sa


from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship

# revision identifiers, used by Alembic.
revision = 'c0687b086ee3'
down_revision = 'b364a538f0db'

Base = declarative_base()
db = sa
db.Model = Base
db.relationship = relationship


def create_session():
    connection = op.get_bind()
    session_maker = sa.orm.sessionmaker()
    session = session_maker(bind=connection)
    db.session = session


def upgrade():
    create_session()

    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('custom_form', sa.Column('group_id', sa.Integer(), nullable=True))
    op.drop_index('idx_36097_owner_id', table_name='custom_form')
    op.drop_constraint('custom_form_ibfk_1', 'custom_form', type_='foreignkey')
    op.create_foreign_key(op.f('fk_custom_form_group_id_group'), 'custom_form', 'group', ['group_id'], ['id'])
    op.drop_column('custom_form', 'owner_id')
    # ### end Alembic commands ###


def downgrade():
    create_session()

    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('custom_form', sa.Column('owner_id', sa.BIGINT(), autoincrement=False, nullable=True))
    op.drop_constraint(op.f('fk_custom_form_group_id_group'), 'custom_form', type_='foreignkey')
    op.create_foreign_key('custom_form_ibfk_1', 'custom_form', 'user', ['owner_id'], ['id'], onupdate='RESTRICT', ondelete='RESTRICT')
    op.create_index('idx_36097_owner_id', 'custom_form', ['owner_id'], unique=False)
    op.drop_column('custom_form', 'group_id')
    # ### end Alembic commands ###


# vim: ft=python
