Skip to content

Commit

Permalink
PRO-484: User old field migration
Browse files Browse the repository at this point in the history
Миграция чуть подкорректирована, общая логика:
Переезд поля organization -> в модель Education
Переезд только в том случае, если organization заполнено и не было записей в Education
На всякий случай, для непредвиденных вещей формируется дамп в json, с теми пользователями, которые переехали (id + строе поле).
  • Loading branch information
pavuchara committed Oct 24, 2024
1 parent b2d85fe commit 3fe6f61
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 147 deletions.
26 changes: 23 additions & 3 deletions core/management/commands/migrate_education.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import json

from django.db import transaction
from django.conf import settings
from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model

Expand Down Expand Up @@ -32,13 +35,30 @@ def migrate_organization_to_education() -> int:
"""
Migrate old field `organization` to new model `Education`.
Returns count migrated users.
Stored migrated info into `BASE_DIR / "core" / "log" / "migrated_users.json"`
"""
users_with_irganization = CustomUser.objects.exclude(organization=None).exclude(organization="")
user_with_education_ids: list[int] = UserEducation.objects.values_list("user__id", flat=True)
users_with_organization_without_education = (
CustomUser.objects
.exclude(organization=None)
.exclude(organization="")
.exclude(id__in=user_with_education_ids)
)
UserEducation.objects.bulk_create([
UserEducation(
user=user,
organization_name=user.organization,
)
for user in users_with_irganization
for user in users_with_organization_without_education
])
return users_with_irganization.count()

data = [
{"user_id": user.id, "user_organization_field": user.organization}
for user in users_with_organization_without_education
]

file_dump = settings.BASE_DIR / "core" / "log" / "migrated_users.json"
with open(file_dump, "w", encoding="utf-8") as file:
json.dump(data, file, indent=4, ensure_ascii=False)

return users_with_organization_without_education.count()
91 changes: 0 additions & 91 deletions core/management/commands/migrate_old_to_new_fields_trigram.py

This file was deleted.

53 changes: 0 additions & 53 deletions core/management/commands/reverse_old_to_new_fields.py

This file was deleted.

0 comments on commit 3fe6f61

Please sign in to comment.