|
2 | 2 | from django.db import migrations |
3 | 3 | from django.db import models |
4 | 4 |
|
5 | | -from api.logger import logger |
6 | 5 |
|
| 6 | +def migrate_formdata_notes(apps, schema_editor): |
| 7 | + FormComponentResponse = apps.get_model("per", "FormComponentResponse") |
| 8 | + FormData = apps.get_model("per", "FormData") |
7 | 9 |
|
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, |
34 | 33 | ) |
35 | 34 | ) |
36 | 35 |
|
| 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): |
37 | 41 | dependencies = [ |
38 | 42 | ("per", "0098_fix_reversion_data_20240208_0502"), |
39 | 43 | ] |
|
0 commit comments