# -*- coding: latin-1 -*-
from ppms.migration import SQLPacket, PacketCategory, MaxVersionDependency, VersionType, PacketDependency, PacketState
from ppms import ppms
import os
        
class ModifySchemaTables(SQLPacket):
    ''' Modify existing columns to update VoE schema to Venus 
        
        INFO: "CreateSchemaTables" has to be installed first
    '''
    wi_number = 14041
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY

    PACKET_PATH = 'migration.customizing.DB_39_5_0.1_wi14040_create_schema_tables.CreateSchemaTables'
    sql_file = 'wi14041_modify_tables.sql'
    
    def get_dependencies(self):
        return (MaxVersionDependency(version='39.4', version_type=VersionType.CUSTOMIZING),
                PacketDependency(packet_path=self.PACKET_PATH, expected_state=PacketState.INSTALLED))

    def fix(self, runner):
        path = self.get_sql_file_path(self.sql_planta_de_dbms, self.sql_file)

        for query in self.get_queries_from_file(file_path=path, split=True):
            try:
                if not self.change_create_index_to_rename_index(query):
                    self.modify_and_log(query)
            except ppms.DatabaseError as e:
                if 'ORA-12006' in str(e) or 'ORA-02260' in str(e) \
                        or 'ORA-00955' in str(e) or 'ORA-02261' in str(
                        e) or 'ORA-01442' in str(e) or 'sqlserver' in str(e):
                    pass
                else:
                    raise e
        return self.success()
