# Generated by Django 3.1.3 on 2020-12-03 13:13

from django.db import migrations
import datetime

def forwards(apps, schema_editor):
    Ranking = apps.get_model('podminer.Ranking')
    RankingHistory = apps.get_model('podminer.RankingHistory')
    cc=0
    for ranking in Ranking.objects.all():
        current_time=ranking.updated.date()
        last_record=RankingHistory.objects.filter(country=ranking.country,genre=ranking.genre,show=ranking.show)
        last_record=last_record.filter(updated__date__lt=current_time).order_by('-updated').first()
        if last_record:
            ranking.last_ranking=last_record.ranking
            ranking.save()

        cc+=1
        if cc%1000==0:
            print(cc) #to keep track of progress

class Migration(migrations.Migration):

    dependencies = [
        ('podminer', '0010_usermetrics'),
    ]

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

