class SynchroniseArtefactKindsWithLatestEditionFormats < Mongoid::Migration
  def self.up
    # We don't want to run any update callbacks, just change the column
    Artefact.skip_callback(:update, :after, :update_editions)

    Artefact.each do |artefact|
      latest_edition = artefact.latest_edition
      next if latest_edition.nil?
      next if latest_edition.kind_for_artefact == artefact.kind

      puts "Changing #{artefact.slug} (#{artefact.content_id}) from #{artefact.kind} to #{latest_edition.kind_for_artefact}"
      artefact.update_attribute(:kind, latest_edition.kind_for_artefact)
    end

    # restore the update callback for future migrations
    Artefact.set_callback(:update, :after, :update_editions)
  end

  def self.down
  end
end

Tidy up artefacts whose formats have changed
Tidy up artefacts whose formats have changed

The previous commit makes sure that an Artefact's kind is changed whenever
we change the format of an edition.  This migration finds any artefacts
whose kind no longer matches the format of their latest edition and
changes it so that it does match.
