# -*- coding: utf-8 -*-
from ppms import ppms
from ppms.migration.packet import *


class FixFaultyHyperlinkCRCPacket(SQLPacket):
    '''Fix the faulty CRC code of a Hyperlink in the demodata

    The CRC code in question belongs to a hyperlink in the demodata that is
    checked in a Regression test. Without this fix the regtest that checks if
    the Hyperlink functionality works correctly would fail.     
    '''
    version = 1
    done_after_success = True
    run_on_startup = True
    wi_number = 9977
    category = PacketCategory.CATEGORY_MANDATORY

    def fix(self, runner):
        query = """update DT397
                   set DI059199 = -677712692
                     where DI008060 = (select DI026169 from DT031 where DI026133 = '001246')"""

        rows = self.modify_and_log(query)
        if rows == 1:
            return self.success()

        ppms.db_rollback()
        return self.fail('Unexpected row count (%i != 1)' % rows)


class PortGlobalVariable(ChangeDTPPacket):
    '''Port the value of a global variable that was changed in Venus to VoE

    This sets the global variable that determines where Hyperlink files should
    be saved by default to the Venus value. This is necessary for internal 
    tests at PLANTA as our folder structure changed.
    '''
    version = 1
    done_after_success = True
    run_on_startup = True
    wi_number = 9977
    category = PacketCategory.CATEGORY_MANDATORY

    def fix(self, runner):
        new_value = 'T:\\ppms\\ppms-dms\\'
        old_value = 'H:\\ppms\\ppms-dms\\'

        record = self.get_record(452, [1102], [3109, 3107])
        if not record:
            return self.fail()

        if not self.verify_value(record, '_g', 96):
            return self.fail()

        if not self.change_value(record, 'value', old_value, new_value):
            return self.fail()

        return self.success()
