# Generated by Django 4.0.6 on 2022-08-19 09:55

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('question', '0011_alter_question_keywords'),
    ]

    # On 2022.11.20 It took 6 minutes to perform this migration on HAROON's local machine
    def modify(apps, schema_editor):

        try:
            AnswerSheet = apps.get_model("text_book", "AnswerSheet")

            print("reading AnswerSheet data from database")

            db_alias = schema_editor.connection.alias
            anwer_sheets = AnswerSheet.objects.using(db_alias).prefetch_related("question").all()

            i = 0
            count = len(anwer_sheets)
            previous_percent = -1

            for anwer_sheet in anwer_sheets:

                percent = 100 * i//count
                if previous_percent != percent:
                    print(str(percent) + "%")
                    previous_percent = percent
                i += 1

                question = anwer_sheet.question
                question.solution = anwer_sheet.text
                question.save()
        except:
            print("No installed app with label 'text_book'.")

    operations = [
        migrations.RunPython(modify),
    ]
