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

class FixLoadAutoNumber(SQLPacket):
    '''Fixed the function which creates the auto number for load creation.
    This will now create an initial counter record if it can't find one for
    the system's license.
    '''
    
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 17300

    sql_file = 'WI17300_fix_load_auto_nr.sql'
    
    def get_dependencies(self):
        return (MinVersionDependency(version='39.5.6', 
                                     version_type=VersionType.CUSTOMIZING),
                MaxVersionDependency(version='39.5.6', 
                                     version_type=VersionType.CUSTOMIZING))

    def fix(self, runner):
        if self.DBMS == DBMS_ORACLE:
            self.apply_fix_ddl_file(self.sql_planta_de_dbms, self.sql_file)
        else:
            self.apply_fix_ddl_file(self.sql_planta_de_dbms, self.sql_file, split=True, separator='GO')
        return self.success()