2017-11-14 09:43:28 +00:00
|
|
|
"""
|
|
|
|
|
|
2017-11-16 13:44:55 +00:00
|
|
|
Revision ID: 0139_migrate_sms_allowance_data.py
|
|
|
|
|
Revises: 0138_sms_sender_nullable.py
|
2017-11-14 09:43:28 +00:00
|
|
|
Create Date: 2017-11-10 21:42:59.715203
|
|
|
|
|
|
|
|
|
|
"""
|
2024-04-01 15:12:33 -07:00
|
|
|
|
2017-11-14 09:43:28 +00:00
|
|
|
import uuid
|
2023-12-08 21:43:52 -05:00
|
|
|
from datetime import datetime
|
2023-07-17 10:26:23 -07:00
|
|
|
|
2023-12-08 21:43:52 -05:00
|
|
|
from alembic import op
|
2023-07-17 10:26:23 -07:00
|
|
|
from sqlalchemy import text
|
|
|
|
|
|
2023-06-14 13:19:11 -07:00
|
|
|
from app.dao.date_util import get_current_calendar_year_start_year
|
2024-06-14 16:01:04 -06:00
|
|
|
from app.utils import utc_now
|
2017-11-14 09:43:28 +00:00
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
revision = "0139_migrate_sms_allowance_data"
|
|
|
|
|
down_revision = "0138_sms_sender_nullable"
|
2017-11-14 09:43:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def upgrade():
|
2023-06-14 13:19:11 -07:00
|
|
|
current_year = get_current_calendar_year_start_year()
|
2017-11-14 09:43:28 +00:00
|
|
|
default_limit = 250000
|
|
|
|
|
|
|
|
|
|
# Step 1: update the column free_sms_fragment_limit in service table if it is empty
|
|
|
|
|
update_service_table = """
|
2023-07-17 10:26:23 -07:00
|
|
|
UPDATE services SET free_sms_fragment_limit = :default_limit where free_sms_fragment_limit is null
|
|
|
|
|
"""
|
2023-08-29 14:54:30 -07:00
|
|
|
input_params = {"default_limit": default_limit}
|
2023-07-17 10:26:23 -07:00
|
|
|
conn = op.get_bind()
|
|
|
|
|
conn.execute(text(update_service_table), input_params)
|
2017-11-14 09:43:28 +00:00
|
|
|
|
|
|
|
|
# Step 2: insert at least one row for every service in current year if none exist for that service
|
2023-07-17 10:26:23 -07:00
|
|
|
input_params = {
|
|
|
|
|
"current_year": current_year,
|
|
|
|
|
"default_limit": default_limit,
|
2024-06-14 16:01:04 -06:00
|
|
|
"time_now": utc_now(),
|
2023-07-17 10:26:23 -07:00
|
|
|
}
|
2017-11-14 09:43:28 +00:00
|
|
|
insert_row_if_not_exist = """
|
2023-11-17 09:47:32 -05:00
|
|
|
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, :current_year, :default_limit, :time_now, :time_now
|
|
|
|
|
FROM services WHERE id NOT IN
|
2017-11-14 09:43:28 +00:00
|
|
|
(select service_id from annual_billing)
|
2023-07-17 10:26:23 -07:00
|
|
|
"""
|
|
|
|
|
conn.execute(text(insert_row_if_not_exist), input_params)
|
2017-11-14 09:43:28 +00:00
|
|
|
|
|
|
|
|
# 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():
|
2017-11-15 11:25:27 +00:00
|
|
|
# There is no schema change. Only data migration and filling in gaps.
|
2023-08-29 14:54:30 -07:00
|
|
|
print("There is no action for downgrading to the previous version.")
|