<commit_before>"""Assign User role to all existing users.

Revision ID: 5b33357784a
Revises: <PASSWORD>
Create Date: 2013-09-20 15:42:01.558543

"""

# revision identifiers, used by Alembic.
revision = '5b33357784a'
down_revision = '1<PASSWORD>'

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():<commit_msg>Fix role assignment migration so that current users are object editors and program creators and don't lose rights.<commit_after>"""Assign User role to all existing users.

Revision ID: 5b33357784a
Revises: <PASSWORD>
Create Date: 2013-09-20 15:42:01.558543

"""

# revision identifiers, used by Alembic.
revision = '5b33357784a'
down_revision = '<PASSWORD>'

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():
{'fraction_non_alphanumeric': 0.14221824686940965, 'fraction_numerical': 0.02564102564102564, 'mean_word_length': 4.217729393468118, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6351324', 'n_tokens_mistral': 1189, 'n_tokens_neox': 1116, 'n_words': 267}