-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make bed names unique for location (#1783)
* make bed names unique for location * wrap in atomic block * add tests * fix migration * improvements * make validations lowercase * add tests * improve migration queries * merge migrations
- Loading branch information
Showing
7 changed files
with
142 additions
and
4 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
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,50 @@ | ||
# Generated by Django 4.2.8 on 2023-12-26 06:17 | ||
|
||
from django.db import migrations, models | ||
from django.db.models import F, Window | ||
from django.db.models.functions import RowNumber | ||
|
||
|
||
def copy_bed_name(apps, schema_editor): | ||
Bed = apps.get_model("facility", "Bed") | ||
Bed.objects.update(old_name=models.F("name")) | ||
|
||
|
||
def reverse_copy_bed_name(apps, schema_editor): | ||
Bed = apps.get_model("facility", "Bed") | ||
Bed.objects.filter(old_name__isnull=False).update(name=models.F("old_name")) | ||
|
||
|
||
def fix_duplicate_bed_names(apps, schema_editor): | ||
Bed = apps.get_model("facility", "Bed") | ||
|
||
window = Window( | ||
expression=RowNumber(), | ||
partition_by=[F("location"), F("lname")], | ||
order_by=F("id").asc(), | ||
) | ||
|
||
beds = Bed.objects.annotate( | ||
lname=models.functions.Lower("name"), row_number=window | ||
).filter(row_number__gt=1) | ||
|
||
for bed in beds: | ||
bed.name = f"{bed.name} ({bed.row_number - 1})" | ||
|
||
Bed.objects.bulk_update(beds, ["name"], batch_size=2000) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("facility", "0404_merge_20231220_2227"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="bed", | ||
name="old_name", | ||
field=models.CharField(blank=True, max_length=1024, null=True), | ||
), | ||
migrations.RunPython(copy_bed_name, reverse_copy_bed_name), | ||
migrations.RunPython(fix_duplicate_bed_names, migrations.RunPython.noop), | ||
] |
21 changes: 21 additions & 0 deletions
21
care/facility/migrations/0406_bed_unique_bed_name_per_location.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,21 @@ | ||
# Generated by Django 4.2.8 on 2023-12-27 05:10 | ||
|
||
import django.db.models.functions.text | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("facility", "0405_bed_name_constraint"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddConstraint( | ||
model_name="bed", | ||
constraint=models.UniqueConstraint( | ||
django.db.models.functions.text.Lower("name"), | ||
models.F("location"), | ||
name="unique_bed_name_per_location", | ||
), | ||
), | ||
] |
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,14 @@ | ||
# Generated by Django 4.2.8 on 2024-02-10 09:40 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("facility", "0406_bed_unique_bed_name_per_location"), | ||
("facility", "0408_alter_dailyround_resp"), | ||
("facility", "0408_delete_metaicd11diagnosis"), | ||
("facility", "0408_historicalpatientregistration_date_of_delivery_and_more"), | ||
] | ||
|
||
operations = [] |
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