Skip to content

Commit

Permalink
AO3-5578 Add a transaction to Psued image migration (otwcode#5018)
Browse files Browse the repository at this point in the history
Add a transaction
  • Loading branch information
zz9pzza authored and slavalamp committed Jan 12, 2025
1 parent c2bcfec commit 19f0849
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions lib/tasks/after_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -363,24 +363,26 @@ namespace :After do

Pseud.no_touching do
Pseud.find_in_batches.with_index do |batch, index|
batch.each do |pseud|
next if pseud.icon_file_name.blank?

image = pseud.icon_file_name
ext = File.extname(image)
image_original = "original#{ext}"

icon_url = if Rails.env.production?
"https://s3.amazonaws.com/otw-ao3-icons/icons/#{pseud.id}/#{image_original}"
else
"https://s3.amazonaws.com/otw-ao3-icons/staging/icons/#{pseud.id}/#{image_original}"
end
begin
pseud.icon.attach(io: URI.parse(icon_url).open,
filename: image_original,
content_type: pseud.icon_content_type)
rescue StandardError => e
puts "Error '#{e}' copying #{icon_url}"
ActiveRecord::Base.transaction do
batch.each do |pseud|
next if pseud.icon_file_name.blank?

image = pseud.icon_file_name
ext = File.extname(image)
image_original = "original#{ext}"

icon_url = if Rails.env.production?
"https://s3.amazonaws.com/otw-ao3-icons/icons/#{pseud.id}/#{image_original}"
else
"https://s3.amazonaws.com/otw-ao3-icons/staging/icons/#{pseud.id}/#{image_original}"
end
begin
pseud.icon.attach(io: URI.parse(icon_url).open,
filename: image_original,
content_type: pseud.icon_content_type)
rescue StandardError => e
puts "Error '#{e}' copying #{icon_url}"
end
end
end

Expand Down

0 comments on commit 19f0849

Please sign in to comment.