from ppms.migration import SQLPacket, PacketCategory, MaxVersionDependency, VersionType
from ppms.constants import DBMS_ORACLE, DBMS_MSSQL
from ppms import ppms


class MaintainIndexOnDt468(SQLPacket):
    """Maintain the index on DT 468 for performance enhancements"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 21588

    drop_statement = { DBMS_ORACLE: 'drop index dt468_sched_read',
                       DBMS_MSSQL:  'drop index dt468_sched_read on dt468' }
    sql_script = 'WI21588_maintain_index_on_dt468.sql'

    def get_dependencies(self):
        return MaxVersionDependency(version='39.5.10',),

    def fix(self, packet_runner):
        if self.index_exists(table_name='DT468', index_name='dt468_sched_read'):
            self.modify_and_log(self.drop_statement[self.DBMS])
        else:
            self.log('Index dt468_sched_read does not exist yet and therefor does not need to be dropped')

        self.apply_fix_ddl_file(self.sql_planta_de_dbms, self.sql_script, split=True)

        return self.success()
