Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added contraints in consultations and custom migrations (#1917)
* added contraints in consultations and custom migrations * added noop reverse * updated test_utils to handle the constraint * added a test for consultations * removed print statements * added one more test * updated tests for consultations * updated dummy data --------- Co-authored-by: Aakash Singh <mail@singhaakash.dev>
- Loading branch information
1 parent
5b317ca
commit 284c639
Showing
7 changed files
with
176 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Generated by Django 4.2.10 on 2024-02-26 20:00 | ||
|
||
from django.db import migrations | ||
from django.db.models import Count, F, Value, Window | ||
from django.db.models.functions import RowNumber | ||
|
||
|
||
def fix_duplicate_patient_numbers(apps, schema_editor): | ||
PatientConsultation = apps.get_model("facility", "PatientConsultation") | ||
|
||
window = Window( | ||
expression=RowNumber(), | ||
partition_by=[F("patient_no"), F("facility")], | ||
order_by=F("id").asc(), | ||
) | ||
|
||
consultations = PatientConsultation.objects.annotate(row_number=window).filter( | ||
row_number__gt=1 | ||
) | ||
|
||
for consultation in consultations: | ||
consultation.patient_no = ( | ||
f"{consultation.patient_no}_{consultation.row_number - 1}" | ||
) | ||
|
||
PatientConsultation.objects.bulk_update( | ||
consultations, ["patient_no"], batch_size=2000 | ||
) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("facility", "0414_remove_bed_old_name"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(fix_duplicate_patient_numbers, migrations.RunPython.noop), | ||
] |
19 changes: 19 additions & 0 deletions
19
care/facility/migrations/0416_patientconsultation_unique_patient_no_within_facility.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Generated by Django 4.2.10 on 2024-02-26 20:08 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("facility", "0415_auto_20240227_0130"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddConstraint( | ||
model_name="patientconsultation", | ||
constraint=models.UniqueConstraint( | ||
fields=("patient_no", "facility"), | ||
name="unique_patient_no_within_facility", | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters