class ImportAttachmentsDataToForgeActivities < ActiveRecord::Migration
  def up
  attach_arr = select_all("SELECT\n" +
                           "  attachments.author_id as author_id,\n" +
                           "	attachments.created_on as created_on,\n" +
                           "	attachments.id as id,\n" +
                           "	projects.id as project_id\n" +
                           "FROM\n" +
                           "	`attachments`\n" +
                           "LEFT JOIN versions ON attachments.container_type = 'Version'\n" +
                           "AND versions.id = attachments.container_id\n" +
                           "LEFT JOIN projects ON versions.project_id = projects.id\n" +
                           "OR (\n" +
                           "	attachments.container_type = 'Project'\n" +
                           "	AND attachments.container_id = projects.id\n" +
                           ")\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 = 'files'\n" +
                           "			)");
  attach_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"]},'#{Attachment.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["created_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_ATTACHMENT_ACT])
end
end
