# -*- coding: utf-8 -*-
from ppms.migration.packet import *

class DeleteRelation(SQLPacket):
    '''Delete obsolete relation.

    Due to DT032 being deactivated, the relation on it in DT030 might cause errors.
    '''

    wi_number = 11504
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY

    DELETE_RELATION = """DELETE FROM DT416 WHERE DI000389 = '030' AND DI000277 = '032'"""
    DEACTIVATE_DI = """UPDATE DT412 SET ENABLED = '0' WHERE DI000238 = '026174'"""
    DROP_CONSTRAINT = """ALTER TABLE DT030 DROP COLUMN DI026174"""

    def fix(self, runner):
        failures = 0

        if not self.modify_and_log(self.DELETE_RELATION):
            failures += 1

        if not self.modify_and_log(self.DEACTIVATE_DI):
            failures += 1

        if not self.modify_and_log(self.DROP_CONSTRAINT):
            failures += 1

        return self.success()
