class AddJournalDataToForgeActivities < ActiveRecord::Migration
  def up
    journal_arr = select_all("SELECT\n" +
                                        "	`journals`.`id` AS id,\n" +
                                        "	`journals`.`created_on` AS created_on,\n" +
                                        "	`projects`.`id` AS project_id,\n" +
                                        "	`issues`.`updated_on` AS updated_on,\n" +
                                        "	`users`.`id` AS author_id\n" +
                                        "FROM\n" +
                                        "	`journals`\n" +
                                        "LEFT OUTER JOIN `issues` ON `issues`.`id` = `journals`.`journalized_id`\n" +
                                        "LEFT OUTER JOIN `projects` ON `projects`.`id` = `issues`.`project_id`\n" +
                                        "LEFT OUTER JOIN `journal_details` ON `journal_details`.`journal_id` = `journals`.`id`\n" +
                                        "LEFT OUTER JOIN `users` ON `users`.`id` = `journals`.`user_id`\n" +
                                        "AND `users`.`type` IN ('User', 'AnonymousUser')\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" +
                                        "	)\n" +
                                        "  AND (\n" +
                                        " 	journals.journalized_type = 'Issue'\n" +
                                        " 	AND (\n" +
                                        "		journal_details.prop_key = 'status_id'\n" +
                                        "		OR journals.notes <> ''\n" +
                                        " 	)\n" +
                                        " )");
    journal_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"]},'#{Journal.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_JOURNAL_ACT])
  end
end
