Fix bug where all notifications were getting a type = email.

Includes a fix to notification and notification_statistics data.
This commit is contained in:
Rebecca Law
2016-07-06 14:31:52 +01:00
parent c46622993b
commit 8bea8b6a8f
4 changed files with 59 additions and 58 deletions

View File

@@ -0,0 +1,44 @@
"""empty message
Revision ID: 0038_fix_notifications
Revises: 0037_service_sms_sender
Create Date: 2016-07-06 13:28:48.381278
"""
# revision identifiers, used by Alembic.
revision = '0038_fix_notifications'
down_revision = '0037_service_sms_sender'
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 ###