Skip to content

Commit

Permalink
feat #3168 Migrations for existing data
Browse files Browse the repository at this point in the history
  • Loading branch information
joemull committed Jan 20, 2025
1 parent c2576aa commit ed69d4c
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
class Migration(migrations.Migration):

dependencies = [
('repository', '0044_remove_preprintauthor_affiliation_and_more'),
('submission', '0080_remove_frozenauthor_country_and_more'),
('core', '0099_alter_accountrole_options'),
('utils', '0035_rorimport_rorimporterror'),
]

operations = [
Expand All @@ -35,18 +34,7 @@ class Migration(migrations.Migration):
('website', models.CharField(blank=True, max_length=500)),
('locations', models.ManyToManyField(blank=True, null=True, to='core.location')),
],
),
migrations.RemoveField(
model_name='account',
name='country',
),
migrations.RemoveField(
model_name='account',
name='department',
),
migrations.RemoveField(
model_name='account',
name='institution',
options={'ordering': ['ror_display__value']},
),
migrations.CreateModel(
name='OrganizationName',
Expand All @@ -67,16 +55,16 @@ class Migration(migrations.Migration):
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(blank=True, max_length=300, verbose_name='Title, position, or role')),
('department', models.CharField(blank=True, max_length=300, verbose_name='Department, unit, or team')),
('is_primary', models.BooleanField(default=False)),
('start', models.DateField(blank=True, null=True)),
('end', models.DateField(blank=True, null=True)),
('is_primary', models.BooleanField(default=False, help_text='Each account can have one primary affiliation')),
('start', models.DateField(blank=True, null=True, verbose_name='Start date')),
('end', models.DateField(blank=True, null=True, help_text='Leave empty for a current affiliation', verbose_name='End date')),
('account', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
('frozen_author', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='submission.frozenauthor')),
('organization', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='core.organization')),
('preprint_author', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='repository.preprintauthor')),
],
options={
'ordering': ['-pk'],
'ordering': ['is_primary', '-pk'],
},
),
migrations.AddConstraint(
Expand Down
95 changes: 95 additions & 0 deletions src/core/migrations/0101_migrate_affiliation_institution.py
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
),
]
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',
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class Migration(migrations.Migration):

dependencies = [
('core', '0101_migrate_affiliation_institution'),
('repository', '0045_historicalrepository_display_public_metrics_and_more'),
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class Migration(migrations.Migration):

dependencies = [
('core', '0101_migrate_affiliation_institution'),
('submission', '0084_remove_article_jats_article_type_and_more'),
]

Expand Down

0 comments on commit ed69d4c

Please sign in to comment.