Files
notifications-api/migrations/versions/0021_add_delivered_failed_counts.py
Cliff Hill 1157f5639d black, isort, flake8
Signed-off-by: Cliff Hill <Clifford.hill@gsa.gov>
2023-12-08 21:43:52 -05:00

69 lines
2.2 KiB
Python

"""empty message
Revision ID: 0021_add_delivered_failed_counts
Revises: 0020_template_history_fix
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"
import sqlalchemy as sa
from alembic import op
def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.add_column(
"jobs", sa.Column("notifications_delivered", sa.Integer(), nullable=True)
)
op.add_column(
"jobs", sa.Column("notifications_failed", sa.Integer(), nullable=True)
)
conn = op.get_bind()
results = conn.execute("select distinct job_id from notifications")
res = results.fetchall()
for x in res:
if x.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 = :job_id"
),
input_params,
)
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 = :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)
op.alter_column("jobs", "notifications_failed", nullable=False)
### end Alembic commands ###
def downgrade():
### commands auto generated by Alembic - please adjust! ###
op.drop_column("jobs", "notifications_failed")
op.drop_column("jobs", "notifications_delivered")
### end Alembic commands ###