diff --git a/migrations/versions/0021_add_delivered_failed_counts.py b/migrations/versions/0021_add_delivered_failed_counts.py index 41069b954..9381321ac 100644 --- a/migrations/versions/0021_add_delivered_failed_counts.py +++ b/migrations/versions/0021_add_delivered_failed_counts.py @@ -7,6 +7,8 @@ Create Date: 2016-05-23 15:05:25.109346 """ # revision identifiers, used by Alembic. +from sqlalchemy import text + revision = '0021_add_delivered_failed_counts' down_revision = '0020_template_history_fix' @@ -21,19 +23,22 @@ def upgrade(): conn = op.get_bind() results = conn.execute("select distinct job_id from notifications") res = results.fetchall() - + conn = op.get_bind() for x in res: if x.job_id: - op.execute("update jobs set notifications_delivered = (" - "select count(status) from notifications where status = 'delivered' and job_id = '{}' " + input_params = { + "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)" - "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 " "where status in ('failed','technical-failure', 'temporary-failure', 'permanent-failure') " - "and job_id = '{}' group by job_id)" - "where jobs.id = '{}'".format(x.job_id, x.job_id)) + "and job_id = :job_id group by 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_failed = 0 where notifications_failed is null") op.alter_column('jobs', 'notifications_delivered', nullable=False) diff --git a/migrations/versions/0039_fix_notifications.py b/migrations/versions/0039_fix_notifications.py index f8f9b6f6c..d05c72342 100644 --- a/migrations/versions/0039_fix_notifications.py +++ b/migrations/versions/0039_fix_notifications.py @@ -7,6 +7,8 @@ Create Date: 2016-07-06 13:28:48.381278 """ # revision identifiers, used by Alembic. +from sqlalchemy import text + revision = '0039_fix_notifications' down_revision = '0038_test_api_key_type' @@ -26,28 +28,32 @@ def upgrade(): res = results.fetchall() for x in res: - print(' in loop {} {}'.format(x.notification_type, x.created_at)) 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': - sql = "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) + 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 = "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) + 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 = "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) + 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 = "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) + 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 = "update notification_statistics set emails_requested = emails_requested + 1 " \ - " where day = date('{}') and service_id = '{}'".format(created, x.service_id) + 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 = "update notification_statistics set sms_requested = sms_requested + 1 " \ - " where day = date('{}') and service_id = '{}'".format(created, x.service_id) + sql = text("update notification_statistics set sms_requested = sms_requested + 1 " \ + " where day = date(:created) and service_id = :service_id") print(sql) - conn.execute(sql) + conn.execute(sql, input_params) + def downgrade(): ### commands auto generated by Alembic - please adjust! ### diff --git a/migrations/versions/0045_billable_units.py b/migrations/versions/0045_billable_units.py index ab24ea7d6..f8e1af370 100644 --- a/migrations/versions/0045_billable_units.py +++ b/migrations/versions/0045_billable_units.py @@ -7,6 +7,8 @@ Create Date: 2016-08-02 16:36:42.455838 """ # revision identifiers, used by Alembic. +from sqlalchemy import text + revision = '0045_billable_units' down_revision = '0044_jobs_to_notification_hist' @@ -87,15 +89,26 @@ def downgrade(): # caveats: # only approximates character counts - billable * 153 to get at least a decent ballpark # research mode messages assumed to be one message length - update_statement = ''' - UPDATE {} + update_statement_n = ''' + UPDATE notifications 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' ''' conn = op.get_bind() - conn.execute(update_statement.format('notifications', service_ids)) - conn.execute(update_statement.format('notification_history', service_ids)) + input_params = { + "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('notification_history', 'billable_units')