Skip to content

Commit

Permalink
Merge pull request ManageIQ#469 from agrare/remove_vim_strings_from_n…
Browse files Browse the repository at this point in the history
…otifications

Remove VimString objects from Notification.options
  • Loading branch information
Fryguy authored Apr 27, 2020
2 parents 1cab5a1 + 2ce3d0f commit 981ff8d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
36 changes: 36 additions & 0 deletions db/migrate/20200427122455_remove_vim_strings_from_notifications.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class RemoveVimStringsFromNotifications < ActiveRecord::Migration[5.1]
disable_ddl_transaction!
include MigrationHelper

class Notification < ActiveRecord::Base
include ActiveRecord::IdRegions
end

def up
say_with_time("Removing VimStrings from Notifications") do
base_relation = Notification.in_my_region.where("options LIKE ?", "%ruby/string:VimString%")
say_batch_started(base_relation.size)

loop do
count = base_relation.limit(50_000).update_all("options = REGEXP_REPLACE(options, '!ruby/string:VimString', '!ruby/string:String', 'g')")
break if count == 0

say_batch_processed(count)
end
end
end

def down
say_with_time("Restoring VimStrings in Notifications") do
base_relation = Notification.in_my_region.where("options LIKE ?", "%ruby/string:String%")
say_batch_started(base_relation.size)

loop do
count = base_relation.limit(50_000).update_all("options = REGEXP_REPLACE(options, '!ruby/string:String', '!ruby/string:VimString', 'g')")
break if count == 0

say_batch_processed(count)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require_migration

class RemoveVimStringsFromNotifications
class NotificationType < ActiveRecord::Base
end
end

describe RemoveVimStringsFromNotifications do
let(:notification_stub) { migration_stub(:Notification) }
let(:notification_type_stub) { migration_stub(:NotificationType) }

migration_context :up do
it "converts VimStrings to Strings" do
options = <<~OPTIONS
---
:error: !ruby/string:VimString
str: Another task is already in progress.
xsiType: :SOAP::SOAPString
vimType:
:snapshot_op: remove
:subject: sapm-db2a
OPTIONS

notification_type = notification_type_stub.find_or_create_by!(:name => "vm_snapshot_failure")
notification = notification_stub.create!(
:notification_type_id => notification_type.id,
:options => options
)

migrate

options = YAML.load(notification.reload.options)
expect(options[:error].class).to eq(String)
end
end

migration_context :down do
it "restores String to VimString" do
options = <<~OPTIONS
---
:error: !ruby/string:String
str: Another task is already in progress.
xsiType: :SOAP::SOAPString
vimType:
:snapshot_op: remove
:subject: sapm-db2a
OPTIONS

notification_type = notification_type_stub.find_or_create_by!(:name => "vm_snapshot_failure")
notification = notification_stub.create!(
:notification_type_id => notification_type.id,
:options => options
)

migrate

options = notification.reload.options
expect(options).to include("ruby/string:VimString")
end
end
end

0 comments on commit 981ff8d

Please sign in to comment.