<reponame>uktrade/export-wins-data
# Generated by Django 2.2.13 on 2020-06-22 13:31
import yaml

from pathlib import PurePath

from django.db import migrations


fixture = PurePath(__file__).parent / '0004_sectors.yaml'


def update_sectors(apps, schema_editor):
    # This can't use load_yaml_data_in_migration due sector teams being
    # a many to many key
    Sector = apps.get_model('mi', 'Sector')
    with open(fixture, 'rb') as _fixture:
        object_list = yaml.safe_load(_fixture)
        for sector_data in object_list:
            fields = sector_data['fields']
            sector_id = sector_data['pk']
            sector = Sector.objects.get(pk=sector_id)
            if sector.name != fields['name']:
                sector.name = fields['name']
            sector_teams = fields['sector_team']
            sector.sector_team.clear()
            sector.sector_team.add(*sector_teams)
            sector.save()


class Migration(migrations.Migration):

    dependencies = [
        ('mi', '0003_sync_sectors'),
    ]

    operations = [
        migrations.RunPython(update_sectors, migrations.RunPython.noop),
    ]

# Generated by Django 2.2.13 on 2020-06-22 13:31
import yaml

from pathlib import PurePath

from django.db import migrations


fixture = PurePath(__file__).parent / '0004_sectors.yaml'


def update_sectors(apps, schema_editor):
    # This can't use load_yaml_data_in_migration due sector teams being
    # a many to many key
    Sector = apps.get_model('mi', 'Sector')
    with open(fixture, 'rb') as _fixture:
        object_list = yaml.safe_load(_fixture)
        for sector_data in object_list:
            fields = sector_data['fields']
            sector_id = sector_data['pk']
            sector = Sector.objects.get(pk=sector_id)
            if sector.name != fields['name']:
                sector.name = fields['name']
            sector_teams = fields['sector_team']
            sector.sector_team.clear()
            sector.sector_team.add(*sector_teams)
            sector.save()


class Migration(migrations.Migration):

    dependencies = [
        ('mi', '0003_sync_sectors'),
    ]

    operations = [
        migrations.RunPython(update_sectors, migrations.RunPython.noop),
    ]
# Generated by Django 2.2.13 on 2020-06-22 13:31 # This can't use load_yaml_data_in_migration due sector teams being # a many to many key