2018-02-15 16:36:09 +08:00
|
|
|
require_relative '../../app/services/tasks/samples_to_repository_migration_service'
|
2018-02-13 23:52:37 +08:00
|
|
|
|
2018-02-15 16:36:09 +08:00
|
|
|
namespace :samples_to_repository_migration do
|
2018-02-13 23:52:37 +08:00
|
|
|
desc 'Migrates all data from samples to custom repository'
|
|
|
|
task :run, [:last_id] => :environment do |_, args|
|
2018-03-12 05:06:44 +08:00
|
|
|
include ActiveRecord::Sanitization::ClassMethods
|
|
|
|
|
2018-02-13 23:52:37 +08:00
|
|
|
params = { batch_size: 10 }
|
2018-02-16 00:38:45 +08:00
|
|
|
migration_service = Tasks::SamplesToRepositoryMigrationService
|
2018-03-12 05:06:44 +08:00
|
|
|
conn = ActiveRecord::Base.connection
|
2018-02-13 23:52:37 +08:00
|
|
|
if args.present? && args[:last_id].present?
|
|
|
|
params[:start] = args[:last_id].to_i
|
|
|
|
end
|
|
|
|
Team.find_each(params) do |team|
|
2018-02-27 00:21:58 +08:00
|
|
|
puts "******************************* \n\n"
|
|
|
|
puts "Processing Team id => [#{team.id}] \n\n"
|
2018-02-13 23:52:37 +08:00
|
|
|
puts '*******************************'
|
|
|
|
|
2018-02-27 00:21:58 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
team_samples = migration_service.fetch_all_team_samples(team)
|
|
|
|
repository = migration_service.prepare_repository(team)
|
|
|
|
custom_columns = migration_service.get_custom_columns(team, repository)
|
2018-03-12 05:06:44 +08:00
|
|
|
sample_mappings = {}
|
2018-02-27 00:21:58 +08:00
|
|
|
team_samples.each do |item|
|
|
|
|
created_by = item['sample_created_by_id'] || team.created_by_id
|
2018-03-09 22:44:42 +08:00
|
|
|
last_modified_by = item['sample_last_modified_by_id']
|
|
|
|
last_modified_by ||= team.created_by_id
|
2018-03-12 05:06:44 +08:00
|
|
|
timestamp = conn.quote(Time.now.to_s(:db))
|
|
|
|
values = [repository.id, created_by, last_modified_by,
|
|
|
|
conn.quote(item['sample_name']), timestamp, timestamp]
|
|
|
|
list_item_sql = <<-SQL
|
|
|
|
INSERT INTO repository_rows
|
|
|
|
(repository_id, created_by_id, last_modified_by_id, name,
|
|
|
|
created_at, updated_at)
|
|
|
|
VALUES (#{values.join(', ')})
|
|
|
|
RETURNING id
|
|
|
|
SQL
|
|
|
|
result = conn.execute(list_item_sql)
|
|
|
|
row_id = result[0]['id']
|
|
|
|
sample_mappings[item['sample_id']] = row_id
|
|
|
|
|
2018-02-27 00:21:58 +08:00
|
|
|
# check if sample has sample type assigned
|
|
|
|
if item['sample_type_name']
|
|
|
|
column = custom_columns.detect { |el| el['name'] == 'Sample type' }
|
|
|
|
list_item = column.repository_list_items.where(
|
|
|
|
data: item['sample_type_name']
|
|
|
|
).take
|
2018-03-12 05:06:44 +08:00
|
|
|
migration_service.create_list_cell(
|
|
|
|
row_id, column.id, list_item.id,
|
|
|
|
list_item.created_by_id, list_item.last_modified_by_id
|
2018-02-27 00:21:58 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
# check if sample has sample group assigned
|
|
|
|
if item['sample_group_name']
|
|
|
|
column = custom_columns.detect { |el| el['name'] == 'Sample group' }
|
|
|
|
list_item = column.repository_list_items.where(
|
|
|
|
data: item['sample_group_name']
|
|
|
|
).take
|
2018-03-12 05:06:44 +08:00
|
|
|
migration_service.create_list_cell(
|
|
|
|
row_id, column.id, list_item.id,
|
|
|
|
list_item.created_by_id, list_item.last_modified_by_id
|
2018-02-27 00:21:58 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# assign sample group color to the sample
|
|
|
|
if item['sample_group_color']
|
|
|
|
column = custom_columns.detect do |el|
|
2018-03-09 22:44:42 +08:00
|
|
|
el['name'] == 'Sample group color hex'
|
2018-02-27 00:21:58 +08:00
|
|
|
end
|
2018-03-12 05:06:44 +08:00
|
|
|
migration_service.create_text_cell(
|
|
|
|
row_id, column.id, item['sample_group_color'],
|
|
|
|
created_by, last_modified_by
|
2018-02-27 00:21:58 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# append custom fields
|
|
|
|
custom_fields = migration_service.get_sample_custom_fields(
|
|
|
|
item['sample_id']
|
|
|
|
)
|
|
|
|
custom_fields.each do |field|
|
|
|
|
column = custom_columns.detect do |el|
|
|
|
|
el['name'] == field['column_name_reference']
|
|
|
|
end
|
2018-03-12 05:06:44 +08:00
|
|
|
migration_service
|
|
|
|
.create_text_cell(row_id, column.id, field['value'],
|
|
|
|
created_by, last_modified_by)
|
2018-02-27 00:21:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# assign repository item to a tasks
|
|
|
|
assigned_modules = migration_service.get_assigned_sample_module(
|
|
|
|
item['sample_id']
|
|
|
|
)
|
|
|
|
assigned_modules.each do |element|
|
2018-03-12 05:06:44 +08:00
|
|
|
assigned_by = element['assigned_by_id'] || created_by
|
|
|
|
values = [row_id, element['my_module_id'], assigned_by,
|
|
|
|
timestamp, timestamp]
|
|
|
|
cell_sql = <<-SQL
|
|
|
|
INSERT INTO my_module_repository_rows
|
|
|
|
(repository_row_id, my_module_id, assigned_by_id,
|
|
|
|
created_at, updated_at)
|
|
|
|
VALUES (#{values.join(', ')})
|
|
|
|
SQL
|
|
|
|
conn.execute(cell_sql)
|
2018-02-27 00:21:58 +08:00
|
|
|
end
|
|
|
|
end
|
2018-03-12 05:06:44 +08:00
|
|
|
|
|
|
|
# Now update smart annotations
|
|
|
|
migration_service.update_smart_annotations(team, sample_mappings)
|
2018-02-27 00:21:58 +08:00
|
|
|
end
|
2018-02-13 23:52:37 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|