Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ def migrate_fhir_id_to_v2(apps=None, schema_editor=None) -> None:

apps_mod = apps if apps is not None else global_apps
Crosswalk = apps_mod.get_model('bluebutton', 'Crosswalk')
for crosswalk in Crosswalk.objects.all():
for crosswalk in Crosswalk.objects.iterator(chunk_size=1000):
if getattr(crosswalk, '_fhir_id', None):
crosswalk.fhir_id_v2 = getattr(crosswalk, '_fhir_id') # type: ignore[attr-defined]
crosswalk.save()

ArchivedCrosswalk = apps_mod.get_model('bluebutton', 'ArchivedCrosswalk')
for archived in ArchivedCrosswalk.objects.all():
for archived in ArchivedCrosswalk.objects.iterator(chunk_size=1000):
if getattr(archived, '_fhir_id', None):
archived.fhir_id_v2 = getattr(archived, '_fhir_id') # type: ignore[attr-defined]
archived.save()
Expand All @@ -35,13 +35,13 @@ def reverse_migrate_v2_to_fhir_id(apps=None, schema_editor=None) -> None:

apps_mod = apps if apps is not None else global_apps
Crosswalk = apps_mod.get_model('bluebutton', 'Crosswalk')
for crosswalk in Crosswalk.objects.all():
for crosswalk in Crosswalk.objects.iterator(chunk_size=1000):
if getattr(crosswalk, 'fhir_id_v2', None):
crosswalk._fhir_id = getattr(crosswalk, 'fhir_id_v2') # type: ignore[attr-defined]
crosswalk.save()

ArchivedCrosswalk = apps_mod.get_model('bluebutton', 'ArchivedCrosswalk')
for archived in ArchivedCrosswalk.objects.all():
for archived in ArchivedCrosswalk.objects.iterator(chunk_size=1000):
if getattr(archived, 'fhir_id_v2', None):
archived._fhir_id = getattr(archived, 'fhir_id_v2') # type: ignore[attr-defined]
archived.save()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# Generated by Django 4.2.24 on 2025-09-23 13:25

from django.db import migrations, models
from apps.fhir.bluebutton.management.commands.migrate_fhir_id_to_v2 import (
migrate_fhir_id_to_v2,
reverse_migrate_v2_to_fhir_id,
)

# TODO - Migration is Unapplied
# This migration needs to be run after deployment of bluebutton 0008 and when all
Expand All @@ -18,8 +14,7 @@ class Migration(migrations.Migration):
]

operations = [
migrations.RunPython(migrate_fhir_id_to_v2, reverse_migrate_v2_to_fhir_id),
migrations.AddConstraint( # After migration happens, add constraint that a fhir_id must be present
migrations.AddConstraint( # After migrate_fhir_id_to_v2 is run, add constraint that a fhir_id_v2 or v3 must be present
model_name='crosswalk',
constraint=models.CheckConstraint(
check=models.Q(fhir_id_v2__isnull=False) | models.Q(fhir_id_v3__isnull=False),
Expand Down
Loading
Loading