diff --git a/db/migrate/20191204030230_migrate_exercise_single_question_score.rb b/db/migrate/20191204030230_migrate_exercise_single_question_score.rb index 3988ab3f5..a69e8ecef 100644 --- a/db/migrate/20191204030230_migrate_exercise_single_question_score.rb +++ b/db/migrate/20191204030230_migrate_exercise_single_question_score.rb @@ -1,5 +1,12 @@ class MigrateExerciseSingleQuestionScore < ActiveRecord::Migration[5.2] def change + # 删除判断、单选题中生成多条记录,但exercise_choice_id不同的数据 + sql = %Q(delete from exercise_answers where (exercise_question_id, user_id) in + (select * from (select exercise_question_id, user_id from exercise_answers join exercise_questions eq on exercise_question_id = eq.id where eq.question_type in (0, 2) group by exercise_question_id, user_id, exercise_choice_id having count(*) > 1) a) + and id not in (select * from (select min(exercise_answers.id) from exercise_answers join exercise_questions eq on exercise_question_id = eq.id where eq.question_type in (0, 2) group by exercise_question_id, user_id having count(*) > 1 order by exercise_answers.id) b)) + ActiveRecord::Base.connection.execute sql + + # 更新成绩 exercise_answers = ExerciseAnswer.joins(:exercise_question).where("exercise_answers.created_at > '2019-11-01 00:00:00'").where(score: -1, exercise_questions: {question_type: [0, 2]}) exercise_answers.includes(:exercise_choice, exercise_question: :exercise_standard_answers).find_each do |answer| diff --git a/db/migrate/20191204062733_add_temp_migration.rb b/db/migrate/20191204062733_add_temp_migration.rb new file mode 100644 index 000000000..f8692c070 --- /dev/null +++ b/db/migrate/20191204062733_add_temp_migration.rb @@ -0,0 +1,10 @@ +class AddTempMigration < ActiveRecord::Migration[5.2] + def change + # 删除判断、单选题中生成多条记录,但exercise_choice_id不同的数据 + sql = %Q(delete from exercise_answers where (exercise_question_id, user_id) in + (select * from (select exercise_question_id, user_id from exercise_answers join exercise_questions eq on exercise_question_id = eq.id where eq.question_type in (0, 2) group by exercise_question_id, user_id, exercise_choice_id having count(*) > 1) a) + and id not in (select * from (select min(exercise_answers.id) from exercise_answers join exercise_questions eq on exercise_question_id = eq.id where eq.question_type in (0, 2) group by exercise_question_id, user_id having count(*) > 1 order by exercise_answers.id) b)) + ActiveRecord::Base.connection.execute sql + + end +end