2021-08-31 08:43:21 +02:00
|
|
|
# frozen_string_literal: true
|
2021-08-10 14:11:41 +02:00
|
|
|
class SplitCustomWizardLogFields < ActiveRecord::Migration[6.1]
|
|
|
|
def change
|
|
|
|
reversible do |dir|
|
|
|
|
dir.up do
|
2021-08-31 08:43:21 +02:00
|
|
|
# separate wizard/action/user into their own keys
|
2021-08-10 14:11:41 +02:00
|
|
|
|
2021-08-31 08:43:21 +02:00
|
|
|
wizard_logs = PluginStoreRow.where("
|
|
|
|
plugin_name = 'custom_wizard_log'
|
2021-08-10 14:11:41 +02:00
|
|
|
")
|
|
|
|
|
|
|
|
if wizard_logs.exists?
|
|
|
|
wizard_logs.each do |row|
|
|
|
|
begin
|
|
|
|
log_json = JSON.parse(row.value)
|
|
|
|
rescue TypeError, JSON::ParserError
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2021-08-31 08:43:21 +02:00
|
|
|
if log_json.key?('message') && log_json['message'].is_a?(String)
|
2021-08-10 14:11:41 +02:00
|
|
|
|
2021-08-24 14:41:51 +02:00
|
|
|
attr_strs = []
|
|
|
|
|
|
|
|
# assumes no whitespace in the values
|
|
|
|
attr_strs << log_json['message'].slice!(/(wizard: \S*; )/, 1)
|
|
|
|
attr_strs << log_json['message'].slice!(/(action: \S*; )/, 1)
|
|
|
|
attr_strs << log_json['message'].slice!(/(user: \S*; )/, 1)
|
2021-08-10 14:11:41 +02:00
|
|
|
|
|
|
|
attr_strs.each do |attr_str|
|
2021-08-24 14:41:51 +02:00
|
|
|
if attr_str.is_a? String
|
2021-08-31 08:43:21 +02:00
|
|
|
attr_str.gsub!(/[;]/ , "")
|
2021-08-24 14:41:51 +02:00
|
|
|
key, value = attr_str.split(': ')
|
|
|
|
value.strip! if value
|
|
|
|
log_json[key] = value ? value : ''
|
|
|
|
end
|
2021-08-10 14:11:41 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
row.value = log_json.to_json
|
|
|
|
row.save
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
dir.down do
|
|
|
|
wizard_logs = PluginStoreRow.where("
|
2021-08-31 08:43:21 +02:00
|
|
|
plugin_name = 'custom_wizard_log'
|
2021-08-10 14:11:41 +02:00
|
|
|
")
|
|
|
|
|
|
|
|
if wizard_logs.exists?
|
|
|
|
wizard_logs.each do |row|
|
|
|
|
begin
|
|
|
|
log_json = JSON.parse(row.value)
|
|
|
|
rescue TypeError, JSON::ParserError
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
# concatenate wizard/action/user to start of message
|
|
|
|
prefixes = log_json.extract!('wizard', 'action', 'user')
|
|
|
|
|
2021-08-31 08:43:21 +02:00
|
|
|
message_prefix = prefixes.map { |k, v| "#{k}: #{v}" }.join('; ')
|
2021-08-10 14:11:41 +02:00
|
|
|
|
|
|
|
if log_json.key?('message')
|
|
|
|
log_json['message'] = "#{message_prefix}; #{log_json['message']}"
|
|
|
|
else
|
|
|
|
log_json['message'] = message_prefix
|
|
|
|
end
|
|
|
|
|
|
|
|
row.value = log_json.to_json
|
|
|
|
row.save
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|