Skip to content

Add task to delete unlinked Institution and Location objects#1406

Open
Copilot wants to merge 3 commits intomainfrom
copilot/eliminate-institution-location-without-links
Open

Add task to delete unlinked Institution and Location objects#1406
Copilot wants to merge 3 commits intomainfrom
copilot/eliminate-institution-location-without-links

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 15, 2026

O que esse PR faz?

Celery task task_delete_unlinked_institutions_and_locations que limpa objetos Institution e Location órfãos após a migração para campos RawOrganizationMixin.

Três etapas:

  • Step 1: Para todas as instâncias de *History (OwnerHistory, PublisherHistory, SponsorHistory, CopyrightHolderHistory) com qualquer campo RawOrganizationMixin preenchido, seta institution = None
  • Step 2: Identifica Institution sem vínculo com nenhuma *History (via cadeia History.institution → BaseInstitution → Institution), limpa FK/M2M e deleta
  • Step 3: Limpa FK (city, state, country) de todas as Location e deleta

Onde a revisão poderia começar?

institution/tasks.py — arquivo único com a task e helper _build_raw_data_filter().

Como este poderia ser testado manualmente?

from institution.tasks import task_delete_unlinked_institutions_and_locations
result = task_delete_unlinked_institutions_and_locations.apply()
print(result.result)
# {'deleted_institutions': N, 'deleted_locations': M}

Ou via Celery worker/beat com o nome task_delete_unlinked_institutions_and_locations.

Testes automatizados em institution/tests.py cobrem: unlinking com raw data preenchido, preservação de vínculos sem raw data, deleção de Institution órfão, preservação de Institution vinculado, limpeza de FK/M2M antes da deleção, deleção de Location.

Algum cenário de contexto que queira dar?

A task task_replace_institution_by_raw_institution já popula os campos RawOrganizationMixin a partir dos dados do AMJournal. Esta nova task é o passo seguinte: após a migração dos dados para campos raw, os objetos Institution e Location legados que não possuem mais vínculos podem ser eliminados.

A cadeia de FK é indireta: *History.institutionBaseInstitution subclass (Owner, Publisher, etc.) → Institution. O step 2 resolve isso consultando institution__institution_id nas *History.

Screenshots

N/A

Quais são tickets relevantes?

Referências

  • journal/tasks.pytask_replace_institution_by_raw_institution (task predecessora)
  • core/models.pyRawOrganizationMixin (campos raw_text, raw_institution_name, etc.)
  • institution/models.pyBaseInstitution, Institution
Original prompt

This section details on the original issue you should resolve

<issue_title>Crie uma tarefa para eliminar os Institution e Location sem vínculo com outros objetos</issue_title>
<issue_description>### Descrição da tarefa
Crie uma tarefa para eliminar os Institution e Location sem vínculo com outros objetos.

Subtarefas

  • Note que as classes journal.models.*History possuem Institution
  • Percorra todos as instâncias de journal.models.*History, e verifique se algum dos dados de RawOrganizationMixin da instância estiverem completados, faça com que instance.institution = None
  • Percorra todas as instâncias de institution.models.Institution, se nenhuma instância journal.models.*History estiver vinculada, para todos os campos FK ou M2M da instancia, elimine os vínculos como os objetos, e execute instance.delete()
  • Percorra todas as instâncias de location.models.Location, para todos os campos FK ou M2M da instancia, elimine os vínculos como os objetos, e execute instance.delete()
    </issue_description>

Comments on the Issue (you are @copilot in this section)


📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.

Copilot AI and others added 2 commits March 15, 2026 16:29
Create institution/tasks.py with task_delete_unlinked_institutions_and_locations:
- Step 1: Set institution=None on *History instances with RawOrganizationMixin data filled
- Step 2: Delete Institution instances not linked to any *History (clear FK/M2M first)
- Step 3: Delete all Location instances (clear FK/M2M first)

Add comprehensive tests in institution/tests.py

Co-authored-by: robertatakenaka <505143+robertatakenaka@users.noreply.github.com>
Co-authored-by: robertatakenaka <505143+robertatakenaka@users.noreply.github.com>
Copilot AI changed the title [WIP] Create task to eliminate Institution and Location without links to other objects Add task to delete unlinked Institution and Location objects Mar 15, 2026
Copilot AI requested a review from robertatakenaka March 15, 2026 16:32
@robertatakenaka robertatakenaka marked this pull request as ready for review March 16, 2026 11:16
Copilot AI review requested due to automatic review settings March 16, 2026 11:17
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a Celery maintenance task to unlink *History objects from legacy Institution records when RawOrganizationMixin fields are populated, then attempts to delete now-unlinked legacy Institution/Location records, with accompanying automated tests.

Changes:

  • Introduces task_delete_unlinked_institutions_and_locations (+ helper _build_raw_data_filter) to unlink histories and delete orphaned data.
  • Adds a comprehensive Django TestCase suite covering unlinking, deletion, FK/M2M cleanup, and return counts.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
institution/tasks.py New Celery task to unlink *History.institution, delete unlinked Institution, and delete Location records.
institution/tests.py New unit tests validating task behavior and returned deletion counts.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +103 to +107
# Step 3: Delete all Location instances
deleted_locations = Location.objects.count()
Location.objects.update(city=None, state=None, country=None)
Location.objects.all().delete()

Comment on lines +73 to +97
all_institution_ids = set(
Institution.objects.values_list("id", flat=True)
)
linked_institution_ids = set()
for history_class in history_classes:
linked_institution_ids.update(
history_class.objects.filter(
institution__institution__isnull=False,
).values_list("institution__institution_id", flat=True)
)
unlinked_ids = all_institution_ids - linked_institution_ids

# Clear M2M fields for unlinked institutions
for inst in Institution.objects.filter(id__in=unlinked_ids):
inst.institution_type_scielo.clear()

# Clear FK fields and delete
Institution.objects.filter(id__in=unlinked_ids).update(
institution_identification=None,
location=None,
)
deleted_institutions = Institution.objects.filter(
id__in=unlinked_ids
).count()
Institution.objects.filter(id__in=unlinked_ids).delete()
for history_class in history_classes:
linked_institution_ids.update(
history_class.objects.filter(
institution__institution__isnull=False,
Comment on lines +85 to +87
# Clear M2M fields for unlinked institutions
for inst in Institution.objects.filter(id__in=unlinked_ids):
inst.institution_type_scielo.clear()
self.assertTrue(Country.objects.filter(pk=self.country.pk).exists())
self.assertTrue(State.objects.filter(pk=self.state.pk).exists())
self.assertTrue(City.objects.filter(pk=self.city.pk).exists())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crie uma tarefa para eliminar os Institution e Location sem vínculo com outros objetos

3 participants