# -*- coding: latin-1 -*-
from ppms.migration import SQLPacket, PacketCategory
from ppms import ppms
import re
from ppms.constants import *


class DeleteOldMigrationPacketCAPRasterCurves(SQLPacket):
    '''Deletes the old migration packet PortCAPRasterCurves and the load curves it has created.
    '''
    wi_number = 20794
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY

    def delete_old_packet(self):
        self.log_heading("Deleting old migration packet")
        statements = [
            '''delete from MIGRATIONHISTORY where MIGRATIONRULE in (
            select UUID from MIGRATIONRULE where PYTHONPATH in (
            'migration.server.S_39_5_22.WI19215_port_CAPRaster_curves.PortCAPRasterCurves'))''',
            '''delete from MIGRATIONRULE where PYTHONPATH in (
            'migration.server.S_39_5_22.WI19215_port_CAPRaster_curves.PortCAPRasterCurves')'''
        ]

        for statement in statements:
            self.modify_and_log(statement)

    def delete_old_data(self):
        self.log_heading('Deleting old data')
        self.modify_and_log("delete from dt431 where di000717 in ('CAPWEEK', 'CAPMONTH', 'CAPQUART', 'CAPYEAR')")

    def fix(self, runner):
        self.delete_old_packet()
        self.delete_old_data()

        return self.success()
