# -*- coding: utf-8 -*-

from ppms.migration import SQLPacket, PacketCategory, MinVersionDependency, MaxVersionDependency, \
    VersionType
import hql


class CorrectCustomerOLEs(SQLPacket):
    """Sets the field 'use_39' in the OLE table for customer OLEs."""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 18488

    update_use39 = """update DT031 set DI059034 = 1 where DI026139 = '{license}'"""

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

    def get_active_license(self):
        customer_license = hql.query("""select license_id from ModelParameters where active = 1""")[0][0]
        return customer_license

    def fix(self, packet_runner):
        sys_license = self.get_active_license()
        if sys_license == '011':
            return self.fail("This migration packet is for customer licenses only!")

        self.modify_and_log(self.update_use39.format(license=sys_license))

        return self.success()
