<commit_before>namespace :db do
  desc 'Fix world records after times have been deleted.'
  task fix_world_records: :environment do
    Score.distinct(:map).pluck(:map).each do |map|
      (0..3).each do |mode|
        s = Score.where(map: map, mode: mode).order(:time, :updated_at).first_or_initialize
        insert_world_record(s)
      end
    end
  end
end

def insert_world_record(score)
  wr = WorldRecord.where(map: score.map, mode: score.mode).first_or_initialize

  if score.time.nil?
    wr.delete if wr.time.present?
  elsif wr.time != score.time || wr.player_id != score.player_id
    wr.time = score.time
    wr.player_id = score.player_id
    wr.updated_at = score.updated_at
    wr.api_id = score.api_id
    wr.match_guid = score.match_guid
    wr.save!
  end
end<commit_msg>Fixed fix_world_records if no scores on map.<commit_after>namespace :db do
  desc 'Fix world records after times have been deleted.'
  task fix_world_records: :environment do
    WorldRecord.delete_all
    Score.distinct(:map).pluck(:map).each do |map|
      (0..3).each do |mode|
        s = Score.where(map: map, mode: mode).order(:time, :updated_at).first
        insert_world_record(s) if s
      end
    end
  end
end

def insert_world_record(score)
  WorldRecord.create(score.slice(:map, :mode, :time, :player_id,
                                 :api_id, :updated_at, :match_guid))
end
{'fraction_non_alphanumeric': 0.11207519884309472, 'fraction_numerical': 0.0028922631959508315, 'mean_word_length': 3.1313432835820896, '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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2869623', 'n_tokens_mistral': 464, 'n_tokens_neox': 462, 'n_words': 114}