20200620164023_add_fixed_lowercase_index_to_accounts.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132
  1. # frozen_string_literal: true
  2. require Rails.root.join('lib', 'mastodon', 'migration_helpers')
  3. class AddFixedLowercaseIndexToAccounts < ActiveRecord::Migration[5.2]
  4. include Mastodon::MigrationHelpers
  5. disable_ddl_transaction!
  6. def up
  7. if index_name_exists?(:accounts, 'old_index_accounts_on_username_and_domain_lower') && index_name_exists?(:accounts, 'index_accounts_on_username_and_domain_lower')
  8. remove_index :accounts, name: 'index_accounts_on_username_and_domain_lower'
  9. elsif index_name_exists?(:accounts, 'index_accounts_on_username_and_domain_lower')
  10. rename_index :accounts, 'index_accounts_on_username_and_domain_lower', 'old_index_accounts_on_username_and_domain_lower'
  11. end
  12. begin
  13. add_index :accounts, "lower (username), COALESCE(lower(domain), '')", name: 'index_accounts_on_username_and_domain_lower', unique: true, algorithm: :concurrently
  14. rescue ActiveRecord::RecordNotUnique
  15. remove_index :accounts, name: 'index_accounts_on_username_and_domain_lower'
  16. raise CorruptionError, 'index_accounts_on_username_and_domain_lower'
  17. end
  18. 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')
  19. end
  20. def down
  21. add_index :accounts, 'lower (username), lower(domain)', name: 'old_index_accounts_on_username_and_domain_lower', unique: true, algorithm: :concurrently
  22. remove_index :accounts, name: 'index_accounts_on_username_and_domain_lower'
  23. rename_index :accounts, 'old_index_accounts_on_username_and_domain_lower', 'index_accounts_on_username_and_domain_lower'
  24. end
  25. end