Skip to content

Commit

Permalink
Add check and log for already processed assets
Browse files Browse the repository at this point in the history
We want to know if we are skipping assets because they have already been fixed,
so that we store a correct account of what's been changed.
  • Loading branch information
lauraghiorghisor-tw committed Dec 5, 2024
1 parent 1b1297e commit 4202eee
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/tasks/assets.rake
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ namespace :assets do
desc "Fix assets and draft replacements"
task :fix_assets_and_draft_replacements, %i[csv_path] => :environment do |_t, args|
csv_path = args.fetch(:csv_path)

processed_asset_ids = {}

process_file_in_memory(csv_path) do |row|
original_asset_id = row[0]
original_asset = Asset.where(_id: original_asset_id)&.first
Expand All @@ -67,9 +70,15 @@ namespace :assets do

replacement_asset = original_asset.replacement

if replacement_asset && processed_asset_ids[replacement_asset.id.to_s]
puts "Asset ID: #{original_asset_id} - PROCESSED. Replacement #{replacement_asset.id} already processed."
next
end

if replacement_asset && replacement_asset.replacement.nil? && replacement_asset.draft?
begin
delete_and_update_draft(replacement_asset)
processed_asset_ids[replacement_asset.id.to_s] = true
puts "Asset ID: #{original_asset_id} - OK. Draft replacement #{replacement_asset.id} deleted and updated to false."
rescue StandardError
puts "Asset ID: #{original_asset_id} - ERROR. Asset replacement failed to save. Error: #{replacement_asset.errors.full_messages}."
Expand All @@ -80,20 +89,27 @@ namespace :assets do
if is_replacement && replacement_asset.nil? && original_asset.draft?
begin
delete_and_update_draft(original_asset)
processed_asset_ids[original_asset_id] = true
puts "Asset ID: #{original_asset_id} - OK. Asset is a replacement. Asset deleted and updated to false."
rescue StandardError
puts "Asset ID: #{original_asset_id} - ERROR. Asset failed to save. Error: #{original_asset.errors.full_messages}."
end
next
end

if processed_asset_ids[original_asset_id]
puts "Asset ID: #{original_asset_id} - PROCESSED. Asset already processed."
next
end

if original_asset.draft? || original_asset.deleted? || replacement_asset || original_asset.redirect_url
puts "Asset ID: #{original_asset_id} - SKIPPED. Asset is draft (#{original_asset.draft?}), deleted (#{original_asset.deleted?}), replaced (#{!replacement_asset.nil?}), or redirected (#{!original_asset.redirect_url.nil?})."
next
end

begin
delete_and_update_draft(original_asset, should_update_draft: false)
processed_asset_ids[original_asset_id] = true
puts "Asset ID: #{original_asset_id} - OK. Asset has been deleted."
rescue StandardError
puts "Asset ID: #{original_asset_id} - ERROR. Asset failed to save. Error: #{original_asset.errors.full_messages}."
Expand Down
39 changes: 39 additions & 0 deletions spec/lib/tasks/assets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,45 @@
expect { task.invoke(filepath) }.to output(expected_output).to_stdout
end

it "skips already processed replacement assets" do
file.open
csv_file = <<~CSV
6592008029c8c3e4dc76256c
6592008029c8c3e4dc76256d
CSV
file.write(csv_file)
file.close

replacement = FactoryBot.create(:asset, draft: true, deleted_at: nil, replacement_id: nil)
FactoryBot.create(:asset, id: "6592008029c8c3e4dc76256c", replacement_id: replacement.id)
FactoryBot.create(:asset, id: "6592008029c8c3e4dc76256d", replacement_id: replacement.id)

expected_output = <<~OUTPUT
Asset ID: 6592008029c8c3e4dc76256c - OK. Draft replacement #{replacement.id} deleted and updated to false.
Asset ID: 6592008029c8c3e4dc76256d - PROCESSED. Replacement #{replacement.id} already processed.
OUTPUT
expect { task.invoke(filepath) }.to output(expected_output).to_stdout
end

it "skips already processed assets" do
file.open
csv_file = <<~CSV
6592008029c8c3e4dc76256c
6592008029c8c3e4dc76256d
CSV
file.write(csv_file)
file.close

replacement = FactoryBot.create(:asset, id: "6592008029c8c3e4dc76256d", draft: true, deleted_at: nil, replacement_id: nil)
FactoryBot.create(:asset, id: "6592008029c8c3e4dc76256c", replacement_id: replacement.id)

expected_output = <<~OUTPUT
Asset ID: 6592008029c8c3e4dc76256c - OK. Draft replacement #{replacement.id} deleted and updated to false.
Asset ID: 6592008029c8c3e4dc76256d - PROCESSED. Asset already processed.
OUTPUT
expect { task.invoke(filepath) }.to output(expected_output).to_stdout
end

it "only updates the draft state of the replacement if the asset replacement is already deleted" do
replacement = FactoryBot.create(:asset, draft: true, deleted_at: Time.zone.now, replacement_id: nil)
FactoryBot.create(:asset, id: asset_id, replacement_id: replacement.id)
Expand Down

0 comments on commit 4202eee

Please sign in to comment.