<commit_before>class AddPartialIndexToScores < ActiveRecord::Migration[4.2]
  tag :predeploy

  def up
    duplicate_enrollment_ids = Score.
      group(:enrollment_id, :grading_period_id).
      having('count(*) > 1').
      select(:enrollment_id)
    saved_scores = Score.where(enrollment_id: duplicate_enrollment_ids, grading_period_id: nil).
      order(:enrollment_id, :workflow_state, :created_at).
      select('DISTINCT ON (enrollment_id) id')
    Score.where(enrollment_id: duplicate_enrollment_ids, grading_period_id: nil).
      where.not(id: saved_scores).delete_all

    add_index :scores, :enrollment_id, unique: true, where: 'grading_period_id is null'
  end

  def down
    remove_index :scores, :enrollment_id<commit_msg>Add an `algorithm: concurrently` to our scores partial index<commit_after>class AddPartialIndexToScores < ActiveRecord::Migration[4.2]
  tag :predeploy
  disable_ddl_transaction!

  def up
    duplicate_enrollment_ids = Score.
      group(:enrollment_id, :grading_period_id).
      having('count(*) > 1').
      select(:enrollment_id)
    saved_scores = Score.where(enrollment_id: duplicate_enrollment_ids, grading_period_id: nil).
      order(:enrollment_id, :workflow_state, :created_at).
      select('DISTINCT ON (enrollment_id) id')
    Score.where(enrollment_id: duplicate_enrollment_ids, grading_period_id: nil).
      where.not(id: saved_scores).delete_all

    add_index :scores, :enrollment_id, unique: true, where: 'grading_period_id is null', algorithm: :concurrently
  end

  def down
    remove_index :scores, :enrollment_id
{'fraction_non_alphanumeric': 0.10088832487309644, 'fraction_numerical': 0.0038071065989847717, 'mean_word_length': 4.840740740740741, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4820289', 'n_tokens_mistral': 532, 'n_tokens_neox': 531, 'n_words': 104}