-
Notifications
You must be signed in to change notification settings - Fork 28
/
0040_auto_20221122_0613.py
54 lines (45 loc) · 1.94 KB
/
0040_auto_20221122_0613.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
50
51
52
53
54
# Generated by Django 4.1.1 on 2022-11-22 06:13
from django.db import migrations, connection, transaction
def populate_concept_id_in_localized_texts(apps, schema_editor):
import time
start = time.time()
"""
1. Populates concept_id in localized_texts table using concepts_names (many-to-many).
2. After this migration:
- localized_texts table will have concept_id for each name
- localized_texts without any concept_id can be deleted, as they are all descriptions which were migrated earlier
- count of concepts_names (old) should equal to count of localized_texts with concept_id
"""
def get_statement():
return "update localized_texts as l set concept_id = n.concept_id from (values"
with connection.cursor() as cursor:
cursor.execute("select concept_id, localizedtext_id from concepts_names;")
names = cursor.fetchall()
statement = get_statement()
count = 0
for name in names:
statement += f'{name},'
count += 1
if count == 10000:
if statement.endswith(','):
statement = statement[:-1]
print("Updating 10000...")
statement += ') as n(concept_id, localizedtext_id) where n.localizedtext_id = l.id;'
cursor.execute(statement)
statement = get_statement()
count = 0
if count > 0:
if statement.endswith(','):
statement = statement[:-1]
statement += ') as n(concept_id, localizedtext_id) where n.localizedtext_id = l.id;'
print(f"Updating last {count}...")
cursor.execute(statement)
print('Time: ', time.time() - start)
class Migration(migrations.Migration):
atomic = False
dependencies = [
('concepts', '0039_auto_20221122_0552'),
]
operations = [
migrations.RunPython(populate_concept_id_in_localized_texts)
]