20200620164023_add_fixed_lowercase_index_to_accounts.rb 1.6 KB

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