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.

84 lines
3.5 KiB

  1. class FixAccountsUniqueIndex < ActiveRecord::Migration[5.2]
  2. disable_ddl_transaction!
  3. def up
  4. say ''
  5. say 'WARNING: This migration may take a *long* time for large instances'
  6. say 'It will *not* lock tables for any significant time, but it may run'
  7. say 'for a very long time. We will pause for 10 seconds to allow you to'
  8. say 'interrupt this migration if you are not ready.'
  9. say ''
  10. say 'This migration will irreversibly delete user accounts with duplicate'
  11. say 'usernames. You may use the `rake mastodon:maintenance:find_duplicate_usernames`'
  12. say 'task to manually deal with such accounts before running this migration.'
  13. 10.downto(1) do |i|
  14. say "Continuing in #{i} second#{i == 1 ? '' : 's'}...", true
  15. sleep 1
  16. end
  17. duplicates = Account.connection.select_all('SELECT string_agg(id::text, \',\') AS ids FROM accounts GROUP BY lower(username), lower(domain) HAVING count(*) > 1').to_hash
  18. duplicates.each do |row|
  19. deduplicate_account!(row['ids'].split(','))
  20. end
  21. remove_index :accounts, name: 'index_accounts_on_username_and_domain_lower' if index_name_exists?(:accounts, 'index_accounts_on_username_and_domain_lower')
  22. safety_assured { execute 'CREATE UNIQUE INDEX CONCURRENTLY index_accounts_on_username_and_domain_lower ON accounts (lower(username), lower(domain))' }
  23. remove_index :accounts, name: 'index_accounts_on_username_and_domain' if index_name_exists?(:accounts, 'index_accounts_on_username_and_domain')
  24. end
  25. def down
  26. raise ActiveRecord::IrreversibleMigration
  27. end
  28. private
  29. def deduplicate_account!(account_ids)
  30. accounts = Account.where(id: account_ids).to_a
  31. accounts = account.first.local? ? accounts.sort_by(&:created_at) : accounts.sort_by(&:updated_at).reverse
  32. reference_account = accounts.shift
  33. accounts.each do |other_account|
  34. if other_account.public_key == reference_account.public_key
  35. # The accounts definitely point to the same resource, so
  36. # it's safe to re-attribute content and relationships
  37. merge_accounts!(reference_account, other_account)
  38. elsif other_account.local?
  39. # Since domain is in the GROUP BY clause, both accounts
  40. # are always either going to be local or not local, so only
  41. # one check is needed. Since we cannot support two users with
  42. # the same username locally, one has to go. 😢
  43. other_account.user.destroy
  44. end
  45. other_account.destroy
  46. end
  47. end
  48. def merge_accounts!(main_account, duplicate_account)
  49. [Status, Favourite, Mention, StatusPin, StreamEntry].each do |klass|
  50. klass.where(account_id: duplicate_account.id).update_all(account_id: main_account.id)
  51. end
  52. # Since it's the same remote resource, the remote resource likely
  53. # already believes we are following/blocking, so it's safe to
  54. # re-attribute the relationships too. However, during the presence
  55. # of the index bug users could have *also* followed the reference
  56. # account already, therefore mass update will not work and we need
  57. # to check for (and skip past) uniqueness errors
  58. [Follow, FollowRequest, Block, Mute].each do |klass|
  59. klass.where(account_id: duplicate_account.id).find_each do |record|
  60. record.update(account_id: main_account.id)
  61. rescue ActiveRecord::RecordNotUnique
  62. next
  63. end
  64. klass.where(target_account_id: duplicate_account.id).find_each do |record|
  65. record.update(target_account_id: main_account.id)
  66. rescue ActiveRecord::RecordNotUnique
  67. next
  68. end
  69. end
  70. end
  71. end