class ImportDocumentDataToForgeActivities < ActiveRecord::Migration
  def up
    doc_arr = select_all("SELECT\n" +
                             "	`documents`.`id` AS id,\n" +
                             "	`documents`.`project_id` AS project_id,\n" +
                             "	`documents`.`created_on` AS created_on,\n" +
                             "	`documents`.`user_id` AS author_id\n" +
                             "FROM\n" +
                             "	`documents`\n" +
                             "LEFT OUTER JOIN `projects` ON `projects`.`id` = `documents`.`project_id`\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 = 'documents'\n" +
                             ")");
    doc_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"]},'#{Document.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_DOCUMENT_ACT])
  end
end
