6
6
7
7
"""
8
8
9
+ from collections import namedtuple
9
10
from typing import Optional
10
11
from uuid import uuid4
11
12
@@ -31,38 +32,27 @@ class SubmissionMetadata(Base):
31
32
metadata_submission = Column (JSONB , nullable = False )
32
33
33
34
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" ),
62
52
]
63
53
64
54
65
- def do_rename ( mapping : dict [ str , str ] ):
55
+ def upgrade ( ):
66
56
session = orm .Session (bind = op .get_bind ())
67
57
mappings = []
68
58
for submission_metadata in session .query (SubmissionMetadata ):
@@ -77,25 +67,53 @@ def do_rename(mapping: dict[str, str]):
77
67
print (f"WARNING: sampleData not in migratable format for { submission_metadata .id } " )
78
68
continue
79
69
80
- for tab , rows in sample_data .items ():
70
+ for tab_key , rows in sample_data .items ():
81
71
if not isinstance (rows , list ):
82
72
continue
83
73
for row in rows :
84
74
if not isinstance (row , dict ):
85
75
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 )
89
81
90
82
mappings .append ({"id" : submission_metadata .id , "metadata_submission" : metadata_submission })
91
83
92
84
session .bulk_update_mappings (SubmissionMetadata , mappings )
93
85
session .commit ()
94
86
95
87
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
98
93
94
+ if isinstance (metadata_submission , list ):
95
+ continue
99
96
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