Skip to content

Commit ca5709d

Browse files
Migrations needed for modifications on the new batches NIH table (#1997)
* Migrations needed for modifications on the new batches NIH table * Lint fixes * PR review suggestions
1 parent dbb972c commit ca5709d

File tree

3 files changed

+62
-19
lines changed

3 files changed

+62
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
class AddOriginalBatchIdToSamples < ActiveRecord::Migration
22
def change
33
add_column :samples, :original_batch_id, :integer
4+
5+
Sample.preload(:batch).find_each do |sample|
6+
if sample.batch
7+
sample.original_batch_id = sample.batch.id
8+
sample.save
9+
elsif sample.box_id && sample.old_batch_number
10+
institution_ids = TransferPackage
11+
.select(:sender_institution_id)
12+
.joins(:box_transfers)
13+
.where(box_transfers: { box_id: sample.box_id })
14+
.distinct
15+
16+
original_batch = Batch.where(
17+
institution_id: institution_ids,
18+
batch_number: sample.old_batch_number
19+
).take
20+
if original_batch
21+
sample.original_batch_id = original_batch.id
22+
sample.save
23+
end
24+
end
25+
end
426
end
527
end

lib/tasks/batches_info.rake

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require 'csv'
2+
3+
namespace :batches_info do
4+
desc "Update batches information"
5+
task :import, [:csv_file] => :environment do |task, args|
6+
path = args[:csv_file] || abort("Usage: $> rake batches_info:import[path/to/file.csv] RAILS_ENV={env}")
7+
8+
CSV.foreach(path, headers: true) do |row|
9+
batch = Batch.find_by(batch_number: row['virus_batch_number'])
10+
if batch
11+
batch.virus_shortname = row["virus_shortname"]
12+
batch.target_organism_name = row["target_organism_name"]
13+
batch.target_organism_taxonomy_id = row["target_organism_taxonomy_id"]
14+
batch.isolate_name = row["isolate_name"]
15+
batch.gisaid_id = row["gisaid_id"]
16+
batch.nucleotide_db_id = row["nucleotide_db_id"]
17+
batch.pango_lineage = row["pango_lineage"]
18+
batch.who_label = row["who_label"]
19+
batch.gisaid_clade = row["gisaid_clade"]
20+
batch.virus_sample_source = row["virus_sample_source"]
21+
batch.virus_sample_source_url = row["virus_sample_source_url"]
22+
batch.virus_source = row["virus_source"]
23+
batch.virus_location = row["virus_location"]
24+
batch.virus_sample_type = row["virus_sample_type"]
25+
batch.virus_sample_formulation = row["virus_sample_formulation"]
26+
batch.virus_sample_concentration = row["virus_sample_concentration"]
27+
batch.virus_sample_concentration_unit = row["virus_sample_concentration_unit"]
28+
batch.reference_gene = row["virus_sample_concentration_reference_gene"]
29+
batch.virus_sample_genome_equivalents = row["virus_sample_genome_equivalents"]
30+
batch.virus_sample_genome_equivalents_unit = row["virus_sample_genome_equivalents_unit"]
31+
batch.virus_sample_genome_equivalents_reference_gene = row["virus_sample_genome_equivalents_reference_gene"]
32+
batch.virus_preinactivation_tcid50 = row["virus_preinactivation_tcid50"]
33+
batch.virus_preinactivation_tcid50_unit = row["virus_preinactivation_tcid50_unit"]
34+
batch.virus_sample_grow_cell_line = row["virus_sample_grow_cell_line"]
35+
36+
batch.save
37+
end
38+
end
39+
end
40+
end

lib/tasks/batches_labels.rake

-19
This file was deleted.

0 commit comments

Comments
 (0)