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


class CreateTableSapPspAae(SQLPacket):
    """Adds the new tables and columns for the SAP-PSP-Elements."""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 20495

    sql_script = 'WI20495_create_table_sap_psp_aae.sql'
    sql_rollback_script = 'WI20495_rollback_table_sap_psp_aae.sql'

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

    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, separator='GO$')

        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, separator='GO$')
