Skip to content

Commit e64cc67

Browse files
committed
Using overview to match legacy data
1 parent b87e01e commit e64cc67

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

per/migrations/0099_migrate_notes.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,42 @@
22
from django.db import migrations
33
from django.db import models
44

5-
from api.logger import logger
65

6+
def migrate_formdata_notes(apps, schema_editor):
7+
FormComponentResponse = apps.get_model("per", "FormComponentResponse")
8+
FormData = apps.get_model("per", "FormData")
79

8-
class Migration(migrations.Migration):
9-
def migrate_formdata_notes(apps, schema_editor):
10-
# NOTE: No any exact match for relationships
11-
FormComponentResponse = apps.get_model("per", "FormComponentResponse")
12-
FormData = apps.get_model("per", "FormData")
13-
qs = FormComponentResponse.objects.annotate(
14-
new_notes=models.Subquery(
15-
FormData.objects.filter(
16-
question__component=models.OuterRef("component"),
17-
question=74,
18-
)
19-
.order_by(
20-
"-form__area",
21-
"-form__created_at",
22-
"form",
23-
"question__question_num",
24-
)
25-
.filter(notes__isnull=False)
26-
.values("notes_en")[:1],
27-
output_field=models.CharField(),
28-
),
29-
).filter(new_notes__isnull=False)
30-
31-
print(
32-
qs.update(
33-
notes=models.F("new_notes"),
10+
qs = FormComponentResponse.objects.annotate(
11+
new_notes=models.Subquery(
12+
# Copy notes from FormData using overview
13+
FormData.objects.filter(
14+
form__overview=models.OuterRef('arearesponse__perassessment__overview'),
15+
question__component=models.OuterRef('component'),
16+
question=74,
17+
).values('notes_en'),
18+
output_field=models.CharField(),
19+
),
20+
).filter(new_notes__isnull=False)
21+
22+
form_component_responses = []
23+
for form_component_response_id, assessment_id, new_notes in qs.values_list(
24+
'id',
25+
models.F('arearesponse__perassessment'),
26+
'new_notes',
27+
):
28+
print(f'- Copying data for {assessment_id=} {form_component_response_id=}')
29+
form_component_responses.append(
30+
FormComponentResponse(
31+
id=form_component_response_id,
32+
notes=new_notes,
3433
)
3534
)
3635

36+
print(f'Total form_component_responses notes copied: {len(form_component_responses)}')
37+
FormComponentResponse.objects.bulk_update(form_component_responses, fields=('notes',))
38+
39+
40+
class Migration(migrations.Migration):
3741
dependencies = [
3842
("per", "0098_fix_reversion_data_20240208_0502"),
3943
]

0 commit comments

Comments
 (0)