# -*- coding: utf-8 -*-
from ppms import ppms
from ppms.constants import *
from ppms.migration import SQLPacket, PacketCategory, MaxVersionDependency, VersionType, MinVersionDependency, \
    PacketDependency, PacketState

ALTER_TABLE_STATEMENTS = {DBMS_ORACLE: ("alter table DT498 add PLACEHOLDER varchar2(80 char)",
                                        "alter table DT499 add PLACEHOLDER varchar2(80 char)"),

                          DBMS_MSSQL:  ("alter table DT498 add PLACEHOLDER varchar(80)",
                                        "alter table DT499 add PLACEHOLDER varchar(80)")}


class A_PortProcessPlaceholderSchema(SQLPacket):
    """Port the process placeholder schema changes"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13731

    def get_dependencies(self):
        return (MinVersionDependency(version='39.4', version_type=VersionType.CUSTOMIZING),
                MaxVersionDependency(version='39.5', version_type=VersionType.CUSTOMIZING))

    def fix(self, runner):
        for statement in ALTER_TABLE_STATEMENTS[ppms.db_get_dbms_name()]:
            self.modify_and_log(statement)

        return self.success()


class B_AddProcessDeletionFlagCustomizing(SQLPacket):
    """Port the process placeholder customizings"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13731

    FILENAME = 'wi13731_process_placeholder.sql'
    __PATH = r'migration.hotfix.S_39_5_14.wi13731_process_placeholder.A_PortProcessPlaceholderSchema'

    def get_dependencies(self):
        return (MaxVersionDependency(version='39.4', version_type=VersionType.CUSTOMIZING),
                PacketDependency(packet_path=self.__PATH, expected_state=PacketState.INSTALLED))

    def fix(self, runner):
        self.apply_dml_file(self.sql_planta_de_dbms, self.FILENAME)

        return self.success()