-
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.
- Loading branch information
Showing
2 changed files
with
42 additions
and
0 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
41 changes: 41 additions & 0 deletions
41
src/identifiers/migrations/0009_deduplicate_identifiers_20220527.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,41 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.29 on 2022-03-15 20:06 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
def deduplicate_identifiers(apps, schema_editor): | ||
Identifier = apps.get_model("identifiers", "Identifier") | ||
Journal = apps.get_model("journal", "Journal") | ||
journals = Journal.objects.all() | ||
for journal in journals: | ||
identifiers_in_journal = Identifier.objects.filter(article__journal=journal) | ||
for id_type in ['doi', 'uri', 'pubid']: | ||
identifiers_of_type = identifiers_in_journal.filter(id_type=id_type) | ||
for doi_string in set(identifiers_of_type.values_list('identifier', flat=True)): | ||
to_keep_pk = identifiers_of_type.filter(identifier=doi_string).values_list('id', flat=True)[0] | ||
duplicate_pks = identifiers_of_type.filter(identifier=doi_string).values_list('id', flat=True)[1:] | ||
duplicate_identifiers = [identifier for identifier in Identifier.objects.filter(pk__in=duplicate_pks)] | ||
print(id_type, to_keep_pk, doi_string) | ||
if duplicate_identifiers: | ||
print('\n\n\n') | ||
print('To keep:') | ||
print(id_type, to_keep_pk, doi_string) | ||
print('Duplicates:') | ||
for dup in duplicate_identifiers: | ||
print(id_type, dup.pk, dup.identifier) | ||
print('\n\n\n') | ||
Identifier.objects.filter(pk__in=duplicate_pks).delete() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
atomic = False | ||
dependencies = [ | ||
('identifiers', '0008_batch_doi_registration_continued_20220524'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(deduplicate_identifiers, reverse_code=migrations.RunPython.noop), | ||
] |