2016-07-06 14:31:52 +01:00
"""empty message
2016-07-06 14:46:46 +01:00
Revision ID: 0039_fix_notifications
Revises: 0038_test_api_key_type
2016-07-06 14:31:52 +01:00
Create Date: 2016-07-06 13:28:48.381278
"""
# revision identifiers, used by Alembic.
2023-07-18 11:27:27 -07:00
from sqlalchemy import text
2023-08-29 14:54:30 -07:00
revision = "0039_fix_notifications"
down_revision = "0038_test_api_key_type"
2016-07-06 14:31:52 +01:00
import sqlalchemy as sa
2023-12-08 21:43:52 -05:00
from alembic import op
2016-07-06 14:31:52 +01:00
def upgrade ():
2023-08-29 14:54:30 -07:00
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)"
)
2016-07-06 14:31:52 +01:00
conn = op . get_bind ()
2023-08-29 14:54:30 -07:00
reset_counts = (
"update notification_statistics set emails_requested = 0, emails_delivered = 0, emails_failed=0,"
"sms_requested = 0, sms_delivered = 0, sms_failed=0 where day > '2016-06-30'"
)
2016-07-06 16:42:49 +01:00
op . execute ( reset_counts )
2024-03-13 12:42:11 -04:00
all_notifications = text (
"select * from notifications where date(created_at) > '2016-06-30' order by created_at;"
)
2016-07-06 14:31:52 +01:00
2016-07-06 16:42:49 +01:00
results = conn . execute ( all_notifications )
2016-07-06 14:31:52 +01:00
res = results . fetchall ()
for x in res :
2016-07-06 16:42:49 +01:00
created = x . created_at . strftime ( "%Y-%m- %d " )
2023-08-29 14:54:30 -07:00
input_params = { "created" : created , "service_id" : x . service_id }
if x . notification_type == "email" and x . status == "delivered" :
sql = text (
"update notification_statistics set emails_requested = emails_requested + 1, "
"emails_delivered = emails_delivered + 1 where day = date(:created) and service_id = :service_id"
)
if x . notification_type == "sms" and x . status == "delivered" :
sql = text (
"update notification_statistics set sms_requested = sms_requested + 1, "
"sms_delivered = sms_delivered + 1 where day = date(:created) and service_id = :service_id"
)
if x . notification_type == "email" and x . status in [
"technical-failure" ,
"temporary-failure" ,
"permanent-failure" ,
]:
sql = text (
"update notification_statistics set emails_requested = emails_requested + 1, "
"emails_failed = emails_failed + 1 where day = date(:created) and service_id = :service_id"
)
if x . notification_type == "sms" and x . status in [
"technical-failure" ,
"temporary-failure" ,
"permanent-failure" ,
]:
sql = text (
"update notification_statistics set sms_requested = sms_requested + 1, "
"sms_failed = sms_failed + 1 where day = date(:created) and service_id = :service_id"
)
if x . notification_type == "email" and x . status in [
"created" ,
"sending" ,
"pending" ,
]:
sql = text (
"update notification_statistics set emails_requested = emails_requested + 1 "
" where day = date(:created) and service_id = :service_id"
)
if x . notification_type == "sms" and x . status in [
"created" ,
"sending" ,
"pending" ,
]:
sql = text (
"update notification_statistics set sms_requested = sms_requested + 1 "
" where day = date(:created) and service_id = :service_id"
)
2016-07-06 16:42:49 +01:00
print ( sql )
2023-07-18 11:27:27 -07:00
conn . execute ( sql , input_params )
2016-07-06 14:31:52 +01:00
def downgrade ():
### commands auto generated by Alembic - please adjust! ###
pass
### end Alembic commands ###