-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new table: `UserEducation` (M:O) Custom migrations for `organization` -> `UserEducation`: ``` python manage.py migrate_education python manage.py reverse_migrate_education ``` The migrations themselves are "safe" because they affect only the new entity, without erasing or editing existing information. `organization` - the built field for education functionality is fully preserved, the front must be ready for the new format. Accordingly, to carry out the migration, the front must be ready for the new format of filling in information on education. After all migrations and when the front is ready, it (the `organization` field) will simply need to be deleted.
- Loading branch information
Showing
8 changed files
with
325 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from django.db import transaction | ||
from django.core.management.base import BaseCommand | ||
from django.contrib.auth import get_user_model | ||
|
||
from users.models import UserEducation | ||
|
||
|
||
CustomUser = get_user_model() | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Use: python manage.py migrate_education. | ||
""" | ||
def handle(self, *args, **kwargs): | ||
self.stdout.write("Start manual migration ...") | ||
try: | ||
total_user_migrate = migrate_organization_to_education() | ||
self.stdout.write( | ||
self.style.SUCCESS( | ||
f"Manual migration complete, users migrated: {total_user_migrate}" | ||
) | ||
) | ||
except Exception as e: | ||
self.stderr.write( | ||
self.style.ERROR(f"Migration failed: {str(e)}") | ||
) | ||
|
||
|
||
@transaction.atomic | ||
def migrate_organization_to_education() -> int: | ||
""" | ||
Migrate old field `organization` to new model `Education`. | ||
Returns count migrated users. | ||
""" | ||
users_with_irganization = CustomUser.objects.exclude(organization=None).exclude(organization="") | ||
UserEducation.objects.bulk_create([ | ||
UserEducation( | ||
user=user, | ||
organization_name=user.organization, | ||
) | ||
for user in users_with_irganization | ||
]) | ||
return users_with_irganization.count() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from django.db import transaction | ||
from django.contrib.auth import get_user_model | ||
from django.core.management.base import BaseCommand, CommandParser | ||
|
||
from users.models import UserEducation | ||
|
||
|
||
CustomUser = get_user_model() | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Use: python manage.py reverse_migrate_education. | ||
""" | ||
|
||
def add_arguments(self, parser: CommandParser) -> None: | ||
parser.add_argument( | ||
"--confirm", | ||
action="store_true", | ||
help="Confirm delete Users educations from UserEducation model" | ||
) | ||
|
||
def handle(self, *args, **kwargs): | ||
confirm = kwargs["confirm"] | ||
self.stdout.write(self.style.WARNING( | ||
"You are about to DELETE ALL INSTANCES in the UserEducation model.")) | ||
|
||
if not confirm: | ||
answer = input("Type 'yes' to continue, or 'no' to cancel: ").lower() | ||
if answer != "yes": | ||
self.stdout.write(self.style.ERROR("Manual migrations canceled.")) | ||
return | ||
|
||
self.stdout.write("Starting manual migrations...") | ||
|
||
try: | ||
deleted_instances = delete_all_instances_usereducation() | ||
self.stdout.write(self.style.SUCCESS("Manual migrations completed successfully.")) | ||
self.stdout.write(self.style.SUCCESS(f"Deleted: {deleted_instances}")) | ||
except Exception as e: | ||
self.stderr.write(self.style.ERROR(f"Manual migrations failed: {str(e)}")) | ||
|
||
|
||
@transaction.atomic | ||
def delete_all_instances_usereducation() -> int: | ||
""" | ||
Destroy all UserEducation instances. | ||
""" | ||
count = UserEducation.objects.count() | ||
UserEducation.objects.all().delete() | ||
return count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
users/migrations/0049_alter_customuser_key_skills_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# Generated by Django 4.2.11 on 2024-09-02 16:01 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import users.validators | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("users", "0048_customuser_dataset_migration_applied"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="customuser", | ||
name="key_skills", | ||
field=models.CharField( | ||
blank=True, | ||
help_text="Устаревшее поле -> skills", | ||
max_length=512, | ||
null=True, | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="customuser", | ||
name="organization", | ||
field=models.CharField( | ||
blank=True, | ||
help_text="Устаревшее поле -> UserEducation", | ||
max_length=255, | ||
null=True, | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="customuser", | ||
name="speciality", | ||
field=models.CharField( | ||
blank=True, | ||
help_text="Устаревшее поле -> v2_speciality", | ||
max_length=255, | ||
null=True, | ||
), | ||
), | ||
migrations.CreateModel( | ||
name="UserEducation", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
( | ||
"organization_name", | ||
models.CharField( | ||
max_length=255, verbose_name="Наименование организации" | ||
), | ||
), | ||
( | ||
"description", | ||
models.CharField( | ||
blank=True, | ||
max_length=255, | ||
null=True, | ||
verbose_name="Краткое описание", | ||
), | ||
), | ||
( | ||
"entry_year", | ||
models.PositiveSmallIntegerField( | ||
blank=True, | ||
null=True, | ||
validators=[users.validators.user_entry_year_education_validator], | ||
verbose_name="Год поступления", | ||
), | ||
), | ||
( | ||
"user", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="education", | ||
to=settings.AUTH_USER_MODEL, | ||
verbose_name="Пользователь", | ||
), | ||
), | ||
], | ||
options={ | ||
"verbose_name": "Образование", | ||
"verbose_name_plural": "Образование", | ||
}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.