From 6af81e09a2409345d5bde1bf3e0a252333b267fb Mon Sep 17 00:00:00 2001 From: Brandon Dunne Date: Thu, 18 Jan 2018 15:12:22 -0500 Subject: [PATCH] Handle cases where a Picture with that id doesn't exist https://bugzilla.redhat.com/show_bug.cgi?id=1532286 --- ...1162442_move_pictures_blobs_to_pictures.rb | 2 +- ...42_move_pictures_blobs_to_pictures_spec.rb | 26 ++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/db/migrate/20180111162442_move_pictures_blobs_to_pictures.rb b/db/migrate/20180111162442_move_pictures_blobs_to_pictures.rb index 3099be565..e75a5cae4 100644 --- a/db/migrate/20180111162442_move_pictures_blobs_to_pictures.rb +++ b/db/migrate/20180111162442_move_pictures_blobs_to_pictures.rb @@ -18,7 +18,7 @@ def up say_with_time("Moving picture content from BinaryBlobs to the pictures table") do BinaryBlob.in_my_region.includes(:binary_blob_parts).where(:resource_type => "Picture").find_each do |blob| - Picture.update(blob.resource_id, :content => blob.data, :extension => blob.data_type, :md5 => blob.md5) if blob.resource_id + Picture.find_by(:id => blob.resource_id).try(:update_attributes!, :content => blob.data, :extension => blob.data_type, :md5 => blob.md5) blob.destroy end end diff --git a/spec/migrations/20180111162442_move_pictures_blobs_to_pictures_spec.rb b/spec/migrations/20180111162442_move_pictures_blobs_to_pictures_spec.rb index 7c94ae811..11756a216 100644 --- a/spec/migrations/20180111162442_move_pictures_blobs_to_pictures_spec.rb +++ b/spec/migrations/20180111162442_move_pictures_blobs_to_pictures_spec.rb @@ -6,8 +6,32 @@ let(:picture_stub) { migration_stub :Picture } migration_context :up do - it 'Destroys picture blobs without a picture' do + it 'Destroys picture blobs with an invalid picture id' do + picture = picture_stub.create! blob = binary_blob_stub.create!( + :data_type => "png", + :md5 => "ce114e4501d2f4e2dcea3e17b546f339", + :part_size => 1.megabyte, + :resource_id => picture.id + 42, + :resource_type => "Picture", + :size => 14, + ) + binary_blob_part_stub.create!( + :binary_blob_id => blob.id, + :data => "This is a test", + :md5 => "ce114e4501d2f4e2dcea3e17b546f339", + :size => 14, + ) + + migrate + + expect(picture_stub.count).to eq(1) + expect(binary_blob_stub.count).to eq(0) + expect(binary_blob_part_stub.count).to eq(0) + end + + it 'Destroys picture blobs with a nil picture id' do + blob = binary_blob_stub.create!( :data_type => "png", :md5 => "ce114e4501d2f4e2dcea3e17b546f339", :part_size => 1.megabyte,