-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
dbb972c
commit ca5709d
Showing
3 changed files
with
62 additions
and
19 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
db/migrate/20231030102203_add_original_batch_id_to_samples.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.