diff --git a/app/models/sample.rb b/app/models/sample.rb index cb965a3a41..38c8bb6dd6 100644 --- a/app/models/sample.rb +++ b/app/models/sample.rb @@ -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 diff --git a/lib/seek/samples/extractor.rb b/lib/seek/samples/extractor.rb index 7c4835d8df..98136d8739 100644 --- a/lib/seek/samples/extractor.rb +++ b/lib/seek/samples/extractor.rb @@ -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 @@ -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 diff --git a/lib/tasks/seek_upgrades.rake b/lib/tasks/seek_upgrades.rake index c5ba4e07a7..a376bf1274 100644 --- a/lib/tasks/seek_upgrades.rake +++ b/lib/tasks/seek_upgrades.rake @@ -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 @@ -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 ## diff --git a/test/unit/jobs/sample_data_persist_job_test.rb b/test/unit/jobs/sample_data_persist_job_test.rb index d22ede3254..c75730fa37 100644 --- a/test/unit/jobs/sample_data_persist_job_test.rb +++ b/test/unit/jobs/sample_data_persist_job_test.rb @@ -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 @@ -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 diff --git a/test/unit/sample_test.rb b/test/unit/sample_test.rb index 310bc1bcb3..3bd94595d3 100644 --- a/test/unit/sample_test.rb +++ b/test/unit/sample_test.rb @@ -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), @@ -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