Skip to content

Commit

Permalink
Merge pull request #685 from sanger/develop
Browse files Browse the repository at this point in the history
Develop into Master
  • Loading branch information
stevieing authored Oct 8, 2024
2 parents 5ace068 + 2e18c6a commit eee9edc
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .release-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.21.0
1.22.0
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ GEM
guard-compat (~> 1.1)
rspec (>= 2.99.0, < 4.0)
hashie (5.0.0)
i18n (1.14.5)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
json (2.7.2)
language_server-protocol (3.17.0.3)
Expand Down Expand Up @@ -160,10 +160,10 @@ GEM
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.32.3)
parser (>= 3.3.1.0)
rubocop-performance (1.21.1)
rubocop-performance (1.22.1)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rails (2.26.1)
rubocop-rails (2.26.2)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.52.0, < 2.0)
Expand Down
39 changes: 39 additions & 0 deletions lib/tasks/update_id_sample_lims.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

# Rails task to update the id_sample_lims column based on the name column in samples table for samples
# Why is this task needed?
# The id_sample_lims column in the samples table is used to store the ID of the sample in Traction.
# However, this is clashing with IDs in the sample table in the warehouse, causing problems for NPG.
# NPG relies on the uniqueness of these IDs as they use Sequencescape IDs.
# To resolve this issue, we need to update the id_sample_lims column to the value of the name column for samples where id_lims is "Traction".
namespace :sample_table do
# This rake task updates the id_sample_lims column to the value of the name column
# for samples where id_lims is "Traction". If the name is already present in the
# id_sample_lims column, the sample will be skipped.
#
# Usage: bundle exec rake sample_table:update_id_sample_lims
#
# The task performs the following steps:
# 1. Fetch all samples where id_lims is "Traction".
# 2. For each sample, check if the name is already present in the id_sample_lims column.
# 3. If the name is not present, update the id_sample_lims column to the value of the name column.
# 4. Skip the sample if the name is already present in the id_sample_lims column.
#
desc 'Update id_sample_lims column to the value of the name column if id_lims is "Traction"'

task update_id_sample_lims: :environment do
Sample.where(id_lims: 'Traction').find_each do |sample|
# Skip updating the sample if the id_sample_lims already contains the sample's name
next if Sample.exists?(id_sample_lims: sample.name)

puts "Updating id_sample_lims for sample #{sample.id} to #{sample.name}"
sample.id_sample_lims = sample.name

begin
sample.save!
rescue ActiveRecord::ActiveRecordError, StandardError => e
puts "Failed to update id_sample_lims for sample #{sample.id}: #{e.message}"
end
end
end
end
39 changes: 39 additions & 0 deletions spec/lib/tasks/update_id_sample_lims.rake_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

require 'spec_helper'
require 'rake'
# only load Rake tasks if they haven't been loaded already
Rails.application.load_tasks if Rake::Task.tasks.empty?

RSpec.describe 'RakeTasks' do
describe 'sample_table:update_id_sample_lims' do
let!(:traction_sample) { create(:sample, id_lims: 'Traction', name: 'SampleName1', id_sample_lims: 'OldValue1', uuid_sample_lims: SecureRandom.uuid) }
let!(:non_traction_sample) { create(:sample, id_lims: 'Other', name: 'SampleName2', id_sample_lims: 'OldValue2', uuid_sample_lims: SecureRandom.uuid) }
let!(:existing_sample) { create(:sample, id_lims: 'Traction', name: 'SampleName1', id_sample_lims: 'OldValue2', uuid_sample_lims: SecureRandom.uuid) }

before do
Rake::Task['sample_table:update_id_sample_lims'].reenable
end

it 'updates id_sample_lims to the value of the name column for traction samples' do
expect { Rake::Task['sample_table:update_id_sample_lims'].invoke }.to change { traction_sample.reload.id_sample_lims }.from('OldValue1').to('SampleName1').and output(
/Updating id_sample_lims for sample #{traction_sample.id} to SampleName1/
).to_stdout
end

it 'does not change id_sample_lims for non-traction samples' do
expect { Rake::Task['sample_table:update_id_sample_lims'].invoke }.not_to(change { non_traction_sample.reload.id_sample_lims })
end

it 'does not change id_sample_lims if the name is already in id_sample_lims' do
expect { Rake::Task['sample_table:update_id_sample_lims'].invoke }.not_to(change { existing_sample.reload.id_sample_lims })
end
it 'raises an error if saving the sample fails' do
allow_any_instance_of(Sample).to receive(:save!).and_raise(ActiveRecord::ActiveRecordError, 'Simulated save error')

expect do
Rake::Task['sample_table:update_id_sample_lims'].invoke
end.to output(/Failed to update id_sample_lims for sample #{traction_sample.id}: Simulated save error/).to_stdout
end
end
end

0 comments on commit eee9edc

Please sign in to comment.