Skip to content

Commit

Permalink
OpenConceptLab/ocl_issues#1659 | Concept/Mapping migration to update …
Browse files Browse the repository at this point in the history
…updated by
  • Loading branch information
snyaggarwal committed Sep 12, 2023
1 parent 4a0955c commit aa057b1
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
33 changes: 33 additions & 0 deletions core/concepts/migrations/0070_auto_20230912_0246.py
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)
]
33 changes: 33 additions & 0 deletions core/mappings/migrations/0048_auto_20230912_0327.py
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)
]

0 comments on commit aa057b1

Please sign in to comment.