You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.6 KiB

  1. class AddFixedLowercaseIndexToAccounts < ActiveRecord::Migration[5.2]
  2. disable_ddl_transaction!
  3. class CorruptionError < StandardError
  4. def cause
  5. nil
  6. end
  7. def backtrace
  8. []
  9. end
  10. end
  11. def up
  12. if index_name_exists?(:accounts, 'old_index_accounts_on_username_and_domain_lower') && index_name_exists?(:accounts, 'index_accounts_on_username_and_domain_lower')
  13. remove_index :accounts, name: 'index_accounts_on_username_and_domain_lower'
  14. elsif index_name_exists?(:accounts, 'index_accounts_on_username_and_domain_lower')
  15. rename_index :accounts, 'index_accounts_on_username_and_domain_lower', 'old_index_accounts_on_username_and_domain_lower'
  16. end
  17. begin
  18. add_index :accounts, "lower (username), COALESCE(lower(domain), '')", name: 'index_accounts_on_username_and_domain_lower', unique: true, algorithm: :concurrently
  19. rescue ActiveRecord::RecordNotUnique
  20. raise CorruptionError, 'Migration failed because of index corruption, see https://docs.joinmastodon.org/admin/troubleshooting/index-corruption/#fixing'
  21. end
  22. 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')
  23. end
  24. def down
  25. add_index :accounts, 'lower (username), lower(domain)', name: 'old_index_accounts_on_username_and_domain_lower', unique: true, algorithm: :concurrently
  26. remove_index :accounts, name: 'index_accounts_on_username_and_domain_lower'
  27. rename_index :accounts, 'old_index_accounts_on_username_and_domain_lower', 'index_accounts_on_username_and_domain_lower'
  28. end
  29. end