discourse/plugins/poll/db/migrate/20151016163051_merge_polls_...

21 lines
617 B
Ruby
Raw Normal View History

class MergePollsVotes < ActiveRecord::Migration
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]
2017-07-27 21:20:09 -04:00
polls_votes["#{user_id}"] = ::JSON.parse(pcf.value || "{}")
end
pcf = PostCustomField.find_or_create_by(name: "polls-votes", post_id: post_id)
2017-07-27 21:20:09 -04:00
pcf.value = ::JSON.parse(pcf.value || "{}").merge(polls_votes).to_json
pcf.save
end
end
def down
end
end