# Generated by Django 2.2.15 on 2021-02-23 20:05
from collections import Counter

from django.db import migrations, models


def purge_absolute_schedules(apps, schema_editor):
    """ Removes all but one entry of any duplicates in FileToProcess. """
    FileToProcess = apps.get_model('database', 'FileToProcess')
    files = Counter(FileToProcess.objects.values_list("s3_file_path", flat=True))
    
    for path, count in files.items():
        if count > 1:
            # get ids, remove the "first", delete all that remain.
            pks = list(FileToProcess.objects.filter(s3_file_path=path).values_list("pk", flat=True))
            pks.pop(0)
            FileToProcess.objects.filter(pk__in=pks).delete()


class Migration(migrations.Migration):
    
    dependencies = [
        ('database', '0048_auto_20210212_1946'),
    ]
    
    operations = [
        migrations.RunPython(purge_absolute_schedules, migrations.RunPython.noop),
        migrations.AlterField(
            model_name='filetoprocess',
            name='s3_file_path',
            field=models.CharField(max_length=256, unique=True),
        ),
    ]
