class UpdateUserActivities < ActiveRecord::Migration
  def up
    count = UserActivity.all.count / 30 + 2
    transaction do
      for i in 1 ... count do i
      UserActivity.page(i).per(30).each do |activity|
        if activity.container_type.to_s == 'Project'
          forge_activity = ForgeActivity.where("forge_act_type = '#{activity.act_type.to_s}' and forge_act_id = #{activity.act_id}").first
          if forge_activity
            activity.user_id = forge_activity.user_id
          else
            activity.user_id = 0
          end

        elsif activity.container_type.to_s == 'Course'
          course_activity = CourseActivity.where("course_act_type = '#{activity.act_type.to_s}' and course_act_id = #{activity.act_id}").first
          if course_activity
            activity.user_id = course_activity.user_id
          else
            activity.user_id = 0
          end
        end
        activity.save
      end
      end
    end
  end

  def down
    count = UserActivity.all.count / 30 + 2
    transaction do
      for i in 1 ... count do i
      UserActivity.page(i).per(30).each do |activity|
        activity.user_id = nil
        activity.save
      end
      end
    end
  end
end
