# Generated by Django 3.0.8 on 2020-11-13 11:48

from django.db import migrations, transaction

from dataworkspace.apps.datasets.constants import TagType


def _migrate(apps, model_name):
    Dataset = apps.get_model("datasets", model_name)
    Tag = apps.get_model("datasets", "Tag")

    for dataset in Dataset.objects.all():
        for source_tag in dataset.source_tags.all():
            t, _ = Tag.objects.get_or_create(name=source_tag.name, type=TagType.SOURCE)
            dataset.tags.add(t)


@transaction.atomic
def migrate_source_tags_to_tags(apps, schema_editor):
    _migrate(apps, "Dataset")
    _migrate(apps, "ReferenceDataset")


class Migration(migrations.Migration):

    dependencies = [
        ("datasets", "0058_auto_20201113_1136"),
    ]

    operations = [migrations.RunPython(migrate_source_tags_to_tags)]
