# -*- coding: utf-8 -*-
from ppms.migration import SQLPacket, PacketCategory, MaxVersionDependency, VersionType
from ppms.constants import DBMS_ORACLE, DBMS_MSSQL
from ppms import ppms


class MoveDeviationOutOfScheduling(SQLPacket):
    """This drops column DI001296, which was former calculated by scheduling, as this a virtual field now."""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 20311
    
    def get_dependencies(self):
        return MaxVersionDependency(version='39.5.9', version_type=VersionType.CUSTOMIZING),
    
    def fix(self, packet_runner):
        try:
            if self.DBMS == DBMS_MSSQL:
                self.modify_and_log("alter table dt461 drop constraint DT461DI001296_DEFAULT")
                self.modify_and_log("alter table dt463 drop constraint DT463DI001177_DEFAULT")
    
            self.modify_and_log("alter table dt461 drop column DI001296")
            self.modify_and_log("alter table dt461 drop column DI001177")
        except ppms.DatabaseError:
            # as this is also a server package, the column might already be gone
            pass
        
        return self.success()