<commit_before>class RemoveStudentRiskLevelRecords < ActiveRecord::Migration[5.1]
  # See https://github.com/studentinsights/studentinsights/issues/1071
  def change
    students = Student.all
    good_student_risk_level_ids = students.map(&:student_risk_level).map(&:id)
    bad_student_risk_level_ids = StudentRiskLevel.all.where.not(id: good_student_risk_level_ids)
    puts "Found: #{students.size} students"
    puts "Found: #{good_student_risk_level_ids.size} StudentRiskLevel records through Student associations"
    puts "Found: #{bad_student_risk_level_ids.size} other StudentRiskLevel records"

    puts "Checking some conditions..."
    if good_student_risk_level_ids.size <= 0
      raise "no good records"
    elsif bad_student_risk_level_ids.size >= StudentRiskLevel.all.size
      raise "more bad records than expected"
    elsif bad_student_risk_level_ids.size + good_student_risk_level_ids.size != StudentRiskLevel.all.size
      raise "bad and good records don't equal total"
    end

    puts "Destroying..."
    StudentRiskLevel.where(id: bad_student_risk_level_ids).destroy!
    puts "Done."

    puts "Confirming..."
    remaining_good_count = Student.all.map(&:student_risk_level).map(&:id).size
    puts "Found #{remaining_good_count} StudentRiskLevel records through Student associations"
    all_count = StudentRiskLevel.all.size
    puts "Found #{all_count} StudentRiskLevel records total"
  end

end
<commit_msg>Add guards for development and test dbs<commit_after>class RemoveStudentRiskLevelRecords < ActiveRecord::Migration[5.1]
  # See https://github.com/studentinsights/studentinsights/issues/1071
  def change
    students = Student.all
    good_student_risk_level_ids = students.map(&:student_risk_level).map(&:id)
    bad_student_risk_level_ids = StudentRiskLevel.all.where.not(id: good_student_risk_level_ids)
    puts "Found: #{students.size} students"
    puts "Found: #{good_student_risk_level_ids.size} StudentRiskLevel records through Student associations"
    puts "Found: #{bad_student_risk_level_ids.size} other StudentRiskLevel records"

    puts "Checking some conditions..."
    if Rails.env.production? && good_student_risk_level_ids.size <= 0
      raise "no good records"
    elsif StudentRiskLevel.all.size != 0 && bad_student_risk_level_ids.size >= StudentRiskLevel.all.size
      raise "more bad records than expected"
    elsif bad_student_risk_level_ids.size + good_student_risk_level_ids.size != StudentRiskLevel.all.size
      raise "bad and good records don't equal total"
    end

    puts "Destroying..."
    StudentRiskLevel.where(id: bad_student_risk_level_ids).destroy!
    puts "Done."

    puts "Confirming..."
    remaining_good_count = Student.all.map(&:student_risk_level).map(&:id).size
    puts "Found #{remaining_good_count} StudentRiskLevel records through Student associations"
    all_count = StudentRiskLevel.all.size
    puts "Found #{all_count} StudentRiskLevel records total"
  end

end

{'fraction_non_alphanumeric': 0.09201213346814964, 'fraction_numerical': 0.005055611729019211, 'mean_word_length': 4.71868978805395, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '5216111', 'n_tokens_mistral': 944, 'n_tokens_neox': 906, 'n_words': 195}