-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat #3168 Migrations for existing data
- Loading branch information
Showing
5 changed files
with
132 additions
and
18 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
95 changes: 95 additions & 0 deletions
95
src/core/migrations/0101_migrate_affiliation_institution.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,95 @@ | ||
# Generated by Django 4.2.16 on 2025-01-17 16:21 | ||
|
||
from django.db import migrations | ||
from django.db.models import Q | ||
|
||
|
||
def naive_create_organization(apps, old_institution=None): | ||
if not old_institution: | ||
return None | ||
|
||
Organization = apps.get_model("core", "Organization") | ||
OrganizationName = apps.get_model("core", "OrganizationName") | ||
|
||
organization = Organization.objects.create() | ||
OrganizationName.objects.create( | ||
value=old_institution, | ||
custom_label_for=organization, | ||
) | ||
return organization | ||
|
||
|
||
def naive_create_affiliation( | ||
apps, | ||
old_institution, | ||
old_department, | ||
account=None, | ||
frozen_author=None, | ||
preprint_author=None, | ||
): | ||
Affiliation = apps.get_model("core", "Affiliation") | ||
organization = naive_create_organization(apps, old_institution) | ||
|
||
# Create or update the actual affiliation if the associated | ||
# account / frozen author / preprint author has been saved already | ||
affiliation = Affiliation.objects.create( | ||
account=account, | ||
frozen_author=frozen_author, | ||
preprint_author=preprint_author, | ||
organization=organization, | ||
department=old_department, | ||
is_primary=True, | ||
) | ||
return affiliation | ||
|
||
|
||
def migrate_affiliation_institution(apps, schema_editor): | ||
Account = apps.get_model("core", "Account") | ||
FrozenAuthor = apps.get_model("submission", "FrozenAuthor") | ||
PreprintAuthor = apps.get_model("repository", "PreprintAuthor") | ||
|
||
for account in Account.objects.filter( | ||
~Q(institution__exact='') | ||
| ~Q(department__exact='') | ||
): | ||
naive_create_affiliation( | ||
apps, | ||
account.institution, | ||
account.department, | ||
account=account, | ||
) | ||
|
||
for frozen_author in FrozenAuthor.objects.filter( | ||
~Q(institution__exact='') | ||
| ~Q(department__exact='') | ||
): | ||
naive_create_affiliation( | ||
apps, | ||
frozen_author.institution, | ||
frozen_author.department, | ||
frozen_author=frozen_author, | ||
) | ||
|
||
for preprint_author in PreprintAuthor.objects.filter( | ||
affiliation__isnull=True, | ||
): | ||
naive_create_affiliation( | ||
apps, | ||
preprint_author.affiliation, | ||
'', | ||
preprint_author=preprint_author, | ||
) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('core', '0100_location_organization_affiliation'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython( | ||
migrate_affiliation_institution, | ||
reverse_code=migrations.RunPython.noop | ||
), | ||
] |
29 changes: 29 additions & 0 deletions
29
src/core/migrations/0102_remove_account_country_affiliation_organization.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,29 @@ | ||
# Generated by Django 4.2.14 on 2024-07-26 13:26 | ||
|
||
import core.models | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('submission', '0080_remove_frozenauthor_country_and_more'), | ||
('core', '0101_migrate_affiliation_institution'), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name='account', | ||
name='country', | ||
), | ||
migrations.RemoveField( | ||
model_name='account', | ||
name='department', | ||
), | ||
migrations.RemoveField( | ||
model_name='account', | ||
name='institution', | ||
), | ||
] |
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