# -*- coding: utf-8 -*-
# Generated by Django 1.9.13 on 2017-08-26 06:34
from __future__ import unicode_literals

# from django.core.management import call_command
from django.db import migrations


def move_old_aims_columns():
    '''
    Generate the SQL to move old information from the aims columns before the merge with IATI
    '''
    sql = []
    updatecode = 'UPDATE aims_{table} SET {column} = (SELECT {column} FROM oldaims_{table} WHERE oldaims_{table}.{old_table_pk} = aims_{table}.{new_table_pk});'

    columns = {
        'activity': ['openly_status', 'completion', 'total_commitment', 'total_commitment_currency_id',
                     'total_commitment_usd'],
        'location': ['area_id'],
        'title': [],
        'transaction': [],
        'valuetype': [],
    }

    for table, columns in columns.items():
        for colname in columns:
            sql.append(
                updatecode.format(table=table, column=colname, old_table_pk='remote_data_id', new_table_pk='id')
            )
        sql.append('DROP TABLE oldaims_{table}'.format(table=table))
    return sql


class Migration(migrations.Migration):

    dependencies = [
        ('aims', '0045_prepare_move_oldaims_aims'),
    ]

    operations = [
        migrations.RunSQL(';\n'.join(move_old_aims_columns())),
    ]
