From 6227ee65680a735400ca1bb72374ceb2d41a2e89 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Tue, 3 Dec 2019 21:03:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=81=E7=A7=BB=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...20191203124938_add_index_to_poll_answer.rb | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/db/migrate/20191203124938_add_index_to_poll_answer.rb b/db/migrate/20191203124938_add_index_to_poll_answer.rb index 2360bc485..0969e2784 100644 --- a/db/migrate/20191203124938_add_index_to_poll_answer.rb +++ b/db/migrate/20191203124938_add_index_to_poll_answer.rb @@ -1,17 +1,17 @@ class AddIndexToPollAnswer < ActiveRecord::Migration[5.2] def change - # remove_index :poll_votes, :poll_question_id - # remove_index :poll_votes, :user_id - # remove_index :poll_votes, :poll_answer_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 + remove_index :poll_votes, :poll_question_id + remove_index :poll_votes, :user_id + remove_index :poll_votes, :poll_answer_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 end end