Skip to content

Commit 83d47e1

Browse files
committed
Differentiate mg/mt slots when downgrading
1 parent 0943fb7 commit 83d47e1

File tree

1 file changed

+55
-37
lines changed

1 file changed

+55
-37
lines changed

nmdc_server/migrations/versions/0cb677fa8d5e_update_jgi_submission-schema_slot_names.py

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
"""
88

9+
from collections import namedtuple
910
from typing import Optional
1011
from uuid import uuid4
1112

@@ -31,38 +32,27 @@ class SubmissionMetadata(Base):
3132
metadata_submission = Column(JSONB, nullable=False)
3233

3334

34-
RENAMED_SLOTS = [
35-
# ( old name, new name )
36-
("dna_absorb1", "nuc_acid_absorb1"),
37-
("rna_absorb1", "nuc_acid_absorb1"),
38-
("dna_absorb2", "nuc_acid_absorb2"),
39-
("rna_absorb2", "nuc_acid_absorb2"),
40-
("dna_concentration", "nuc_acid_concentration"),
41-
("rna_concentration", "nuc_acid_concentration"),
42-
("dna_cont_type", "cont_type"),
43-
("rna_cont_type", "cont_type"),
44-
("dna_cont_well", "cont_well"),
45-
("rna_cont_well", "cont_well"),
46-
("dna_container_id", "container_name"),
47-
("rna_container_id", "container_name"),
48-
("dna_dnase", "dnase"),
49-
("dnase_rna", "dnase"),
50-
("dna_samp_id", "jgi_samp_id"),
51-
("rna_samp_id", "jgi_samp_id"),
52-
("dna_sample_format", "jgi_sample_format"),
53-
("rna_sample_format", "jgi_sample_format"),
54-
("dna_sample_name", "jgi_sample_name"),
55-
("rna_sample_name", "jgi_sample_name"),
56-
("dna_seq_project", "jgi_seq_project"),
57-
("rna_seq_project", "jgi_seq_project"),
58-
("dna_seq_project_name", "jgi_seq_project_name"),
59-
("rna_seq_project_name", "jgi_seq_project_name"),
60-
("dna_volume", "jgi_sample_volume"),
61-
("rna_volume", "jgi_sample_volume"),
35+
RenamedSlot = namedtuple("RenamedSlot", ["old_name_mg", "old_name_mt", "new_name"])
36+
37+
38+
RENAMED_SLOTS: list[RenamedSlot] = [
39+
RenamedSlot("dna_absorb1", "rna_absorb1", "nuc_acid_absorb1"),
40+
RenamedSlot("dna_absorb2", "rna_absorb2", "nuc_acid_absorb2"),
41+
RenamedSlot("dna_concentration", "rna_concentration", "nuc_acid_concentration"),
42+
RenamedSlot("dna_cont_type", "rna_cont_type", "cont_type"),
43+
RenamedSlot("dna_cont_well", "rna_cont_well", "cont_well"),
44+
RenamedSlot("dna_container_id", "rna_container_id", "container_name"),
45+
RenamedSlot("dna_dnase", "dnase_rna", "dnase"),
46+
RenamedSlot("dna_samp_id", "rna_samp_id", "jgi_samp_id"),
47+
RenamedSlot("dna_sample_format", "rna_sample_format", "jgi_sample_format"),
48+
RenamedSlot("dna_sample_name", "rna_sample_name", "jgi_sample_name"),
49+
RenamedSlot("dna_seq_project", "rna_seq_project", "jgi_seq_project"),
50+
RenamedSlot("dna_seq_project_name", "rna_seq_project_name", "jgi_seq_project_name"),
51+
RenamedSlot("dna_volume", "rna_volume", "jgi_sample_volume"),
6252
]
6353

6454

65-
def do_rename(mapping: dict[str, str]):
55+
def upgrade():
6656
session = orm.Session(bind=op.get_bind())
6757
mappings = []
6858
for submission_metadata in session.query(SubmissionMetadata):
@@ -77,25 +67,53 @@ def do_rename(mapping: dict[str, str]):
7767
print(f"WARNING: sampleData not in migratable format for {submission_metadata.id}")
7868
continue
7969

80-
for tab, rows in sample_data.items():
70+
for tab_key, rows in sample_data.items():
8171
if not isinstance(rows, list):
8272
continue
8373
for row in rows:
8474
if not isinstance(row, dict):
8575
continue
86-
for old_key, new_key in mapping.items():
87-
if old_key in row:
88-
row[new_key] = row.pop(old_key)
76+
for rename in RENAMED_SLOTS:
77+
if "jgi_mg" in tab_key and rename.old_name_mg in row:
78+
row[rename.new_name] = row.pop(rename.old_name_mg)
79+
elif "jgi_mt" in tab_key and rename.old_name_mt in row:
80+
row[rename.new_name] = row.pop(rename.old_name_mt)
8981

9082
mappings.append({"id": submission_metadata.id, "metadata_submission": metadata_submission})
9183

9284
session.bulk_update_mappings(SubmissionMetadata, mappings)
9385
session.commit()
9486

9587

96-
def upgrade():
97-
do_rename({old: new for old, new in RENAMED_SLOTS})
88+
def downgrade():
89+
session = orm.Session(bind=op.get_bind())
90+
mappings = []
91+
for submission_metadata in session.query(SubmissionMetadata):
92+
metadata_submission = submission_metadata.metadata_submission
9893

94+
if isinstance(metadata_submission, list):
95+
continue
9996

100-
def downgrade():
101-
do_rename({new: old for old, new in RENAMED_SLOTS})
97+
sample_data = metadata_submission.get("sampleData")
98+
99+
if sample_data is None or not isinstance(sample_data, dict):
100+
print(f"WARNING: sampleData not in migratable format for {submission_metadata.id}")
101+
continue
102+
103+
for tab_key, rows in sample_data.items():
104+
if not isinstance(rows, list):
105+
continue
106+
for row in rows:
107+
if not isinstance(row, dict):
108+
continue
109+
for rename in RENAMED_SLOTS:
110+
if rename.new_name in row:
111+
if "jgi_mg" in tab_key:
112+
row[rename.old_name_mg] = row.pop(rename.new_name)
113+
elif "jgi_mt" in tab_key:
114+
row[rename.old_name_mt] = row.pop(rename.new_name)
115+
116+
mappings.append({"id": submission_metadata.id, "metadata_submission": metadata_submission})
117+
118+
session.bulk_update_mappings(SubmissionMetadata, mappings)
119+
session.commit()

0 commit comments

Comments
 (0)