Backfill services_broadcast_settings table

At the moment, we currently have broadcast services that have the
'broadcast' service permission in the DB but don't have a corresponding
row in the service_broadcast_settings table. It's important to give them
a row in this table because this will control which broadcast channel
their messages will go out on. All broadcast services will be expected
to have this row so we can then remove hardcoded defaults from our code
that currently set what channel a message should go out on.

Whilst, when this gets deployed, we will have released
https://github.com/alphagov/notifications-admin/pull/3794, which means
that when a service setting is changed via that form, they will get the
corresponding row in the service_broadcast_settings form, we don't want
to ask every developer to go and fill in this form for every service on
their local dev environment and also do the same to every service we
have in preview, staging and production. Therefore a migration is the
best way to backfill the data.

Note, I made the decision that a service that is in trial mode will be
given the 'severe' channel. This is because this is what most trial mode
services should have. Only the MNOs would ever really have a trial mode
service on the test channel. And given they are in trial mode, it is not
too risky to give them the 'severe' channel.

For services that are live though, although there are no services in
production that have the broadcast permission and are live, it is not
worth taking that risk and accidently setting that service to send an
alert out on the 'severe' channel. We play it say by choosing 'test'.
This commit is contained in:
David McDonald
2021-02-18 18:20:05 +00:00
parent 8e5f956009
commit ce2f550387

View File

@@ -0,0 +1,48 @@
"""
Revision ID: 0348_migrate_broadcast_settings
Revises: 0347_add_dvla_volumes_template
Create Date: 2021-02-18 15:25:30.667098
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
revision = '0348_migrate_broadcast_settings'
down_revision = '0347_add_dvla_volumes_template'
def upgrade():
# For every service that has the broadcast permission we want it to have
# a row in the broadcast_service_settings table
#
# If it doesnt have a row already, then:
# - if the service is in trial mode, add a row and set the channel as 'severe'
# - if the service is in live mode, add a row and set the channel as 'test'
#
# If it does have a row already no action needed
conn = op.get_bind()
find_services_sql = """
SELECT services.id, services.restricted
FROM services
LEFT JOIN service_permissions
ON services.id = service_permissions.service_id
WHERE service_permissions.permission = 'broadcast'
"""
services = conn.execute(find_services_sql)
for service in services:
setting = conn.execute(f"SELECT service_id, channel, provider FROM service_broadcast_settings WHERE service_id = '{service.id}';").first()
if setting:
print(f"Service {service.id} already has service_broadcast_settings. No action required")
else:
channel = "severe" if service.restricted else "test"
print(f"Service {service.id} does not have service_broadcast_settings. Will insert one with channel {channel}")
conn.execute(f"INSERT INTO service_broadcast_settings (service_id, channel, created_at) VALUES ('{service.id}', '{channel}', now());")
def downgrade():
# No downgrade as we do not know what the state of the table was before that it should return to
pass