-
Notifications
You must be signed in to change notification settings - Fork 28
/
0039_auto_20221122_0552.py
49 lines (41 loc) · 2.18 KB
/
0039_auto_20221122_0552.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Generated by Django 4.1.1 on 2022-11-22 05:52
from django.db import migrations
def add_names_for_latest_version(apps, schema_editor):
"""
1. Populates LocalizedText with copy of names of Concept for its latest version.
2. Concept and its latest version used to share names (and descriptions) using many-to-many (concepts_names).
3. After this migration:
- localized_texts will have new entries with concept_id as concept.latest_version.id
- concepts_names will not have anything for any latest versions.
- concepts_names will not have any duplicate localizedtext_id.
- concepts_names for latest versions needs to be deleted for the next migration to populate concept_id in
localized_texts. Latest versions and Concept itself uses the same localizedtext_id.
"""
Concept = apps.get_model('concepts', 'Concept')
LocalizedText = apps.get_model('concepts', 'LocalizedText')
names = []
concepts_names_to_delete = []
for latest_version in Concept.objects.filter(is_latest_version=True).iterator(chunk_size=1000):
for concept_name in Concept.names.through.objects.filter(concept_id=latest_version.id):
concepts_names_to_delete.append(concept_name.id)
localized_text = LocalizedText.objects.filter(id=concept_name.localizedtext_id).first()
if localized_text:
concepts_names_to_delete.append(concept_name.id)
names.append(LocalizedText(
concept_id=latest_version.id,
external_id=localized_text.external_id,
name=localized_text.name,
type=localized_text.type,
locale=localized_text.locale,
locale_preferred=localized_text.locale_preferred,
created_at=localized_text.created_at,
))
LocalizedText.objects.bulk_create(names, batch_size=1000)
Concept.names.through.objects.filter(id__in=concepts_names_to_delete).delete()
class Migration(migrations.Migration):
dependencies = [
('concepts', '0038_auto_20221122_0522'),
]
operations = [
migrations.RunPython(add_names_for_latest_version)
]