Skip to content

Commit

Permalink
Fix correct state for assets that have gone in a weird invalid state …
Browse files Browse the repository at this point in the history
…over the years

"deleted" should no longer be a valid state. patching any data that we come across that has a wrong state.
  • Loading branch information
minhngocd authored and lauraghiorghisor-tw committed Dec 4, 2024
1 parent be6d3b3 commit b262113
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/tasks/assets.rake
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ namespace :assets do
end

def delete_and_update_draft asset
asset.state = "uploaded" if asset.state.to_s == "deleted"
asset.destroy! unless asset.deleted?

asset.parent_document_url = nil if asset.parent_document_url&.include?("draft-origin")
asset.draft = false
asset.save!
Expand Down
30 changes: 30 additions & 0 deletions spec/lib/tasks/assets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@
expect(replacement.reload.parent_document_url).to eq nil
end

it "deletes the asset replacement and fixes invalid upload state if the replacement is not deleted" do
replacement = FactoryBot.create(:asset, draft: true, deleted_at: nil)
FactoryBot.create(:asset, id: asset_id, replacement_id: replacement.id)
replacement.state = "deleted"
replacement.save(validate: false)

expected_output = <<~OUTPUT
Asset ID: #{asset_id} - OK. Draft replacement #{replacement.id} deleted and updated to false.
OUTPUT
expect { task.invoke(filepath) }.to output(expected_output).to_stdout
expect(replacement.reload.deleted_at).not_to be_nil
expect(replacement.reload.draft).to be false
expect(replacement.reload.state).to eq "uploaded"
end

it "only updates the draft state if the asset is already deleted (asset is a replacement)" do
asset = FactoryBot.create(:asset, id: asset_id, draft: true, deleted_at: Time.zone.now)
FactoryBot.create(:asset, replacement_id: asset.id)
Expand Down Expand Up @@ -130,6 +145,21 @@
expect(asset.reload.parent_document_url).to eq nil
end

it "deletes the asset and fixes invalid upload state if the asset is not deleted (asset is a replacement)" do
asset = FactoryBot.create(:asset, id: asset_id, draft: true, deleted_at: nil)
FactoryBot.create(:asset, replacement_id: asset.id)
asset.state = "deleted"
asset.save(validate: false)

expected_output = <<~OUTPUT
Asset ID: #{asset_id} - is a replacement. Asset deleted and updated to false.
OUTPUT
expect { task.invoke(filepath) }.to output(expected_output).to_stdout
expect(asset.reload.deleted_at).not_to be_nil
expect(asset.reload.draft).to be false
expect(asset.reload.state).to eq "uploaded"
end

it "skips the asset if asset is itself redirected (and not replaced by draft or itself a replacement)" do
FactoryBot.create(:asset, id: asset_id, draft: false, redirect_url: "https://example.com")

Expand Down

0 comments on commit b262113

Please sign in to comment.