You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/db/migrate/20191203040140_add_uniq_ind...

17 lines
913 B

5 years ago
class AddUniqIndexToPollVotes < ActiveRecord::Migration[5.2]
def change
5 years ago
# remove_index :poll_votes, column: [:poll_question_id, :user_id]
#
# change_column_default :poll_votes, :poll_question_id, from: nil, to: -1
# PollVote.where(poll_answer_id: nil).update_all(poll_answer_id: -1)
#
# sql = %Q(delete from poll_votes where (poll_question_id, user_id, poll_answer_id) in
# (select * from (select poll_question_id, user_id, poll_answer_id from poll_votes group by poll_question_id, user_id, poll_answer_id having count(*) > 1) a)
# and id not in (select * from (select min(id) from poll_votes group by poll_question_id, user_id, poll_answer_id having count(*) > 1 order by id) b))
# ActiveRecord::Base.connection.execute sql
#
# add_index :poll_votes, [:poll_question_id, :user_id, :poll_answer_id], name: 'poll_answer_index', unique: true
5 years ago
end
end