# -*- coding: latin-1 -*-
from ppms.migration import SQLPacket, PacketCategory, MaxVersionDependency, VersionType
from ppms import ppms
import os


class CreateSchemaTables(SQLPacket):
    """Add new columns to update VoE schema to Venus"""

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

    sql_file = 'wi14040_create_tables.sql'

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

    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, separator=';'):
            try:
                if not self.change_create_index_to_rename_index(query):
                    self.modify_and_log(query)
            except ppms.DatabaseError as e:
                if 'sqlserver' in str(e):
                    self.log(
                        'There was an error in statement: ' + query + '. The statement is probably not needed in this system, please check the logfiles.')
                else:
                    raise e

        return self.success()
