more files

This commit is contained in:
Kenneth Kehl
2023-07-18 11:27:27 -07:00
parent 7276ed0e5a
commit a6c76f7969
3 changed files with 50 additions and 26 deletions

View File

@@ -7,6 +7,8 @@ Create Date: 2016-05-23 15:05:25.109346
""" """
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
from sqlalchemy import text
revision = '0021_add_delivered_failed_counts' revision = '0021_add_delivered_failed_counts'
down_revision = '0020_template_history_fix' down_revision = '0020_template_history_fix'
@@ -21,19 +23,22 @@ def upgrade():
conn = op.get_bind() conn = op.get_bind()
results = conn.execute("select distinct job_id from notifications") results = conn.execute("select distinct job_id from notifications")
res = results.fetchall() res = results.fetchall()
conn = op.get_bind()
for x in res: for x in res:
if x.job_id: if x.job_id:
op.execute("update jobs set notifications_delivered = (" input_params = {
"select count(status) from notifications where status = 'delivered' and job_id = '{}' " "job_id": x.job_id
}
conn.execute(text("update jobs set notifications_delivered = ("
"select count(status) from notifications where status = 'delivered' and job_id = :job_id "
"group by job_id)" "group by job_id)"
"where jobs.id = '{}'".format(x.job_id, x.job_id)) "where jobs.id = :job_id"), input_params)
op.execute("update jobs set notifications_failed = (" conn.execute(text("update jobs set notifications_failed = ("
"select count(status) from notifications " "select count(status) from notifications "
"where status in ('failed','technical-failure', 'temporary-failure', 'permanent-failure') " "where status in ('failed','technical-failure', 'temporary-failure', 'permanent-failure') "
"and job_id = '{}' group by job_id)" "and job_id = :job_id group by job_id)"
"where jobs.id = '{}'".format(x.job_id, x.job_id)) "where jobs.id = :job_id"), input_params)
op.execute("update jobs set notifications_delivered = 0 where notifications_delivered is null") op.execute("update jobs set notifications_delivered = 0 where notifications_delivered is null")
op.execute("update jobs set notifications_failed = 0 where notifications_failed is null") op.execute("update jobs set notifications_failed = 0 where notifications_failed is null")
op.alter_column('jobs', 'notifications_delivered', nullable=False) op.alter_column('jobs', 'notifications_delivered', nullable=False)

View File

@@ -7,6 +7,8 @@ Create Date: 2016-07-06 13:28:48.381278
""" """
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
from sqlalchemy import text
revision = '0039_fix_notifications' revision = '0039_fix_notifications'
down_revision = '0038_test_api_key_type' down_revision = '0038_test_api_key_type'
@@ -26,28 +28,32 @@ def upgrade():
res = results.fetchall() res = results.fetchall()
for x in res: for x in res:
print(' in loop {} {}'.format(x.notification_type, x.created_at))
created = x.created_at.strftime("%Y-%m-%d") created = x.created_at.strftime("%Y-%m-%d")
input_params = {
"created": created,
"service_id": x.service_id
}
if x.notification_type == 'email' and x.status == 'delivered': if x.notification_type == 'email' and x.status == 'delivered':
sql = "update notification_statistics set emails_requested = emails_requested + 1, " \ sql = text("update notification_statistics set emails_requested = emails_requested + 1, " \
"emails_delivered = emails_delivered + 1 where day = date('{}') and service_id = '{}'".format(created, x.service_id) "emails_delivered = emails_delivered + 1 where day = date(:created) and service_id = :service_id")
if x.notification_type == 'sms' and x.status == 'delivered': if x.notification_type == 'sms' and x.status == 'delivered':
sql = "update notification_statistics set sms_requested = sms_requested + 1, " \ sql = text("update notification_statistics set sms_requested = sms_requested + 1, " \
"sms_delivered = sms_delivered + 1 where day = date('{}') and service_id = '{}'".format(created, x.service_id) "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']: if x.notification_type == 'email' and x.status in ['technical-failure', 'temporary-failure', 'permanent-failure']:
sql = "update notification_statistics set emails_requested = emails_requested + 1, " \ sql = text("update notification_statistics set emails_requested = emails_requested + 1, " \
"emails_failed = emails_failed + 1 where day = date('{}') and service_id = '{}'".format(created, x.service_id) "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']: if x.notification_type == 'sms' and x.status in ['technical-failure', 'temporary-failure', 'permanent-failure']:
sql = "update notification_statistics set sms_requested = sms_requested + 1, " \ sql = text("update notification_statistics set sms_requested = sms_requested + 1, " \
"sms_failed = sms_failed + 1 where day = date('{}') and service_id = '{}'".format(created, x.service_id) "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']: if x.notification_type == 'email' and x.status in ['created', 'sending', 'pending']:
sql = "update notification_statistics set emails_requested = emails_requested + 1 " \ sql = text("update notification_statistics set emails_requested = emails_requested + 1 " \
" where day = date('{}') and service_id = '{}'".format(created, x.service_id) " where day = date(:created) and service_id = :service_id")
if x.notification_type == 'sms' and x.status in ['created', 'sending', 'pending']: if x.notification_type == 'sms' and x.status in ['created', 'sending', 'pending']:
sql = "update notification_statistics set sms_requested = sms_requested + 1 " \ sql = text("update notification_statistics set sms_requested = sms_requested + 1 " \
" where day = date('{}') and service_id = '{}'".format(created, x.service_id) " where day = date(:created) and service_id = :service_id")
print(sql) print(sql)
conn.execute(sql) conn.execute(sql, input_params)
def downgrade(): def downgrade():
### commands auto generated by Alembic - please adjust! ### ### commands auto generated by Alembic - please adjust! ###

View File

@@ -7,6 +7,8 @@ Create Date: 2016-08-02 16:36:42.455838
""" """
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
from sqlalchemy import text
revision = '0045_billable_units' revision = '0045_billable_units'
down_revision = '0044_jobs_to_notification_hist' down_revision = '0044_jobs_to_notification_hist'
@@ -87,15 +89,26 @@ def downgrade():
# caveats: # caveats:
# only approximates character counts - billable * 153 to get at least a decent ballpark # only approximates character counts - billable * 153 to get at least a decent ballpark
# research mode messages assumed to be one message length # research mode messages assumed to be one message length
update_statement = ''' update_statement_n = '''
UPDATE {} UPDATE notifications
SET content_char_count = GREATEST(billable_units, 1) * 150 SET content_char_count = GREATEST(billable_units, 1) * 150
WHERE service_id in ({}) WHERE service_id in :service_ids
AND notification_type = 'sms'
'''
update_statement_nh = '''
UPDATE notification_history
SET content_char_count = GREATEST(billable_units, 1) * 150
WHERE service_id in :service_ids
AND notification_type = 'sms' AND notification_type = 'sms'
''' '''
conn = op.get_bind() conn = op.get_bind()
conn.execute(update_statement.format('notifications', service_ids)) input_params = {
conn.execute(update_statement.format('notification_history', service_ids)) "service_ids": service_ids
}
conn.execute(text(update_statement_n), input_params)
conn.execute(text(update_statement_nh), input_params)
op.drop_column('notifications', 'billable_units') op.drop_column('notifications', 'billable_units')
op.drop_column('notification_history', 'billable_units') op.drop_column('notification_history', 'billable_units')