<commit_before>"""empty message

Revision ID: 823de677d394
Revises: 320726253d2d
Create Date: 2017-05-24 12:27:57.861464

"""

# revision identifiers, used by Alembic.
revision = '823de677d394'
down_revision = '320726253d2d'

from alembic import op
from datetime import datetime
import sqlalchemy as sa

from portal.models.encounter import Encounter

Session = sa.orm.sessionmaker()


def create_missing_encounters(table_name):
    bind = op.get_bind()
    session = Session(bind=bind)

    if table_name == 'questionnaire_responses':
        user_col = 'subject_id'
    else:
        user_col = 'user_id'

    for obj_id, user_id in session.execute("SELECT id, {} FROM {} where "
                        "encounter_id IS NULL".format(user_col, table_name)):
        enc = Encounter(
            status='finished', auth_method='staff_authenticated',
            start_time=datetime(2000, 01, 01, 01, 01, 01), user_id=user_id)
        session.add(enc)
        session.commit()
        enc = session.merge(enc)
        session.execute('UPDATE {} SET encounter_id = {} where id = {}'.format(
                        table_name, enc.id, obj_id))


def remove_generated_encounters(table_name):
    bind = op.get_bind()
    session = Session(bind=bind)

    for obj_id, enc_id in session.execute("SELECT {table_name}.id, "
                        "encounters.id FROM {table_name} JOIN encounters ON "
                        "{table_name}.encounter_id = encounters.id WHERE "
                        "encounters.status = 'finished' AND "
                        "encounters.auth_method = 'staff_authenticated' AND "
                        "encounters.start_time = '2000-01-01 01:01:01'".format(
                              table_name=table_name)):
        session.execute('UPDATE {} SET encounter_id = NULL WHERE id = {}'
                        ''.format(table_name, obj_id))
        session.execute('DELETE FROM encounters WHERE id = {}'.format(enc_id))


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('clients', 'user_id',
                    existing_type=sa.INTEGER(),
                    nullable=False)
    op.alter_column('grants', 'user_id',
                    existing_type=sa.INTEGER(),
                    nullable=False)

    create_missing_encounters('procedures')
    op.alter_column('procedures', 'encounter_id',
                    existing_type=sa.INTEGER(),
                    nullable=False)

    create_missing_encounters('questionnaire_responses')
    op.alter_column('questionnaire_responses', 'encounter_id',
                    existing_type=sa.INTEGER(),
                    nullable=False)

    op.alter_column('questionnaire_responses', 'subject_id',
                    existing_type=sa.INTEGER(),
                    nullable=False)
    op.alter_column('tokens', 'user_id',
                    existing_type=sa.INTEGER(),
                    nullable=False)<commit_msg>fix migration issue with null qr.subject_ids<commit_after>"""empty message

Revision ID: 823de677d394
Revises: 320726253d2d
Create Date: 2017-05-24 12:27:57.861464

"""

# revision identifiers, used by Alembic.
revision = '823de677d394'
down_revision = '320726253d2d'

from alembic import op
from datetime import datetime
import sqlalchemy as sa

from portal.models.encounter import Encounter

Session = sa.orm.sessionmaker()


def create_missing_encounters(table_name):
    bind = op.get_bind()
    session = Session(bind=bind)

    if table_name == 'questionnaire_responses':
        user_col = 'subject_id'
    else:
        user_col = 'user_id'

    for obj_id, user_id in session.execute("SELECT id, {} FROM {} where "
                        "encounter_id IS NULL".format(user_col, table_name)):
        enc = Encounter(
            status='finished', auth_method='staff_authenticated',
            start_time=datetime(2000, 01, 01, 01, 01, 01), user_id=user_id)
        session.add(enc)
        session.commit()
        enc = session.merge(enc)
        session.execute('UPDATE {} SET encounter_id = {} where id = {}'.format(
                        table_name, enc.id, obj_id))


def remove_generated_encounters(table_name):
    bind = op.get_bind()
    session = Session(bind=bind)

    for obj_id, enc_id in session.execute("SELECT {table_name}.id, "
                        "encounters.id FROM {table_name} JOIN encounters ON "
                        "{table_name}.encounter_id = encounters.id WHERE "
                        "encounters.status = 'finished' AND "
                        "encounters.auth_method = 'staff_authenticated' AND "
                        "encounters.start_time = '2000-01-01 01:01:01'".format(
                              table_name=table_name)):
        session.execute('UPDATE {} SET encounter_id = NULL WHERE id = {}'
                        ''.format(table_name, obj_id))
        session.execute('DELETE FROM encounters WHERE id = {}'.format(enc_id))


def update_questionnaire_subjects():
    bind = op.get_bind()
    session = Session(bind=bind)

    for ques_id, user_id in session.execute(
                        "SELECT questionnaire_responses.id, encounters.user_id"
                        " FROM questionnaire_responses JOIN encounters ON "
                        "questionnaire_responses.encounter_id = encounters.id "
                        "WHERE questionnaire_responses.subject_id IS NULL"):

        session.execute('UPDATE questionnaire_responses SET subject_id = {} '
                        'WHERE id = {}'.format(user_id, ques_id))


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('clients', 'user_id',
                    existing_type=sa.INTEGER(),
                    nullable=False)
    op.alter_column('grants', 'user_id',
                    existing_type=sa.INTEGER(),
                    nullable=False)

    create_missing_encounters('procedures')
    op.alter_column('procedures', 'encounter_id',
                    existing_type=sa.INTEGER(),
                    nullable=False)

    create_missing_encounters('questionnaire_responses')
    op.alter_column('questionnaire_responses', 'encounter_id',
                    existing_type=sa.INTEGER(),
                    nullable=False)

    update_questionnaire_subjects()
    op.alter_column('questionnaire_responses', 'subject_id',
                    existing_type=sa.INTEGER(),
                    nullable=False)
    op.alter_column('tokens', 'user_id',
                    existing_type=sa.INTEGER(),
                    nullable=False)
{'fraction_non_alphanumeric': 0.10571950106480073, 'fraction_numerical': 0.026163675083662914, 'mean_word_length': 2.2875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3620525', 'n_tokens_mistral': 2045, 'n_tokens_neox': 1858, 'n_words': 374}