Skip to content

Commit

Permalink
Migrations needed for modifications on the new batches NIH table (#1997)
Browse files Browse the repository at this point in the history
* Migrations needed for modifications on the new batches NIH table

* Lint fixes

* PR review suggestions
  • Loading branch information
leandroradusky authored Nov 3, 2023
1 parent dbb972c commit ca5709d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
22 changes: 22 additions & 0 deletions db/migrate/20231030102203_add_original_batch_id_to_samples.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
class AddOriginalBatchIdToSamples < ActiveRecord::Migration
def change
add_column :samples, :original_batch_id, :integer

Sample.preload(:batch).find_each do |sample|
if sample.batch
sample.original_batch_id = sample.batch.id
sample.save
elsif sample.box_id && sample.old_batch_number
institution_ids = TransferPackage
.select(:sender_institution_id)
.joins(:box_transfers)
.where(box_transfers: { box_id: sample.box_id })
.distinct

original_batch = Batch.where(
institution_id: institution_ids,
batch_number: sample.old_batch_number
).take
if original_batch
sample.original_batch_id = original_batch.id
sample.save
end
end
end
end
end
40 changes: 40 additions & 0 deletions lib/tasks/batches_info.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'csv'

namespace :batches_info do
desc "Update batches information"
task :import, [:csv_file] => :environment do |task, args|
path = args[:csv_file] || abort("Usage: $> rake batches_info:import[path/to/file.csv] RAILS_ENV={env}")

CSV.foreach(path, headers: true) do |row|
batch = Batch.find_by(batch_number: row['virus_batch_number'])
if batch
batch.virus_shortname = row["virus_shortname"]
batch.target_organism_name = row["target_organism_name"]
batch.target_organism_taxonomy_id = row["target_organism_taxonomy_id"]
batch.isolate_name = row["isolate_name"]
batch.gisaid_id = row["gisaid_id"]
batch.nucleotide_db_id = row["nucleotide_db_id"]
batch.pango_lineage = row["pango_lineage"]
batch.who_label = row["who_label"]
batch.gisaid_clade = row["gisaid_clade"]
batch.virus_sample_source = row["virus_sample_source"]
batch.virus_sample_source_url = row["virus_sample_source_url"]
batch.virus_source = row["virus_source"]
batch.virus_location = row["virus_location"]
batch.virus_sample_type = row["virus_sample_type"]
batch.virus_sample_formulation = row["virus_sample_formulation"]
batch.virus_sample_concentration = row["virus_sample_concentration"]
batch.virus_sample_concentration_unit = row["virus_sample_concentration_unit"]
batch.reference_gene = row["virus_sample_concentration_reference_gene"]
batch.virus_sample_genome_equivalents = row["virus_sample_genome_equivalents"]
batch.virus_sample_genome_equivalents_unit = row["virus_sample_genome_equivalents_unit"]
batch.virus_sample_genome_equivalents_reference_gene = row["virus_sample_genome_equivalents_reference_gene"]
batch.virus_preinactivation_tcid50 = row["virus_preinactivation_tcid50"]
batch.virus_preinactivation_tcid50_unit = row["virus_preinactivation_tcid50_unit"]
batch.virus_sample_grow_cell_line = row["virus_sample_grow_cell_line"]

batch.save
end
end
end
end
19 changes: 0 additions & 19 deletions lib/tasks/batches_labels.rake

This file was deleted.

0 comments on commit ca5709d

Please sign in to comment.