From 9610d43b85b517952f8cf3920a0ec269e3e24d1d Mon Sep 17 00:00:00 2001 From: Roman Blanco Date: Mon, 13 May 2019 13:40:48 +0000 Subject: [PATCH] Removing deleted buttons 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 --- ...0190509142148_update_custom_button_sets.rb | 21 ++++++++++++++++ ...09142148_update_custom_button_sets_spec.rb | 25 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 db/migrate/20190509142148_update_custom_button_sets.rb create mode 100644 spec/migrations/20190509142148_update_custom_button_sets_spec.rb diff --git a/db/migrate/20190509142148_update_custom_button_sets.rb b/db/migrate/20190509142148_update_custom_button_sets.rb new file mode 100644 index 000000000..2a512cdf3 --- /dev/null +++ b/db/migrate/20190509142148_update_custom_button_sets.rb @@ -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 diff --git a/spec/migrations/20190509142148_update_custom_button_sets_spec.rb b/spec/migrations/20190509142148_update_custom_button_sets_spec.rb new file mode 100644 index 000000000..e38c1ddc8 --- /dev/null +++ b/spec/migrations/20190509142148_update_custom_button_sets_spec.rb @@ -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