2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
class MergePollsVotes < ActiveRecord::Migration[4.2]
|
2015-10-16 23:18:22 +08:00
|
|
|
def up
|
|
|
|
PostCustomField
|
|
|
|
.where(name: "polls")
|
|
|
|
.order(:post_id)
|
|
|
|
.pluck(:post_id)
|
|
|
|
.each do |post_id|
|
|
|
|
polls_votes = {}
|
|
|
|
PostCustomField
|
|
|
|
.where(post_id: post_id)
|
|
|
|
.where("name LIKE 'polls-votes-%'")
|
|
|
|
.find_each do |pcf|
|
|
|
|
user_id = pcf.name["polls-votes-".size..-1]
|
2015-10-17 01:54:41 +08:00
|
|
|
polls_votes["#{user_id}"] = ::JSON.parse(pcf.value || "{}")
|
2015-10-16 23:18:22 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
pcf = PostCustomField.find_or_create_by(name: "polls-votes", post_id: post_id)
|
2015-10-17 01:54:41 +08:00
|
|
|
pcf.value = ::JSON.parse(pcf.value || "{}").merge(polls_votes).to_json
|
2015-10-16 23:18:22 +08:00
|
|
|
pcf.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
end
|
|
|
|
end
|