from ppms import ppms
from ppms.migration import PacketCategory, SQLPacket, MinVersionDependency, MaxVersionDependency, VersionType


class CleanupOldKoAGrFields(SQLPacket):
    """ Deletes the old cost calculation procedures."""
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 9158
    
    def get_dependencies(self):
        return (MinVersionDependency(version='39.5.0', 
                                     version_type=VersionType.CUSTOMIZING),
                MaxVersionDependency(version='39.5.3', 
                                     version_type=VersionType.CUSTOMIZING))

    def fix(self, runner):  
        # delete old cost procedures
        procedure_names = ["P_VERDICHTE_PR_SB",
                           "P_UPD_KOST_AUFW_ERL_AKT",
                           "P_UPD_KOST_AUFW_ERL_SB",
                           "P_VERDICHTE_PF",
                           "P_VERDICHTE_PR_AKT"]
        for procedure in procedure_names:
            try:
                self.modify_and_log("drop procedure {procedure}".format(procedure=procedure))
            except ppms.DatabaseError:
                # if procedure is not there, ignore it
                pass
                
        return self.success()
