<commit_before>class EnsureDocumentsCanOnlyBelongToASingleOrganisation < ActiveRecord::Migration
  def up
    # Remove existing duplicates
    ids = select_all(%{
      SELECT id FROM document_organisations WHERE EXISTS (
        SELECT 1 FROM document_organisations duplicates
        WHERE duplicates.organisation_id = document_organisations.organisation_id
        AND duplicates.document_id = document_organisations.document_id
        AND duplicates.id < document_organisations.id)
    }).collect {|x| x["id"]}

    execute %{
      DELETE FROM document_organisations WHERE id IN (#{ids.join(", ")})
    }

    # Prevent future duplicates
    remove_index :document_organisations, :document_id
    add_index :document_organisations, [:document_id, :organisation_id], unique: true
  end

  def down
  end
end<commit_msg>Fix data-correction migration to work where no data needs fixing.<commit_after>class EnsureDocumentsCanOnlyBelongToASingleOrganisation < ActiveRecord::Migration
  def up
    # Remove existing duplicates
    ids = select_all(%{
      SELECT id FROM document_organisations WHERE EXISTS (
        SELECT 1 FROM document_organisations duplicates
        WHERE duplicates.organisation_id = document_organisations.organisation_id
        AND duplicates.document_id = document_organisations.document_id
        AND duplicates.id < document_organisations.id)
    }).collect {|x| x["id"]}

    unless ids.empty?
      execute %{
        DELETE FROM document_organisations WHERE id IN (#{ids.join(", ")})
      }
    end

    # Prevent future duplicates
    remove_index :document_organisations, :document_id
    add_index :document_organisations, [:document_id, :organisation_id], unique: true
  end

  def down
  end
end
{'fraction_non_alphanumeric': 0.07142857142857142, 'fraction_numerical': 0.001152073732718894, 'mean_word_length': 3.8792134831460676, 'pattern_counts': {'":': 0, '<': 7, '<?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': '3114063', 'n_tokens_mistral': 515, 'n_tokens_neox': 458, 'n_words': 127}