# Time 2015-03-11 14:25:45
# Author lizanle
# Description 给每个项目添加一条初始创建信息，如果有menber，最早加进来的就算创始人,并给projects表的更新创建人
class AddProjectCreateInfoToForgeActivities < ActiveRecord::Migration
  def change
    Project.all.each do |e|
      user = e.members.order("members.created_on asc").first
      next if user.nil?
      execute("insert into forge_activities(forge_act_id,
                                                                  forge_act_type,
                                                                  project_id,
                                                                  user_id,
                                                                  created_at,
                                                                  updated_at)
                                     values(#{e.id},'ProjectCreateInfo',#{e.id},#{user.user_id},
                                            '#{user.created_on.to_s.gsub("+0800","").to_datetime.strftime("%Y-%m-%d %H:%M:%S")}',
                                            '#{user.created_on.to_s.gsub("+0800","").to_datetime.strftime("%Y-%m-%d %H:%M:%S")}')")
       execute("update projects set user_id=#{user.user_id} where id =#{e.id}")
    end
  end
end
