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.

30 lines
1.5 KiB

  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
  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