Skip to content

Commit

Permalink
Add migration script for per notes
Browse files Browse the repository at this point in the history
  • Loading branch information
k9845 committed Feb 26, 2024
1 parent 8dd9c08 commit 9eb1444
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions per/migrations/0099_migrate_notes.py
Original file line number Diff line number Diff line change
@@ -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
),
]

0 comments on commit 9eb1444

Please sign in to comment.