|
|
|
@ -16,7 +16,12 @@ require_relative 'migration_utils/utils' |
|
|
|
|
class MigrateQueryTrackerReferencesToType < ActiveRecord::Migration |
|
|
|
|
include Migration::Utils |
|
|
|
|
|
|
|
|
|
COLUMNS = ['filters', 'column_names', 'sort_criteria', 'group_by'] |
|
|
|
|
COLUMNS = { |
|
|
|
|
'filters' => { is_text_column: false }, |
|
|
|
|
'column_names' => { is_text_column: false }, |
|
|
|
|
'sort_criteria' => { is_text_column: false }, |
|
|
|
|
'group_by' => { is_text_column: true } |
|
|
|
|
} |
|
|
|
|
KEY = { 'tracker_id' => 'type_id', 'tracker' => 'type' } |
|
|
|
|
|
|
|
|
|
def up |
|
|
|
@ -35,24 +40,13 @@ class MigrateQueryTrackerReferencesToType < ActiveRecord::Migration |
|
|
|
|
|
|
|
|
|
def update_tracker_reference(keys, columns) |
|
|
|
|
Proc.new do |row| |
|
|
|
|
columns.each do |column| |
|
|
|
|
columns.keys.each do |column| |
|
|
|
|
unless row[column].nil? |
|
|
|
|
value = YAML.load row[column] |
|
|
|
|
|
|
|
|
|
if value.is_a? Array |
|
|
|
|
value.collect! do |e| |
|
|
|
|
if e.is_a? Array |
|
|
|
|
e.collect! {|v| keys.has_key?(v) ? keys[v] : v} |
|
|
|
|
else |
|
|
|
|
keys.has_key?(e.to_s) ? keys[e.to_s].to_sym : e |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
elsif value.is_a? Hash |
|
|
|
|
keys.select {|k| value[k.to_s]} |
|
|
|
|
.each_pair {|k, v| value[v] = value.delete k} |
|
|
|
|
if columns[column][:is_text_column] |
|
|
|
|
process_text_data(row, column, keys) |
|
|
|
|
else |
|
|
|
|
row[column] = process_yaml_data(row, column, keys) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
row[column] = YAML.dump value |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
@ -60,10 +54,43 @@ class MigrateQueryTrackerReferencesToType < ActiveRecord::Migration |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def process_yaml_data(row, column, keys) |
|
|
|
|
value = YAML.load row[column] |
|
|
|
|
|
|
|
|
|
if value.is_a? Array |
|
|
|
|
value.collect! do |e| |
|
|
|
|
if e.is_a? Array |
|
|
|
|
e.collect! {|v| keys.has_key?(v) ? keys[v] : v} |
|
|
|
|
else |
|
|
|
|
keys.has_key?(e.to_s) ? keys[e.to_s].to_sym : e |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
elsif value.is_a? Hash |
|
|
|
|
keys.select {|k| value[k.to_s]} |
|
|
|
|
.each_pair {|k, v| value[v] = value.delete k} |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
YAML.dump value |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def process_text_data(row, column, keys) |
|
|
|
|
value = row[column] |
|
|
|
|
|
|
|
|
|
keys.each_key do |k| |
|
|
|
|
regex = Regexp.new(k) |
|
|
|
|
replace = keys[k] |
|
|
|
|
|
|
|
|
|
value.gsub!(regex, replace) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
value |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def update_tracker_references_with_keys(keys) |
|
|
|
|
filter = COLUMNS.map{|c| "#{c} LIKE '%tracker%'"} |
|
|
|
|
filter = COLUMNS.keys |
|
|
|
|
.map{|c| "#{c} LIKE '%tracker%'"} |
|
|
|
|
.join(" OR ") |
|
|
|
|
|
|
|
|
|
update_column_values('queries', COLUMNS, update_tracker_reference(keys, COLUMNS), filter) |
|
|
|
|
update_column_values('queries', COLUMNS.keys, update_tracker_reference(keys, COLUMNS), filter) |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|