"""Email invites for joining a team.

Revision ID: 51d04d0e7e6f
Revises: 34fd69f63809
Create Date: 2014-09-15 23:51:35.478232

"""

# revision identifiers, used by Alembic.
revision = '51d04d0e7e6f'
down_revision = '34fd69f63809'

from alembic import op
import sqlalchemy as sa


def upgrade(tables):
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('teammemberinvite',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.Column('email', sa.String(length=255), nullable=True),
    sa.Column('team_id', sa.Integer(), nullable=False),
    sa.Column('inviter_id', sa.Integer(), nullable=False),
    sa.Column('invite_token', sa.String(length=255), nullable=False),
    sa.ForeignKeyConstraint(['inviter_id'], ['user.id'], ),
    sa.ForeignKeyConstraint(['team_id'], ['team.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('teammemberinvite_inviter_id', 'teammemberinvite', ['inviter_id'], unique=False)
    op.create_index('teammemberinvite_team_id', 'teammemberinvite', ['team_id'], unique=False)
    op.create_index('teammemberinvite_user_id', 'teammemberinvite', ['user_id'], unique=False)
    ### end Alembic commands ###

    # Manually add the new logentrykind types
    op.bulk_insert(tables.logentrykind,
    [
        {'id':42, 'name':'org_invite_team_member'},
        {'id':43, 'name':'org_team_member_invite_accepted'},
        {'id':44, 'name':'org_team_member_invite_declined'},
        {'id':45, 'name':'org_delete_team_member_invite'},
    ])

    op.bulk_insert(tables.notificationkind,
    [
        {'id':10, 'name':'org_team_invite'},
    ])

def downgrade(tables):
    ### commands auto generated by Alembic - please adjust! ###
    op.execute(
        (tables.logentrykind.delete()
         .where(tables.logentrykind.c.name == op.inline_literal('org_invite_team_member')))
        )

    op.execute(
        (tables.logentrykind.delete()
         .where(tables.logentrykind.c.name == op.inline_literal('org_team_member_invite_accepted')))
        )

    op.execute(
        (tables.logentrykind.delete()
         .where(tables.logentrykind.c.name == op.inline_literal('org_team_member_invite_declined')))
        )

    op.execute(
        (tables.logentrykind.delete()
         .where(tables.logentrykind.c.name == op.inline_literal('org_delete_team_member_invite')))
        )

    op.execute(
        (tables.notificationkind.delete()
         .where(tables.notificationkind.c.name == op.inline_literal('org_team_invite')))
        )

    op.drop_table('teammemberinvite')
    ### end Alembic commands ###
