"""Assign User role to all existing users.

Revision ID: 5b33357784a
Revises: 1afc3824d35b
Create Date: 2013-09-20 15:42:01.558543

"""

# revision identifiers, used by Alembic.
revision = '5b33357784a'
down_revision = '1afc3824d35b'

import sqlalchemy as sa
from alembic import op
from sqlalchemy.sql import table, column, select

person_table = table('people',
    column('id', sa.Integer),
    )

role_table = table('roles',
    column('id', sa.Integer),
    column('name', sa.String),
    )

user_roles_table = table('user_roles',
    column('id', sa.Integer),
    column('role_id', sa.Integer),
    column('person_id', sa.Integer),
    column('context_id', sa.Integer),
    )

def upgrade():
  users = select([person_table.c.id])
  user_role = select([role_table.c.id])\
      .where(role_table.c.name == 'User')\
      .limit(1)
  #FIXME this could be done better in a more recent version of sqlalchemy
  #once 0.8.3 is released
  #op.execute(user_roles_table.insert()\
      #.from_select(['user_id'], users)\
      #.from_select(['role_id'], role)\
      #.values(context_id=None,))

  #FIXME workaround until we can do the proper static generation of the sql
  #statement
  connection = op.get_bind()
  users = connection.execute(users).fetchall()
  user_role = connection.execute(user_role).fetchone()
  for user in users:
    op.execute(user_roles_table.insert().values(
      person_id=user['id'], role_id=user_role['id'], context_id=None))

def downgrade():
  '''Intentionally does nothing as we can't distinguish between migration
  added assignments and not.
  '''
  pass

"""Assign User role to all existing users.

Revision ID: 5b33357784a
Revises: 1afc3824d35b
Create Date: 2013-09-20 15:42:01.558543

"""

# revision identifiers, used by Alembic.
revision = '5b33357784a'
down_revision = '1afc3824d35b'

import sqlalchemy as sa
from alembic import op
from sqlalchemy.sql import table, column, select

person_table = table('people',
    column('id', sa.Integer),
    )

role_table = table('roles',
    column('id', sa.Integer),
    column('name', sa.String),
    )

user_roles_table = table('user_roles',
    column('id', sa.Integer),
    column('role_id', sa.Integer),
    column('person_id', sa.Integer),
    column('context_id', sa.Integer),
    )

def upgrade():
  users = select([person_table.c.id])
  object_editor = select([role_table.c.id])\
      .where(role_table.c.name == 'ObjectEditor')\
      .limit(1)
  program_creator = select([role_table.c.id])\
      .where(role_table.c.name == 'ProgramCreator')\
      .limit(1)
  #FIXME this could be done better in a more recent version of sqlalchemy
  #once 0.8.3 is released
  #op.execute(user_roles_table.insert()\
      #.from_select(['user_id'], users)\
      #.from_select(['role_id'], role)\
      #.values(context_id=None,))

  #FIXME workaround until we can do the proper static generation of the sql
  #statement
  connection = op.get_bind()
  users = connection.execute(users).fetchall()
  object_editor = connection.execute(object_editor).fetchone()
  for user in users:
    op.execute(user_roles_table.insert().values(
      person_id=user['id'], role_id=object_editor['id'], context_id=None))
    op.execute(user_roles_table.insert().values(
      person_id=user['id'], role_id=program_creator['id'], context_id=None))

def downgrade():
  '''Intentionally does nothing as we can't distinguish between migration
  added assignments and not.
  '''
  pass

Fix role assignment migration so that current users are object editors and program creators and don't lose rights.