# Time 2015-02-37 15:03:42
# Author lizanle
# Description 将Issue中的数据导入forge_activities表
class ImportIssueDataToForgeActivities < ActiveRecord::Migration
  def up
    issue_arr = select_all("SELECT\n" +
                                      "	`issues`.`id` AS id,\n" +
                                      "	`issues`.`project_id` AS project_id,\n" +
                                      "	`issues`.`author_id` AS author_id,\n" +
                                      "	`issues`.`created_on` AS created_on,\n" +
                                      "	`issues`.`updated_on` AS updated_on\n" +
                                      "FROM\n" +
                                      "	`issues`\n" +
                                      "LEFT OUTER JOIN `projects` ON `projects`.`id` = `issues`.`project_id`\n" +
                                      "LEFT OUTER JOIN `users` ON `users`.`id` = `issues`.`author_id`\n" +
                                      "AND `users`.`type` IN ('User', 'AnonymousUser')\n" +
                                      "LEFT OUTER JOIN `trackers` ON `trackers`.`id` = `issues`.`tracker_id`\n" +
                                      "WHERE\n" +
                                      "	(\n" +
                                      "			projects. STATUS <> 9\n" +
                                      "			AND projects.id IN (\n" +
                                      "				SELECT\n" +
                                      "					em.project_id\n" +
                                      "				FROM\n" +
                                      "					enabled_modules em\n" +
                                      "				WHERE\n" +
                                      "					em. NAME = 'issue_tracking'\n" +
                                      "		  )\n" +
                                      "	)");
    issue_arr.each do |e|
      ForgeActivity.connection.execute("insert into forge_activities(forge_act_id,
                                                                  forge_act_type,
                                                                  project_id,
                                                                  user_id,
                                                                  created_at,
                                                                  updated_at)
                                     values(#{e["id"]},'#{Issue.to_s}',#{e["project_id"]},#{e["author_id"]},
                                            '#{e["created_on"].to_s.gsub("+0800","").to_datetime.strftime("%Y-%m-%d %H:%M:%S")}',
                                            '#{e["updated_on"].to_s.gsub("+0800","").to_datetime.strftime("%Y-%m-%d %H:%M:%S")}')")
    end
  end

  def down
    ForgeActivity.delete_all([" forge_act_type = ?",ForgeActivity::TYPE_OF_ISSUE_ACT])
  end
end
