Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Picture content from BinaryBlobs to Pictures table #153

Merged
merged 1 commit into from
Jan 17, 2018

Conversation

bdunne
Copy link
Member

@bdunne bdunne commented Jan 11, 2018

@miq-bot miq-bot added the wip label Jan 11, 2018
@bdunne bdunne changed the title [WIP] Migrate Picture content from BinaryBlobs to Pictures table Migrate Picture content from BinaryBlobs to Pictures table Jan 11, 2018
@bdunne
Copy link
Member Author

bdunne commented Jan 11, 2018

@miq-bot add_labels core enhancement gaprindashvili/yes

@miq-bot
Copy link
Member

miq-bot commented Jan 11, 2018

@bdunne Cannot apply the following label because they are not recognized: core enhancement gaprindashvili/yes

@miq-bot miq-bot removed the wip label Jan 11, 2018
@@ -0,0 +1,51 @@
class MovePicturesBlobsToPictures < ActiveRecord::Migration[5.0]
class BinaryBlob < ActiveRecord::Base
has_many :binary_blob_parts, -> { order(:id) }, :dependent => :delete_all
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs the :class designation to ensure it uses the stub.

class MovePicturesBlobsToPictures < ActiveRecord::Migration[5.0]
class BinaryBlob < ActiveRecord::Base
has_many :binary_blob_parts, -> { order(:id) }, :dependent => :delete_all
belongs_to :resource, :polymorphic => true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need this.

add_column :pictures, :md5, :string

say_with_time("Moving picture content from BinaryBlobs to the pictures table") do
BinaryBlob.where(:resource_type => "Picture").all.each do |blob|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change this to a find_each just to ensure we batch it properly just in case there are lot of these.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add a .includes(:binary_blob_parts) to avoid an N+1?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also also do you need in_my_region? Even though they are not replicated, someone might have enabled that.

Picture.in_my_region.all.each do |picture|
size = picture.content.try(:length).to_i

BinaryBlob.create!(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't create the binaryblob if the size is 0. Though that seems like a real edge case.

@miq-bot
Copy link
Member

miq-bot commented Jan 16, 2018

Checked commit bdunne@84cd462 with ruby 2.3.3, rubocop 0.52.0, haml-lint 0.20.0, and yamllint 1.10.0
2 files checked, 0 offenses detected
Everything looks fine. 🍪

@Fryguy Fryguy merged commit 91b0fc0 into ManageIQ:master Jan 17, 2018
@Fryguy Fryguy added this to the Sprint 78 Ending Jan 29, 2018 milestone Jan 17, 2018
@bdunne bdunne deleted the move_pictures branch January 17, 2018 21:59
@himdel
Copy link
Contributor

himdel commented Jan 18, 2018

== 20180111162442 MovePicturesBlobsToPictures: migrating ======================          
-- add_column(:pictures, :content, :binary)                                              
   -> 0.0045s                                                                            
-- add_column(:pictures, :extension, :string)
   -> 0.0005s
-- add_column(:pictures, :md5, :string)
   -> 0.0010s
-- Moving picture content from BinaryBlobs to the pictures table
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

Couldn't find MovePicturesBlobsToPictures::Picture without an ID
bin/rails:4:in `require'
bin/rails:4:in `<main>'
ActiveRecord::RecordNotFound: Couldn't find MovePicturesBlobsToPictures::Picture without an ID
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate                                                                 
(See full trace by running task with --trace)                                            
                                                                                         
== Command ["bin/rails db:migrate"] failed in /home/himdel/manageiq ==

@himdel
Copy link
Contributor

himdel commented Jan 18, 2018

happens during up, blob looks like this:

#<MovePicturesBlobsToPictures::BinaryBlob id: 10000000014757, resource_type: "Picture", resource_id: nil, md5: "7d4b8a5fbbdfdd85da991e00e22e1545", size: 14853, part_size: 1048576, name: nil, data_type: "jpeg">
#<MovePicturesBlobsToPictures::BinaryBlob id: 10000000014758, resource_type: "Picture", resource_id: nil, md5: "fe95acfc63996cc4610c83273308b692", size: 17648, part_size: 1048576, name: nil, data_type: "jpeg">

(just these 2 fail in my db)

Not sure if there can be other links to the blobs and these are still used or if this is an aritfact of something not getting deleted and we should just drop these..?

@Fryguy
Copy link
Member

Fryguy commented Jan 18, 2018

How weird that you have nil pictures...that should not be possible, unless the original table was dependent => nullify?

@himdel
Copy link
Contributor

himdel commented Jan 18, 2018

It could theoretically have to do with updating GenericObjectDefintion via the API to remove the picture.

Looks like it does something along the lines of god.update_attributes!(:picture => nil).
But I'm not really sure if it's a problem still present in the current implementation or if this only happened during review.

@simaishi
Copy link
Contributor

I'm holding off backporting to Gaprindashvili. Let me know if/when it's ok to backport.

@Fryguy
Copy link
Member

Fryguy commented Jan 18, 2018

@jntullo ?

@Fryguy
Copy link
Member

Fryguy commented Jan 18, 2018

@bdunne Can you update the PR to "deal with" resource_id being nil? However, separately, we should not be putting the DB in this state, so let's also fix whatever bug is causing this to happen.

@jntullo
Copy link

jntullo commented Jan 18, 2018

@Fryguy will look into that - also look into other seemingly related spec failures in the API.

@bdunne
Copy link
Member Author

bdunne commented Jan 18, 2018

@simaishi I think this is all set for backport with #158

simaishi pushed a commit that referenced this pull request Jan 18, 2018
Migrate Picture content from BinaryBlobs to Pictures table
(cherry picked from commit 91b0fc0)

https://bugzilla.redhat.com/show_bug.cgi?id=1536046
@simaishi
Copy link
Contributor

Gaprindashvili backport details:

$ git log -1
commit 6faac0b77f8d0ca90015ad6be847fcd52f4987e0
Author: Jason Frey <[email protected]>
Date:   Wed Jan 17 16:21:22 2018 -0500

    Merge pull request #153 from bdunne/move_pictures
    
    Migrate Picture content from BinaryBlobs to Pictures table
    (cherry picked from commit 91b0fc0430a7ebe601960be693ffeddf2ce842b2)
    
    https://bugzilla.redhat.com/show_bug.cgi?id=1536046

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants