<commit_before>"""remove obsolete personal groups

Revision ID: 5e7b5346c330
Revises: <PASSWORD>
Create Date: 2018-05-29 16:04:20.724597

"""

import os, sys
cur_file = os.path.abspath(__file__)
root_dir = os.path.dirname(cur_file)    # version
root_dir = os.path.dirname(root_dir)    # alembic
root_dir = os.path.dirname(root_dir)    # magpie
root_dir = os.path.dirname(root_dir)    # root
sys.path.insert(0, root_dir)

from alembic import op
from alembic.context import get_context
from sqlalchemy.dialects.postgresql.base import PGDialect
from sqlalchemy.orm import sessionmaker
from magpie import models, ANONYMOUS_USER, ADMIN_GROUP, USER_GROUP
from ziggurat_definitions import *

Session = sessionmaker()

# revision identifiers, used by Alembic.
revision = '5e7b5346c330'
down_revision = '2a6c63397399'
branch_labels = None
depends_on = None


def upgrade():
    context = get_context()
    session = Session(bind=op.get_bind())
    if isinstance(context.connection.engine.dialect, PGDialect):
        all_users = session.query(models.User)
        all_groups = session.query(models.Group)
        all_user_group_refs = models.UserGroup.all(db_session=session)

        ignore_users = {ANONYMOUS_USER}
        ignore_groups = {ADMIN_GROUP, USER_GROUP}
        user_names = {usr.user_name for usr in all_users} - ignore_users

        # parse through 'personal' groups matching an existing user
        for group in all_groups:
            group_name = group.group_name
            if group_name in user_names and group_name not in ignore_groups:

                # get the real user
                user = UserService.by_user_name(user_name=group_name, db_session=session)
<commit_msg>add db migration remove ANONYMOUS group<commit_after>"""remove obsolete personal groups

Revision ID: 5e7b5346c330
Revises: 2a6c63397399
Create Date: 2018-05-29 16:04:20.724597

"""

import os, sys
cur_file = os.path.abspath(__file__)
root_dir = os.path.dirname(cur_file)    # version
root_dir = os.path.dirname(root_dir)    # alembic
root_dir = os.path.dirname(root_dir)    # magpie
root_dir = os.path.dirname(root_dir)    # root
sys.path.insert(0, root_dir)

from alembic import op
from alembic.context import get_context
from sqlalchemy.dialects.postgresql.base import PGDialect
from sqlalchemy.orm import sessionmaker
from magpie import models, ANONYMOUS_USER, ADMIN_GROUP, USER_GROUP
from ziggurat_definitions import *

Session = sessionmaker()

# revision identifiers, used by Alembic.
revision = '5e7b5346c330'
down_revision = '2a6c63397399'
branch_labels = None
depends_on = None


def upgrade():
    context = get_context()
    session = Session(bind=op.get_bind())
    if isinstance(context.connection.engine.dialect, PGDialect):
        all_users = session.query(models.User)
        all_groups = session.query(models.Group)
        all_user_group_refs = models.UserGroup.all(db_session=session)

        ignore_groups = {ADMIN_GROUP, USER_GROUP}
        user_names = {usr.user_name for usr in all_users}

        # parse through 'personal' groups matching an existing user
        for group in all_groups:
            group_name = group.group_name
            if group_name in user_names and group_name not in ignore_groups:

                # get the real user
                user = UserService.by_user_name(user_name=group_name, db_session=session)

{'fraction_non_alphanumeric': 0.08166915052160954, 'fraction_numerical': 0.03219076005961252, 'mean_word_length': 3.8497109826589595, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '6890723', 'n_tokens_mistral': 1184, 'n_tokens_neox': 1092, 'n_words': 262}