class ImportMessageDataToForgeActivities < ActiveRecord::Migration
  def up
    issue_arr = select_all("SELECT\n" +
                               "	`messages`.`id` AS id,\n" +
                               "	`messages`.`author_id` AS author_id,\n" +
                               "	`projects`.`id` AS project_id,\n" +
                               "	`messages`.`created_on` AS created_on,\n" +
                               "  `messages`.`updated_on` AS updated_on\n" +
                               "FROM\n" +
                               "	`messages`\n" +
                               "LEFT OUTER JOIN `boards` ON `boards`.`id` = `messages`.`board_id`\n" +
                               "LEFT OUTER JOIN `projects` ON `projects`.`id` = `boards`.`project_id`\n" +
                               "LEFT OUTER JOIN `users` ON `users`.`id` = `messages`.`author_id`\n" +
                               "AND `users`.`type` IN ('User', 'AnonymousUser')\n" +
                               "WHERE\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 = 'boards'\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"]},'#{Message.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_MESSAGE_ACT])
  end
end
