class AlterCourseContributorScoreDefault < ActiveRecord::Migration
  def up
    change_column :course_contributor_scores, :message_num, :integer, :default => 0
    change_column :course_contributor_scores, :message_reply_num, :integer, :default => 0
    change_column :course_contributor_scores, :news_reply_num, :integer, :default => 0
    change_column :course_contributor_scores, :news_num, :integer, :default => 0
    change_column :course_contributor_scores, :resource_num, :integer, :default => 0
    change_column :course_contributor_scores, :journal_num, :integer, :default => 0
    change_column :course_contributor_scores, :journal_reply_num, :integer, :default => 0
    change_column :course_contributor_scores, :homework_journal_num, :integer, :default => 0
    change_column :course_contributor_scores, :total_score, :integer, :default => 0

    count = CourseContributorScore.all.count / 30 + 2
    transaction do
      for i in 1 ... count do i
        CourseContributorScore.page(i).per(30).each do |cs|
          if cs.message_num.nil?
            cs.message_num = 0
          end
          if cs.message_reply_num.nil?
            cs.message_reply_num = 0
          end
          if cs.news_reply_num.nil?
            cs.news_reply_num = 0
          end
          if cs.news_num.nil?
            cs.news_num = 0
          end
          if cs.resource_num.nil?
            cs.resource_num = 0
          end
          if cs.journal_num.nil?
            cs.journal_num = 0
          end
          if cs.journal_reply_num.nil?
            cs.journal_reply_num = 0
          end
          if cs.homework_journal_num.nil?
            cs.homework_journal_num = 0
          end
          if cs.total_score.nil?
            cs.total_score = 0
          end
          cs.save
        end
      end
    end
  end

  def down
  end
end
