chromesetting
cxt 5 years ago
parent 977bcb3d4a
commit 76102a1200

@ -1,16 +1,16 @@
class AddIndexToExerciseChoice < ActiveRecord::Migration[5.2]
def change
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
remove_index :exercise_answers, name: :index_on_question_id_user_id
# 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
#
# remove_index :exercise_answers, name: :index_on_question_id_user_id
end
end

@ -1,15 +1,15 @@
class AddIndexToPollAnswer < 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

Loading…
Cancel
Save