From caf9a0bb150e6aa0a02dbc538b5c48145b320825 Mon Sep 17 00:00:00 2001
From: cxt <853663049@qq.com>
Date: Tue, 3 Dec 2019 20:27:03 +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

---
 ...32557_add_uniq_index_to_exercise_answer.rb | 12 +++++-----
 ...1203040140_add_uniq_index_to_poll_votes.rb | 22 +++++++++----------
 ...203122643_add_index_to_exercise_answers.rb |  5 +++++
 3 files changed, 22 insertions(+), 17 deletions(-)
 create mode 100644 db/migrate/20191203122643_add_index_to_exercise_answers.rb

diff --git a/db/migrate/20191203032557_add_uniq_index_to_exercise_answer.rb b/db/migrate/20191203032557_add_uniq_index_to_exercise_answer.rb
index b46ff0e34..8104d336a 100644
--- a/db/migrate/20191203032557_add_uniq_index_to_exercise_answer.rb
+++ b/db/migrate/20191203032557_add_uniq_index_to_exercise_answer.rb
@@ -5,12 +5,12 @@ class AddUniqIndexToExerciseAnswer < ActiveRecord::Migration[5.2]
     # change_column_default :exercise_answers, :exercise_choice_id, from: nil, to: -1
     # ExerciseAnswer.where(exercise_choice_id: nil).update_all(exercise_choice_id: -1)
 
-    sql = %Q(delete from exercise_answers where (exercise_question_id, user_id, exercise_choice_id) in
-            (select * from (select exercise_question_id, user_id, exercise_choice_id from exercise_answers group by exercise_question_id, user_id, exercise_choice_id having count(*) > 1) a)
-            and id not in (select * from (select min(id) from exercise_answers group by exercise_question_id, user_id, exercise_choice_id having count(*) > 1 order by id) b))
-    ActiveRecord::Base.connection.execute sql
-
-    add_index :exercise_answers, [:exercise_question_id, :user_id, :exercise_choice_id], name: 'exercise_choice_index', unique: true
+    # sql = %Q(delete from exercise_answers where (exercise_question_id, user_id, exercise_choice_id) in
+    #         (select * from (select exercise_question_id, user_id, exercise_choice_id from exercise_answers group by exercise_question_id, user_id, exercise_choice_id having count(*) > 1) a)
+    #         and id not in (select * from (select min(id) from exercise_answers group by exercise_question_id, user_id, exercise_choice_id having count(*) > 1 order by id) b))
+    # ActiveRecord::Base.connection.execute sql
+    #
+    # add_index :exercise_answers, [:exercise_question_id, :user_id, :exercise_choice_id], name: 'exercise_choice_index', unique: true
 
 
   end
diff --git a/db/migrate/20191203040140_add_uniq_index_to_poll_votes.rb b/db/migrate/20191203040140_add_uniq_index_to_poll_votes.rb
index cdf94647a..971e74b9e 100644
--- a/db/migrate/20191203040140_add_uniq_index_to_poll_votes.rb
+++ b/db/migrate/20191203040140_add_uniq_index_to_poll_votes.rb
@@ -1,16 +1,16 @@
 class AddUniqIndexToPollVotes < ActiveRecord::Migration[5.2]
   def change
-    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
+    # 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
 
   end
 end
diff --git a/db/migrate/20191203122643_add_index_to_exercise_answers.rb b/db/migrate/20191203122643_add_index_to_exercise_answers.rb
new file mode 100644
index 000000000..e05b0ee52
--- /dev/null
+++ b/db/migrate/20191203122643_add_index_to_exercise_answers.rb
@@ -0,0 +1,5 @@
+class AddIndexToExerciseAnswers < ActiveRecord::Migration[5.2]
+  def change
+    add_index :exercise_answers, column: [:exercise_question_id, :user_id]
+  end
+end