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


class FixLoadCreation(SQLPacket):
    """Fixes the creation of loads with Oracle when having more than one
    system parameter record."""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 17218
    sql_file_view = 'WI17218_fix_load_creation.sql'
    sql_file_trigger = 'WI17218_fix_load_creation_trigger.sql'

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

    def fix(self, runner):
        if self.DBMS == DBMS_ORACLE:
            # fix ORA only, as mssql doesn't have the problem
            self.apply_dml_file(self.sql_planta_de_dbms, self.sql_file_view)
            self.apply_fix_ddl_file(self.sql_planta_de_dbms, self.sql_file_trigger)

        return self.success()