Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data Migration #2763

Draft
wants to merge 27 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
4094827
Migration for Orgs, Facility, Patients and Consultations
sainak Jan 21, 2025
22f5b95
fix migrations
sainak Jan 21, 2025
4ea4792
Patient Notes and Notes Edit | Migration
amjithtitus09 Jan 21, 2025
51ddbf4
added symptoms migration
khavinshankar Jan 21, 2025
57b91bf
Shifting to Resource Migration (#2765)
Jacobjeevan Jan 21, 2025
e5ecbfc
Delete care/facility/migrations/0485_migrate_shifting_request.py
sainak Jan 21, 2025
33309da
fix reverse migrations
sainak Jan 21, 2025
eb858f3
FacilityUser | Migration
amjithtitus09 Jan 21, 2025
887f0ae
disable auto time on emr while migrating and refactored
sainak Jan 22, 2025
639a97e
patient medical history notes and fixes
sainak Jan 22, 2025
de91baf
fixes
sainak Jan 22, 2025
ac061f6
Merge branch 'develop' into data-migration
sainak Jan 22, 2025
4739b19
rebase migrations
sainak Jan 22, 2025
12679a6
Fixes
sainak Jan 23, 2025
6a49085
Merge commit '2f9fa831ff52d72e81d8fa5bba361ba17287a6ef' into data-mig…
sainak Jan 23, 2025
ebdb12e
rebase migrations
sainak Jan 23, 2025
136b569
add prescription migration
rithviknishad Jan 23, 2025
36229c4
fix migrations
sainak Jan 23, 2025
c1a6254
prescriptions: fill migrated_emr_prescription_id
rithviknishad Jan 23, 2025
349bc50
update dependency
sainak Jan 23, 2025
983a3e1
cleanup prescription migration
rithviknishad Jan 24, 2025
d4faab7
Merge remote-tracking branch 'origin/develop' into data-migration
sainak Jan 24, 2025
9604ca2
rebase migrations
sainak Jan 24, 2025
31d2037
rebase migrations
sainak Jan 24, 2025
3ac2fb5
Merge remote-tracking branch 'origin/develop' into data-migration
sainak Jan 24, 2025
da49a78
Add diseases to notes
sainak Jan 25, 2025
6811214
cleanup
sainak Jan 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generated by Django 5.1.4 on 2025-01-14 09:20

import django.db.models.deletion
from django.db import migrations, models

class Migration(migrations.Migration):

dependencies = [
('contenttypes', '0002_remove_content_type_name'),
('facility', '0476_facility_default_internal_organization_and_more'),
('users', '0021_rename_gender_user_old_gender_user_geo_organization_and_more'),
]

operations = [
migrations.DeleteModel(
name='HistoricalPatientRegistration',
),
migrations.AddField(
model_name='patientconsultation',
name='migrated_emr_encounter_id',
field=models.BigIntegerField(blank=True, null=True),
),
migrations.AddField(
model_name='patientregistration',
name='migrated_emr_patient_id',
field=models.BigIntegerField(blank=True, null=True),
),
migrations.CreateModel(
name='MigrationTracking',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('old_model_obj_id', models.BigIntegerField()),
('new_model_obj_id', models.BigIntegerField()),
('field', models.CharField(max_length=255)),
('data', models.TimeField()),
('new_model', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='new_model', to='contenttypes.contenttype')),
('old_model', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='old_model', to='contenttypes.contenttype')),
],
),
]
107 changes: 107 additions & 0 deletions care/facility/migrations/0478_migrate_organizations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Generated by Django 5.1.4 on 2025-01-21 08:28

import logging
from django.db import migrations

logger = logging.getLogger(__name__)

MIGRARION_ID = 413370
COUNTRY = "India"

def migrate_organizations(apps, schema_editor):
Organization = apps.get_model("emr", "Organization")
State = apps.get_model("users", "State")
District = apps.get_model("users", "District")
LocalBody = apps.get_model("users", "LocalBody")
Ward = apps.get_model("users", "Ward")

local_body_type_map = {
1: "grama_panchayath",
2: "block_panchayath",
3: "district_panchayath",
4: "nagar_panchayath",
10: "municipality",
20: "corporation",
50: "others",
}

logger.debug("Migrating Organization")
for state_obj in State.objects.all():
state_org = Organization.objects.create(
name=state_obj.name,
org_type="govt",
system_generated=True,
metadata={
"country": COUNTRY,
"govt_org_type": "state",
"govt_org_children_type": "district",
},
meta= {"migration_id": MIGRARION_ID},
)
logger.debug(f"Created State: {state_org.name=}")
for district_obj in District.objects.filter(state=state_obj):
district_org = Organization.objects.create(
name=district_obj.name,
root_org=state_org,
parent=state_org,
org_type="govt",
system_generated=True,
metadata={
"country": COUNTRY,
"govt_org_type": "district",
"govt_org_children_type": "local_body",
},
meta= {"migration_id": MIGRARION_ID},
)
logger.debug(f"Created District: {state_org.name=}, {district_org.name=}")
for local_body_obj in LocalBody.objects.filter(district=district_obj):
lb_type = local_body_type_map.get(local_body_obj.body_type, "others")
local_body_org = Organization.objects.create(
name=local_body_obj.name,
root_org=state_org,
parent=district_org,
org_type="govt",
system_generated=True,
metadata={
"country": COUNTRY,
"govt_org_type": "local_body",
"govt_org_local_body_type": lb_type,
"govt_org_children_type": "ward",
},
meta= {"migration_id": MIGRARION_ID},
)
logger.debug(f"Created Local Body: {state_org.name=}, \
{district_org.name=}, {local_body_org.name=}")
for ward_obj in Ward.objects.filter(local_body=local_body_obj):
ward_org = Organization.objects.create(
name=ward_obj.name,
root_org=state_org,
parent=local_body_org,
org_type="govt",
system_generated=True,
metadata={
"country": COUNTRY,
"govt_org_type": "ward",
"govt_org_ward_number": ward_obj.number,
},
meta= {"migration_id": MIGRARION_ID},
)
logger.debug(f"Created Ward: {state_org.name=}, \
{district_org.name=}, {local_body_org.name=}, \
{ward_org.name=}")


def reverse_migrate_organizations(apps, schema_editor):
logger.debug("Reversing Migration Organization")
schema_editor.execute("DELETE FROM emr_organization WHERE meta->>'migration_id' = %s", [MIGRARION_ID])


class Migration(migrations.Migration):

dependencies = [
('facility', '0477_historicalpatientregistration_migrated_emr_patient_id_and_more'),
]

operations = [
migrations.RunPython(migrate_organizations, reverse_migrate_organizations),
]
88 changes: 88 additions & 0 deletions care/facility/migrations/0479_migrate_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Generated by Django 5.1.4 on 2025-01-21 08:41
import logging
from django.db import migrations

logger = logging.getLogger(__name__)

MIGRARION_ID = 413370


def _get_org(Organization, obj):
state = None
district = None
local_body = None
ward = None
if obj.state:
state = Organization.objects.filter(
name=obj.state.name,
root_org__isnull=True,
level_cache=0
).first()
if obj.district:
query = {
"name": obj.district.name,
"level_cache": 1,
}
if state:
query["root_org"] = state
district = Organization.objects.filter(**query).first()
if obj.local_body:
query = {
"name": obj.local_body.name,
"level_cache": 2,
}
if state:
query["root_org"] = state
if district:
query["parent"] = district
local_body = Organization.objects.filter(**query).first()
if obj.ward:
query = {
"name": obj.ward.name,
"level_cache": 3,
}
if state:
query["root_org"] = state
if local_body:
query["parent"] = local_body
ward = Organization.objects.filter(**query).first()
return ward or local_body or district or state or None



def migrate_users(apps, schema_editor):
User = apps.get_model("users", "User")
Organization = apps.get_model("emr", "Organization")
logger.debug("Migrating Users")
bulk_update = []
for user in User.objects.filter(geo_organization__isnull=True).select_related("state", "district", "local_body", "ward"):
if user.geo_organization:
continue
geo_org = _get_org(Organization, user)
if not geo_org:
continue
user.geo_organization = geo_org
logger.debug(f"User: {user.id=}, {geo_org.name=}")
bulk_update.append(user)
User.objects.bulk_update(bulk_update, ["geo_organization"])


def reverse_migrate_users(apps, schema_editor):
User = apps.get_model("users", "User")
User.objects.filter(
geo_organization__meta__migration_id=MIGRARION_ID
).update(
geo_organization=None
)


class Migration(migrations.Migration):

dependencies = [
('facility', '0478_migrate_organizations'),
('users', '0021_rename_gender_user_old_gender_user_geo_organization_and_more'),
]

operations = [
migrations.RunPython(migrate_users, reverse_migrate_users),
]
85 changes: 85 additions & 0 deletions care/facility/migrations/0480_migrate_facilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Generated by Django 5.1.4 on 2025-01-21 08:42
import logging
from django.db import migrations

logger = logging.getLogger(__name__)

MIGRARION_ID = 413370


def _get_org(Organization, obj):
state = None
district = None
local_body = None
ward = None
if obj.state:
state = Organization.objects.filter(
name=obj.state.name,
root_org__isnull=True,
level_cache=0
).first()
if obj.district:
query = {
"name": obj.district.name,
"level_cache": 1,
}
if state:
query["root_org"] = state
district = Organization.objects.filter(**query).first()
if obj.local_body:
query = {
"name": obj.local_body.name,
"level_cache": 2,
}
if state:
query["root_org"] = state
if district:
query["parent"] = district
local_body = Organization.objects.filter(**query).first()
if obj.ward:
query = {
"name": obj.ward.name,
"level_cache": 3,
}
if state:
query["root_org"] = state
if local_body:
query["parent"] = local_body
ward = Organization.objects.filter(**query).first()
return ward or local_body or district or state or None


def migrate_facilities(apps, schema_editor):
Facility = apps.get_model("facility", "Facility")
Organization = apps.get_model("emr", "Organization")
bulk_update = []
for facility in Facility.objects.filter(geo_organization__isnull=True).select_related("state", "district", "local_body", "ward"):
if facility.geo_organization:
continue
geo_org = _get_org(Organization, facility)
if not geo_org:
continue
facility.geo_organization = geo_org
#TODO: create facility organization
bulk_update.append(facility)
Facility.objects.bulk_update(bulk_update, ["geo_organization"])


def reverse_migrate_facilities(apps, schema_editor):
Facility = apps.get_model("facility", "Facility")
Facility.objects.filter(
geo_organization__meta__migration_id=MIGRARION_ID
).update(
geo_organization=None
)


class Migration(migrations.Migration):

dependencies = [
('facility', '0479_migrate_users'),
]

operations = [
migrations.RunPython(migrate_facilities, reverse_migrate_facilities),
]
Loading
Loading