# -*- coding: utf-8 -*-
import contextlib

from ppms.migration import BasePacket, SucceedOldPacket, PacketCategory, SinglePacketRunner, MaxVersionDependency


class CallOldUuidPacket(SucceedOldPacket):
    """Run CreateUUIDColumnsPacket """
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 18702

    UUID_PACKET = 'migration.hotfix.S_39_5_0.wi10491_create_uuid_columns.CreateUUIDColumnsPacket'

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

    def fix(self, packet_runner):
        uuid_packet = BasePacket.from_packet_path(packet_path=self.UUID_PACKET)

        if uuid_packet is None:
            self.log('CreateUUIDColumnsPacket does not exist, but that\'s okay!')
            return self.success()

        if uuid_packet.done:
            self.log('CreateUUIDColumnsPacket was set to done - reverting that for now...')
            uuid_packet.done = False

        self.log('Running CreateUUIDColumnsPacket')
        SinglePacketRunner(uuid_packet.migrationrule).run()
        self.log('Packet finished!')

        if uuid_packet.done:
            self.log('CreateUUIDColumnsPacket succeeded - we are done here')
            return self.success()

        return self.fail('CreateUUIDColumnsPacket did not succeed!')
