Skip to content

Commit

Permalink
Merge pull request seek4science#1608 from seek4science/extracted_samp…
Browse files Browse the repository at this point in the history
…les_projects

Extracted samples projects
  • Loading branch information
fherreazcue authored Oct 11, 2023
2 parents 11a30b4 + a10ebe5 commit 56a6f0f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
8 changes: 0 additions & 8 deletions app/models/sample.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,6 @@ def extracted?
!!originating_data_file
end

def projects
extracted? ? originating_data_file.projects : super
end

def project_ids
extracted? ? originating_data_file.project_ids : super
end

def creators
extracted? ? originating_data_file.creators : super
end
Expand Down
4 changes: 4 additions & 0 deletions lib/seek/samples/extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def persist

last_id = Sample.last.try(:id) || 0
sample_type = samples.first.sample_type
project_ids = samples.first.project_ids
disable_authorization_checks { Sample.import(samples, validate: false, batch_size: 2000) }
SampleTypeUpdateJob.new(sample_type, false).queue_job

Expand All @@ -38,6 +39,9 @@ def persist
samples = Sample.where(sample_type: sample_type, title: samples.collect(&:title), contributor: contributor).where(
'id > ?', last_id
)
samples.each do |sample|
sample.project_ids = project_ids
end
ReindexingQueue.enqueue(samples)
AuthLookupUpdateQueue.enqueue(samples)
end
Expand Down
23 changes: 23 additions & 0 deletions lib/tasks/seek_upgrades.rake
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace :seek do
task upgrade_version_tasks: %i[
environment
decouple_extracted_samples_policies
decouple_extracted_samples_projects
]

# these are the tasks that are executes for each upgrade as standard, and rarely change
Expand Down Expand Up @@ -61,6 +62,28 @@ namespace :seek do
puts " ... finished creating independent policies of #{decoupled.to_s} extracted samples"
end

task(decouple_extracted_samples_projects: [:environment]) do
puts '... copying project ids for extracted samples...'
decoupled = 0
hash_array = []
disable_authorization_checks do
Sample.find_each do |sample|
# check if the sample was extracted from a datafile and their projects are linked
if sample.extracted? && sample.project_ids.empty?
sample.originating_data_file.project_ids.each do |project_id|
hash_array << { project_id: project_id, sample_id: sample.id }
end
decoupled += 1
end
end
unless hash_array.empty?
class ProjectsSample < ActiveRecord::Base; end;
ProjectsSample.insert_all(hash_array)
end
end
puts " ... finished copying project ids of #{decoupled.to_s} extracted samples"
end

private

##
Expand Down
5 changes: 4 additions & 1 deletion test/unit/jobs/sample_data_persist_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ def setup
@person = FactoryBot.create(:project_administrator)
User.current_user = @person.user

@project_id = @person.projects.first.id

@data_file = FactoryBot.create :data_file, content_blob: FactoryBot.create(:sample_type_populated_template_content_blob),
policy: FactoryBot.create(:private_policy), contributor: @person
refute @data_file.sample_template?
assert_empty @data_file.possible_sample_types

@sample_type = SampleType.new title: 'from template', uploaded_template: true,
project_ids: [@person.projects.first.id], contributor: @person
project_ids: [@project_id], contributor: @person
@sample_type.content_blob = FactoryBot.create(:sample_type_template_content_blob)
@sample_type.build_attributes_from_template
# this is to force the full name to be 2 words, so that one row fails
Expand Down Expand Up @@ -47,6 +49,7 @@ def setup
assert_equal 3, @data_file.extracted_samples.count
assert_equal @sample_type, @data_file.extracted_samples.first.sample_type
assert_equal @person, @data_file.extracted_samples.first.contributor
assert_equal [@project_id], @data_file.extracted_samples.first.project_ids
end

test 'persists samples and associate with assay' do
Expand Down
8 changes: 4 additions & 4 deletions test/unit/sample_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ class SampleTest < ActiveSupport::TestCase
refute sample2.can_view?(nil)
end

test 'extracted samples inherit projects from data file' do
test 'extracted samples copy projects from data file' do
person = FactoryBot.create(:person)
create_sample_attribute_type
data_file = FactoryBot.create :data_file, content_blob: FactoryBot.create(:sample_type_populated_template_content_blob),
Expand All @@ -769,9 +769,9 @@ class SampleTest < ActiveSupport::TestCase
data_file.save!
end

assert_equal new_projects.sort, sample.projects.sort
assert_equal sample.projects.sort, data_file.projects.sort
assert_equal sample.project_ids.sort, data_file.project_ids.sort
assert_not_equal new_projects.sort, sample.projects.sort
assert_not_equal sample.projects.sort, data_file.projects.sort
assert_not_equal sample.project_ids.sort, data_file.project_ids.sort
end

test 'extracted samples inherit creators from data file' do
Expand Down

0 comments on commit 56a6f0f

Please sign in to comment.