# Generated by Django 3.2.16 on 2023-02-15 15:34

from django.db import migrations, models


def run_data_migration(apps, schema_editor):
    Resource = apps.get_model('datacite', 'Resource')

    subjects = {}
    for resource in Resource.objects.all():
        for subject in resource.subjects.all():
            if subject.subject in subjects:
                subjects[subject.subject].resources.add(resource)
                subject.delete()
            else:
                subjects[subject.subject] = subject
                subject.resources.add(resource)


class Migration(migrations.Migration):

    dependencies = [
        ('datacite', '0009_change_ordering'),
    ]

    operations = [
        migrations.AddField(
            model_name='resource',
            name='subjects',
            field=models.ManyToManyField(blank=True, related_name='resources', to='datacite.Subject'),
        ),
        migrations.RunPython(run_data_migration),
        migrations.RemoveField(
            model_name='subject',
            name='resource',
        ),
    ]
