-
Notifications
You must be signed in to change notification settings - Fork 27
/
0038_auto_20221122_0522.py
59 lines (46 loc) · 1.99 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Generated by Django 4.1.1 on 2022-11-22 05:22
from django.db import migrations, connection, transaction
def populate_concept_descriptions(apps, schema_editor):
import time
start = time.time()
"""
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.
"""
def get_statement():
return "insert into concept_descriptions(concept_id, external_id, name, type, locale, locale_preferred, created_at) values "
with connection.cursor() as cursor:
cursor.execute("""select concept_id, localizedtext_id from concepts_descriptions;""")
rows = cursor.fetchall()
statement = get_statement()
count = 0
for row in rows:
cursor.execute(
"select external_id, name, type, locale, locale_preferred, created_at from localized_texts where id = %s",
[row[1]]
)
locale = cursor.fetchone()
name = locale[1].replace("'", "''")
statement += f"({row[0]}, '{locale[0]}', '{name}', '{locale[2]}', '{locale[3]}', {locale[4]}, '{locale[5]}'),"
count += 1
if count == 10000:
if statement.endswith(','):
statement = statement[:-1] + ';'
print(f"Inserting {count}...")
cursor.execute(statement)
statement = get_statement()
count = 0
if count > 0:
if statement.endswith(','):
statement = statement[:-1] + ';'
print(f"Inserting Last {count}...")
cursor.execute(statement)
print('Time: ', time.time() - start)
class Migration(migrations.Migration):
atomic = False
dependencies = [
('concepts', '0037_localizedtext_concept'),
]
operations = [
migrations.RunPython(populate_concept_descriptions)
]