forked from ManageIQ/manageiq-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ManageIQ#469 from agrare/remove_vim_strings_from_n…
…otifications Remove VimString objects from Notification.options
- Loading branch information
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
db/migrate/20200427122455_remove_vim_strings_from_notifications.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 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 |
61 changes: 61 additions & 0 deletions
61
spec/migrations/20200427122455_remove_vim_strings_from_notifications_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,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 |