class RemoveQuestionSets < ActiveRecord::Migration[7.0]
  def change
    question_set_ids = InterviewQuestion.where(respondent_category_id: nil).distinct.select(:question_set_id).pluck(:question_set_id)
    QuestionSet.where.not(id: question_set_ids).destroy_all
    QuestionSet.where(id: question_set_ids).each do |qs|
      rc = qs.project.respondent_categories.first
      qs.interview_questions.update_all(question_set_id: nil, respondent_category_id: rc)
    end
    drop_table :question_sets
    remove_column :interview_questions, :question_set_id
  end
end
