-
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.
[api] Handle database data changes with a gem.
Use the data-migrate gem to handle changes to data with their own set of migrations just like we handle changes to the database structure. This gives us a set structure to update existing data in the database and gives us a single command to run (rake db:migrate:with_data) in deployments.
- Loading branch information
Showing
11 changed files
with
91 additions
and
81 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
8 changes: 4 additions & 4 deletions
8
src/api/spec/lib/tasks/databases_spec.rb → ...tifications_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
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