mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 10:21:14 -05:00
Merge pull request #1389 from alphagov/vb-sms-allowance-data-migration
migrate data from service table to annual billing
This commit is contained in:
50
migrations/versions/0138_migrate_sms_allowance_data.py
Normal file
50
migrations/versions/0138_migrate_sms_allowance_data.py
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
"""
|
||||||
|
|
||||||
|
Revision ID: 0138_migrate_sms_allowance_data.py
|
||||||
|
Revises: 0137_stats_template_usage.py
|
||||||
|
Create Date: 2017-11-10 21:42:59.715203
|
||||||
|
|
||||||
|
"""
|
||||||
|
from datetime import datetime
|
||||||
|
from alembic import op
|
||||||
|
import uuid
|
||||||
|
from app.dao.date_util import get_current_financial_year_start_year
|
||||||
|
|
||||||
|
|
||||||
|
revision = '0138_migrate_sms_allowance_data'
|
||||||
|
down_revision = '0137_notification_template_hist'
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
current_year = get_current_financial_year_start_year()
|
||||||
|
default_limit = 250000
|
||||||
|
|
||||||
|
# Step 1: update the column free_sms_fragment_limit in service table if it is empty
|
||||||
|
update_service_table = """
|
||||||
|
UPDATE services SET free_sms_fragment_limit = {} where free_sms_fragment_limit is null
|
||||||
|
""".format(default_limit)
|
||||||
|
|
||||||
|
op.execute(update_service_table)
|
||||||
|
|
||||||
|
# Step 2: insert at least one row for every service in current year if none exist for that service
|
||||||
|
insert_row_if_not_exist = """
|
||||||
|
INSERT INTO annual_billing
|
||||||
|
(id, service_id, financial_year_start, free_sms_fragment_limit, created_at, updated_at)
|
||||||
|
SELECT uuid_in(md5(random()::text)::cstring), id, {}, {}, '{}', '{}'
|
||||||
|
FROM services WHERE id NOT IN
|
||||||
|
(select service_id from annual_billing)
|
||||||
|
""".format(current_year, default_limit, datetime.utcnow(), datetime.utcnow())
|
||||||
|
op.execute(insert_row_if_not_exist)
|
||||||
|
|
||||||
|
# Step 3: copy the free_sms_fragment_limit data from the services table across to annual_billing table.
|
||||||
|
update_sms_allowance = """
|
||||||
|
UPDATE annual_billing SET free_sms_fragment_limit = services.free_sms_fragment_limit
|
||||||
|
FROM services
|
||||||
|
WHERE annual_billing.service_id = services.id
|
||||||
|
"""
|
||||||
|
op.execute(update_sms_allowance)
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# There is no schema change. Only data migration and filling in gaps.
|
||||||
|
print('There is no action for downgrading to the previous version.')
|
||||||
Reference in New Issue
Block a user