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


class AddSapInterfaceColumns(SQLPacket):
    """Adds timestamp and structure fields for the SAP PSP interface."""

    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 20496

    sql_script = 'WI20496_add_sap_interface_columns.sql'
    sql_rollback_script = 'WI20496_rollback_sap_interface_columns.sql'

    REQUIRED_PACKET_PATH = 'migration.customizing.DB_39_5_10.WI20495_create_table_sap_psp_aae.CreateTableSapPspAae'

    def get_dependencies(self):
        return (MinVersionDependency(version='39.5.8', version_type=VersionType.CUSTOMIZING),
                MaxVersionDependency(version='39.5.9', version_type=VersionType.CUSTOMIZING),
                PacketDependency(packet_path=self.REQUIRED_PACKET_PATH, expected_state=PacketState.INSTALLED))

    def fix(self, packet_runner):
        if self.DBMS == DBMS_ORACLE:
            self.apply_fix_ddl_file(self.sql_planta_de_dbms, self.sql_script, split=True)
        elif self.DBMS == DBMS_MSSQL:
            self.apply_fix_ddl_file(self.sql_planta_de_dbms, self.sql_script, split=True)
        return self.success()

    def rollback(self):
        if self.DBMS == DBMS_ORACLE:
            self.apply_fix_ddl_file(self.sql_planta_de_dbms, self.sql_script, split=True)
        elif self.DBMS == DBMS_MSSQL:
            self.apply_fix_ddl_file(self.sql_planta_de_dbms, self.sql_script, split=True)
