|
|
|
@ -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,8 +40,21 @@ 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? |
|
|
|
|
if columns[column][:is_text_column] |
|
|
|
|
process_text_data(row, column, keys) |
|
|
|
|
else |
|
|
|
|
row[column] = process_yaml_data(row, column, keys) |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
UpdateResult.new(row, true) |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def process_yaml_data(row, column, keys) |
|
|
|
|
value = YAML.load row[column] |
|
|
|
|
|
|
|
|
|
if value.is_a? Array |
|
|
|
@ -52,18 +70,27 @@ class MigrateQueryTrackerReferencesToType < ActiveRecord::Migration |
|
|
|
|
.each_pair {|k, v| value[v] = value.delete k} |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
row[column] = YAML.dump value |
|
|
|
|
end |
|
|
|
|
YAML.dump value |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
UpdateResult.new(row, true) |
|
|
|
|
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 |
|
|
|
|