<commit_before>"""v3.7: migrate old reply_to email

Revision ID: ff26585932ec
Revises: fa07bd604a75
Create Date: 2021-07-15 14:17:17.624748

"""

# revision identifiers, used by Alembic.
revision = 'ff26585932ec'
down_revision = 'fa07bd604a75'

from alembic import op
from sqlalchemy import orm
from privacyidea.models import EventHandlerOption
from privacyidea.lib.eventhandler.usernotification import NOTIFY_TYPE


def upgrade():
    bind = op.get_bind()
    session = orm.Session(bind=bind)
    try:
        for row in session.query(EventHandlerOption).filter(EventHandlerOption.Key == 'reply_to'):
            reply_email = row.Value
            evh_id = row.eventhandler_id
            row.Value = NOTIFY_TYPE.EMAIL
            row.save()
            EventHandlerOption(evh_id, 'reply_to email', reply_email).save()

    except Exception as e:
        session.rollback()
        print(e)

    finally:
        session.commit()


def downgrade():
    pass<commit_msg>add failsafe<commit_after>"""v3.7: migrate old reply_to email

Revision ID: ff26585932ec
Revises: fa07bd<PASSWORD>
Create Date: 2021-07-15 14:17:17.624748

"""

# revision identifiers, used by Alembic.
revision = 'ff26585932ec'
down_revision = 'fa07bd604a75'

from alembic import op
from sqlalchemy import orm
from privacyidea.models import EventHandlerOption
from privacyidea.lib.eventhandler.usernotification import NOTIFY_TYPE
from sqlalchemy import and_


def upgrade():
    bind = op.get_bind()
    session = orm.Session(bind=bind)
    try:
        for row in session.query(EventHandlerOption).filter(EventHandlerOption.Key == 'reply_to'):
            evh_id = row.eventhandler_id
            check = session.query(EventHandlerOption).filter(and_(EventHandlerOption.Key.like('reply_to %'),
                                                                  EventHandlerOption.eventhandler_id == evh_id)).first()
            if not check:
                reply_email = row.Value
                row.Value = NOTIFY_TYPE.EMAIL
                row.save()
                EventHandlerOption(evh_id, 'reply_to email', reply_email).save()
            else:
                continue

    except Exception as e:
        session.rollback()
        print(e)

    finally:
        session.commit()


def downgrade():
    pass
{'fraction_non_alphanumeric': 0.0895979020979021, 'fraction_numerical': 0.04326923076923077, 'mean_word_length': 2.703883495145631, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4024244', 'n_tokens_mistral': 757, 'n_tokens_neox': 676, 'n_words': 159}