Skip to content
Snippets Groups Projects
Unverified Commit 42373e20 authored by Khemissi Amir's avatar Khemissi Amir Committed by GitHub
Browse files

Resources migration: Enabled migration tasks with batch processing. (#4010)

parent 779c15d9
No related branches found
No related tags found
No related merge requests found
...@@ -2,14 +2,17 @@ ...@@ -2,14 +2,17 @@
namespace :migrations do namespace :migrations do
COMMON = { COMMON = {
headers: { "Content-Type" => "application/json" } headers: { "Content-Type" => "application/json" },
batch_size: 1000,
}.freeze }.freeze
desc "Migrates v2 resources to v3" desc "Migrates v2 resources to v3"
task :roles, [] => :environment do |_task, _args| task :roles, [] => :environment do |_task, _args|
has_encountred_issue = 0 has_encountred_issue = 0
Role.select(:id, :name).where.not(name: Role::RESERVED_ROLE_NAMES).each do |r| Role.select(:id, :name)
.where.not(name: Role::RESERVED_ROLE_NAMES)
.find_each(batch_size: COMMON[:batch_size]) do |r|
params = { role: { name: r.name } } params = { role: { name: r.name } }
response = Net::HTTP.post(uri('roles'), payload(params), COMMON[:headers]) response = Net::HTTP.post(uri('roles'), payload(params), COMMON[:headers])
...@@ -17,11 +20,11 @@ namespace :migrations do ...@@ -17,11 +20,11 @@ namespace :migrations do
when Net::HTTPCreated when Net::HTTPCreated
puts green "Succesfully migrated Role:" puts green "Succesfully migrated Role:"
puts cyan " ID: #{r.id}" puts cyan " ID: #{r.id}"
puts cyan " Name: #{r.name}" puts cyan " Name: #{params[:role][:name]}"
else else
puts red "Unable to migrate Role:" puts red "Unable to migrate Role:"
puts yellow " ID: #{r.id}" puts yellow " ID: #{r.id}"
puts yellow " Name: #{r.name}" puts yellow " Name: #{params[:role][:name]}"
has_encountred_issue = 1 # At least one of the migrations failed. has_encountred_issue = 1 # At least one of the migrations failed.
end end
end end
...@@ -32,11 +35,13 @@ namespace :migrations do ...@@ -32,11 +35,13 @@ namespace :migrations do
exit has_encountred_issue exit has_encountred_issue
end end
task :users, [] => :environment do |_task, _args| task :users, [:start, :stop] => :environment do |_task, args|
start, stop = range(args)
has_encountred_issue = 0 has_encountred_issue = 0
# TODO: Optimize this by running in batches. User.select(:id, :uid, :name, :email, :social_uid, :language, :role_id)
User.select(:id, :uid, :name, :email, :social_uid, :language, :role_id).each do |u| .find_each(start: start, finish: stop, batch_size: COMMON[:batch_size]) do |u|
params = { user: { name: u.name, email: u.email, external_id: u.social_uid, language: u.language, role: u.role.name } } params = { user: { name: u.name, email: u.email, external_id: u.social_uid, language: u.language, role: u.role.name } }
response = Net::HTTP.post(uri('users'), payload(params), COMMON[:headers]) response = Net::HTTP.post(uri('users'), payload(params), COMMON[:headers])
...@@ -94,4 +99,16 @@ namespace :migrations do ...@@ -94,4 +99,16 @@ namespace :migrations do
res = { "v2" => { "encrypted_params" => encrypt_params(params) } } res = { "v2" => { "encrypted_params" => encrypt_params(params) } }
res.to_json res.to_json
end end
def range(args)
start = args[:start].to_i
start = 1 unless start.positive?
stop = args[:stop].to_i
stop = nil unless stop.positive?
raise red "Unable to migrate: Invalid provided range [start: #{start}, finish: #{stop}]" if stop && start > stop
[start, stop]
end
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment