class SetPositionsForExistingInterviewQuestions < ActiveRecord::Migration[7.0]
  def up
    InterviewQuestion.transaction do
      # Get unique respondent_category_ids
      category_ids = InterviewQuestion.distinct.pluck(:respondent_category_id)

      category_ids.each do |category_id|
        InterviewQuestion.where(respondent_category_id: category_id)
                         .order(created_at: :asc)
                         .each_with_index do |question, index|
          question.update_column(:position, index + 1)
        end
      end
    end
  end

  def down
    InterviewQuestion.update_all(position: nil)
  end
end
