diff --git a/app/services/tasks/samples_to_repository_migration_service.rb b/app/services/tasks/samples_to_repository_migration_service.rb index 6270bf296..fa5592564 100644 --- a/app/services/tasks/samples_to_repository_migration_service.rb +++ b/app/services/tasks/samples_to_repository_migration_service.rb @@ -1,3 +1,7 @@ +# Helper module for dealing with the migration from samples +# to custom repositories. We need to query with SQL because probably we will not +# have the "Sample" and other related models at the time this code will execute + module Tasks module SamplesToRepositoryMigrationService def self.prepare_repository(team, copy_num = 0) @@ -67,7 +71,8 @@ module Tasks RepositoryListItem.create( data: item.fetch('name') { "sample group item (#{index})" }, created_by_id: item.fetch('created_by_id') { team.created_by_id }, - last_modified_by_id: item.fetch('last_modified_by_id') { team.created_by_id }, + last_modified_by_id: + item.fetch('last_modified_by_id') { team.created_by_id }, repository_column: repository_columns.first, repository: repository ) @@ -77,7 +82,8 @@ module Tasks RepositoryListItem.create( data: item.fetch('name') { "sample group item (#{index})" }, created_by_id: item.fetch('created_by_id') { team.created_by_id }, - last_modified_by_id: item.fetch('last_modified_by_id') { team.created_by_id }, + last_modified_by_id: + item.fetch('last_modified_by_id') { team.created_by_id }, repository_column: repository_columns.last, repository: repository ) @@ -108,5 +114,23 @@ module Tasks SQL ActiveRecord::Base.connection.execute(assigned_samples_sql).to_a end + + def self.fetch_all_team_samples(team) + samples_sql = <<-SQL + SELECT samples.id AS sample_id, + samples.name AS sample_name, + samples.user_id AS sample_created_by_id, + samples.last_modified_by_id AS sample_last_modified_by_id, + sample_types.name AS sample_type_name, + sample_groups.name AS sample_group_name, + sample_groups.color AS sample_group_color + FROM samples + JOIN sample_types ON samples.sample_type_id = sample_types.id + JOIN sample_groups ON samples.sample_type_id = sample_groups.id + WHERE samples.team_id = #{team.id} + SQL + + ActiveRecord::Base.connection.execute(samples_sql).to_a + end end end diff --git a/lib/tasks/samples_to_repository_migration.rake b/lib/tasks/samples_to_repository_migration.rake index b029821ab..28385b326 100644 --- a/lib/tasks/samples_to_repository_migration.rake +++ b/lib/tasks/samples_to_repository_migration.rake @@ -4,7 +4,7 @@ namespace :samples_to_repository_migration do desc 'Migrates all data from samples to custom repository' task :run, [:last_id] => :environment do |_, args| params = { batch_size: 10 } - migration_service = SamplesToRepositoryMigrationService + migration_service = Tasks::SamplesToRepositoryMigrationService if args.present? && args[:last_id].present? params[:start] = args[:last_id].to_i end @@ -12,20 +12,8 @@ namespace :samples_to_repository_migration do puts "******************************* \n\n\n\n" puts "Processing Team id => [#{team.id}] \n\n\n\n" puts '*******************************' - - samples_sql = <<-SQL - SELECT samples.id AS sample_id, - samples.name AS sample_name, - samples.user_id AS sample_created_by_id, - samples.last_modified_by_id AS sample_last_modified_by_id, - sample_types.name AS sample_type_name, - sample_groups.name AS sample_group_name, - sample_groups.color AS sample_group_color - FROM samples - JOIN sample_types ON samples.sample_type_id = sample_types.id - JOIN sample_groups ON samples.sample_type_id = sample_groups.id - WHERE samples.team_id = #{team.id} - SQL + # byebug + migration_service.fetch_all_team_samples(team) repository = migration_service.prepare_repository(team) custom_columns = migration_service.prepare_text_value_custom_columns(team, repository) + @@ -33,7 +21,7 @@ namespace :samples_to_repository_migration do team, repository ) - byebug + # byebug end end end diff --git a/spec/services/tasks/samples_to_repository_migration_service_spec.rb b/spec/services/tasks/samples_to_repository_migration_service_spec.rb index ad9241e0f..5463fe47c 100644 --- a/spec/services/tasks/samples_to_repository_migration_service_spec.rb +++ b/spec/services/tasks/samples_to_repository_migration_service_spec.rb @@ -4,7 +4,6 @@ describe Tasks::SamplesToRepositoryMigrationService do let(:user) { create :user, email: 'happy.user@scinote.net' } let(:team) { create :team, created_by: user } let(:user_team) { create :user_team, user: user, team: team } - let(:sample) { create :sample, name: 'My sample', user: user, team: team } describe '#prepare_repository/2' do context 'creates and return a new custom repository named' do @@ -141,6 +140,7 @@ describe Tasks::SamplesToRepositoryMigrationService do end describe '#get_sample_custom_fields/1' do + let(:sample) { create :sample, name: 'My sample', user: user, team: team } let(:custom_field) do create :custom_field, name: 'My Custom column', user: user, @@ -178,6 +178,7 @@ describe Tasks::SamplesToRepositoryMigrationService do end describe '#get_assigned_sample_module/1' do + let(:sample) { create :sample, name: 'My sample', user: user, team: team } let(:my_module) { create :my_module } let(:subject) do Tasks::SamplesToRepositoryMigrationService @@ -238,4 +239,32 @@ describe Tasks::SamplesToRepositoryMigrationService do end end end + + describe 'fetch_all_team_samples/1' do + let(:subject) do + Tasks::SamplesToRepositoryMigrationService.fetch_all_team_samples(team) + end + + context 'team has samples' do + before do + 100.times do |index| + create :sample, name: "Sample (#{index})", user: user, team: team + end + end + + it { is_expected.to be_an Array } + it { expect(subject.length).to eq 100 } + + it 'returns an array of all team samples' do + subject.each_with_index do |element, index| + expect(element.fetch('sample_name')). to eq "Sample (#{index})" + end + end + end + + context 'team does not have samples' do + it { is_expected.to be_an Array } + it { is_expected.to be_empty } + end + end end