Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add migrator for moving relevant_protocols to protocol_link PR21 #76

Merged
merged 5 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions nmdc_schema/migrators/migrator_from_X_to_PR21.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from nmdc_schema.migrators.migrator_base import MigratorBase
from nmdc_schema.migrators.helpers import load_yaml_asset

class Migrator(MigratorBase):
"""Migrates data from schema X to PR21"""

_from_version = "X"
_to_version = "PR21"

def __init__(self, *args, **kwargs) -> None:
"""Invokes parent constructor and populates collection-to-transformations map."""

super().__init__(*args, **kwargs)

# Populate the "collection-to-transformers" map for this specific migration.
self._agenda = dict(
study_set=[self.move_relevant_protocols_to_protocol_link],
)

def move_relevant_protocols_to_protocol_link(self, study: dict):
r"""
Moves any urls that exist in the relevant_protocols slot to a dictionary format (e.g. Protocol class is the range)
in the protocol_link slot. It then removes the relevant_protocols slot from the study.

>>> m = Migrator()
>>> m.move_relevant_protocols_to_protocol_link({'id': 123, 'relevant_protocols': ['www.ex1.org', 'www.ex2.gov']})
{'id': 123, 'protocol_link': [{'url': 'www.ex1.org', 'type': 'nmdc:Protocol'}, {'url': 'www.ex2.gov', 'type': 'nmdc:Protocol'}]}
>>> m.move_relevant_protocols_to_protocol_link({'id': 123, 'relevant_protocols': ['www.ex3.org']})
{'id': 123, 'protocol_link': [{'url': 'www.ex3.org', 'type': 'nmdc:Protocol'}]}
"""

if "relevant_protocols" in study:
id = study["id"]
study["protocol_link"] = []
for protocol in study["relevant_protocols"]:
new_protocol_struct = {
"url": protocol,
"type": "nmdc:Protocol"
}
study["protocol_link"].append(new_protocol_struct)
self.logger.info(f"Moving {protocol} for study{id} from relevant_protocols slot to protocol_link")

study.pop("relevant_protocols")


return study


3 changes: 1 addition & 2 deletions project.Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -636,5 +636,4 @@ local/nmdc-sty-11-aygzgv51-tdb: local/nmdc-schema-v8.0.0.owl.ttl local/nmdc-sty-
.PHONY: filtered-status
filtered-status:
git status | grep -v 'project/' | grep -v 'nmdc_schema/.*yaml' | grep -v 'nmdc_schema/.*json' | \
grep -v 'nmdc.py' | grep -v 'nmdc_schema_accepting_legacy_ids.py'

grep -v 'nmdc.py' | grep -v 'nmdc_schema_accepting_legacy_ids.py'
Loading