2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-11-14 20:47:53 +00:00
|
|
|
class CorrectDedicationContacts < ActiveRecord::Migration[4.2]
|
2018-10-04 20:29:30 +00:00
|
|
|
def up
|
|
|
|
json_dedications = Qx.select('id', 'dedication').from(:donations)
|
2019-07-30 21:29:24 +00:00
|
|
|
.where("dedication IS NOT NULL AND dedication != ''")
|
|
|
|
.and_where('is_valid_json(dedication)').ex
|
|
|
|
parsed_dedications = json_dedications.map { |i| { id: i['id'], dedication: JSON.parse(i['dedication']) } }
|
|
|
|
with_contact_to_correct = parsed_dedications.select { |i| !i[:dedication]['contact'].blank? && i[:dedication]['contact'].is_a?(String) }
|
|
|
|
really_icky_dedications, easy_to_split_strings = with_contact_to_correct.partition { |i| i[:dedication]['contact'].split(' - ').count > 3 }
|
|
|
|
|
|
|
|
easy_to_split_strings.map do |i|
|
|
|
|
split_contact = i[:dedication]['contact'].split(' - ')
|
|
|
|
i[:dedication]['contact'] = {
|
|
|
|
email: split_contact[0],
|
|
|
|
phone: split_contact[1],
|
|
|
|
address: split_contact[2]
|
|
|
|
}
|
|
|
|
puts i
|
|
|
|
i
|
|
|
|
end.each_with_index do |i, index|
|
|
|
|
unless i[:id]
|
|
|
|
raise Error("Item at index #{index} is invalid. Object:#{i}")
|
|
|
|
end
|
|
|
|
|
|
|
|
Qx.update(:donations).where('id = $id', id: i[:id]).set(dedication: JSON.generate(i[:dedication])).ex
|
|
|
|
end
|
2018-10-04 20:29:30 +00:00
|
|
|
|
|
|
|
puts "Corrected #{easy_to_split_strings.count} records."
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
puts ''
|
|
|
|
puts ''
|
|
|
|
puts 'You must manually fix the following dedications: '
|
2018-10-04 20:29:30 +00:00
|
|
|
really_icky_dedications.each do |i|
|
|
|
|
puts i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
json_dedications = Qx.select('id', 'dedication').from(:donations)
|
2019-07-30 21:29:24 +00:00
|
|
|
.where("dedication IS NOT NULL AND dedication != ''")
|
|
|
|
.and_where('is_valid_json(dedication)').ex
|
2018-10-04 20:29:30 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
parsed_dedications = json_dedications.map { |i| { 'id' => i['id'], 'dedication' => JSON.parse(i['dedication']) } }
|
2018-10-04 20:29:30 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
with_contact_to_correct = parsed_dedications.select { |i| i['dedication']['contact'].is_a?(Hash) }
|
2018-10-04 20:29:30 +00:00
|
|
|
|
|
|
|
puts "#{with_contact_to_correct.count} to revert"
|
|
|
|
with_contact_to_correct.each do |i|
|
|
|
|
contact_string = "#{i['dedication']['contact']['email']} - #{i['dedication']['contact']['phone']} - #{i['dedication']['contact']['address']}"
|
|
|
|
i['dedication']['contact'] = contact_string
|
2019-07-30 21:29:24 +00:00
|
|
|
Qx.update(:donations).where('id = $id', id: i['id']).set(dedication: JSON.generate(i['dedication'])).ex
|
2018-10-04 20:29:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|