From 0e711b563fe28068a34cc893c48ac81f5240b6c6 Mon Sep 17 00:00:00 2001 From: Jason Frey Date: Thu, 18 Jan 2018 16:37:55 -0500 Subject: [PATCH] Merge pull request #159 from bdunne/more_pictures_edge_cases Handle cases where a Picture with that id doesn't exist (cherry picked from commit 40c5ced8ebbbdf72c86759b8d504c673076244b9) https://bugzilla.redhat.com/show_bug.cgi?id=1536046 --- ...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..d16f5f536 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.where(:id => blob.resource_id).update_all(:content => blob.data, :extension => blob.data_type, :md5 => blob.md5) if blob.resource_id 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,