class ModifyCollaIndex < ActiveRecord::Migration[5.2] def change remove_index :exercise_users, [:user_id, :exercise_id] if index_exists?(:exercise_users, [:user_id, :exercise_id]) remove_index :exercise_users, [:exercise_id, :user_id] if index_exists?(:exercise_users, [:exercise_id, :user_id]) sql = %Q(delete from exercise_users where (user_id, exercise_id) in (select * from (select user_id, exercise_id from exercise_users group by user_id, exercise_id having count(*) > 1) a) and id not in (select * from (select min(id) from exercise_users group by user_id, exercise_id having count(*) > 1 order by id) b)) ActiveRecord::Base.connection.execute sql add_index :exercise_users, [:exercise_id, :user_id], unique: true, name: "index_on_exercise_id_user_id" remove_index :exercise_shixun_answers, :user_id remove_index :student_works, [:user_id, :homework_common_id] if index_exists?(:student_works, [:user_id, :homework_common_id]) remove_index :student_works, [:homework_common_id, :user_id] if index_exists?(:student_works, [:homework_common_id, :user_id]) sql = %Q(delete from student_works where (user_id, homework_common_id) in (select * from (select user_id, homework_common_id from student_works group by user_id, homework_common_id having count(*) > 1) a) and id not in (select * from (select min(id) from student_works group by user_id, homework_common_id having count(*) > 1 order by id) b)) ActiveRecord::Base.connection.execute sql add_index :student_works, [:homework_common_id, :user_id], unique: true, name: "index_on_homework_common_id_user_id" remove_index :poll_users, [:user_id, :poll_id] if index_exists?(:poll_users, [:user_id, :poll_id]) remove_index :poll_users, [:poll_id, :user_id] if index_exists?(:poll_users, [:poll_id, :user_id]) sql = %Q(delete from poll_users where (user_id, poll_id) in (select * from (select user_id, poll_id from poll_users group by user_id, poll_id having count(*) > 1) a) and id not in (select * from (select min(id) from poll_users group by user_id, poll_id having count(*) > 1 order by id) b)) ActiveRecord::Base.connection.execute sql add_index :poll_users, [:poll_id, :user_id], unique: true, name: "index_poll_id_and_user_id" remove_index :graduation_works, [:user_id, :graduation_task_id] if index_exists?(:graduation_works, [:user_id, :graduation_task_id]) remove_index :graduation_works, [:graduation_task_id, :user_id] if index_exists?(:graduation_works, [:graduation_task_id, :user_id]) sql = %Q(delete from graduation_works where (user_id, graduation_task_id) in (select * from (select user_id, graduation_task_id from graduation_works group by user_id, graduation_task_id having count(*) > 1) a) and id not in (select * from (select min(id) from graduation_works group by user_id, graduation_task_id having count(*) > 1 order by id) b)) ActiveRecord::Base.connection.execute sql add_index :graduation_works, [:graduation_task_id, :user_id], unique: true, name: "index_graduation_task_id_and_user_id" remove_index :course_members, [:user_id, :course_id, :role] if index_exists?(:course_members, [:user_id, :course_id, :role]) remove_index :course_members, :course_id if index_exists?(:course_members, :course_id) remove_index :course_members, :user_id if index_exists?(:course_members, :user_id) sql = %Q(delete from course_members where (course_id, user_id, role) in (select * from (select course_id, user_id, role from course_members group by course_id, user_id, role having count(*) > 1) a) and id not in (select * from (select min(id) from course_members group by course_id, user_id, role having count(*) > 1 order by id) b)) ActiveRecord::Base.connection.execute sql add_index :course_members, [:course_id, :user_id, :role], unique: true, name: "index_course_id_user_id_role" end end