<commit_before>class AddFixedLowercaseIndexToAccounts < ActiveRecord::Migration[5.2]
  disable_ddl_transaction!

  def up
    rename_index :accounts, 'index_accounts_on_username_and_domain_lower', 'old_index_accounts_on_username_and_domain_lower' unless index_name_exists?(:accounts, 'old_index_accounts_on_username_and_domain_lower')
    add_index :accounts, "lower (username), COALESCE(lower(domain), '')", name: 'index_accounts_on_username_and_domain_lower', unique: true, algorithm: :concurrently
    remove_index :accounts, name: 'old_index_accounts_on_username_and_domain_lower'
  end

  def down
    add_index :accounts, 'lower (username), lower(domain)', name: 'old_index_accounts_on_username_and_domain_lower', unique: true, algorithm: :concurrently
    remove_index :accounts, name: 'index_accounts_on_username_and_domain_lower'
    rename_index :accounts, 'old_index_accounts_on_username_and_domain_lower', 'index_accounts_on_username_and_domain_lower'
  end
end<commit_msg>Fix old migration script not being able to run if it fails midway (#15361)<commit_after>class AddFixedLowercaseIndexToAccounts < ActiveRecord::Migration[5.2]
  disable_ddl_transaction!

  class CorruptionError < StandardError
    def cause
      nil
    end

    def backtrace
      []
    end
  end

  def up
    if index_name_exists?(:accounts, 'old_index_accounts_on_username_and_domain_lower') && index_name_exists?(:accounts, 'index_accounts_on_username_and_domain_lower')
      remove_index :accounts, name: 'index_accounts_on_username_and_domain_lower'
    elsif index_name_exists?(:accounts, 'index_accounts_on_username_and_domain_lower')
      rename_index :accounts, 'index_accounts_on_username_and_domain_lower', 'old_index_accounts_on_username_and_domain_lower'
    end

    begin
      add_index :accounts, "lower (username), COALESCE(lower(domain), '')", name: 'index_accounts_on_username_and_domain_lower', unique: true, algorithm: :concurrently
    rescue ActiveRecord::RecordNotUnique
      raise CorruptionError, 'Migration failed because of index corruption, see https://docs.joinmastodon.org/admin/troubleshooting/index-corruption/#fixing'
    end

    remove_index :accounts, name: 'old_index_accounts_on_username_and_domain_lower' if index_name_exists?(:accounts, 'old_index_accounts_on_username_and_domain_lower')
  end

  def down
    add_index :accounts, 'lower (username), lower(domain)', name: 'old_index_accounts_on_username_and_domain_lower', unique: true, algorithm: :concurrently
    remove_index :accounts, name: 'index_accounts_on_username_and_domain_lower'
    rename_index :accounts, 'old_index_accounts_on_username_and_domain_lower', 'index_accounts_on_username_and_domain_lower'
  end
end
{'fraction_non_alphanumeric': 0.07933579335793357, 'fraction_numerical': 0.0033210332103321034, 'mean_word_length': 7.471875, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 1, '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': '4627203', 'n_tokens_mistral': 889, 'n_tokens_neox': 877, 'n_words': 154}