|
| 1 | +""" |
| 2 | +Revision ID: 0480_move_heartbeat_templates |
| 3 | +Revises: 0479_update_reports_url |
| 4 | +Create Date: 2025-04-02 00:00:00 |
| 5 | +""" |
| 6 | +from datetime import datetime, timezone |
| 7 | + |
| 8 | +from alembic import op |
| 9 | +from flask import current_app |
| 10 | + |
| 11 | +revision = "0480_move_heartbeat_templates" |
| 12 | +down_revision = "0479_update_reports_url" |
| 13 | + |
| 14 | +TEMPLATE_ID_EMAIL_LOW = current_app.config["HEARTBEAT_TEMPLATE_EMAIL_LOW"] |
| 15 | +TEMPLATE_ID_EMAIL_MEDIUM = current_app.config["HEARTBEAT_TEMPLATE_EMAIL_MEDIUM"] |
| 16 | +TEMPLATE_ID_EMAIL_HIGH = current_app.config["HEARTBEAT_TEMPLATE_EMAIL_HIGH"] |
| 17 | +TEMPLATE_ID_SMS_LOW = current_app.config["HEARTBEAT_TEMPLATE_SMS_LOW"] |
| 18 | +TEMPLATE_ID_SMS_MEDIUM = current_app.config["HEARTBEAT_TEMPLATE_SMS_MEDIUM"] |
| 19 | +TEMPLATE_ID_SMS_HIGH = current_app.config["HEARTBEAT_TEMPLATE_SMS_HIGH"] |
| 20 | + |
| 21 | +NOTIFY_USER_ID = "6af522d0-2915-4e52-83a3-3690455a5fe6" |
| 22 | +NOTIFY_SERVICE_ID = "d6aa2c68-a2d9-4437-ab19-3ae8eb202553" |
| 23 | +NOTIFY_HEARTBEAT_SERVICE_ID = "30b2fb9c-f8ad-49ad-818a-ed123fc00758" |
| 24 | + |
| 25 | +TEMPLATE_IDS = [TEMPLATE_ID_EMAIL_LOW, TEMPLATE_ID_EMAIL_MEDIUM, TEMPLATE_ID_EMAIL_HIGH, |
| 26 | + TEMPLATE_ID_SMS_LOW, TEMPLATE_ID_SMS_MEDIUM, TEMPLATE_ID_SMS_HIGH] |
| 27 | + |
| 28 | +TEMPLATE_UPDATE = """ |
| 29 | + UPDATE templates SET service_id = '{}' |
| 30 | + WHERE id = '{}' |
| 31 | +""" |
| 32 | + |
| 33 | +TEMPLATE_HISTORY_UPDATE = """ |
| 34 | + UPDATE templates_history SET service_id = '{}' |
| 35 | + WHERE id = '{}' |
| 36 | +""" |
| 37 | + |
| 38 | +NOW = datetime.now(timezone.utc) |
| 39 | + |
| 40 | +def upgrade(): |
| 41 | + heartbeat_service_insert = f"""INSERT INTO services (id, name, created_at, active, count_as_live, message_limit, sms_daily_limit, email_annual_limit, sms_annual_limit, restricted, research_mode, prefix_sms, organisation_type, email_from, created_by_id, version) |
| 42 | + VALUES ('{NOTIFY_HEARTBEAT_SERVICE_ID}', 'GCNotify Heartbeat', '{NOW}', True, False, 20000, 20000, 5000000, 5000000, False, False, False, 'central', 'gc.notify.heartbeat.notification.gc', |
| 43 | + '{NOTIFY_USER_ID}', 1) |
| 44 | + """ |
| 45 | + op.execute(heartbeat_service_insert) |
| 46 | + |
| 47 | + heartbeat_service_history_insert = f"""INSERT INTO services_history (id, name, created_at, active, count_as_live, message_limit, sms_daily_limit, email_annual_limit, sms_annual_limit, restricted, research_mode, prefix_sms, organisation_type, email_from, created_by_id, version) |
| 48 | + VALUES ('{NOTIFY_HEARTBEAT_SERVICE_ID}', 'GCNotify Heartbeat', '{NOW}', True, False, 20000, 20000, 5000000, 5000000, False, False, False, 'central', 'gc.notify.heartbeat.notification.gc', |
| 49 | + '{NOTIFY_USER_ID}', 1) |
| 50 | + """ |
| 51 | + op.execute(heartbeat_service_history_insert) |
| 52 | + |
| 53 | + for send_type in ('sms', 'email'): |
| 54 | + heartbeat_service_permissions_insert = f"""INSERT INTO service_permissions (service_id, permission, created_at) VALUES ('{NOTIFY_HEARTBEAT_SERVICE_ID}', '{send_type}', '{NOW}')""" |
| 55 | + op.execute(heartbeat_service_permissions_insert) |
| 56 | + |
| 57 | + # Copy the service permissions from the existing Notify service to the new Heartbeat service. |
| 58 | + perms_insert = f""" |
| 59 | + INSERT INTO permissions (id, service_id, user_id, permission, created_at) |
| 60 | + SELECT uuid_in(md5(random()::text)::cstring), '{NOTIFY_HEARTBEAT_SERVICE_ID}', user_id, permission, '{NOW}' |
| 61 | + FROM permissions |
| 62 | + WHERE service_id = '{NOTIFY_SERVICE_ID}' |
| 63 | + """ |
| 64 | + op.execute(perms_insert) |
| 65 | + |
| 66 | + # The annual billing is required for new services. Let's copy the annual billing |
| 67 | + # data from existing Notify service to the new heartbeat service. |
| 68 | + annual_billing_insert = f""" |
| 69 | + INSERT INTO annual_billing |
| 70 | + (id, service_id, financial_year_start, free_sms_fragment_limit, created_at, updated_at) |
| 71 | + SELECT uuid_in(md5(random()::text)::cstring), '{NOTIFY_HEARTBEAT_SERVICE_ID}', financial_year_start, free_sms_fragment_limit, created_at, updated_at |
| 72 | + FROM annual_billing |
| 73 | + WHERE service_id = '{NOTIFY_SERVICE_ID}' |
| 74 | + ORDER BY financial_year_start DESC |
| 75 | + LIMIT 1 |
| 76 | + """ |
| 77 | + op.execute(annual_billing_insert) |
| 78 | + |
| 79 | + user_to_service_insert = f""" |
| 80 | + INSERT INTO user_to_service |
| 81 | + (user_id, service_id) |
| 82 | + SELECT user_id, '{NOTIFY_HEARTBEAT_SERVICE_ID}' |
| 83 | + FROM user_to_service |
| 84 | + WHERE service_id = '{NOTIFY_SERVICE_ID}' |
| 85 | + """ |
| 86 | + op.execute(user_to_service_insert) |
| 87 | + |
| 88 | + for template_id in TEMPLATE_IDS: |
| 89 | + op.execute( |
| 90 | + TEMPLATE_UPDATE.format( |
| 91 | + NOTIFY_HEARTBEAT_SERVICE_ID, |
| 92 | + template_id |
| 93 | + ) |
| 94 | + ) |
| 95 | + |
| 96 | + op.execute( |
| 97 | + TEMPLATE_HISTORY_UPDATE.format( |
| 98 | + NOTIFY_HEARTBEAT_SERVICE_ID, |
| 99 | + template_id |
| 100 | + ) |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +def downgrade(): |
| 105 | + annual_billing_delete = f"""DELETE FROM annual_billing WHERE service_id = '{NOTIFY_HEARTBEAT_SERVICE_ID}'""" |
| 106 | + op.execute(annual_billing_delete) |
| 107 | + |
| 108 | + user_to_service_delete = f"""DELETE FROM user_to_service WHERE service_id = '{NOTIFY_HEARTBEAT_SERVICE_ID}'""" |
| 109 | + op.execute(user_to_service_delete) |
| 110 | + |
| 111 | + for template_id in TEMPLATE_IDS: |
| 112 | + op.execute( |
| 113 | + TEMPLATE_UPDATE.format( |
| 114 | + NOTIFY_SERVICE_ID, |
| 115 | + template_id |
| 116 | + ) |
| 117 | + ) |
| 118 | + |
| 119 | + op.execute( |
| 120 | + TEMPLATE_HISTORY_UPDATE.format( |
| 121 | + NOTIFY_SERVICE_ID, |
| 122 | + template_id |
| 123 | + ) |
| 124 | + ) |
| 125 | + |
| 126 | + heartbeat_permissions_delete = f"""DELETE FROM permissions WHERE service_id = '{NOTIFY_HEARTBEAT_SERVICE_ID}'""" |
| 127 | + op.execute(heartbeat_permissions_delete) |
| 128 | + |
| 129 | + heartbeat_service_permissions_delete = f"""DELETE FROM service_permissions WHERE service_id = '{NOTIFY_HEARTBEAT_SERVICE_ID}'""" |
| 130 | + op.execute(heartbeat_service_permissions_delete) |
| 131 | + |
| 132 | + heartbeat_service_delete = f"""DELETE FROM services WHERE id = '{NOTIFY_HEARTBEAT_SERVICE_ID}'""" |
| 133 | + op.execute(heartbeat_service_delete) |
| 134 | + |
| 135 | + heartbeat_service_history_delete = f"""DELETE FROM services_history WHERE id = '{NOTIFY_HEARTBEAT_SERVICE_ID}'""" |
| 136 | + op.execute(heartbeat_service_history_delete) |
0 commit comments