-
Notifications
You must be signed in to change notification settings - Fork 28
/
0038_auto_20221122_0522.py
38 lines (31 loc) · 1.5 KB
/
0038_auto_20221122_0522.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
# Generated by Django 4.1.1 on 2022-11-22 05:22
from django.db import migrations
def populate_concept_descriptions(apps, schema_editor):
"""
1. Populates new concept_descriptions table from localized_texts table using many-to-many concepts_descriptions.
2. Count of concepts_descriptions (old) and concept_descriptions (new) should be same after this migration.
"""
Concept = apps.get_model('concepts', 'Concept')
LocalizedText = apps.get_model('concepts', 'LocalizedText')
ConceptDescription = apps.get_model('concepts', 'ConceptDescription')
descriptions = []
for concept_description in Concept.descriptions.through.objects.iterator(chunk_size=1000):
localized_text = LocalizedText.objects.filter(id=concept_description.localizedtext_id).first()
if localized_text:
descriptions.append(ConceptDescription(
concept_id=concept_description.concept_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,
))
ConceptDescription.objects.bulk_create(descriptions, batch_size=1000)
class Migration(migrations.Migration):
dependencies = [
('concepts', '0037_localizedtext_concept'),
]
operations = [
migrations.RunPython(populate_concept_descriptions)
]