-
Notifications
You must be signed in to change notification settings - Fork 125
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 EmsRefresh.refresh queue args to data #107
Merged
Fryguy
merged 1 commit into
ManageIQ:master
from
agrare:migrate_ems_refresh_refresh_queue_args_to_data
Oct 26, 2017
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
db/migrate/20171025122732_move_ems_refresh_args_to_data.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class MoveEmsRefreshArgsToData < ActiveRecord::Migration[5.0] | ||
class MiqQueue < ActiveRecord::Base | ||
serialize :args, Array | ||
end | ||
|
||
def up | ||
say_with_time('Move MiqQueue refresh args to data') do | ||
MiqQueue.where(:class_name => 'EmsRefresh', :method_name => 'refresh').each do |queue_item| | ||
begin | ||
targets = queue_item.args.first | ||
data = Marshal.dump(targets) unless targets.nil? | ||
|
||
queue_item.update_attributes(:msg_data => data, :args => []) | ||
rescue | ||
# If Marshal.load fails we want to delete the queue item | ||
queue_item.delete | ||
end | ||
end | ||
end | ||
end | ||
|
||
def down | ||
say_with_time('Move MiqQueue refresh data to args') do | ||
MiqQueue.where(:class_name => 'EmsRefresh', :method_name => 'refresh').each do |queue_item| | ||
begin | ||
args = queue_item.msg_data && Marshal.load(queue_item.msg_data) | ||
|
||
queue_item.update_attributes(:args => args, :msg_data => nil) | ||
rescue | ||
# If Marshal.load fails we want to delete the queue item | ||
queue_item.delete | ||
end | ||
end | ||
end | ||
end | ||
end |
88 changes: 88 additions & 0 deletions
88
spec/migrations/20171025122732_move_ems_refresh_args_to_data_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
require_migration | ||
|
||
describe MoveEmsRefreshArgsToData do | ||
let(:miq_queue_stub) { migration_stub(:MiqQueue) } | ||
let(:targets) { [['Vm', 1], ['Host', 2]] } | ||
let(:refresh_queue_options) do | ||
{ | ||
:class_name => 'EmsRefresh', | ||
:method_name => 'refresh', | ||
:role => 'ems_inventory', | ||
:queue_name => 'ems_1', | ||
:zone => 'default', | ||
} | ||
end | ||
let(:refresh_new_target_queue_options) do | ||
{ | ||
:class_name => 'EmsRefresh', | ||
:method_name => 'refresh_new_target', | ||
:role => 'ems_inventory', | ||
:queue_name => 'ems_1', | ||
:zone => 'default', | ||
} | ||
end | ||
|
||
migration_context :up do | ||
it "Moves EmsRefresh.refresh queue args to data" do | ||
q_item = miq_queue_stub.create!(refresh_queue_options.merge(:args => [targets])) | ||
|
||
migrate | ||
|
||
expect(Marshal.load(q_item.reload.msg_data)).to match_array(targets) | ||
end | ||
|
||
it "Leaves EmsRefresh.refresh args empty" do | ||
q_item = miq_queue_stub.create!(refresh_queue_options.merge(:args => [targets])) | ||
|
||
migrate | ||
|
||
expect(q_item.reload.args).to match_array([]) | ||
end | ||
|
||
it "Ignores unrelated queue items" do | ||
args = [{:ems_ref => "vm-123"}] | ||
q_item = miq_queue_stub.create!(refresh_new_target_queue_options.merge(:args => args)) | ||
|
||
migrate | ||
|
||
expect(q_item.reload.args).to match_array(args) | ||
end | ||
end | ||
|
||
migration_context :down do | ||
it "Moves EmsRefresh.refresh data to args" do | ||
q_item = miq_queue_stub.create!(refresh_queue_options.merge(:msg_data => Marshal.dump(targets))) | ||
|
||
migrate | ||
|
||
expect(q_item.reload.args).to match_array(targets) | ||
end | ||
|
||
it "If there are no args" do | ||
q_item = miq_queue_stub.create!(refresh_queue_options.merge(:msg_data => nil)) | ||
|
||
migrate | ||
|
||
expect(q_item.reload.args).to match_array([]) | ||
end | ||
|
||
it "Handle invalid marshal format errors" do | ||
# "\x03\x00\x00" is an empty array in Marshal version 3.0 | ||
q_item = miq_queue_stub.create!(refresh_queue_options.merge(:msg_data => "\x03\x00\x00")) | ||
|
||
migrate | ||
|
||
expect { q_item.reload }.to raise_exception(ActiveRecord::RecordNotFound) | ||
end | ||
|
||
it "Deletes invalid queue items but migrates the rest" do | ||
# "\x03\x00\x00" is an empty array in Marshal version 3.0 | ||
miq_queue_stub.create!(refresh_queue_options.merge(:msg_data => "\x03\x00\x00")) | ||
q_item = miq_queue_stub.create!(refresh_queue_options.merge(:msg_data => Marshal.dump(targets))) | ||
|
||
migrate | ||
|
||
expect(q_item.reload.args).to match_array(targets) | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a version of this for the up migration?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't sure how to test
Marshal.dump
failing due to a version issue, ideas?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me try putting an anonymous class in the args, that should do it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scratch that, that won't even serializing the value to args
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see...it's only the load part that fails. 👍