-
Notifications
You must be signed in to change notification settings - Fork 437
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 #3701 from evanrolfe/refactor/notification_payload…
…_serialisation [api][webui] Update Notification to use JSON serialisation.
- Loading branch information
Showing
11 changed files
with
119 additions
and
18 deletions.
There are no files selected for viewing
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
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
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
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
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
55 changes: 55 additions & 0 deletions
55
src/api/db/data/20170831143534_convert_notifications_event_payload_to_json.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,55 @@ | ||
class ConvertNotificationsEventPayloadToJson < ActiveRecord::Migration[5.1] | ||
def self.up | ||
Notification20170831143534.transaction do | ||
Notification20170831143534.all.find_each do |notification| | ||
json = yaml_to_json(notification.event_payload) | ||
notification.update_attributes!(event_payload: json) | ||
end | ||
end | ||
end | ||
|
||
def self.down | ||
raise ActiveRecord::IrreversibleMigration | ||
end | ||
end | ||
|
||
def yaml_to_json(yaml) | ||
YAML.safe_load(yaml) | ||
.traverse do |value| | ||
if value.is_a? String | ||
value.force_encoding('UTF-8') | ||
else | ||
value | ||
end | ||
end | ||
.to_json | ||
end | ||
|
||
# Notification model only for migration in order to avoid errors coming from the serialization in the actual Notification model | ||
class Notification20170831143534 < ::ApplicationRecord | ||
self.table_name = 'notifications' | ||
self.inheritance_column = :_type_disabled | ||
end | ||
|
||
# Hash extension is used to run force_encoding against each string value in the hash | ||
# in data migration to convert yaml to json serialisation for event payloads | ||
class Hash | ||
def traverse(&block) | ||
traverse_value(self, &block) | ||
end | ||
|
||
private | ||
|
||
def traverse_value(value, &block) | ||
if value.is_a? Hash | ||
value.each { |key, sub_value| value[key] = traverse_value(sub_value, &block) } | ||
|
||
elsif value.is_a? Array | ||
value.map { |element| traverse_value(element, &block) } | ||
|
||
else | ||
yield(value) | ||
|
||
end | ||
end | ||
end |
Empty file.
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
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
21 changes: 21 additions & 0 deletions
21
src/api/spec/db/data/convert_notifications_event_payload_to_json_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,21 @@ | ||
require 'rails_helper' | ||
require Rails.root.join('db/data/20170831143534_convert_notifications_event_payload_to_json.rb') | ||
|
||
RSpec.describe ConvertNotificationsEventPayloadToJson do | ||
describe '.up' do | ||
let!(:yaml) { "---\nhello: world\nhow:\n- are\n- you\n- today?\nim: fine thanks\n" } | ||
let!(:notification) { create(:notification, type: 'Notification::RssFeedItem') } | ||
|
||
before do | ||
sql = "UPDATE `notifications` SET `event_payload` = '#{yaml}' WHERE id = #{notification.id}" | ||
ActiveRecord::Base.connection.execute(sql) | ||
end | ||
|
||
subject! { ConvertNotificationsEventPayloadToJson.up } | ||
|
||
it 'converts the notifications event_payload from yaml to json' do | ||
json_hash = { "hello" => "world", "how" => ["are", "you", "today?"], "im" => "fine thanks"} | ||
expect(Notification.first.event_payload).to eq(json_hash) | ||
end | ||
end | ||
end |
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