class ModifyCourseGroupForCourses < ActiveRecord::Migration[5.2]
  def change
    course_ids = [1135, 1208, 1309, 1528, 1563, 1800, 2025, 2086, 2346, 2915, 2917, 2919, 2954, 3064, 3136, 3387, 3423,
                  3429, 3467, 3651, 3704, 3732]
    groups = CourseGroup.where(course_id: course_ids).order("created_at desc").group(:name)
    ActiveRecord::Base.transaction do
      groups.each do |g|
        CourseGroup.where(name: g.name).where.not(id: g.id).each do |cg|
          cg.course_members.each do |cm|
            cm.update_attributes!(course_group_id: g.id)
          end
          cg.exercise_group_settings.update_all(course_group_id: g.id)
          cg.attachment_group_settings.update_all(course_group_id: g.id)
          cg.homework_group_reviews.update_all(course_group_id: g.id)
          cg.homework_group_settings.update_all(course_group_id: g.id)
          #cg.teacher_course_groups.update_all(course_group_id: g.id)
          cg.destroy!
        end
      end
    end
  end
end
