|
| 1 | +"""Update JGI submission-schema slot names |
| 2 | +
|
| 3 | +Revision ID: 0cb677fa8d5e |
| 4 | +Revises: e27443f0837e |
| 5 | +Create Date: 2025-08-06 19:53:09.430148 |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +from collections import namedtuple |
| 10 | +from typing import Optional |
| 11 | +from uuid import uuid4 |
| 12 | + |
| 13 | +from alembic import op |
| 14 | +from sqlalchemy import Column, orm |
| 15 | +from sqlalchemy.dialects.postgresql import JSONB, UUID |
| 16 | +from sqlalchemy.ext.declarative import declarative_base |
| 17 | + |
| 18 | +# revision identifiers, used by Alembic. |
| 19 | +revision: str = "0cb677fa8d5e" |
| 20 | +down_revision: Optional[str] = "e27443f0837e" |
| 21 | +branch_labels: Optional[str] = None |
| 22 | +depends_on: Optional[str] = None |
| 23 | + |
| 24 | + |
| 25 | +Base = declarative_base() |
| 26 | + |
| 27 | + |
| 28 | +class SubmissionMetadata(Base): |
| 29 | + __tablename__ = "submission_metadata" |
| 30 | + |
| 31 | + id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4) |
| 32 | + metadata_submission = Column(JSONB, nullable=False) |
| 33 | + |
| 34 | + |
| 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"), |
| 52 | +] |
| 53 | + |
| 54 | + |
| 55 | +def upgrade(): |
| 56 | + session = orm.Session(bind=op.get_bind()) |
| 57 | + mappings = [] |
| 58 | + for submission_metadata in session.query(SubmissionMetadata): |
| 59 | + metadata_submission = submission_metadata.metadata_submission |
| 60 | + |
| 61 | + if isinstance(metadata_submission, list): |
| 62 | + continue |
| 63 | + |
| 64 | + sample_data = metadata_submission.get("sampleData") |
| 65 | + |
| 66 | + if sample_data is None or not isinstance(sample_data, dict): |
| 67 | + print(f"WARNING: sampleData not in migratable format for {submission_metadata.id}") |
| 68 | + continue |
| 69 | + |
| 70 | + for tab_key, rows in sample_data.items(): |
| 71 | + if not isinstance(rows, list): |
| 72 | + continue |
| 73 | + for row in rows: |
| 74 | + if not isinstance(row, dict): |
| 75 | + continue |
| 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) |
| 81 | + |
| 82 | + mappings.append({"id": submission_metadata.id, "metadata_submission": metadata_submission}) |
| 83 | + |
| 84 | + session.bulk_update_mappings(SubmissionMetadata, mappings) |
| 85 | + session.commit() |
| 86 | + |
| 87 | + |
| 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 |
| 93 | + |
| 94 | + if isinstance(metadata_submission, list): |
| 95 | + continue |
| 96 | + |
| 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