Skip to content

Commit 4820e44

Browse files
authored
Merge pull request #2056 from IFRCGo/fix/per-migration-notes
Migrate all notes from existing PER to new
2 parents 04f8e45 + edf75a9 commit 4820e44

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Generated by Django 3.2.23 on 2024-02-22 05:45
2+
from django.db import migrations
3+
from django.db import models
4+
5+
6+
# NOTE: Same as 0099_migrate_notes but this migrates all of the notes using Question "Component performance"
7+
# For more info look at per/migrations/0086_migrate_old_form.py
8+
def migrate_formdata_notes(apps, schema_editor):
9+
FormComponentResponse = apps.get_model("per", "FormComponentResponse")
10+
FormData = apps.get_model("per", "FormData")
11+
12+
qs = FormComponentResponse.objects.annotate(
13+
new_notes=models.Subquery(
14+
# Copy notes from FormData using overview
15+
FormData.objects.filter(
16+
form__overview=models.OuterRef('arearesponse__perassessment__overview'),
17+
question__component=models.OuterRef('component'),
18+
question__question__iexact='Component performance',
19+
).values('notes_en'),
20+
output_field=models.CharField(),
21+
),
22+
).filter(new_notes__isnull=False)
23+
24+
form_component_responses = []
25+
for form_component_response_id, assessment_id, new_notes in qs.values_list(
26+
'id',
27+
models.F('arearesponse__perassessment'),
28+
'new_notes',
29+
):
30+
print(f'- Copying data for {assessment_id=} {form_component_response_id=}')
31+
form_component_responses.append(
32+
FormComponentResponse(
33+
id=form_component_response_id,
34+
notes=new_notes,
35+
)
36+
)
37+
38+
print(f'Total form_component_responses notes copied: {len(form_component_responses)}')
39+
FormComponentResponse.objects.bulk_update(form_component_responses, fields=('notes',))
40+
41+
42+
class Migration(migrations.Migration):
43+
dependencies = [
44+
("per", "0099_migrate_notes"),
45+
]
46+
47+
operations = [
48+
migrations.RunPython(migrate_formdata_notes, reverse_code=migrations.RunPython.noop),
49+
]

0 commit comments

Comments
 (0)