broadcast migrations: replace older ones & add final removal

This commit is contained in:
stvnrlly
2022-10-25 11:52:56 -04:00
parent d37c2a53b8
commit 4737cefb1c
26 changed files with 616 additions and 53 deletions

View File

@@ -13,8 +13,9 @@ down_revision = '0321_drop_postage_constraints'
def upgrade():
pass
op.execute("INSERT INTO service_permission_types VALUES ('broadcast')")
def downgrade():
pass
op.execute("DELETE FROM service_permissions WHERE permission = 'broadcast'")
op.execute("DELETE FROM service_permission_types WHERE name = 'broadcast'")

View File

@@ -14,6 +14,16 @@ revision = '0323_broadcast_message'
down_revision = '0322_broadcast_service_perm'
name = 'template_type'
tmp_name = 'tmp_' + name
old_options = ('sms', 'email', 'letter')
new_options = old_options + ('broadcast',)
new_type = sa.Enum(*new_options, name=name)
old_type = sa.Enum(*old_options, name=name)
STATUSES = [
'draft',
'pending-approval',
@@ -26,8 +36,66 @@ STATUSES = [
def upgrade():
pass
op.execute(f'ALTER TYPE {name} RENAME TO {tmp_name}')
new_type.create(op.get_bind())
for table in ['templates', 'templates_history', 'service_contact_list']:
op.execute(f'ALTER TABLE {table} ALTER COLUMN template_type TYPE {name} USING template_type::text::{name}')
op.execute(f'DROP TYPE {tmp_name}')
broadcast_status_type = op.create_table(
'broadcast_status_type',
sa.Column('name', sa.String(), nullable=False),
sa.PrimaryKeyConstraint('name')
)
op.bulk_insert(broadcast_status_type, [{'name': state} for state in STATUSES])
op.create_table(
'broadcast_message',
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('service_id', postgresql.UUID(as_uuid=True)),
sa.Column('template_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('template_version', sa.Integer(), nullable=False),
sa.Column('_personalisation', sa.String()),
sa.Column('areas', postgresql.JSONB(none_as_null=True, astext_type=sa.Text())),
sa.Column('status', sa.String()),
sa.Column('starts_at', sa.DateTime()),
sa.Column('finishes_at', sa.DateTime()),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('approved_at', sa.DateTime()),
sa.Column('cancelled_at', sa.DateTime()),
sa.Column('updated_at', sa.DateTime()),
sa.Column('created_by_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('approved_by_id', postgresql.UUID(as_uuid=True)),
sa.Column('cancelled_by_id', postgresql.UUID(as_uuid=True)),
sa.ForeignKeyConstraint(['approved_by_id'], ['users.id'], ),
sa.ForeignKeyConstraint(['cancelled_by_id'], ['users.id'], ),
sa.ForeignKeyConstraint(['created_by_id'], ['users.id'], ),
sa.ForeignKeyConstraint(['service_id'], ['services.id'], ),
sa.ForeignKeyConstraint(['template_id', 'template_version'], ['templates_history.id', 'templates_history.version'], ),
sa.PrimaryKeyConstraint('id')
)
op.add_column('templates', sa.Column('broadcast_data', postgresql.JSONB(none_as_null=True, astext_type=sa.Text())))
op.add_column('templates_history', sa.Column('broadcast_data', postgresql.JSONB(none_as_null=True, astext_type=sa.Text())))
def downgrade():
pass
op.execute("DELETE FROM template_folder_map WHERE template_id IN (SELECT id FROM templates WHERE template_type = 'broadcast')")
op.execute("DELETE FROM template_redacted WHERE template_id IN (SELECT id FROM templates WHERE template_type = 'broadcast')")
op.execute("DELETE FROM templates WHERE template_type = 'broadcast'")
op.execute("DELETE FROM templates_history WHERE template_type = 'broadcast'")
op.execute(f'ALTER TYPE {name} RENAME TO {tmp_name}')
old_type.create(op.get_bind())
for table in ['templates', 'templates_history', 'service_contact_list']:
op.execute(f'ALTER TABLE {table} ALTER COLUMN template_type TYPE {name} USING template_type::text::{name}')
op.execute(f'DROP TYPE {tmp_name}')
op.drop_column('templates_history', 'broadcast_data')
op.drop_column('templates', 'broadcast_data')
op.drop_table('broadcast_message')
op.drop_table('broadcast_status_type')

View File

@@ -14,8 +14,30 @@ down_revision = '0325_int_letter_rates_fix'
def upgrade():
pass
op.create_table('broadcast_event',
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=True),
sa.Column('broadcast_message_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('sent_at', sa.DateTime(), nullable=False),
sa.Column('message_type', sa.String(), nullable=False),
sa.Column('transmitted_content', postgresql.JSONB(none_as_null=True, astext_type=sa.Text()), nullable=True),
sa.Column('transmitted_areas', postgresql.JSONB(none_as_null=True, astext_type=sa.Text()), nullable=False),
sa.Column('transmitted_sender', sa.String(), nullable=False),
sa.Column('transmitted_starts_at', sa.DateTime(), nullable=True),
sa.Column('transmitted_finishes_at', sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(['broadcast_message_id'], ['broadcast_message.id'], ),
sa.ForeignKeyConstraint(['service_id'], ['services.id'], ),
sa.PrimaryKeyConstraint('id')
)
# this shouldn't be nullable. it defaults to `[]` in python.
op.alter_column('broadcast_message', 'areas', existing_type=postgresql.JSONB(astext_type=sa.Text()), nullable=False)
# this can't be nullable. it defaults to 'draft' in python.
op.alter_column('broadcast_message', 'status', existing_type=sa.VARCHAR(), nullable=False)
op.create_foreign_key(None, 'broadcast_message', 'broadcast_status_type', ['status'], ['name'])
def downgrade():
pass
op.drop_constraint('broadcast_message_status_fkey', 'broadcast_message', type_='foreignkey')
op.alter_column('broadcast_message', 'status', existing_type=sa.VARCHAR(), nullable=True)
op.alter_column('broadcast_message', 'areas', existing_type=postgresql.JSONB(astext_type=sa.Text()), nullable=True)
op.drop_table('broadcast_event')

View File

@@ -14,7 +14,7 @@ down_revision = '0328_international_letters_perm'
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
pass
op.execute("TRUNCATE broadcast_event, broadcast_message;")
# ### end Alembic commands ###

View File

@@ -18,9 +18,57 @@ user_id = '6af522d0-2915-4e52-83a3-3690455a5fe6'
service_id = 'd6aa2c68-a2d9-4437-ab19-3ae8eb202553'
template_id = '46152f7c-6901-41d5-8590-a5624d0d4359'
broadcast_invitation_template_name = 'Notify broadcast invitation email'
broadcast_invitation_subject = "((user_name)) has invited you to join ((service_name)) on GOV.UK Notify"
broadcast_invitation_content = """((user_name)) has invited you to join ((service_name)) on GOV.UK Notify.
In an emergency, use Notify to broadcast an alert, warning the public about an imminent risk to life.
Use this link to join the team:
((url))
This invitation will stop working at midnight tomorrow. This is to keep ((service_name)) secure.
Thanks
GOV.UK Notify team
https://www.gov.uk/notify
"""
def upgrade():
pass
insert_query = """
INSERT INTO {}
(id, name, template_type, created_at, content, archived, service_id,
subject, created_by_id, version, process_type, hidden)
VALUES
('{}', '{}', 'email', '{}', '{}', False, '{}', '{}', '{}', 1, 'normal', False)
"""
op.execute(insert_query.format(
'templates_history',
template_id,
broadcast_invitation_template_name,
datetime.utcnow(),
broadcast_invitation_content,
service_id,
broadcast_invitation_subject,
user_id
))
op.execute(insert_query.format(
'templates',
template_id,
broadcast_invitation_template_name,
datetime.utcnow(),
broadcast_invitation_content,
service_id,
broadcast_invitation_subject,
user_id
))
def downgrade():
pass
op.get_bind()
op.execute("delete from templates where id = '{}'".format(template_id))
op.execute("delete from templates_history where id = '{}'".format(template_id))

View File

@@ -18,7 +18,64 @@ organisation_id = '38e4bf69-93b0-445d-acee-53ea53fe02df'
def upgrade():
pass
# we've already done this manually on production
if environment != "production":
insert_sql = """
INSERT INTO organisation
(
id,
name,
active,
created_at,
agreement_signed,
crown,
organisation_type
)
VALUES (
:id,
:name,
:active,
current_timestamp,
:agreement_signed,
:crown,
:organisation_type
)
"""
update_service_set_broadcast_org_sql = """
UPDATE services
SET organisation_id = :organisation_id
WHERE id in (
SELECT service_id
FROM service_permissions
WHERE permission = 'broadcast'
)
"""
conn = op.get_bind()
conn.execute(
sa.text(insert_sql),
id=organisation_id,
name=f'Broadcast Services ({environment})',
active=True,
agreement_signed=None,
crown=None,
organisation_type='central',
)
conn.execute(
sa.text(update_service_set_broadcast_org_sql),
organisation_id=organisation_id
)
def downgrade():
pass
update_service_remove_org_sql = """
UPDATE services
SET organisation_id = NULL, updated_at = current_timestamp
WHERE organisation_id = :organisation_id
"""
delete_sql = """
DELETE FROM organisation
WHERE id = :organisation_id
"""
conn = op.get_bind()
conn.execute(sa.text(update_service_remove_org_sql), organisation_id=organisation_id)
conn.execute(sa.text(delete_sql), organisation_id=organisation_id)

View File

@@ -22,8 +22,28 @@ STATUSES = [
def upgrade():
pass
broadcast_provider_message_status_type = op.create_table(
'broadcast_provider_message_status_type',
sa.Column('name', sa.String(), nullable=False),
sa.PrimaryKeyConstraint('name')
)
op.bulk_insert(broadcast_provider_message_status_type, [{'name': status} for status in STATUSES])
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
'broadcast_provider_message',
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('broadcast_event_id', postgresql.UUID(as_uuid=True), nullable=True),
sa.Column('provider', sa.String(), nullable=True),
sa.Column('status', sa.String(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(['broadcast_event_id'], ['broadcast_event.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('broadcast_event_id', 'provider')
)
def downgrade():
pass
op.drop_table('broadcast_provider_message')
op.drop_table('broadcast_provider_message_status_type')

View File

@@ -14,8 +14,15 @@ down_revision = '0332_broadcast_provider_msg'
def upgrade():
pass
op.create_table(
'service_broadcast_provider_restriction',
sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('provider', sa.String(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(['service_id'], ['services.id'], ),
sa.PrimaryKeyConstraint('service_id')
)
def downgrade():
pass
op.drop_table('service_broadcast_provider_restriction')

View File

@@ -15,11 +15,24 @@ down_revision = '0333_service_broadcast_provider'
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
pass
op.execute("create sequence broadcast_provider_message_number_seq")
op.create_table(
'broadcast_provider_message_number',
sa.Column(
'broadcast_provider_message_number',
sa.Integer(),
server_default=sa.text("nextval('broadcast_provider_message_number_seq')"),
nullable=False
),
sa.Column('broadcast_provider_message_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.ForeignKeyConstraint(['broadcast_provider_message_id'], ['broadcast_provider_message.id'], ),
sa.PrimaryKeyConstraint('broadcast_provider_message_number')
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
pass
op.drop_table('broadcast_provider_message_number')
op.execute("drop sequence broadcast_provider_message_number_seq")
# ### end Alembic commands ###

View File

@@ -14,8 +14,14 @@ down_revision = '0334_broadcast_message_number'
def upgrade():
pass
op.add_column('broadcast_message', sa.Column('content', sa.Text(), nullable=True))
op.alter_column('broadcast_message', 'template_id', nullable=True)
op.alter_column('broadcast_message', 'template_version', nullable=True)
def downgrade():
pass
# downgrade fails if there are broadcasts without a template. This is deliberate cos I don't feel comfortable
# deleting broadcasts.
op.alter_column('broadcast_message', 'template_id', nullable=False)
op.alter_column('broadcast_message', 'template_version', nullable=False)
op.drop_column('broadcast_message', 'content')

View File

@@ -11,14 +11,28 @@ from notifications_utils.template import BroadcastMessageTemplate
from sqlalchemy.dialects import postgresql
from sqlalchemy.orm.session import Session
revision = '0336_broadcast_msg_content_2'
down_revision = '0335_broadcast_msg_content'
def upgrade():
pass
conn = op.get_bind()
results = conn.execute(sa.text("""
UPDATE
broadcast_message
SET
content = templates_history.content
FROM
templates_history
WHERE
broadcast_message.content is NULL and
broadcast_message.template_id = templates_history.id and
broadcast_message.template_version = templates_history.version
;
"""))
def downgrade():
pass
op.alter_column('broadcast_message', 'content', nullable=True)

View File

@@ -14,8 +14,13 @@ down_revision = '0336_broadcast_msg_content_2'
def upgrade():
pass
op.alter_column('broadcast_message', 'created_by_id', nullable=True)
op.add_column('broadcast_message', sa.Column('api_key_id', postgresql.UUID(), nullable=True))
op.create_foreign_key(None, 'broadcast_message', 'api_keys', ['api_key_id'], ['id'])
op.add_column('broadcast_message', sa.Column('reference', sa.String(length=255), nullable=True))
def downgrade():
pass
op.alter_column('broadcast_message', 'created_by_id', nullable=False)
op.drop_column('broadcast_message', 'api_key_id')
op.add_column('broadcast_message', 'reference')

View File

@@ -15,11 +15,11 @@ down_revision = '0339_service_billing_details'
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
pass
op.add_column('broadcast_message', sa.Column('stubbed', sa.Boolean(), nullable=True))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
pass
op.drop_column('broadcast_message', 'stubbed')
# ### end Alembic commands ###

View File

@@ -16,11 +16,28 @@ CHANNEL_TYPES = ["test", "severe"]
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('broadcast_channel_types',
sa.Column('name', sa.String(length=255), nullable=False),
sa.PrimaryKeyConstraint('name')
)
op.create_table('service_broadcast_settings',
sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('channel', sa.String(length=255), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(['channel'], ['broadcast_channel_types.name'], ),
sa.ForeignKeyConstraint(['service_id'], ['services.id'], ),
sa.PrimaryKeyConstraint('service_id')
)
# ### end Alembic commands ###
for channel in CHANNEL_TYPES:
op.execute(f"INSERT INTO broadcast_channel_types VALUES ('{channel}')")
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('service_broadcast_settings')
op.drop_table('broadcast_channel_types')
# ### end Alembic commands ###

View File

@@ -15,11 +15,22 @@ down_revision = '0343_org_billing_details'
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
pass
op.execute("UPDATE broadcast_message SET stubbed = False WHERE stubbed is null")
op.alter_column(
'broadcast_message',
'stubbed',
existing_type=sa.BOOLEAN(),
nullable=False
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
pass
op.alter_column(
'broadcast_message',
'stubbed',
existing_type=sa.BOOLEAN(),
nullable=True
)
# ### end Alembic commands ###

View File

@@ -14,8 +14,26 @@ down_revision = '0344_stubbed_not_nullable'
def upgrade():
pass
op.add_column('service_broadcast_settings', sa.Column('provider', sa.String(), nullable=True))
sql = """
select service_id, provider
from service_broadcast_provider_restriction
where service_id NOT IN (select service_id from service_broadcast_settings)
"""
insert_sql = """
insert into service_broadcast_settings(service_id, channel, provider, created_at, updated_at)
values('{}', 'test', '{}', now(), null)
"""
conn = op.get_bind()
results = conn.execute(sql)
restrictions = results.fetchall()
for x in restrictions:
f = insert_sql.format(x.service_id, x.provider)
conn.execute(f)
def downgrade():
pass
# Downgrade does not try and fully undo the upgrade, in particular it does not
# delete the rows added to the service_broadcast_settings table
op.drop_column('service_broadcast_settings', 'provider')

View File

@@ -14,7 +14,35 @@ down_revision = '0347_add_dvla_volumes_template'
def upgrade():
pass
# 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

View File

@@ -11,10 +11,22 @@ import sqlalchemy as sa
revision = '0352_broadcast_provider_types'
down_revision = '0351_unique_key_annual_billing'
PROVIDER_TYPES = ('ee', 'three', 'vodafone', 'o2', 'all')
def upgrade():
pass
op.create_table('broadcast_provider_types',
sa.Column('name', sa.String(length=255), nullable=False),
sa.PrimaryKeyConstraint('name'))
for provider in PROVIDER_TYPES:
op.execute(f"INSERT INTO broadcast_provider_types VALUES ('{provider}')")
op.create_foreign_key('service_broadcast_settings_provider_fkey',
'service_broadcast_settings',
'broadcast_provider_types',
['provider'],
['name'])
def downgrade():
pass
op.drop_constraint('service_broadcast_settings_provider_fkey', 'service_broadcast_settings', type_='foreignkey')
op.drop_table('broadcast_provider_types')

View File

@@ -13,8 +13,10 @@ down_revision = '0352_broadcast_provider_types'
def upgrade():
pass
op.execute("UPDATE service_broadcast_settings SET provider = 'all' WHERE provider is null")
op.alter_column('service_broadcast_settings', 'provider', existing_type=sa.VARCHAR(), nullable=False)
def downgrade():
pass
op.alter_column('service_broadcast_settings', 'provider', existing_type=sa.VARCHAR(), nullable=True)
op.execute("UPDATE service_broadcast_settings SET provider = null WHERE provider = 'all'")

View File

@@ -12,8 +12,11 @@ down_revision = '0353_broadcast_provider_not_null'
def upgrade():
pass
op.execute("INSERT INTO broadcast_channel_types VALUES ('government')")
def downgrade():
pass
# This can't be downgraded if there are rows in service_broadcast_settings which
# have the channel set to government or if broadcasts have already been sent on the
# government channel - it would break foreign key constraints.
op.execute("DELETE FROM broadcast_channel_types WHERE name = 'government'")

View File

@@ -12,8 +12,11 @@ down_revision = '0357_validate_constraint'
def upgrade():
pass
op.execute("INSERT INTO broadcast_channel_types VALUES ('operator')")
def downgrade():
pass
# This can't be downgraded if there are rows in service_broadcast_settings which
# have the channel set to operator or if broadcasts have already been sent on the
# operator channel - it would break foreign key constraints.
op.execute("DELETE FROM broadcast_channel_types WHERE name = 'operator'")

View File

@@ -11,10 +11,40 @@ import sqlalchemy as sa
revision = '0359_more_permissions'
down_revision = '0358_operator_channel'
enum_name = 'permission_types'
tmp_name = 'tmp_' + enum_name
old_options = (
'manage_users',
'manage_templates',
'manage_settings',
'send_texts',
'send_emails',
'send_letters',
'manage_api_keys',
'platform_admin',
'view_activity',
)
old_type = sa.Enum(*old_options, name=enum_name)
def upgrade():
pass
# ALTER TYPE must be run outside of a transaction block (see link below for details)
# https://alembic.sqlalchemy.org/en/latest/api/runtime.html#alembic.runtime.migration.MigrationContext.autocommit_block
with op.get_context().autocommit_block():
op.execute("ALTER TYPE permission_types ADD VALUE 'create_broadcasts'")
op.execute("ALTER TYPE permission_types ADD VALUE 'approve_broadcasts'")
op.execute("ALTER TYPE permission_types ADD VALUE 'cancel_broadcasts'")
op.execute("ALTER TYPE permission_types ADD VALUE 'reject_broadcasts'")
def downgrade():
pass
op.execute(
"DELETE FROM permissions WHERE permission in "
"('create_broadcasts', 'approve_broadcasts', 'cancel_broadcasts', 'reject_broadcasts')"
)
op.execute(f'ALTER TYPE {enum_name} RENAME TO {tmp_name}')
old_type.create(op.get_bind())
op.execute(f'ALTER TABLE permissions ALTER COLUMN permission TYPE {enum_name} USING permission::text::{enum_name}')
op.execute(f'DROP TYPE {tmp_name}')

View File

@@ -14,8 +14,8 @@ down_revision = '0361_new_user_bcast_permissions'
def upgrade():
pass
op.add_column('broadcast_message', sa.Column('cap_event', sa.String(length=255), nullable=True))
def downgrade():
pass
op.drop_column('broadcast_message', 'cap_event')

View File

@@ -13,11 +13,33 @@ down_revision = '0362_broadcast_msg_event'
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
pass
op.add_column('broadcast_message', sa.Column('created_by_api_key_id', postgresql.UUID(as_uuid=True), nullable=True))
op.add_column(
'broadcast_message', sa.Column('cancelled_by_api_key_id', postgresql.UUID(as_uuid=True), nullable=True)
)
op.drop_constraint('broadcast_message_api_key_id_fkey', 'broadcast_message', type_='foreignkey')
op.create_foreign_key(
'broadcast_message_created_by_api_key_id_fkey',
'broadcast_message',
'api_keys',
['created_by_api_key_id'],
['id']
)
op.create_foreign_key(
'broadcast_message_cancelled_by_api_key_id_fkey',
'broadcast_message',
'api_keys',
['cancelled_by_api_key_id'],
['id']
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
pass
op.drop_constraint('broadcast_message_created_by_api_key_id_fkey', 'broadcast_message', type_='foreignkey')
op.drop_constraint('broadcast_message_cancelled_by_api_key_id_fkey', 'broadcast_message', type_='foreignkey')
op.create_foreign_key('broadcast_message_api_key_id_fkey', 'broadcast_message', 'api_keys', ['api_key_id'], ['id'])
op.drop_column('broadcast_message', 'cancelled_by_api_key_id')
op.drop_column('broadcast_message', 'created_by_api_key_id')
# ### end Alembic commands ###

View File

@@ -14,8 +14,20 @@ down_revision = '0363_cancelled_by_api_key'
def upgrade():
pass
# move data over
op.execute("UPDATE broadcast_message SET created_by_api_key_id=api_key_id WHERE created_by_api_key_id IS NULL")
op.create_check_constraint(
"ck_broadcast_message_created_by_not_null",
"broadcast_message",
"created_by_id is not null or created_by_api_key_id is not null"
)
op.drop_column('broadcast_message', 'api_key_id')
def downgrade():
pass
op.add_column('broadcast_message', sa.Column('api_key_id', postgresql.UUID(), autoincrement=False, nullable=True))
op.execute("UPDATE broadcast_message SET api_key_id=created_by_api_key_id") # move data over
op.drop_constraint(
"ck_broadcast_message_created_by_not_null",
"broadcast_message"
)

View File

@@ -0,0 +1,144 @@
"""
Revision ID: 0379_remove_broadcasts
Revises: 0378_add_org_names
Create Date: 2022-10-25 14:41:29.429928
"""
from alembic import op
import sqlalchemy as sa
import psycopg2
from sqlalchemy.dialects import postgresql
revision = '0379_remove_broadcasts'
down_revision = '0378_add_org_names'
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('broadcast_provider_message_number')
op.drop_table('broadcast_provider_message_status_type')
op.drop_table('service_broadcast_settings')
op.drop_table('broadcast_provider_types')
op.drop_table('broadcast_provider_message')
op.drop_table('broadcast_event')
op.drop_table('broadcast_message')
op.drop_table('broadcast_status_type')
op.drop_table('broadcast_channel_types')
op.drop_table('service_broadcast_provider_restriction')
op.drop_column('templates', 'broadcast_data')
op.drop_column('templates_history', 'broadcast_data')
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('templates_history', sa.Column('broadcast_data', postgresql.JSONB(astext_type=sa.Text()), autoincrement=False, nullable=True))
op.add_column('templates', sa.Column('broadcast_data', postgresql.JSONB(astext_type=sa.Text()), autoincrement=False, nullable=True))
op.create_table('service_broadcast_provider_restriction',
sa.Column('service_id', postgresql.UUID(), autoincrement=False, nullable=False),
sa.Column('provider', sa.VARCHAR(), autoincrement=False, nullable=False),
sa.Column('created_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
sa.ForeignKeyConstraint(['service_id'], ['services.id'], name='service_broadcast_provider_restriction_service_id_fkey'),
sa.PrimaryKeyConstraint('service_id', name='service_broadcast_provider_restriction_pkey')
)
op.create_table('broadcast_channel_types',
sa.Column('name', sa.VARCHAR(length=255), autoincrement=False, nullable=False),
sa.PrimaryKeyConstraint('name', name='broadcast_channel_types_pkey'),
postgresql_ignore_search_path=False
)
op.create_table('broadcast_status_type',
sa.Column('name', sa.VARCHAR(), autoincrement=False, nullable=False),
sa.PrimaryKeyConstraint('name', name='broadcast_status_type_pkey'),
postgresql_ignore_search_path=False
)
op.create_table('service_broadcast_settings',
sa.Column('service_id', postgresql.UUID(), autoincrement=False, nullable=False),
sa.Column('channel', sa.VARCHAR(length=255), autoincrement=False, nullable=False),
sa.Column('created_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
sa.Column('updated_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
sa.Column('provider', sa.VARCHAR(), autoincrement=False, nullable=False),
sa.ForeignKeyConstraint(['channel'], ['broadcast_channel_types.name'], name='service_broadcast_settings_channel_fkey'),
sa.ForeignKeyConstraint(['provider'], ['broadcast_provider_types.name'], name='service_broadcast_settings_provider_fkey'),
sa.ForeignKeyConstraint(['service_id'], ['services.id'], name='service_broadcast_settings_service_id_fkey'),
sa.PrimaryKeyConstraint('service_id', name='service_broadcast_settings_pkey')
)
op.create_table('broadcast_event',
sa.Column('id', postgresql.UUID(), autoincrement=False, nullable=False),
sa.Column('service_id', postgresql.UUID(), autoincrement=False, nullable=True),
sa.Column('broadcast_message_id', postgresql.UUID(), autoincrement=False, nullable=False),
sa.Column('sent_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
sa.Column('message_type', sa.VARCHAR(), autoincrement=False, nullable=False),
sa.Column('transmitted_content', postgresql.JSONB(astext_type=sa.Text()), autoincrement=False, nullable=True),
sa.Column('transmitted_areas', postgresql.JSONB(astext_type=sa.Text()), autoincrement=False, nullable=False),
sa.Column('transmitted_sender', sa.VARCHAR(), autoincrement=False, nullable=False),
sa.Column('transmitted_starts_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
sa.Column('transmitted_finishes_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
sa.ForeignKeyConstraint(['broadcast_message_id'], ['broadcast_message.id'], name='broadcast_event_broadcast_message_id_fkey'),
sa.ForeignKeyConstraint(['service_id'], ['services.id'], name='broadcast_event_service_id_fkey'),
sa.PrimaryKeyConstraint('id', name='broadcast_event_pkey'),
postgresql_ignore_search_path=False
)
op.create_table('broadcast_message',
sa.Column('id', postgresql.UUID(), autoincrement=False, nullable=False),
sa.Column('service_id', postgresql.UUID(), autoincrement=False, nullable=True),
sa.Column('template_id', postgresql.UUID(), autoincrement=False, nullable=True),
sa.Column('template_version', sa.INTEGER(), autoincrement=False, nullable=True),
sa.Column('_personalisation', sa.VARCHAR(), autoincrement=False, nullable=True),
sa.Column('areas', postgresql.JSONB(astext_type=sa.Text()), autoincrement=False, nullable=False),
sa.Column('status', sa.VARCHAR(), autoincrement=False, nullable=False),
sa.Column('starts_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
sa.Column('finishes_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
sa.Column('created_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
sa.Column('approved_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
sa.Column('cancelled_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
sa.Column('updated_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
sa.Column('created_by_id', postgresql.UUID(), autoincrement=False, nullable=True),
sa.Column('approved_by_id', postgresql.UUID(), autoincrement=False, nullable=True),
sa.Column('cancelled_by_id', postgresql.UUID(), autoincrement=False, nullable=True),
sa.Column('content', sa.TEXT(), autoincrement=False, nullable=True),
sa.Column('reference', sa.VARCHAR(length=255), autoincrement=False, nullable=True),
sa.Column('stubbed', sa.BOOLEAN(), autoincrement=False, nullable=False),
sa.Column('cap_event', sa.VARCHAR(length=255), autoincrement=False, nullable=True),
sa.Column('created_by_api_key_id', postgresql.UUID(), autoincrement=False, nullable=True),
sa.Column('cancelled_by_api_key_id', postgresql.UUID(), autoincrement=False, nullable=True),
sa.CheckConstraint('(created_by_id IS NOT NULL) OR (created_by_api_key_id IS NOT NULL)', name='ck_broadcast_message_created_by_not_null'),
sa.ForeignKeyConstraint(['approved_by_id'], ['users.id'], name='broadcast_message_approved_by_id_fkey'),
sa.ForeignKeyConstraint(['cancelled_by_api_key_id'], ['api_keys.id'], name='broadcast_message_cancelled_by_api_key_id_fkey'),
sa.ForeignKeyConstraint(['cancelled_by_id'], ['users.id'], name='broadcast_message_cancelled_by_id_fkey'),
sa.ForeignKeyConstraint(['created_by_api_key_id'], ['api_keys.id'], name='broadcast_message_created_by_api_key_id_fkey'),
sa.ForeignKeyConstraint(['created_by_id'], ['users.id'], name='broadcast_message_created_by_id_fkey'),
sa.ForeignKeyConstraint(['service_id'], ['services.id'], name='broadcast_message_service_id_fkey'),
sa.ForeignKeyConstraint(['status'], ['broadcast_status_type.name'], name='broadcast_message_status_fkey'),
sa.ForeignKeyConstraint(['template_id', 'template_version'], ['templates_history.id', 'templates_history.version'], name='broadcast_message_template_id_template_version_fkey'),
sa.PrimaryKeyConstraint('id', name='broadcast_message_pkey'),
postgresql_ignore_search_path=False
)
op.create_table('broadcast_provider_message',
sa.Column('id', postgresql.UUID(), autoincrement=False, nullable=False),
sa.Column('broadcast_event_id', postgresql.UUID(), autoincrement=False, nullable=True),
sa.Column('provider', sa.VARCHAR(), autoincrement=False, nullable=True),
sa.Column('status', sa.VARCHAR(), autoincrement=False, nullable=True),
sa.Column('created_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
sa.Column('updated_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
sa.ForeignKeyConstraint(['broadcast_event_id'], ['broadcast_event.id'], name='broadcast_provider_message_broadcast_event_id_fkey'),
sa.PrimaryKeyConstraint('id', name='broadcast_provider_message_pkey'),
sa.UniqueConstraint('broadcast_event_id', 'provider', name='broadcast_provider_message_broadcast_event_id_provider_key'),
postgresql_ignore_search_path=False
)
op.create_table('broadcast_provider_types',
sa.Column('name', sa.VARCHAR(length=255), autoincrement=False, nullable=False),
sa.PrimaryKeyConstraint('name', name='broadcast_provider_types_pkey')
)
op.create_table('broadcast_provider_message_status_type',
sa.Column('name', sa.VARCHAR(), autoincrement=False, nullable=False),
sa.PrimaryKeyConstraint('name', name='broadcast_provider_message_status_type_pkey')
)
op.create_table('broadcast_provider_message_number',
sa.Column('broadcast_provider_message_number', sa.INTEGER(), server_default=sa.text("nextval('broadcast_provider_message_number_seq'::regclass)"), autoincrement=True, nullable=False),
sa.Column('broadcast_provider_message_id', postgresql.UUID(), autoincrement=False, nullable=False),
sa.ForeignKeyConstraint(['broadcast_provider_message_id'], ['broadcast_provider_message.id'], name='broadcast_provider_message_nu_broadcast_provider_message_i_fkey'),
sa.PrimaryKeyConstraint('broadcast_provider_message_number', name='broadcast_provider_message_number_pkey')
)
# ### end Alembic commands ###