commit
						4a9e5fc85f
					
				| @ -0,0 +1,17 @@ | |||||||
|  | class AddChoiceIndexToExerciseAnswers < ActiveRecord::Migration[5.2] | ||||||
|  |   def change | ||||||
|  |     add_column :exercise_answers, :choice_index, :integer, default: 1 | ||||||
|  | 
 | ||||||
|  |     multi_questions = ExerciseQuestion.where(question_type: 1) | ||||||
|  |     multi_questions.includes(:exercise_choices, :exercise_answers).find_each do |question| | ||||||
|  |       exercise_answers = question.exercise_answers | ||||||
|  |       exercise_answers.find_each do |answer| | ||||||
|  |         choice_index = question.exercise_choices.pluck(:id).index(answer.exercise_choice_id).to_i + 1 | ||||||
|  |         answer.update_column('choice_index', choice_index) | ||||||
|  |       end | ||||||
|  |       puts "multi_questions: #{question.id}" | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|  |     ExerciseAnswer.joins(:exercise_question).where(exercise_questions: {question_type: 3}).update_all("choice_index = exercise_choice_id") | ||||||
|  |   end | ||||||
|  | end | ||||||
| @ -0,0 +1,13 @@ | |||||||
|  | class AddChoiceIndexUniqIndexToExerciseAnswers < ActiveRecord::Migration[5.2] | ||||||
|  |   def change | ||||||
|  |     sql = %Q(delete from exercise_answers where (exercise_question_id, user_id, choice_index) in | ||||||
|  |             (select * from (select exercise_question_id, user_id, choice_index from exercise_answers group by exercise_question_id, user_id, choice_index having count(*) > 1) a) | ||||||
|  |             and id not in (select * from (select max(id) from exercise_answers group by exercise_question_id, user_id, choice_index having count(*) > 1 order by id) b)) | ||||||
|  |     ActiveRecord::Base.connection.execute sql | ||||||
|  | 
 | ||||||
|  |     add_index :exercise_answers, [:exercise_question_id, :user_id, :choice_index], name: 'exercise_user_choice_index', unique: true | ||||||
|  | 
 | ||||||
|  |     remove_index :exercise_answers, name: :exercise_choice_index | ||||||
|  | 
 | ||||||
|  |   end | ||||||
|  | end | ||||||
					Loading…
					
					
				
		Reference in new issue