|
| 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 typing import Optional |
| 10 | +from uuid import uuid4 |
| 11 | + |
| 12 | +from alembic import op |
| 13 | +from sqlalchemy import Column, orm |
| 14 | +from sqlalchemy.dialects.postgresql import JSONB, UUID |
| 15 | +from sqlalchemy.ext.declarative import declarative_base |
| 16 | + |
| 17 | +# revision identifiers, used by Alembic. |
| 18 | +revision: str = "0cb677fa8d5e" |
| 19 | +down_revision: Optional[str] = "e27443f0837e" |
| 20 | +branch_labels: Optional[str] = None |
| 21 | +depends_on: Optional[str] = None |
| 22 | + |
| 23 | + |
| 24 | +Base = declarative_base() |
| 25 | + |
| 26 | + |
| 27 | +class SubmissionMetadata(Base): |
| 28 | + __tablename__ = "submission_metadata" |
| 29 | + |
| 30 | + id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4) |
| 31 | + metadata_submission = Column(JSONB, nullable=False) |
| 32 | + |
| 33 | + |
| 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"), |
| 62 | +] |
| 63 | + |
| 64 | + |
| 65 | +def do_rename(mapping: dict[str, str]): |
| 66 | + session = orm.Session(bind=op.get_bind()) |
| 67 | + mappings = [] |
| 68 | + for submission_metadata in session.query(SubmissionMetadata): |
| 69 | + metadata_submission = submission_metadata.metadata_submission |
| 70 | + |
| 71 | + if isinstance(metadata_submission, list): |
| 72 | + continue |
| 73 | + |
| 74 | + sample_data = metadata_submission.get("sampleData") |
| 75 | + |
| 76 | + if sample_data is None or not isinstance(sample_data, dict): |
| 77 | + print(f"WARNING: sampleData not in migratable format for {submission_metadata.id}") |
| 78 | + continue |
| 79 | + |
| 80 | + for tab, rows in sample_data.items(): |
| 81 | + if not isinstance(rows, list): |
| 82 | + continue |
| 83 | + for row in rows: |
| 84 | + if not isinstance(row, dict): |
| 85 | + continue |
| 86 | + for old_key, new_key in mapping.items(): |
| 87 | + if old_key in row: |
| 88 | + row[new_key] = row.pop(old_key) |
| 89 | + |
| 90 | + mappings.append({"id": submission_metadata.id, "metadata_submission": metadata_submission}) |
| 91 | + |
| 92 | + session.bulk_update_mappings(SubmissionMetadata, mappings) |
| 93 | + session.commit() |
| 94 | + |
| 95 | + |
| 96 | +def upgrade(): |
| 97 | + do_rename({old: new for old, new in RENAMED_SLOTS}) |
| 98 | + |
| 99 | + |
| 100 | +def downgrade(): |
| 101 | + do_rename({new: old for old, new in RENAMED_SLOTS}) |
0 commit comments