Skip to content

Commit

Permalink
Removing deleted buttons
Browse files Browse the repository at this point in the history
after manageiq/pull/18368 the purpose of `set_data[:button_order]`
has been changed, and an error is raised after going through an array,
where the button does not exist anymore
  • Loading branch information
romanblanco committed May 14, 2019
1 parent 07d8380 commit 9610d43
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
21 changes: 21 additions & 0 deletions db/migrate/20190509142148_update_custom_button_sets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class UpdateCustomButtonSets < ActiveRecord::Migration[5.2]
class MiqSet < ActiveRecord::Base
serialize :set_data
end

class CustomButton < ActiveRecord::Base; end

def up
say_with_time("Removing deleted buttons from Custom Button Sets") do
MiqSet.select(:id, :set_data).where(:set_type => 'CustomButtonSet').each do |cbs|
next if cbs.set_data[:button_order].blank?
existing_buttons = CustomButton
.where(:id => cbs.set_data[:button_order])
.order("position(id::text in '#{cbs.set_data[:button_order].join(',')}')")
.ids
cbs.set_data[:button_order] = existing_buttons
cbs.save!
end
end
end
end
25 changes: 25 additions & 0 deletions spec/migrations/20190509142148_update_custom_button_sets_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require_migration

describe UpdateCustomButtonSets do
let(:custom_button_set_stub) { migration_stub :MiqSet }
let(:custom_button_stub) { migration_stub :CustomButton }

migration_context :up do
it 'Removes non-existing buttons while keeping the buttons sorted' do
cb1 = custom_button_stub.create!(:id => 1)
cb2 = custom_button_stub.create!(:id => 2)
cb4 = custom_button_stub.create!(:id => 4)
cb5 = custom_button_stub.create!(:id => 5)
cb7 = custom_button_stub.create!(:id => 7)
cbs = custom_button_set_stub.create!(
:set_type => 'CustomButtonSet',
:set_data => { :button_order => [
cb1.id, cb7.id, cb2.id, cb4.id, 6, cb5.id, 3]})

migrate

cbs.reload
expect(cbs.set_data[:button_order]).to eql([cb1.id, cb7.id, cb2.id, cb4.id, cb5.id])
end
end
end

0 comments on commit 9610d43

Please sign in to comment.