  # @TODO Remove this file once the split dates task has run
namespace :split_dates do
  desc "Populate the split dates introduced September 2017"
  task :populate, [:threads] => :environment do |_, args|
    threads = Integer(args.fetch(:threads, 5))
    Tasks::SplitDates.populate_threaded(threads)
  end

  desc "Validate that the dates on editions are correct"
  task validate: :environment do
    Tasks::SplitDates.validate
  end
end

  # @TODO Remove this file once the split dates task has run
namespace :split_dates do
  desc "Populate the split dates introduced September 2017"
  task :populate, [:threads] => :environment do |_, args|
    threads = Integer(args.fetch(:threads, 5))
    Tasks::SplitDates.populate_threaded(threads)
  end

  desc "Validate that the dates on editions are correct"
  task validate: :environment do
    Tasks::SplitDates.validate
  end

  desc "Backfill publishing_api_* dates using temporary_* dates"
  task backfill: :environment do
    scope = Edition.select(:id,
                           :temporary_first_published_at,
                           :publishing_api_first_published_at,
                           :temporary_last_edited_at,
                           :publishing_api_last_edited_at)

    total = Edition.count
    start_time = Time.current
    done = 0

    scope.find_in_batches(batch_size: 10_000) do |batch|
      batch.each do |e|
        if e.temporary_first_published_at != e.publishing_api_first_published_at ||
            e.temporary_last_edited_at != e.publishing_api_last_edited_at

          e.update_columns(
            publishing_api_first_published_at: e.temporary_first_published_at,
            publishing_api_last_edited_at: e.temporary_last_edited_at,
          )
        end
      end

      done += batch.count
      time_elapsed = Time.at(Time.current - start_time).utc.strftime("%H:%M:%S")
      puts "Processed #{done}/#{total} - time elapsed: #{time_elapsed}"
    end
  end
end

Add rake task to backfill publishing_api_* dates
Add rake task to backfill publishing_api_* dates

This task takes all the edition timestamps that are prefixed with
`temporary_` and backfills their equivalent prefixed with
`publishing_api_`.

This is to allow switching to the `publishing_api_*` dates and work
towards the removal of the `temporary_*` ones.

On my dev VM this took 1:43 to run.
