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

@@ -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')