Skip to content

Commit 1f0d647

Browse files
authored
Merge pull request #476 from PROCOLLAB-github/feature/user_field_migrations
PRO-484: User old field migration
2 parents b2d85fe + 377dbaf commit 1f0d647

4 files changed

Lines changed: 37 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 / "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 / "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.

log/migrated_users.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"user_id": 3,
4+
"user_organization_field": "Учеба 1"
5+
},
6+
{
7+
"user_id": 4,
8+
"user_organization_field": "Учеба 2"
9+
},
10+
{
11+
"user_id": 5,
12+
"user_organization_field": "Учеба 3"
13+
}
14+
]

0 commit comments

Comments
 (0)