Files
notifications-api/migrations/versions/0045_billable_units.py

141 lines
4.6 KiB
Python
Raw Normal View History

"""empty message
Revision ID: 0045_billable_units
Revises: 0044_jobs_to_notification_hist
Create Date: 2016-08-02 16:36:42.455838
"""
# revision identifiers, used by Alembic.
from sqlalchemy import bindparam, text
2023-07-18 11:27:27 -07:00
2023-08-29 14:54:30 -07:00
revision = "0045_billable_units"
down_revision = "0044_jobs_to_notification_hist"
import sqlalchemy as sa
from alembic import op
from sqlalchemy.orm.session import Session
from app.models import Service
2023-08-29 14:54:30 -07:00
def upgrade():
op.add_column("notifications", sa.Column("billable_units", sa.Integer()))
op.add_column("notification_history", sa.Column("billable_units", sa.Integer()))
2023-08-29 14:54:30 -07:00
op.execute("update notifications set billable_units = 0")
op.execute("update notification_history set billable_units = 0")
2023-08-29 14:54:30 -07:00
op.alter_column("notifications", "billable_units", nullable=False)
op.alter_column("notification_history", "billable_units", nullable=False)
conn = op.get_bind()
# caveats
# only adjusts notifications for services that have never been in research mode. On live, research mode was
# limited to only services that we have set up ourselves so deemed this acceptable.
2023-08-29 14:54:30 -07:00
billable_services = conn.execute(
"""
SELECT id FROM services_history WHERE id not in (select id from services_history where research_mode)
2023-08-29 14:54:30 -07:00
"""
)
# set to 'null' if there are no billable services so we don't get a syntax error in the update statement
2023-08-29 14:54:30 -07:00
service_ids = ",".join(f"{service.id}" for service in billable_services) or "null"
2023-08-29 14:54:30 -07:00
update_statement_n = """
2023-07-18 12:21:14 -07:00
UPDATE notifications
SET billable_units = (
CASE
WHEN content_char_count <= 160 THEN 1
ELSE ceil(content_char_count::float / 153::float)
END
)
WHERE content_char_count is not null
2023-07-18 12:21:14 -07:00
AND service_id in (:service_ids)
AND notification_type = 'sms'
2023-08-29 14:54:30 -07:00
"""
2023-07-18 12:21:14 -07:00
2023-08-29 14:54:30 -07:00
update_statement_nh = """
2023-07-18 12:21:14 -07:00
UPDATE notification_history
SET billable_units = (
CASE
WHEN content_char_count <= 160 THEN 1
ELSE ceil(content_char_count::float / 153::float)
END
)
WHERE content_char_count is not null
AND service_id in (:service_ids)
AND notification_type = 'sms'
2023-08-29 14:54:30 -07:00
"""
conn = op.get_bind()
2023-07-18 12:21:14 -07:00
2023-08-29 14:54:30 -07:00
query = text(update_statement_n).bindparams(
bindparam("service_ids", expanding=False)
)
2023-07-18 12:21:14 -07:00
conn.execute(query, service_ids=service_ids)
2023-08-29 14:54:30 -07:00
query = text(update_statement_nh).bindparams(
bindparam("service_ids", expanding=False)
)
2023-07-18 12:21:14 -07:00
conn.execute(query, service_ids=service_ids)
2023-08-29 14:54:30 -07:00
op.drop_column("notifications", "content_char_count")
op.drop_column("notification_history", "content_char_count")
def downgrade():
2023-08-29 14:54:30 -07:00
op.add_column(
"notifications",
sa.Column(
"content_char_count", sa.INTEGER(), autoincrement=False, nullable=True
),
)
2023-08-29 14:54:30 -07:00
op.add_column(
"notification_history",
sa.Column(
"content_char_count", sa.INTEGER(), autoincrement=False, nullable=True
),
)
conn = op.get_bind()
# caveats
# only adjusts notifications for services that have never been in research mode. On live, research mode was
# limited to only services that we have set up ourselves
2023-08-29 14:54:30 -07:00
billable_services = conn.execute(
"""
SELECT id FROM services_history WHERE id not in (select id from services_history where research_mode)
2023-08-29 14:54:30 -07:00
"""
)
# set to 'null' if there are no billable services so we don't get a syntax error in the update statement
2023-08-29 14:54:30 -07:00
service_ids = ",".join(f"{service.id}" for service in billable_services) or "null"
# caveats:
# only approximates character counts - billable * 153 to get at least a decent ballpark
# research mode messages assumed to be one message length
2023-08-29 14:54:30 -07:00
update_statement_n = """
2023-07-18 11:27:27 -07:00
UPDATE notifications
SET content_char_count = GREATEST(billable_units, 1) * 150
2023-07-18 12:21:14 -07:00
WHERE service_id in (:service_ids)
2023-07-18 11:27:27 -07:00
AND notification_type = 'sms'
2023-08-29 14:54:30 -07:00
"""
2023-07-18 11:27:27 -07:00
2023-08-29 14:54:30 -07:00
update_statement_nh = """
2023-07-18 11:27:27 -07:00
UPDATE notification_history
SET content_char_count = GREATEST(billable_units, 1) * 150
2023-07-18 12:21:14 -07:00
WHERE service_id in (:service_ids)
AND notification_type = 'sms'
2023-08-29 14:54:30 -07:00
"""
conn = op.get_bind()
2023-08-29 14:54:30 -07:00
query = text(update_statement_n).bindparams(
bindparam("service_ids", expanding=False)
)
2023-07-18 12:21:14 -07:00
conn.execute(query, service_ids=service_ids)
2023-08-29 14:54:30 -07:00
query = text(update_statement_nh).bindparams(
bindparam("service_ids", expanding=False)
)
2023-07-18 12:21:14 -07:00
conn.execute(query, service_ids=service_ids)
2023-07-18 11:27:27 -07:00
2023-08-29 14:54:30 -07:00
op.drop_column("notifications", "billable_units")
op.drop_column("notification_history", "billable_units")