old_contents: """empty message
  
  Revision ID: 49380ad07c88
  Revises: 0031_add_manage_team_permission
  Create Date: 2016-03-01 17:08:12.184393
  
  """
  
  # revision identifiers, used by Alembic.
  revision = '49380ad07c88'
  down_revision = '0031_add_manage_team_permission'
  
  from alembic import op
  import sqlalchemy as sa
  
  
  def upgrade():
      ### commands auto generated by Alembic - please adjust! ###
      conn = op.get_bind()
      permissions = conn.execute("SELECT (id, permission) FROM permissions").fetchall()
      print(permissions)
      op.drop_column('permissions', 'permission')
      op.drop_constraint('uix_service_user_permission', 'permissions', type_='unique')
      op.add_column('permissions', sa.Column('permission', sa.Enum('manage_service', 'send_messages', 'manage_api_keys', 'manage_templates', 'manage_team', 'view_activity', name='permission_types'), nullable=False))
      for p in permissions:
      	conn.execute("UPDATE permissions SET permission='{}' WHERE ID='{}'".format(p[1], p[0]))
      op.create_unique_constraint('uix_service_user_permission', 'permissions', ['service_id', 'user_id', '_permission'])
      ### end Alembic commands ###
  
  
  def downgrade():
      ### commands auto generated by Alembic - please adjust! ###
      conn = op.get_bind()
      permissions = conn.execute("SELECT (id, permission) FROM permissions").fetchall()
      op.drop_column('permissions', 'permission')
      op.drop_constraint('uix_service_user_permission', 'permissions', type_='unique')
      op.add_column('permissions', sa.Column('permission', sa.VARCHAR(length=255), autoincrement=False, nullable=False))
      for p in permissions:
      	conn.execute("UPDATE permissions SET permission='{}' WHERE ID='{}'".format(p[1], p[0]))
      op.create_unique_constraint('uix_service_user_permission', 'permissions', ['service_id', 'user_id', 'permission'])
      ### end Alembic commands ###
  
new_contents: """empty message
  
  Revision ID: 0032_update_permission_to_enum
  Revises: 0031_add_manage_team_permission
  Create Date: 2016-03-01 17:08:12.184393
  
  """
  
  # revision identifiers, used by Alembic.
  revision = '0032_update_permission_to_enum'
  down_revision = '0031_add_manage_team_permission'
  
  from alembic import op
  import sqlalchemy as sa
  
  
  def upgrade():
      ### commands auto generated by Alembic - please adjust! ###
      conn = op.get_bind()
      permissions = conn.execute("SELECT id, permission FROM permissions").fetchall()
      op.drop_constraint('uix_service_user_permission', 'permissions', type_='unique')
      op.drop_column('permissions', 'permission')
      permission_types = sa.Enum('manage_service', 'send_messages', 'manage_api_keys', 'manage_templates', 'manage_team', 'view_activity', name='permission_types')
      permission_types.create(op.get_bind())
      op.add_column('permissions', sa.Column('permission', permission_types, nullable=True))
      for p in permissions:
      	conn.execute("UPDATE permissions SET permission='{}' WHERE id='{}'".format(str(p[1]), str(p[0])))
      op.create_unique_constraint('uix_service_user_permission', 'permissions', ['service_id', 'user_id', 'permission'])
      op.alter_column('permissions', 'permission', nullable=False)
      ### end Alembic commands ###
  
  
  def downgrade():
      ### commands auto generated by Alembic - please adjust! ###
      conn = op.get_bind()
      permissions = conn.execute("SELECT id, permission FROM permissions").fetchall()
      op.drop_constraint('uix_service_user_permission', 'permissions', type_='unique')
      op.drop_column('permissions', 'permission')
      try:
          sa.Enum(name='permission_types').drop(conn, checkfirst=False)
      except:
          pass
      op.add_column('permissions', sa.Column('permission', sa.VARCHAR(length=255), autoincrement=False, nullable=True))
      for p in permissions:
      	conn.execute("UPDATE permissions SET permission='{}' WHERE ID='{}'".format(str(p[1]), str(p[0])))
      op.create_unique_constraint('uix_service_user_permission', 'permissions', ['service_id', 'user_id', 'permission'])
      op.alter_column('permissions', 'permission', nullable=False)
      ### end Alembic commands ###
  
subject: Upgrade and downgrade now works.