Skip to content

Commit

Permalink
Add migration to generate probleminstances
Browse files Browse the repository at this point in the history
  • Loading branch information
matijapretnar committed Mar 27, 2023
1 parent 3484f91 commit fd5257f
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions web/courses/migrations/0005_generate_probleminstances.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 4.1.2 on 2023-02-19 11:02

from django.db import migrations
from django.db import models


def generate_probleminstances(apps, schema_editor):
# We can't import the Person model directly as it may be a newer
# version than this migration expects. We use the historical version.
Problem = apps.get_model('problems', 'Problem')
ProblemInstance = apps.get_model('courses', 'ProblemInstance')
problems = Problem.objects.values_list('id', 'problem_set_id', 'visible', '_order')
instances = [ProblemInstance(
problem_id=id,
problem_set_id=problem_set_id,
visible=visible,
_order=_order
) for id, problem_set_id, visible, _order in problems
]
instances = ProblemInstance.objects.bulk_create(instances, batch_size=1000)
problem_id_to_instance_id = {instance.problem_id: instance.id for instance in instances}
for class_name in ["Attempt", "HistoricalAttempt"]:
Attempt = apps.get_model('attempts', class_name)
attempts = Attempt.objects.annotate(problem_id=models.F('part__problem_id'))
for attempt in attempts:
attempt.problem_instance_id = problem_id_to_instance_id[attempt.problem_id]
Attempt.objects.bulk_update(attempts, ['problem_instance_id'], batch_size=1000)


class Migration(migrations.Migration):
dependencies = [
("courses", "0004_probleminstance"),
("attempts", "0004_attempt_problem_instance_and_more"),
]

operations = [
migrations.RunPython(generate_probleminstances),
]

0 comments on commit fd5257f

Please sign in to comment.