require 'csv'

logger = Logger.new($stdout)
affected_files = Dir[File.expand_path(File.dirname(__FILE__)) + "/*upload_{dft,dclg,brac,pins}_publications.csv"]
affected_files.each do |affected_file|
  logger.info "Processing file #{affected_file}"
  csv = CSV.read(affected_file, headers: true)
  csv.each_with_index do |data_row, ix|
    row = Whitehall::Uploader::PublicationRow.new(data_row.to_hash, ix + 1, logger)
    begin
      attachment_urls = (1..50).map { |x| data_row["attachment_#{x}_url"] }.compact
      found, missing = attachment_urls.partition { |url|
        AttachmentSource.find_by_url(url)
      }
      if missing.any?
        if found.any?
          edition = AttachmentSource.find_by_url(url).attachment.editions.last
          logger.warn "Row #{ix + 2} '#{row.legacy_url}' missing attachments (Edition #{edition.id}) - #{missing.inspect}"
        else
          logger.warn "Row #{ix + 2} '#{row.legacy_url}' missing attachments - #{missing.inspect}"
        end
      end
    end
  end
  logger.info "Finished processing file #{affected_file}"
end

Check that publication attachments were actually created.
Check that publication attachments were actually created.

When the publication data migrations were run in production, I
suspect that the code didn't properly enforce the presence of all
attachments - i.e. the publication could be created even if the
attachments were missing.

In order to fix this, we need to know the scale of the problem.
