diff --git a/migrations/versions/0330_broadcast_invite_email.py b/migrations/versions/0330_broadcast_invite_email.py index 2127f8832..2ae8dd910 100644 --- a/migrations/versions/0330_broadcast_invite_email.py +++ b/migrations/versions/0330_broadcast_invite_email.py @@ -10,6 +10,7 @@ Create Date: 2020-09-15 14:17:01.963181 from datetime import datetime from alembic import op +from sqlalchemy import text revision = '0330_broadcast_invite_email' down_revision = '0329_purge_broadcast_data' @@ -37,38 +38,41 @@ https://www.gov.uk/notify def upgrade(): - insert_query = """ - INSERT INTO {} + insert_query_t = """ + INSERT INTO templates (id, name, template_type, created_at, content, archived, service_id, subject, created_by_id, version, process_type, hidden) VALUES - ('{}', '{}', 'email', '{}', '{}', False, '{}', '{}', '{}', 1, 'normal', False) + (:template_id, :template_name, 'email', :time_now, :content, False, :service_id, :subject, :user_id, 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 - )) + insert_query_th = """ + INSERT INTO templates_history + (id, name, template_type, created_at, content, archived, service_id, + subject, created_by_id, version, process_type, hidden) + VALUES + (:template_id, :template_name, 'email', :time_now, :content, False, :service_id, :subject, :user_id, 1, 'normal', False) + """ + conn = op.get_bind() - op.execute(insert_query.format( - 'templates', - template_id, - broadcast_invitation_template_name, - datetime.utcnow(), - broadcast_invitation_content, - service_id, - broadcast_invitation_subject, - user_id - )) + input_params = { + "template_id": template_id, + "template_name": broadcast_invitation_template_name, + "time_now": datetime.utcnow(), + "content": broadcast_invitation_content, + "service_id": service_id, + "subject": broadcast_invitation_subject, + "user_id": user_id + + } + conn.execute(text(insert_query_t), input_params) + conn.execute(text(insert_query_th), input_params) def downgrade(): - op.get_bind() - op.execute("delete from templates where id = '{}'".format(template_id)) - op.execute("delete from templates_history where id = '{}'".format(template_id)) + conn = op.get_bind() + input_params = { + "template_id": template_id + } + conn.execute(text("delete from templates where id = :template_id"), input_params) + conn.execute(text("delete from templates_history where id = :template_id"), input_params) diff --git a/migrations/versions/0383_update_default_templates.py b/migrations/versions/0383_update_default_templates.py index 3313abfa4..3b9edff6d 100644 --- a/migrations/versions/0383_update_default_templates.py +++ b/migrations/versions/0383_update_default_templates.py @@ -8,6 +8,7 @@ Create Date: 2023-01-10 11:42:25.633265 import json from alembic import op import sqlalchemy as sa +from sqlalchemy import text from sqlalchemy.dialects import postgresql from flask import current_app @@ -16,38 +17,33 @@ down_revision = '0382_remove_old_providers' def upgrade(): - update = """ - UPDATE {} SET name = '{}', template_type = '{}', content = '{}', subject = '{}' - WHERE id = '{}' + update_t = """ + UPDATE templates SET name = :name, template_type = :type, content = :content, subject = :subject + WHERE id = :id """ + update_th = """ + UPDATE templates_history SET name = :name, template_type = :type, content = :content, subject = :subject + WHERE id = :id + """ + conn = op.get_bind() with open(current_app.config['CONFIG_FILES'] + '/templates.json') as f: data = json.load(f) for d in data: - for table_name in 'templates', 'templates_history': - op.execute( - update.format( - table_name, - d['name'], - d['type'], - '\n'.join(d['content']), - d.get('subject'), - d['id'] - ) - ) + input_params = { + 'name': d['name'], + 'type': d['type'], + 'content': '\n'.join(d['content']), + 'subject': d.get('subject'), + 'id': d['id'] + } + conn.execute( + text(update_t), input_params + ) + conn.execute( + text(update_th), input_params + ) - # op.execute( - # """ - # INSERT INTO template_redacted - # ( - # template_id, - # redact_personalisation, - # updated_at, - # updated_by_id - # ) VALUES ( '{}', false, current_timestamp, '{}' ) - # """.format(d['id'], current_app.config['NOTIFY_USER_ID']) - # ) - def downgrade(): # with associated code changes, edits to templates should no longer be made via migration.