<commit_before>class CopyIsbnFromPublicationToAttachment < ActiveRecord::Migration
  def up
    Document.where(document_type: 'Publication').each do |publication|
      # Get the published edition of this publication
      published_edition = publication.published_edition

      # Only proceed if there's a published edition with an ISBN, and at least one attachment that we can move it to
      if published_edition && published_edition.isbn.present? && published_edition.attachments.any?

        # If there's more than one attachment then print the edition ID as we'll need to check whether we've copied
        # the ISBN to the correct attachment
        if published_edition.attachments.length > 1
          puts "*NOTE* Edition #{published_edition.id} has multiple attachments.  Manually check that we've copied the ISBN to the correct one."
        end

        first_attachment = published_edition.attachments.order(:created_at).first

        # Fail fast if we're trying to overwrite an existing ISBN with something different
        if first_attachment.isbn.present? && first_attachment.isbn != published_edition.isbn
          raise "The ISBN on the attachment is different from the one on the publication.  Aborting."
        else
          first_attachment.update_attribute(:isbn, published_edition.isbn)
        end
      end
    end
  end

  def down
    # Intentionally blank
  end
end
<commit_msg>Update the migration that copies ISBNs to attachments.<commit_after>class CopyIsbnFromPublicationToAttachment < ActiveRecord::Migration
  def up
    Document.where(document_type: 'Publication').each do |publication|
      # Get the published edition of this publication
      published_edition = publication.published_edition

      # Only proceed if there's a published edition with an ISBN
      if published_edition && published_edition.isbn.present?

        # If we don't have an attachment to copy the ISBN to then print the edition ID and
        # ISBN so that we don't lose potentially useful data when we remove the ISBN
        # from the editions table.
        if published_edition.attachments.empty?
          puts "*NOTE* Edition #{published_edition.id} has an ISBN (#{published_edition.isbn}) but no attachments to copy it to."
        else

          # If there's more than one attachment then print the edition ID as we'll need to check whether we've copied
          # the ISBN to the correct attachment
          if published_edition.attachments.length > 1
            puts "*NOTE* Edition #{published_edition.id} has multiple attachments.  Manually check that we've copied the ISBN to the correct one."
          end

          first_attachment = published_edition.attachments.order(:created_at).first

          # Fail fast if we're trying to overwrite an existing ISBN with something different
          if first_attachment.isbn.present? && first_attachment.isbn != published_edition.isbn
            raise "The ISBN on the attachment is different from the one on the publication.  Aborting."
          else
            first_attachment.update_attribute(:isbn, published_edition.isbn)
          end
        end
      end
    end
  end

  def down
    # Intentionally blank
  end
end

{'fraction_non_alphanumeric': 0.05493482309124767, 'fraction_numerical': 0.0006207324643078833, 'mean_word_length': 2.998759305210918, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6648359', 'n_tokens_mistral': 825, 'n_tokens_neox': 809, 'n_words': 323}