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

class z_AdjustSchema(SQLPacket):
    ''' Adjust the schema so that it matches with our current schema.
    '''
    wi_number = 16856
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    sql_file = 'WI16856_adjust_schema.sql'

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

    def fix(self, runner):
        errors = 0
        path = self.get_sql_file_path(self.sql_planta_de_dbms, self.sql_file)
        for statement in self.get_queries_from_file(file_path=path, split=True, separator=';'):
            try:
                self.modify_and_log(statement)
            except ppms.DatabaseError:
                errors += 1
                self.log(
                    """Following statement failed: """ + statement + """.\n Most likely the changes are already done, please check the logfile.""")
        return self.success()
