<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import django.utils.timezone
from django.conf import settings


def create_volume(old_volume, Instance, InstanceSourceTmp, VolumeTmp):

    old_source = old_volume.instancesource_ptr
    source = InstanceSourceTmp.objects.create(
        provider=old_source.provider,
        identifier=old_source.identifier,
        created_by=old_source.created_by,
        created_by_identity=old_source.created_by_identity,
        start_date=old_source.start_date,
        end_date=old_source.end_date
    )

    # Update instance pointers
    instances = Instance.objects.filter(source_id=old_volume.instancesource_ptr_id)

    for instance in instances:
        instance.source_tmp = source
        instance.save()
    new_volume = VolumeTmp.objects.create(
        size=old_volume.size,
        name=old_volume.name,
        description=old_volume.description,
        instance_source=source
    )

    # Update old_volume status projects
    projects = old_volume.projects_set.all()

    for entry in projects:
        entry.volume_tmp = new_volume
        entry.save()
    print 'Updated %s projects on Volume %s' % (len(projects),old_source.identifier)

    # Update old_volume status history
    history = old_volume.volumestatushistory_set.all()

    for entry in history:
        entry.volume_tmp = new_volume
        entry.save()


def create_machine(apps, old_machine):
    InstanceSourceTmp = apps.get_model("core", "InstanceSourceTmp")
    Instance = apps.get_model("core", "Instance")
    ProviderMachineTmp = apps.get_model("core", "ProviderMachineTmp")

    old_source = old_machine.instancesource_ptr
    source = InstanceSourceTmp.objects.create(
        provider=old_source.provider,
        identifier=old_source.identifier,
        created_by=old_source.created_by,
        created_by_identity=old_source.created_by_identity,
        start_date=old_source.start_date,
        end_date=old_source.end_date
    )

    # Update instance pointers (match by 'string' rather than ID)
    instances = Instance.objects.filter(
            source__identifier=old_source.identifier)
    for instance in instances:
        instance.source_tmp = source
        instance.save()

    return ProviderMachineTmp.objects.create(
        application_id=old_machine.application.id,
        version=old_machine.version,
        instance_source=source
    )
def restore_machine_request(machine_request, ProviderMachine, machine_cache):
    try:
        parent = ProviderMachine.objects.get(id=machine_request.parent_machine_id)
    except ProviderMachine.DoesNotExist:
        machine_request.parent_machine = machine_request.instance.source.providermachine
        machine_request.save()
        parent = ProviderMachine.objects.get(id=machine_request.parent_machine.id)

    new_parent_machine = machine_cache[parent.id]
    machine_request.parent_machine_tmp_id = new_parent_machine.id

    if machine_request.new_machine_id:
        try:
            new_machine = machine_request.new_machine
            new_new_machine = machine_cache[new_machine.id]
            machine_request.new_machine_tmp_id = new_new_machine.id
        except Exception as dne:
            print 'Warn: The new machine created from Instance %s (Named %s) has been lost (Provider %s)'\
                    % (machine_request.instance.provider_alias,
                       machine_request.new_machine_name,
                       machine_request.new_machine_provider.location)
            machine_request.new_machine_id = None

    machine_request.save()

def associate_machine(machine, new_machine):

    new_machine.licenses = machine.licenses.all()
    new_machine.save()

    # Update membership
    members = machine.providermachinemembership_set.all()

    for member in members:
        member.provider_machine_tmp = new_machine
        member.save()

def copy_data_to_new_models(apps, schema_editor):
    Instance = apps.get_model("core", "Instance")
    InstanceSourceTmp = apps.get_model("core", "InstanceSourceTmp")
    VolumeTmp = apps.get_model("core", "VolumeTmp")
    ProviderMachine = apps.get_model("core", "ProviderMachine")
    Volume = apps.get_model("core", "Volume")
    MachineRequest = apps.get_model("core", "MachineRequest")

        # Update machine requests
        MachineRequest = apps.get_model("core", "MachineRequest")
        machine_requests = MachineRequest.objects.filter(new_machine_id=machine.id)

    machine_cache = {}
    for machine in machines:
        new_machine = create_machine(apps, machine)
        machine_cache[machine.id] = new_machine

    #These associations must be made AFTER all ProviderMachines are built
    for machine_request in machine_requests:
        restore_machine_request(machine_request, ProviderMachine, machine_cache)
    for machine in machines:
        new_machine = machine_cache[machine.id]
        associate_machine(machine, new_machine)

    for volume in volumes:
        create_volume(apps, volume, Instance, InstanceSourceTmp, VolumeTmp)<commit_msg>Worked and ran into the 'already corrected' deferred-trigger problem.<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import django.utils.timezone
from django.conf import settings


def create_volume(old_volume, Instance, InstanceSourceTmp, VolumeTmp):

    old_source = old_volume.instancesource_ptr
    source = InstanceSourceTmp.objects.create(
        provider=old_source.provider,
        identifier=old_source.identifier,
        created_by=old_source.created_by,
        created_by_identity=old_source.created_by_identity,
        start_date=old_source.start_date,
        end_date=old_source.end_date
    )

    # Update instance pointers
    instances = Instance.objects.filter(source_id=old_volume.instancesource_ptr_id)

    for instance in instances:
        instance.source_tmp = source
        instance.save()
    new_volume = VolumeTmp.objects.create(
        size=old_volume.size,
        name=old_volume.name,
        description=old_volume.description,
        instance_source=source
    )

    # Update old_volume status projects
    projects = old_volume.projects.all()

    for entry in projects:
        entry.volume_tmp = new_volume
        entry.save()
    print 'Updated %s projects on Volume %s' % (len(projects),old_source.identifier)

    # Update old_volume status history
    history = old_volume.volumestatushistory_set.all()

    for entry in history:
        entry.volume_tmp = new_volume
        entry.save()


def create_machine(apps, old_machine):
    InstanceSourceTmp = apps.get_model("core", "InstanceSourceTmp")
    Instance = apps.get_model("core", "Instance")
    ProviderMachineTmp = apps.get_model("core", "ProviderMachineTmp")

    old_source = old_machine.instancesource_ptr
    source = InstanceSourceTmp.objects.create(
        provider=old_source.provider,
        identifier=old_source.identifier,
        created_by=old_source.created_by,
        created_by_identity=old_source.created_by_identity,
        start_date=old_source.start_date,
        end_date=old_source.end_date
    )

    # Update instance pointers (match by 'string' rather than ID)
    instances = Instance.objects.filter(
            source__identifier=old_source.identifier)
    for instance in instances:
        instance.source_tmp = source
        instance.save()

    return ProviderMachineTmp.objects.create(
        application_id=old_machine.application.id,
        version=old_machine.version,
        instance_source=source
    )
def restore_machine_request(machine_request, ProviderMachine, machine_cache):
    try:
        parent = ProviderMachine.objects.get(id=machine_request.parent_machine_id)
    except ProviderMachine.DoesNotExist:
        machine_request.parent_machine = machine_request.instance.source.providermachine
        machine_request.save()
        parent = ProviderMachine.objects.get(id=machine_request.parent_machine.id)

    new_parent_machine = machine_cache[parent.id]
    machine_request.parent_machine_tmp_id = new_parent_machine.id

    if machine_request.new_machine_id:
        try:
            new_machine = machine_request.new_machine
            new_new_machine = machine_cache[new_machine.id]
            machine_request.new_machine_tmp_id = new_new_machine.id
        except Exception as dne:
            print 'Warn: The new machine created from Instance %s (Named %s) has been lost (Provider %s)'\
                    % (machine_request.instance.provider_alias,
                       machine_request.new_machine_name,
                       machine_request.new_machine_provider.location)
            machine_request.new_machine_id = None

    machine_request.save()

def associate_machine(machine, new_machine):

    new_machine.licenses = machine.licenses.all()
    new_machine.save()

    # Update membership
    members = machine.providermachinemembership_set.all()

    for member in members:
        member.provider_machine_tmp = new_machine
        member.save()

def copy_data_to_new_models(apps, schema_editor):
    Instance = apps.get_model("core", "Instance")
    InstanceSourceTmp = apps.get_model("core", "InstanceSourceTmp")
    VolumeTmp = apps.get_model("core", "VolumeTmp")
    ProviderMachine = apps.get_model("core", "ProviderMachine")
    Volume = apps.get_model("core", "Volume")
    MachineRequest = apps.get_model("core", "MachineRequest")

        # Update machine requests
        MachineRequest = apps.get_model("core", "MachineRequest")
        machine_requests = MachineRequest.objects.filter(new_machine_id=machine.id)

    machine_cache = {}
    for machine in machines:
        new_machine = create_machine(apps, machine)
        machine_cache[machine.id] = new_machine

    #These associations must be made AFTER all ProviderMachines are built
    for machine_request in machine_requests:
        restore_machine_request(machine_request, ProviderMachine, machine_cache)
    for machine in machines:
        new_machine = machine_cache[machine.id]
        associate_machine(machine, new_machine)

    for volume in volumes:
        create_volume(volume, Instance, InstanceSourceTmp, VolumeTmp)
{'fraction_non_alphanumeric': 0.0792647344874538, 'fraction_numerical': 0.00019451468585878233, 'mean_word_length': 3.455372616984402, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3941458', 'n_tokens_mistral': 2926, 'n_tokens_neox': 2870, 'n_words': 554}