@@ -0,0 +1,1765 @@
+%22%22%22%0A%0ARevision ID: 013_migrate_sms_allowance_data.py%0ARevises: 0135_stats_template_usage.py%0ACreate Date: 2017-11-10 21:42:59.715203%0A%0A%22%22%22%0Afrom datetime import datetime%0Afrom alembic import op%0Aimport uuid%0Afrom app.dao.date_util import get_current_financial_year_start_year%0A%0A%0Arevision = '0136_migrate_sms_allowance_data'%0Adown_revision = '0135_stats_template_usage'%0A%0A%0Adef upgrade():%0A    current_year = get_current_financial_year_start_year()%0A    default_limit = 250000%0A%0A    # Step 1: update the column free_sms_fragment_limit in service table if it is empty%0A    update_service_table = %22%22%22%0A        UPDATE services SET free_sms_fragment_limit = %7B%7D where free_sms_fragment_limit is null%0A    %22%22%22.format(default_limit)%0A%0A    op.execute(update_service_table)%0A%0A    # Step 2: insert at least one row for every service in current year if none exist for that service%0A    insert_row_if_not_exist = %22%22%22%0A        INSERT INTO annual_billing %0A        (id, service_id, financial_year_start, free_sms_fragment_limit, created_at, updated_at) %0A         SELECT uuid_in(md5(random()::text %7C%7C now()::text)::cstring), id, %7B%7D, %7B%7D, '%7B%7D', '%7B%7D' %0A         FROM services WHERE id NOT IN %0A        (select service_id from annual_billing)%0A    %22%22%22.format(current_year, default_limit, datetime.utcnow(), datetime.utcnow())%0A    op.execute(insert_row_if_not_exist)%0A%0A    # Step 3: copy the free_sms_fragment_limit data from the services table across to annual_billing table.%0A    update_sms_allowance = %22%22%22%0A        UPDATE annual_billing SET free_sms_fragment_limit = services.free_sms_fragment_limit%0A        FROM services%0A        WHERE annual_billing.service_id = services.id%0A    %22%22%22%0A    op.execute(update_sms_allowance)%0A%0A%0Adef downgrade():%0A    print('There is no action for downgrading to the previous version.')

migrate data from service table to annual billing