<commit_before>namespace :after_party do
  desc 'Deployment task: clone_service_for_transferred_procedures'
  task clone_service_for_transferred_procedures: :environment do
    puts "Running deploy task 'clone_service_for_transferred_procedures'"

    procedures = Procedure.includes(:service).where.not(service_id: nil)
    procedures_to_fix_in_array = procedures.select do |p|
      p.administrateur_id != p.service.administrateur_id
    end
    procedures_to_fix = Procedure.where(id: procedures_to_fix_in_array.map(&:id))

    service_and_admin_list = procedures_to_fix.group(:service_id, :administrateur_id).count.keys
    progress = ProgressReport.new(service_and_admin_list.count)

    service_and_admin_list.each do |service_id, administrateur_id|
      cloned_service = Service.find(service_id).clone_and_assign_to_administrateur(Administrateur.find(administrateur_id))
      cloned_service.save!

      procedures_to_fix
        .where(service_id: service_id, administrateur_id: administrateur_id)
        .update_all(service_id: cloned_service.id)

      progress.inc
    end

    progress.finish

    # Update task as completed.  If you remove the line below, the task will
    # run with every deploy (or every time you call after_party:run).
    AfterParty::TaskRecord.create version: '20181219164523'
  end # task :clone_service_for_transferred_procedures
end # namespace :after_party<commit_msg>Task should not fail if service already exists<commit_after>namespace :after_party do
  desc 'Deployment task: clone_service_for_transferred_procedures'
  task clone_service_for_transferred_procedures: :environment do
    puts "Running deploy task 'clone_service_for_transferred_procedures'"

    procedures = Procedure.includes(:service).where.not(service_id: nil)
    procedures_to_fix_in_array = procedures.select do |p|
      p.administrateur_id != p.service.administrateur_id
    end
    procedures_to_fix = Procedure.where(id: procedures_to_fix_in_array.map(&:id))

    service_and_admin_list = procedures_to_fix.group(:service_id, :administrateur_id).count.keys
    progress = ProgressReport.new(service_and_admin_list.count)

    service_and_admin_list.each do |service_id, administrateur_id|
      cloned_service = Service.find(service_id).clone_and_assign_to_administrateur(Administrateur.find(administrateur_id))

      if cloned_service.save
        puts "Fixing Service #{service_id} for Administrateur #{administrateur_id}"
        procedures_to_fix
          .where(service_id: service_id, administrateur_id: administrateur_id)
          .update_all(service_id: cloned_service.id)
      else
        puts "Cannot fix Service #{service_id} for Administrateur #{administrateur_id}, it should be fixed manually. Errors : #{cloned_service.errors.full_messages}"
      end
      progress.inc
    end

    progress.finish

    # Update task as completed.  If you remove the line below, the task will
    # run with every deploy (or every time you call after_party:run).
    AfterParty::TaskRecord.create version: '20181219164523'
  end # task :clone_service_for_transferred_procedures
end # namespace :after_party
{'fraction_non_alphanumeric': 0.07309288222151293, 'fraction_numerical': 0.00893712097031599, 'mean_word_length': 4.868913857677903, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1796689', 'n_tokens_mistral': 962, 'n_tokens_neox': 963, 'n_words': 198}