This commit is contained in:
Kenneth Kehl
2023-08-29 14:54:30 -07:00
parent 19dcd7a48b
commit 1ecb747c6d
588 changed files with 34091 additions and 23580 deletions
+53 -42
View File
@@ -9,8 +9,8 @@ Create Date: 2016-08-02 16:36:42.455838
# revision identifiers, used by Alembic.
from sqlalchemy import text, bindparam
revision = '0045_billable_units'
down_revision = '0044_jobs_to_notification_hist'
revision = "0045_billable_units"
down_revision = "0044_jobs_to_notification_hist"
from alembic import op
import sqlalchemy as sa
@@ -18,30 +18,31 @@ from sqlalchemy.orm.session import Session
from app.models import Service
def upgrade():
op.add_column('notifications', sa.Column('billable_units', sa.Integer()))
op.add_column('notification_history', sa.Column('billable_units', sa.Integer()))
op.add_column("notifications", sa.Column("billable_units", sa.Integer()))
op.add_column("notification_history", sa.Column("billable_units", sa.Integer()))
op.execute('update notifications set billable_units = 0')
op.execute('update notification_history set billable_units = 0')
op.alter_column('notifications', 'billable_units', nullable=False)
op.alter_column('notification_history', 'billable_units', nullable=False)
op.execute("update notifications set billable_units = 0")
op.execute("update notification_history set billable_units = 0")
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.
billable_services = conn.execute('''
billable_services = conn.execute(
"""
SELECT id FROM services_history WHERE id not in (select id from services_history where research_mode)
''')
"""
)
# set to 'null' if there are no billable services so we don't get a syntax error in the update statement
service_ids = ','.join(f"{service.id}" for service in billable_services) or 'null'
service_ids = ",".join(f"{service.id}" for service in billable_services) or "null"
update_statement_n = '''
update_statement_n = """
UPDATE notifications
SET billable_units = (
CASE
@@ -52,9 +53,9 @@ def upgrade():
WHERE content_char_count is not null
AND service_id in (:service_ids)
AND notification_type = 'sms'
'''
"""
update_statement_nh = '''
update_statement_nh = """
UPDATE notification_history
SET billable_units = (
CASE
@@ -65,30 +66,34 @@ def upgrade():
WHERE content_char_count is not null
AND service_id in (:service_ids)
AND notification_type = 'sms'
'''
"""
conn = op.get_bind()
query = text(update_statement_n).bindparams(bindparam("service_ids", expanding=False))
query = text(update_statement_n).bindparams(
bindparam("service_ids", expanding=False)
)
conn.execute(query, service_ids=service_ids)
query = text(update_statement_nh).bindparams(bindparam("service_ids", expanding=False))
query = text(update_statement_nh).bindparams(
bindparam("service_ids", expanding=False)
)
conn.execute(query, service_ids=service_ids)
op.drop_column('notifications', 'content_char_count')
op.drop_column('notification_history', 'content_char_count')
op.drop_column("notifications", "content_char_count")
op.drop_column("notification_history", "content_char_count")
def downgrade():
op.add_column('notifications', sa.Column(
'content_char_count',
sa.INTEGER(),
autoincrement=False,
nullable=True)
op.add_column(
"notifications",
sa.Column(
"content_char_count", sa.INTEGER(), autoincrement=False, nullable=True
),
)
op.add_column('notification_history', sa.Column(
'content_char_count',
sa.INTEGER(),
autoincrement=False,
nullable=True)
op.add_column(
"notification_history",
sa.Column(
"content_char_count", sa.INTEGER(), autoincrement=False, nullable=True
),
)
conn = op.get_bind()
@@ -96,34 +101,40 @@ def downgrade():
# 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
billable_services = conn.execute('''
billable_services = conn.execute(
"""
SELECT id FROM services_history WHERE id not in (select id from services_history where research_mode)
''')
"""
)
# set to 'null' if there are no billable services so we don't get a syntax error in the update statement
service_ids = ','.join(f"{service.id}" for service in billable_services) or 'null'
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
update_statement_n = '''
update_statement_n = """
UPDATE notifications
SET content_char_count = GREATEST(billable_units, 1) * 150
WHERE service_id in (:service_ids)
AND notification_type = 'sms'
'''
"""
update_statement_nh = '''
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()
query = text(update_statement_n).bindparams(bindparam("service_ids", expanding=False))
query = text(update_statement_n).bindparams(
bindparam("service_ids", expanding=False)
)
conn.execute(query, service_ids=service_ids)
query = text(update_statement_nh).bindparams(bindparam("service_ids", expanding=False))
query = text(update_statement_nh).bindparams(
bindparam("service_ids", expanding=False)
)
conn.execute(query, service_ids=service_ids)
op.drop_column('notifications', 'billable_units')
op.drop_column('notification_history', 'billable_units')
op.drop_column("notifications", "billable_units")
op.drop_column("notification_history", "billable_units")