From 64bc58436b02d9bb1c0e94da01aea16220716e43 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Wed, 4 Dec 2019 11:46:03 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E8=AF=95=E5=8D=B7=E6=88=90=E7=BB=A9?= =?UTF-8?q?=E8=BF=81=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0_migrate_exercise_single_question_score.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 db/migrate/20191204030230_migrate_exercise_single_question_score.rb diff --git a/db/migrate/20191204030230_migrate_exercise_single_question_score.rb b/db/migrate/20191204030230_migrate_exercise_single_question_score.rb new file mode 100644 index 000000000..04f438daa --- /dev/null +++ b/db/migrate/20191204030230_migrate_exercise_single_question_score.rb @@ -0,0 +1,18 @@ +class MigrateExerciseSingleQuestionScore < ActiveRecord::Migration[5.2] + def change + 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| + + question = answer.exercise_question + exercise_user = ExerciseUser.find_by(exercise_id: question.exercise_id, user_id: answer.user_id) + user_choice_position = answer.exercise_choice&.choice_position + + if user_choice_position && (user_choice_position.to_i == question.exercise_standard_answers.take&.exercise_choice_id.to_i) && exercise_user + answer.update!(score: question.question_score) + score = exercise_user.score + question.question_score + objective_score = exercise_user.objective_score + question.question_score + exercise_user.update!(score: score, objective_score: objective_score) + end + end + end +end From 7552d596037a0b58202412065f3c64bb9cd4987c Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Wed, 4 Dec 2019 11:50:07 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E8=AF=95=E5=8D=B7=E6=88=90=E7=BB=A9?= =?UTF-8?q?=E8=BF=81=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...204030230_migrate_exercise_single_question_score.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/db/migrate/20191204030230_migrate_exercise_single_question_score.rb b/db/migrate/20191204030230_migrate_exercise_single_question_score.rb index 04f438daa..463bbc8ee 100644 --- a/db/migrate/20191204030230_migrate_exercise_single_question_score.rb +++ b/db/migrate/20191204030230_migrate_exercise_single_question_score.rb @@ -7,11 +7,13 @@ class MigrateExerciseSingleQuestionScore < ActiveRecord::Migration[5.2] exercise_user = ExerciseUser.find_by(exercise_id: question.exercise_id, user_id: answer.user_id) user_choice_position = answer.exercise_choice&.choice_position - if user_choice_position && (user_choice_position.to_i == question.exercise_standard_answers.take&.exercise_choice_id.to_i) && exercise_user + if user_choice_position && (user_choice_position.to_i == question.exercise_standard_answers.take&.exercise_choice_id.to_i) answer.update!(score: question.question_score) - score = exercise_user.score + question.question_score - objective_score = exercise_user.objective_score + question.question_score - exercise_user.update!(score: score, objective_score: objective_score) + if exercise_user && exercise_user.commit_status == 1 + score = exercise_user.score + question.question_score + objective_score = exercise_user.objective_score + question.question_score + exercise_user.update!(score: score, objective_score: objective_score) + end end end end From f8cf185ce1fd71863bc0e16346b7eb20926203a4 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Wed, 4 Dec 2019 11:51:10 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E8=AF=95=E5=8D=B7=E6=88=90=E7=BB=A9?= =?UTF-8?q?=E8=BF=81=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...191204030230_migrate_exercise_single_question_score.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/db/migrate/20191204030230_migrate_exercise_single_question_score.rb b/db/migrate/20191204030230_migrate_exercise_single_question_score.rb index 463bbc8ee..3988ab3f5 100644 --- a/db/migrate/20191204030230_migrate_exercise_single_question_score.rb +++ b/db/migrate/20191204030230_migrate_exercise_single_question_score.rb @@ -5,11 +5,11 @@ class MigrateExerciseSingleQuestionScore < ActiveRecord::Migration[5.2] question = answer.exercise_question exercise_user = ExerciseUser.find_by(exercise_id: question.exercise_id, user_id: answer.user_id) - user_choice_position = answer.exercise_choice&.choice_position - if user_choice_position && (user_choice_position.to_i == question.exercise_standard_answers.take&.exercise_choice_id.to_i) - answer.update!(score: question.question_score) - if exercise_user && exercise_user.commit_status == 1 + if exercise_user && exercise_user.commit_status == 1 + user_choice_position = answer.exercise_choice&.choice_position + if user_choice_position && (user_choice_position.to_i == question.exercise_standard_answers.take&.exercise_choice_id.to_i) + answer.update!(score: question.question_score) score = exercise_user.score + question.question_score objective_score = exercise_user.objective_score + question.question_score exercise_user.update!(score: score, objective_score: objective_score) From 4633bbbb31ee050c82c27aee0c7a500445602472 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Wed, 4 Dec 2019 14:28:29 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E5=88=A0=E9=99=A4=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...204030230_migrate_exercise_single_question_score.rb | 7 +++++++ db/migrate/20191204062733_add_temp_migration.rb | 10 ++++++++++ 2 files changed, 17 insertions(+) create mode 100644 db/migrate/20191204062733_add_temp_migration.rb 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 From a4e0cd9f33bb36fce68cab003c88665dfedc3caf Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Wed, 4 Dec 2019 14:32:44 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E5=88=A0=E9=99=A4=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...204030230_migrate_exercise_single_question_score.rb | 2 +- db/migrate/20191204062733_add_temp_migration.rb | 2 +- .../20191204063225_add_exercise_temp_migration.rb | 10 ++++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20191204063225_add_exercise_temp_migration.rb diff --git a/db/migrate/20191204030230_migrate_exercise_single_question_score.rb b/db/migrate/20191204030230_migrate_exercise_single_question_score.rb index a69e8ecef..ddb9a2794 100644 --- a/db/migrate/20191204030230_migrate_exercise_single_question_score.rb +++ b/db/migrate/20191204030230_migrate_exercise_single_question_score.rb @@ -2,7 +2,7 @@ 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) + (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 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 diff --git a/db/migrate/20191204062733_add_temp_migration.rb b/db/migrate/20191204062733_add_temp_migration.rb index f8692c070..691e5e5ca 100644 --- a/db/migrate/20191204062733_add_temp_migration.rb +++ b/db/migrate/20191204062733_add_temp_migration.rb @@ -2,7 +2,7 @@ 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) + (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 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 diff --git a/db/migrate/20191204063225_add_exercise_temp_migration.rb b/db/migrate/20191204063225_add_exercise_temp_migration.rb new file mode 100644 index 000000000..7f9428911 --- /dev/null +++ b/db/migrate/20191204063225_add_exercise_temp_migration.rb @@ -0,0 +1,10 @@ +class AddExerciseTempMigration < 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 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 From 93560f3067775caf2e01b5072f5e7e1269c675cf Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Wed, 4 Dec 2019 14:45:18 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E5=88=A0=E9=99=A4=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...204030230_migrate_exercise_single_question_score.rb | 2 +- db/migrate/20191204062733_add_temp_migration.rb | 10 ---------- .../20191204063225_add_exercise_temp_migration.rb | 10 ---------- 3 files changed, 1 insertion(+), 21 deletions(-) delete mode 100644 db/migrate/20191204062733_add_temp_migration.rb delete mode 100644 db/migrate/20191204063225_add_exercise_temp_migration.rb diff --git a/db/migrate/20191204030230_migrate_exercise_single_question_score.rb b/db/migrate/20191204030230_migrate_exercise_single_question_score.rb index ddb9a2794..c71fea157 100644 --- a/db/migrate/20191204030230_migrate_exercise_single_question_score.rb +++ b/db/migrate/20191204030230_migrate_exercise_single_question_score.rb @@ -7,7 +7,7 @@ class MigrateExerciseSingleQuestionScore < ActiveRecord::Migration[5.2] 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 = ExerciseAnswer.joins(:exercise_question).where(score: -1, exercise_questions: {question_type: [0, 2]}) exercise_answers.includes(:exercise_choice, exercise_question: :exercise_standard_answers).find_each do |answer| question = answer.exercise_question diff --git a/db/migrate/20191204062733_add_temp_migration.rb b/db/migrate/20191204062733_add_temp_migration.rb deleted file mode 100644 index 691e5e5ca..000000000 --- a/db/migrate/20191204062733_add_temp_migration.rb +++ /dev/null @@ -1,10 +0,0 @@ -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 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 diff --git a/db/migrate/20191204063225_add_exercise_temp_migration.rb b/db/migrate/20191204063225_add_exercise_temp_migration.rb deleted file mode 100644 index 7f9428911..000000000 --- a/db/migrate/20191204063225_add_exercise_temp_migration.rb +++ /dev/null @@ -1,10 +0,0 @@ -class AddExerciseTempMigration < 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 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