Skip to content

Commit 3fe6f61

Browse files
committed
PRO-484: User old field migration
Миграция чуть подкорректирована, общая логика: Переезд поля organization -> в модель Education Переезд только в том случае, если organization заполнено и не было записей в Education На всякий случай, для непредвиденных вещей формируется дамп в json, с теми пользователями, которые переехали (id + строе поле).
1 parent b2d85fe commit 3fe6f61

3 files changed

Lines changed: 23 additions & 147 deletions

File tree

core/management/commands/migrate_education.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import json
2+
13
from django.db import transaction
4+
from django.conf import settings
25
from django.core.management.base import BaseCommand
36
from django.contrib.auth import get_user_model
47

@@ -32,13 +35,30 @@ def migrate_organization_to_education() -> int:
3235
"""
3336
Migrate old field `organization` to new model `Education`.
3437
Returns count migrated users.
38+
Stored migrated info into `BASE_DIR / "core" / "log" / "migrated_users.json"`
3539
"""
36-
users_with_irganization = CustomUser.objects.exclude(organization=None).exclude(organization="")
40+
user_with_education_ids: list[int] = UserEducation.objects.values_list("user__id", flat=True)
41+
users_with_organization_without_education = (
42+
CustomUser.objects
43+
.exclude(organization=None)
44+
.exclude(organization="")
45+
.exclude(id__in=user_with_education_ids)
46+
)
3747
UserEducation.objects.bulk_create([
3848
UserEducation(
3949
user=user,
4050
organization_name=user.organization,
4151
)
42-
for user in users_with_irganization
52+
for user in users_with_organization_without_education
4353
])
44-
return users_with_irganization.count()
54+
55+
data = [
56+
{"user_id": user.id, "user_organization_field": user.organization}
57+
for user in users_with_organization_without_education
58+
]
59+
60+
file_dump = settings.BASE_DIR / "core" / "log" / "migrated_users.json"
61+
with open(file_dump, "w", encoding="utf-8") as file:
62+
json.dump(data, file, indent=4, ensure_ascii=False)
63+
64+
return users_with_organization_without_education.count()

core/management/commands/migrate_old_to_new_fields_trigram.py

Lines changed: 0 additions & 91 deletions
This file was deleted.

core/management/commands/reverse_old_to_new_fields.py

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)