"""Rename gid to default_gid in usergroups and modules

Revision ID: 3520cf26d8d8
Revises: 4ff2bc4eb9bf
Create Date: 2015-07-21 22:51:53.672633

"""

# revision identifiers, used by Alembic.
revision = '3520cf26d8d8'
down_revision = '4ff2bc4eb9bf'

from alembic import op
import sqlalchemy as sa


UPGRADE = """
"""
DOWNGRADE = """
"""


def iter_statements(stmts):
    for st in [x for x in stmts.split('\n') if x]:
        op.execute(st)


def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("users") as batch_op:
        batch_op.add_column(sa.Column('default_gid', sa.Integer(), nullable=True))
        batch_op.create_foreign_key('fk_defaul_gid_usergroups', 'usergroups', ['default_gid'], ['id'])

    with op.batch_alter_table("modules") as batch_op:
        batch_op.add_column(sa.Column('default_gid', sa.Integer(), nullable=True))
        batch_op.create_foreign_key('fk_defaukt_gid_usergroups', 'usergroups', ['default_gid'], ['id'])
    iter_statements(UPGRADE)

    with op.batch_alter_table("modules") as batch_op:
        batch_op.drop_column('gid')
    with op.batch_alter_table("users") as batch_op:
        batch_op.drop_column('gid')
    ### end Alembic commands ###


def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.add_column('users', sa.Column('gid', sa.INTEGER(), autoincrement=False, nullable=True))
    op.add_column('modules', sa.Column('gid', sa.INTEGER(), autoincrement=False, nullable=True))
    iter_statements(DOWNGRADE)
    op.drop_column('users', 'default_gid')
    op.drop_column('modules', 'default_gid')
    ### end Alembic commands ###
