20180528141303_fix_accounts_unique_index.rb 4.2 KB

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