-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OpenConceptLab/ocl_issues#1659 | Concept/Mapping migration to update …
…updated by
- Loading branch information
1 parent
4a0955c
commit aa057b1
Showing
2 changed files
with
66 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Generated by Django 4.2.4 on 2023-09-12 02:46 | ||
|
||
from django.db import migrations | ||
from django.db.models import F, OuterRef | ||
from django.db.models.functions import Coalesce | ||
|
||
|
||
def update_updated_by(apps, schema_editor): | ||
Concept = apps.get_model('concepts', 'Concept') | ||
# Find the latest versions for each versioned_object_id | ||
latest_versions = Concept.objects.filter( | ||
versioned_object_id=OuterRef('versioned_object_id') | ||
).filter(is_latest_version=True).order_by('-created_at') | ||
|
||
# Use Subquery to get the latest_version for each concept | ||
subquery = latest_versions.values('updated_by')[:1] | ||
|
||
# Update only the concepts where a latest version exists | ||
Concept.objects.filter( | ||
id=F('versioned_object_id'), | ||
versioned_object_id__in=latest_versions.values('versioned_object_id') | ||
).update(updated_by=Coalesce(subquery, F('updated_by'))) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('concepts', '0069_concept_concepts_parent_active'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(update_updated_by) | ||
] |
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,33 @@ | ||
# Generated by Django 4.2.4 on 2023-09-12 03:27 | ||
|
||
from django.db import migrations | ||
from django.db.models import OuterRef, F | ||
from django.db.models.functions import Coalesce | ||
|
||
|
||
def update_updated_by(apps, schema_editor): | ||
Mapping = apps.get_model('mappings', 'Mapping') | ||
# Find the latest versions for each versioned_object_id | ||
latest_versions = Mapping.objects.filter( | ||
versioned_object_id=OuterRef('versioned_object_id') | ||
).filter(is_latest_version=True).order_by('-created_at') | ||
|
||
# Use Subquery to get the latest_version for each concept | ||
subquery = latest_versions.values('updated_by')[:1] | ||
|
||
# Update only the concepts where a latest version exists | ||
Mapping.objects.filter( | ||
id=F('versioned_object_id'), | ||
versioned_object_id__in=latest_versions.values('versioned_object_id') | ||
).update(updated_by=Coalesce(subquery, F('updated_by'))) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('mappings', '0047_mapping_checksums'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(update_updated_by) | ||
] |