from alembic import op
import re
import sqlalchemy as sa
from sqlalchemy.orm import sessionmaker

from portal.models.app_text import AppText
from portal.models.organization import Organization

"""empty message

Revision ID: 9b1bedfa916b
Revises: 441185240f62
Create Date: 2017-10-26 15:24:32.623899

"""

# revision identifiers, used by Alembic.
revision = '9b1bedfa916b'
down_revision = '441185240f62'

Session = sessionmaker()


def update_org_name(old, new):
    bind = op.get_bind()
    session = Session(bind=bind)

    session.execute("UPDATE organizations SET name='{}' "
                    "WHERE name='{}'".format(new, old))
    for at in session.query(AppText).filter(AppText.name.contains(old)):
        at.name = at.name.replace(old, new)
    session.commit()


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

    for at in session.query(AppText).all():
        if 'truenth eproms' in at.custom_text.lower():
            at.custom_text = re.sub(r' [eE]PROMs', '', at.custom_text)
        at.custom_text = at.custom_text.replace('ePROMs', 'TrueNTH')
    session.commit()


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    update_org_name('CRV', 'TrueNTH Global Registry')
    update_eproms_refs()
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    update_org_name('TrueNTH Global Registry', 'CRV')
    # ### end Alembic commands ###

from alembic import op
import re
import sqlalchemy as sa
from sqlalchemy.orm import sessionmaker

from portal.models.app_text import AppText
from portal.models.organization import Organization

"""empty message

Revision ID: 9b1bedfa916b
Revises: 441185240f62
Create Date: 2017-10-26 15:24:32.623899

"""

# revision identifiers, used by Alembic.
revision = '9b1bedfa916b'
down_revision = '441185240f62'

Session = sessionmaker()


def update_org_name(old, new):
    bind = op.get_bind()
    session = Session(bind=bind)

    session.execute("UPDATE organizations SET name='{}' "
                    "WHERE name='{}'".format(new, old))
    for at in session.query(AppText).filter(AppText.name.contains(old)):
        at.name = at.name.replace(old, new)
    session.commit()


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    update_org_name('CRV', 'TrueNTH Global Registry')

    bind = op.get_bind()
    session = Session(bind=bind)

    for at in session.query(AppText).all():
        if 'truenth eproms' in at.custom_text.lower():
            at.custom_text = re.sub(r' [eE]PROMs', '', at.custom_text)
        at.custom_text = at.custom_text.replace('ePROMs', 'TrueNTH')
    session.commit()

    new_crv_kwargs = '{"cutoff_days": "30,60,90", "org_id": "10000"}'
    new_iron_kwargs = '{"cutoff_days": "7,14,21,30", "org_id": "20000"}'

    session.execute("UPDATE scheduled_jobs SET name = '{}', kwargs='{}' "
                    "WHERE name = '{}'".format('TNGR site summary email',
                                               new_crv_kwargs,
                                               'CRV site summary email'))
    session.execute("UPDATE scheduled_jobs SET kwargs = '{}' WHERE "
                    "name = '{}'".format(new_iron_kwargs,
                                         'IRONMAN site summary email'))
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    update_org_name('TrueNTH Global Registry', 'CRV')

    bind = op.get_bind()
    session = Session(bind=bind)

    new_crv_kwargs = '{"cutoff_days": "30,60,90", "org": "CRV"}'
    new_iron_kwargs = '{"cutoff_days": "7,14,21,30", "org": "IRONMAN"}'

    session.execute("UPDATE scheduled_jobs SET name = '{}', kwargs='{}' "
                    "WHERE name = '{}'".format('CRV site summary email',
                                               new_crv_kwargs,
                                               'TNGR site summary email'))
    session.execute("UPDATE scheduled_jobs SET kwargs = '{}' WHERE "
                    "name = '{}'".format(new_iron_kwargs,
                                         'IRONMAN site summary email'))
    # ### end Alembic commands ###

update site summary scheduledjob names/kwargs as part of migration