Files
notifications-api/migrations/versions/0039_fix_notifications.py

45 lines
2.2 KiB
Python
Raw Normal View History

"""empty message
2016-07-06 14:46:46 +01:00
Revision ID: 0039_fix_notifications
Revises: 0038_test_api_key_type
Create Date: 2016-07-06 13:28:48.381278
"""
# revision identifiers, used by Alembic.
2016-07-06 14:46:46 +01:00
revision = '0039_fix_notifications'
down_revision = '0038_test_api_key_type'
from alembic import op
import sqlalchemy as sa
def upgrade():
op.execute('update notifications set notification_type = (select cast(cast(template_type as text) as notification_type) from templates where templates.id= notifications.template_id)')
conn = op.get_bind()
del_sql = "select n.id, results.* from (select 'failed' as stat_type, count(status) as count, notification_type, date(created_at) as day, service_id " \
"from notifications where status in ('temporary-failure', 'permanent-failure', 'technical-failure') group by service_id, notification_type, date(created_at) " \
"union select 'delivered' as stat_type, count(status) , notification_type, date(created_at) as day, service_id " \
"from notifications where status in ('delivered') group by service_id, notification_type, date(created_at)) as results, " \
"notification_statistics n " \
"where n.day = results.day and n.service_id = results.service_id order by results.day;"
results = conn.execute(del_sql)
res = results.fetchall()
for x in res:
if x.stat_type == 'delivered' and x.notification_type == 'email':
op.execute("update notification_statistics set emails_delivered = {} where id = '{}'".format(x.count, x.id))
if x.stat_type == 'failed' and x.notification_type == 'email':
op.execute("update notification_statistics set emails_failed = {} where id = '{}'".format(x.count, x.id))
if x.stat_type == 'delivered' and x.notification_type == 'sms':
op.execute("update notification_statistics set sms_delivered = {} where id = '{}'".format(x.count, x.id))
if x.stat_type == 'failed' and x.notification_type == 'sms':
op.execute("update notification_statistics set sms_failed = {} where id = '{}'".format(x.count, x.id))
def downgrade():
### commands auto generated by Alembic - please adjust! ###
pass
### end Alembic commands ###