From 9eb14441abaa7c417be343f8891c7056546303ea Mon Sep 17 00:00:00 2001 From: k9845 Date: Thu, 22 Feb 2024 13:59:17 +0545 Subject: [PATCH] Add migration script for per notes --- per/migrations/0099_migrate_notes.py | 45 ++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 per/migrations/0099_migrate_notes.py diff --git a/per/migrations/0099_migrate_notes.py b/per/migrations/0099_migrate_notes.py new file mode 100644 index 0000000000..eafdad57fb --- /dev/null +++ b/per/migrations/0099_migrate_notes.py @@ -0,0 +1,45 @@ +# Generated by Django 3.2.23 on 2024-02-22 05:45 +from django.db import migrations + +from api.logger import logger + +class Migration(migrations.Migration): + + def migrate_formdata_notes(apps, schema_editor): + Form = apps.get_model('per', 'Form') + FormData = apps.get_model('per', 'FormData') + FormComponentResponse = apps.get_model('per', 'FormComponentResponse') + count = 0 + for form in Form.objects.all(): + logger.info(f"Processing Form {form.id}") + area = form.area + for component_response in area.formcomponent_set.all(): + logger.info(f"Processing Component Response {component_response.id}") + form_component_response = FormComponentResponse.objects.filter( + component=component_response, + ) + for response in form_component_response: + logger.info(f"Processing Response {response.id}") + form_data_entries = FormData.objects.filter( + form=form, + question__component=response.component, + question_id=74 + ).select_related('form', 'question', 'question__component') + if form_data_entries.exists(): + form_data = form_data_entries.first() + response.notes = form_data.notes + response.save(update_fields=['notes']) + count += 1 + print(f'Total {count} notes updated') + + dependencies = [ + ('per', '0098_fix_reversion_data_20240208_0502'), + ] + + operations = [ + migrations.RunPython( + migrate_formdata_notes, + reverse_code=migrations.RunPython.noop + ), + ] +