class UpdateVulnerabilityStatuses < ActiveRecord::Migration
  def up
    Result.where("(results.metadata #>> '{vulnerabilities}' != '')").each do |result|
      vulnerabilities = result.metadata.try(:[],"vulnerabilities")
      if(vulnerabilities.present?)
        vulnerabilities.each do |v|
          if(v["status"].to_s.downcase.include?("open") || v["status"].to_s.downcase == "new")
            if(v["jira_ids"].present?)
              v["status"] = "Ticketed"
            else
              v["status"] = "New"
            end
          end
        end
      end
      result.save if result.changed?
    end
  end

  def down
    Result.where("(results.metadata #>> '{vulnerabilities}' != '')").find_each(batch_size: 100) do |result|
      vulnerabilities = result.metadata.try(:[],"vulnerabilities")
      if(vulnerabilities.present?)
        vulnerabilities.each do |v|
          if(Vulnerability.open_statuses.include?(v["status"].to_s.downcase))
            if(v["jira_ids"].present?)
              v["status"] = "Open"
            end
          end
        end
      end
      result.save if result.changed?
    end
  end
end
