Skip to content

Commit 16417b9

Browse files
committed
Add migration to generate probleminstances
1 parent dca4c21 commit 16417b9

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Generated by Django 4.1.2 on 2023-02-19 11:02
2+
3+
from django.db import migrations
4+
from django.db import models
5+
6+
7+
def generate_probleminstances(apps, schema_editor):
8+
# We can't import the Person model directly as it may be a newer
9+
# version than this migration expects. We use the historical version.
10+
Problem = apps.get_model('problems', 'Problem')
11+
ProblemInstance = apps.get_model('courses', 'ProblemInstance')
12+
problems = Problem.objects.values_list('id', 'problem_set_id', 'visible', '_order')
13+
instances = [ProblemInstance(
14+
problem_id=id,
15+
problem_set_id=problem_set_id,
16+
visible=visible,
17+
_order=_order
18+
) for id, problem_set_id, visible, _order in problems
19+
]
20+
instances = ProblemInstance.objects.bulk_create(instances, batch_size=1000)
21+
problem_id_to_instance_id = {instance.problem_id: instance.id for instance in instances}
22+
for class_name in ["Attempt", "HistoricalAttempt"]:
23+
Attempt = apps.get_model('attempts', class_name)
24+
attempts = Attempt.objects.annotate(problem_id=models.F('part__problem_id'))
25+
for attempt in attempts:
26+
attempt.problem_instance_id = problem_id_to_instance_id[attempt.problem_id]
27+
Attempt.objects.bulk_update(attempts, ['problem_instance_id'], batch_size=1000)
28+
29+
30+
class Migration(migrations.Migration):
31+
dependencies = [
32+
("courses", "0004_probleminstance"),
33+
("attempts", "0004_attempt_problem_instance_and_more"),
34+
]
35+
36+
operations = [
37+
migrations.RunPython(generate_probleminstances),
38+
]

0 commit comments

Comments
 (0)