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.

618 lines
26 KiB

  1. # frozen_string_literal: true
  2. require 'tty-prompt'
  3. require_relative '../../config/boot'
  4. require_relative '../../config/environment'
  5. require_relative 'cli_helper'
  6. module Mastodon
  7. class MaintenanceCLI < Thor
  8. include CLIHelper
  9. def self.exit_on_failure?
  10. true
  11. end
  12. MIN_SUPPORTED_VERSION = 2019_10_01_213028
  13. MAX_SUPPORTED_VERSION = 2020_12_18_054746
  14. # Stubs to enjoy ActiveRecord queries while not depending on a particular
  15. # version of the code/database
  16. class Status < ApplicationRecord; end
  17. class StatusPin < ApplicationRecord; end
  18. class Poll < ApplicationRecord; end
  19. class Report < ApplicationRecord; end
  20. class Tombstone < ApplicationRecord; end
  21. class Favourite < ApplicationRecord; end
  22. class Follow < ApplicationRecord; end
  23. class FollowRequest < ApplicationRecord; end
  24. class Block < ApplicationRecord; end
  25. class Mute < ApplicationRecord; end
  26. class AccountIdentityProof < ApplicationRecord; end
  27. class AccountModerationNote < ApplicationRecord; end
  28. class AccountPin < ApplicationRecord; end
  29. class ListAccount < ApplicationRecord; end
  30. class PollVote < ApplicationRecord; end
  31. class Mention < ApplicationRecord; end
  32. class AccountDomainBlock < ApplicationRecord; end
  33. class AnnouncementReaction < ApplicationRecord; end
  34. class FeaturedTag < ApplicationRecord; end
  35. class CustomEmoji < ApplicationRecord; end
  36. class CustomEmojiCategory < ApplicationRecord; end
  37. class Bookmark < ApplicationRecord; end
  38. class WebauthnCredential < ApplicationRecord; end
  39. class PreviewCard < ApplicationRecord
  40. self.inheritance_column = false
  41. end
  42. class MediaAttachment < ApplicationRecord
  43. self.inheritance_column = nil
  44. end
  45. class AccountStat < ApplicationRecord
  46. belongs_to :account, inverse_of: :account_stat
  47. end
  48. # Dummy class, to make migration possible across version changes
  49. class Account < ApplicationRecord
  50. has_one :user, inverse_of: :account
  51. has_one :account_stat, inverse_of: :account
  52. scope :local, -> { where(domain: nil) }
  53. def local?
  54. domain.nil?
  55. end
  56. def acct
  57. local? ? username : "#{username}@#{domain}"
  58. end
  59. # This is a duplicate of the AccountMerging concern because we need it to
  60. # be independent from code version.
  61. def merge_with!(other_account)
  62. # Since it's the same remote resource, the remote resource likely
  63. # already believes we are following/blocking, so it's safe to
  64. # re-attribute the relationships too. However, during the presence
  65. # of the index bug users could have *also* followed the reference
  66. # account already, therefore mass update will not work and we need
  67. # to check for (and skip past) uniqueness errors
  68. owned_classes = [
  69. Status, StatusPin, MediaAttachment, Poll, Report, Tombstone, Favourite,
  70. Follow, FollowRequest, Block, Mute, AccountIdentityProof,
  71. AccountModerationNote, AccountPin, AccountStat, ListAccount,
  72. PollVote, Mention
  73. ]
  74. owned_classes << AccountDeletionRequest if ActiveRecord::Base.connection.table_exists?(:account_deletion_requests)
  75. owned_classes << AccountNote if ActiveRecord::Base.connection.table_exists?(:account_notes)
  76. owned_classes.each do |klass|
  77. klass.where(account_id: other_account.id).find_each do |record|
  78. begin
  79. record.update_attribute(:account_id, id)
  80. rescue ActiveRecord::RecordNotUnique
  81. next
  82. end
  83. end
  84. end
  85. target_classes = [Follow, FollowRequest, Block, Mute, AccountModerationNote, AccountPin]
  86. target_classes << AccountNote if ActiveRecord::Base.connection.table_exists?(:account_notes)
  87. target_classes.each do |klass|
  88. klass.where(target_account_id: other_account.id).find_each do |record|
  89. begin
  90. record.update_attribute(:target_account_id, id)
  91. rescue ActiveRecord::RecordNotUnique
  92. next
  93. end
  94. end
  95. end
  96. end
  97. end
  98. class User < ApplicationRecord
  99. belongs_to :account, inverse_of: :user
  100. end
  101. desc 'fix-duplicates', 'Fix duplicates in database and rebuild indexes'
  102. long_desc <<~LONG_DESC
  103. Delete or merge duplicate accounts, statuses, emojis, etc. and rebuild indexes.
  104. This is useful if your database indexes are corrupted because of issues such as https://wiki.postgresql.org/wiki/Locale_data_changes
  105. Mastodon has to be stopped to run this task, which will take a long time and may be destructive.
  106. LONG_DESC
  107. def fix_duplicates
  108. @prompt = TTY::Prompt.new
  109. if ActiveRecord::Migrator.current_version < MIN_SUPPORTED_VERSION
  110. @prompt.warn 'Your version of the database schema is too old and is not supported by this script.'
  111. @prompt.warn 'Please update to at least Mastodon 3.0.0 before running this script.'
  112. exit(1)
  113. elsif ActiveRecord::Migrator.current_version > MAX_SUPPORTED_VERSION
  114. @prompt.warn 'Your version of the database schema is more recent than this script, this may cause unexpected errors.'
  115. exit(1) unless @prompt.yes?('Continue anyway?')
  116. end
  117. @prompt.warn 'This task will take a long time to run and is potentially destructive.'
  118. @prompt.warn 'Please make sure to stop Mastodon and have a backup.'
  119. exit(1) unless @prompt.yes?('Continue?')
  120. deduplicate_accounts!
  121. deduplicate_users!
  122. deduplicate_account_domain_blocks!
  123. deduplicate_account_identity_proofs!
  124. deduplicate_announcement_reactions!
  125. deduplicate_conversations!
  126. deduplicate_custom_emojis!
  127. deduplicate_custom_emoji_categories!
  128. deduplicate_domain_allows!
  129. deduplicate_domain_blocks!
  130. deduplicate_unavailable_domains!
  131. deduplicate_email_domain_blocks!
  132. deduplicate_media_attachments!
  133. deduplicate_preview_cards!
  134. deduplicate_statuses!
  135. deduplicate_tags!
  136. deduplicate_webauthn_credentials!
  137. Rails.cache.clear
  138. @prompt.say 'Finished!'
  139. end
  140. private
  141. def deduplicate_accounts!
  142. remove_index_if_exists!(:accounts, 'index_accounts_on_username_and_domain_lower')
  143. @prompt.say 'Deduplicating accounts… for local accounts, you will be asked to chose which account to keep unchanged.'
  144. find_duplicate_accounts.each do |row|
  145. accounts = Account.where(id: row['ids'].split(',')).to_a
  146. if accounts.first.local?
  147. deduplicate_local_accounts!(accounts)
  148. else
  149. deduplicate_remote_accounts!(accounts)
  150. end
  151. end
  152. @prompt.say 'Restoring index_accounts_on_username_and_domain_lower…'
  153. if ActiveRecord::Migrator.current_version < 20200620164023
  154. ActiveRecord::Base.connection.add_index :accounts, 'lower (username), lower(domain)', name: 'index_accounts_on_username_and_domain_lower', unique: true
  155. else
  156. ActiveRecord::Base.connection.add_index :accounts, "lower (username), COALESCE(lower(domain), '')", name: 'index_accounts_on_username_and_domain_lower', unique: true
  157. end
  158. end
  159. def deduplicate_users!
  160. remove_index_if_exists!(:users, 'index_users_on_confirmation_token')
  161. remove_index_if_exists!(:users, 'index_users_on_email')
  162. remove_index_if_exists!(:users, 'index_users_on_remember_token')
  163. remove_index_if_exists!(:users, 'index_users_on_reset_password_token')
  164. @prompt.say 'Deduplicating user records…'
  165. # Deduplicating email
  166. ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM users GROUP BY email HAVING count(*) > 1").each do |row|
  167. users = User.where(id: row['ids'].split(',')).sort_by(&:updated_at).reverse
  168. ref_user = users.shift
  169. @prompt.warn "Multiple users registered with e-mail address #{ref_user.email}."
  170. @prompt.warn "e-mail will be disabled for the following accounts: #{user.map(&:account).map(&:acct).join(', ')}"
  171. @prompt.warn 'Please reach out to them and set another address with `tootctl account modify` or delete them.'
  172. i = 0
  173. users.each do |user|
  174. user.update!(email: "#{i} " + user.email)
  175. end
  176. end
  177. ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM users WHERE confirmation_token IS NOT NULL GROUP BY confirmation_token HAVING count(*) > 1").each do |row|
  178. users = User.where(id: row['ids'].split(',')).sort_by(&:created_at).reverse.drop(1)
  179. @prompt.warn "Unsetting confirmation token for those accounts: #{users.map(&:account).map(&:acct).join(', ')}"
  180. users.each do |user|
  181. user.update!(confirmation_token: nil)
  182. end
  183. end
  184. ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM users WHERE remember_token IS NOT NULL GROUP BY remember_token HAVING count(*) > 1").each do |row|
  185. users = User.where(id: row['ids'].split(',')).sort_by(&:updated_at).reverse.drop(1)
  186. @prompt.warn "Unsetting remember token for those accounts: #{users.map(&:account).map(&:acct).join(', ')}"
  187. users.each do |user|
  188. user.update!(remember_token: nil)
  189. end
  190. end
  191. ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM users WHERE reset_password_token IS NOT NULL GROUP BY reset_password_token HAVING count(*) > 1").each do |row|
  192. users = User.where(id: row['ids'].split(',')).sort_by(&:updated_at).reverse.drop(1)
  193. @prompt.warn "Unsetting password reset token for those accounts: #{users.map(&:account).map(&:acct).join(', ')}"
  194. users.each do |user|
  195. user.update!(reset_password_token: nil)
  196. end
  197. end
  198. @prompt.say 'Restoring users indexes…'
  199. ActiveRecord::Base.connection.add_index :users, ['confirmation_token'], name: 'index_users_on_confirmation_token', unique: true
  200. ActiveRecord::Base.connection.add_index :users, ['email'], name: 'index_users_on_email', unique: true
  201. ActiveRecord::Base.connection.add_index :users, ['remember_token'], name: 'index_users_on_remember_token', unique: true
  202. ActiveRecord::Base.connection.add_index :users, ['reset_password_token'], name: 'index_users_on_reset_password_token', unique: true
  203. end
  204. def deduplicate_account_domain_blocks!
  205. remove_index_if_exists!(:account_domain_blocks, 'index_account_domain_blocks_on_account_id_and_domain')
  206. @prompt.say 'Removing duplicate account domain blocks…'
  207. ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM account_domain_blocks GROUP BY account_id, domain HAVING count(*) > 1").each do |row|
  208. AccountDomainBlock.where(id: row['ids'].split(',').drop(1)).delete_all
  209. end
  210. @prompt.say 'Restoring account domain blocks indexes…'
  211. ActiveRecord::Base.connection.add_index :account_domain_blocks, ['account_id', 'domain'], name: 'index_account_domain_blocks_on_account_id_and_domain', unique: true
  212. end
  213. def deduplicate_account_identity_proofs!
  214. remove_index_if_exists!(:account_identity_proofs, 'index_account_proofs_on_account_and_provider_and_username')
  215. @prompt.say 'Removing duplicate account identity proofs…'
  216. ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM account_identity_proofs GROUP BY account_id, provider, provider_username HAVING count(*) > 1").each do |row|
  217. AccountIdentityProof.where(id: row['ids'].split(',')).sort_by(&:id).reverse.drop(1).each(&:destroy)
  218. end
  219. @prompt.say 'Restoring account identity proofs indexes…'
  220. ActiveRecord::Base.connection.add_index :account_identity_proofs, ['account_id', 'provider', 'provider_username'], name: 'index_account_proofs_on_account_and_provider_and_username', unique: true
  221. end
  222. def deduplicate_announcement_reactions!
  223. return unless ActiveRecord::Base.connection.table_exists?(:announcement_reactions)
  224. remove_index_if_exists!(:announcement_reactions, 'index_announcement_reactions_on_account_id_and_announcement_id')
  225. @prompt.say 'Removing duplicate account identity proofs…'
  226. ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM announcement_reactions GROUP BY account_id, announcement_id, name HAVING count(*) > 1").each do |row|
  227. AnnouncementReaction.where(id: row['ids'].split(',')).sort_by(&:id).reverse.drop(1).each(&:destroy)
  228. end
  229. @prompt.say 'Restoring announcement_reactions indexes…'
  230. ActiveRecord::Base.connection.add_index :announcement_reactions, ['account_id', 'announcement_id', 'name'], name: 'index_announcement_reactions_on_account_id_and_announcement_id', unique: true
  231. end
  232. def deduplicate_conversations!
  233. remove_index_if_exists!(:conversations, 'index_conversations_on_uri')
  234. @prompt.say 'Deduplicating conversations…'
  235. ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM conversations WHERE uri IS NOT NULL GROUP BY uri HAVING count(*) > 1").each do |row|
  236. conversations = Conversation.where(id: row['ids'].split(',')).sort_by(&:id).reverse
  237. ref_conversation = conversations.shift
  238. conversations.each do |other|
  239. merge_conversations!(ref_conversation, other)
  240. other.destroy
  241. end
  242. end
  243. @prompt.say 'Restoring conversations indexes…'
  244. ActiveRecord::Base.connection.add_index :conversations, ['uri'], name: 'index_conversations_on_uri', unique: true
  245. end
  246. def deduplicate_custom_emojis!
  247. remove_index_if_exists!(:custom_emojis, 'index_custom_emojis_on_shortcode_and_domain')
  248. @prompt.say 'Deduplicating custom_emojis…'
  249. ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM custom_emojis GROUP BY shortcode, domain HAVING count(*) > 1").each do |row|
  250. emojis = CustomEmoji.where(id: row['ids'].split(',')).sort_by(&:id).reverse
  251. ref_emoji = emojis.shift
  252. emojis.each do |other|
  253. merge_custom_emojis!(ref_emoji, other)
  254. other.destroy
  255. end
  256. end
  257. @prompt.say 'Restoring custom_emojis indexes…'
  258. ActiveRecord::Base.connection.add_index :custom_emojis, ['shortcode', 'domain'], name: 'index_custom_emojis_on_shortcode_and_domain', unique: true
  259. end
  260. def deduplicate_custom_emoji_categories!
  261. remove_index_if_exists!(:custom_emoji_categories, 'index_custom_emoji_categories_on_name')
  262. @prompt.say 'Deduplicating custom_emoji_categories…'
  263. ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM custom_emoji_categories GROUP BY name HAVING count(*) > 1").each do |row|
  264. categories = CustomEmojiCategory.where(id: row['ids'].split(',')).sort_by(&:id).reverse
  265. ref_category = categories.shift
  266. categories.each do |other|
  267. merge_custom_emoji_categories!(ref_category, other)
  268. other.destroy
  269. end
  270. end
  271. @prompt.say 'Restoring custom_emoji_categories indexes…'
  272. ActiveRecord::Base.connection.add_index :custom_emoji_categories, ['name'], name: 'index_custom_emoji_categories_on_name', unique: true
  273. end
  274. def deduplicate_domain_allows!
  275. remove_index_if_exists!(:domain_allows, 'index_domain_allows_on_domain')
  276. @prompt.say 'Deduplicating domain_allows…'
  277. ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM domain_allows GROUP BY domain HAVING count(*) > 1").each do |row|
  278. DomainAllow.where(id: row['ids'].split(',')).sort_by(&:id).reverse.drop(1).each(&:destroy)
  279. end
  280. @prompt.say 'Restoring domain_allows indexes…'
  281. ActiveRecord::Base.connection.add_index :domain_allows, ['domain'], name: 'index_domain_allows_on_domain', unique: true
  282. end
  283. def deduplicate_domain_blocks!
  284. remove_index_if_exists!(:domain_blocks, 'index_domain_blocks_on_domain')
  285. @prompt.say 'Deduplicating domain_allows…'
  286. ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM domain_blocks GROUP BY domain HAVING count(*) > 1").each do |row|
  287. domain_blocks = DomainBlock.where(id: row['ids'].split(',')).by_severity.reverse.to_a
  288. reject_media = domain_blocks.any?(&:reject_media?)
  289. reject_reports = domain_blocks.any?(&:reject_reports?)
  290. reference_block = domain_blocks.shift
  291. private_comment = domain_blocks.reduce(reference_block.private_comment.presence) { |a, b| a || b.private_comment.presence }
  292. public_comment = domain_blocks.reduce(reference_block.public_comment.presence) { |a, b| a || b.public_comment.presence }
  293. reference_block.update!(reject_media: reject_media, reject_reports: reject_reports, private_comment: private_comment, public_comment: public_comment)
  294. domain_blocks.each(&:destroy)
  295. end
  296. @prompt.say 'Restoring domain_blocks indexes…'
  297. ActiveRecord::Base.connection.add_index :domain_blocks, ['domain'], name: 'index_domain_blocks_on_domain', unique: true
  298. end
  299. def deduplicate_unavailable_domains!
  300. return unless ActiveRecord::Base.connection.table_exists?(:unavailable_domains)
  301. remove_index_if_exists!(:unavailable_domains, 'index_unavailable_domains_on_domain')
  302. @prompt.say 'Deduplicating unavailable_domains…'
  303. ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM unavailable_domains GROUP BY domain HAVING count(*) > 1").each do |row|
  304. UnavailableDomain.where(id: row['ids'].split(',')).sort_by(&:id).reverse.drop(1).each(&:destroy)
  305. end
  306. @prompt.say 'Restoring domain_allows indexes…'
  307. ActiveRecord::Base.connection.add_index :unavailable_domains, ['domain'], name: 'index_unavailable_domains_on_domain', unique: true
  308. end
  309. def deduplicate_email_domain_blocks!
  310. remove_index_if_exists!(:email_domain_blocks, 'index_email_domain_blocks_on_domain')
  311. @prompt.say 'Deduplicating email_domain_blocks…'
  312. ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM email_domain_blocks GROUP BY domain HAVING count(*) > 1").each do |row|
  313. domain_blocks = EmailDomainBlock.where(id: row['ids'].split(',')).sort_by { |b| b.parent.nil? ? 1 : 0 }.to_a
  314. domain_blocks.drop(1).each(&:destroy)
  315. end
  316. @prompt.say 'Restoring email_domain_blocks indexes…'
  317. ActiveRecord::Base.connection.add_index :email_domain_blocks, ['domain'], name: 'index_email_domain_blocks_on_domain', unique: true
  318. end
  319. def deduplicate_media_attachments!
  320. remove_index_if_exists!(:media_attachments, 'index_media_attachments_on_shortcode')
  321. @prompt.say 'Deduplicating media_attachments…'
  322. ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM media_attachments WHERE shortcode IS NOT NULL GROUP BY shortcode HAVING count(*) > 1").each do |row|
  323. MediaAttachment.where(id: row['ids'].split(',').drop(1)).update_all(shortcode: nil)
  324. end
  325. @prompt.say 'Restoring media_attachments indexes…'
  326. ActiveRecord::Base.connection.add_index :media_attachments, ['shortcode'], name: 'index_media_attachments_on_shortcode', unique: true
  327. end
  328. def deduplicate_preview_cards!
  329. remove_index_if_exists!(:preview_cards, 'index_preview_cards_on_url')
  330. @prompt.say 'Deduplicating preview_cards…'
  331. ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM preview_cards GROUP BY url HAVING count(*) > 1").each do |row|
  332. PreviewCard.where(id: row['ids'].split(',')).sort_by(&:id).reverse.drop(1).each(&:destroy)
  333. end
  334. @prompt.say 'Restoring preview_cards indexes…'
  335. ActiveRecord::Base.connection.add_index :preview_cards, ['url'], name: 'index_preview_cards_on_url', unique: true
  336. end
  337. def deduplicate_statuses!
  338. remove_index_if_exists!(:statuses, 'index_statuses_on_uri')
  339. @prompt.say 'Deduplicating statuses…'
  340. ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM statuses WHERE uri IS NOT NULL GROUP BY uri HAVING count(*) > 1").each do |row|
  341. statuses = Status.where(id: row['ids'].split(',')).sort_by(&:id)
  342. ref_status = statuses.shift
  343. statuses.each do |status|
  344. merge_statuses!(ref_status, status) if status.account_id == ref_status.account_id
  345. status.destroy
  346. end
  347. end
  348. @prompt.say 'Restoring statuses indexes…'
  349. ActiveRecord::Base.connection.add_index :statuses, ['uri'], name: 'index_statuses_on_uri', unique: true
  350. end
  351. def deduplicate_tags!
  352. remove_index_if_exists!(:tags, 'index_tags_on_name_lower')
  353. @prompt.say 'Deduplicating tags…'
  354. ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM tags GROUP BY lower((name)::text) HAVING count(*) > 1").each do |row|
  355. tags = Tag.where(id: row['ids'].split(',')).sort_by { |t| [t.usable?, t.trendable?, t.listable?].count(false) }
  356. ref_tag = tags.shift
  357. tags.each do |tag|
  358. merge_tags!(ref_tag, tag)
  359. tag.destroy
  360. end
  361. end
  362. @prompt.say 'Restoring tags indexes…'
  363. ActiveRecord::Base.connection.add_index :tags, 'lower((name)::text)', name: 'index_tags_on_name_lower', unique: true
  364. end
  365. def deduplicate_webauthn_credentials!
  366. return unless ActiveRecord::Base.connection.table_exists?(:webauthn_credentials)
  367. remove_index_if_exists!(:webauthn_credentials, 'index_webauthn_credentials_on_external_id')
  368. @prompt.say 'Deduplicating webauthn_credentials…'
  369. ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM webauthn_credentials GROUP BY external_id HAVING count(*) > 1").each do |row|
  370. WebauthnCredential.where(id: row['ids'].split(',')).sort_by(&:id).reverse.drop(1).each(&:destroy)
  371. end
  372. @prompt.say 'Restoring webauthn_credentials indexes…'
  373. ActiveRecord::Base.connection.add_index :webauthn_credentials, ['external_id'], name: 'index_webauthn_credentials_on_external_id', unique: true
  374. end
  375. def deduplicate_local_accounts!(accounts)
  376. accounts = accounts.sort_by(&:id).reverse
  377. @prompt.warn "Multiple local accounts were found for username '#{accounts.first.username}'."
  378. @prompt.warn 'All those accounts are distinct accounts but only the most recently-created one is fully-functionnal.'
  379. accounts.each_with_index do |account, idx|
  380. @prompt.say '%2d. %s: created at: %s; updated at: %s; last logged in at: %s; statuses: %5d; last status at: %s' % [idx, account.username, account.created_at, account.updated_at, account.user&.last_sign_in_at&.to_s || 'N/A', account.account_stat&.statuses_count || 0, account.account_stat&.last_status_at || 'N/A']
  381. end
  382. @prompt.say 'Please chose the one to keep unchanged, other ones will be automatically renamed.'
  383. ref_id = @prompt.ask('Account to keep unchanged:') do |q|
  384. q.required true
  385. q.default 0
  386. q.convert :int
  387. end
  388. accounts.delete_at(ref_id)
  389. i = 0
  390. accounts.each do |account|
  391. i += 1
  392. username = account.username + "_#{i}"
  393. while Account.local.exists?(username: username)
  394. i += 1
  395. username = account.username + "_#{i}"
  396. end
  397. account.update!(username: username)
  398. end
  399. end
  400. def deduplicate_remote_accounts!(accounts)
  401. accounts = accounts.sort_by(&:updated_at).reverse
  402. reference_account = accounts.shift
  403. accounts.each do |other_account|
  404. if other_account.public_key == reference_account.public_key
  405. # The accounts definitely point to the same resource, so
  406. # it's safe to re-attribute content and relationships
  407. reference_account.merge_with!(other_account)
  408. end
  409. other_account.destroy
  410. end
  411. end
  412. def merge_conversations!(main_conv, duplicate_conv)
  413. owned_classes = [ConversationMute, AccountConversation]
  414. owned_classes.each do |klass|
  415. klass.where(conversation_id: duplicate_conv.id).find_each do |record|
  416. begin
  417. record.update_attribute(:account_id, main_conv.id)
  418. rescue ActiveRecord::RecordNotUnique
  419. next
  420. end
  421. end
  422. end
  423. end
  424. def merge_custom_emojis!(main_emoji, duplicate_emoji)
  425. owned_classes = [AnnouncementReaction]
  426. owned_classes.each do |klass|
  427. klass.where(custom_emoji_id: duplicate_emoji.id).update_all(custom_emoji_id: main_emoji.id)
  428. end
  429. end
  430. def merge_custom_emoji_categories!(main_category, duplicate_category)
  431. owned_classes = [CustomEmoji]
  432. owned_classes.each do |klass|
  433. klass.where(category_id: duplicate_category.id).update_all(category_id: main_category.id)
  434. end
  435. end
  436. def merge_statuses!(main_status, duplicate_status)
  437. owned_classes = [Favourite, Mention, Poll]
  438. owned_classes << Bookmark if ActiveRecord::Base.connection.table_exists?(:bookmarks)
  439. owned_classes.each do |klass|
  440. klass.where(status_id: duplicate_status.id).find_each do |record|
  441. begin
  442. record.update_attribute(:status_id, main_status.id)
  443. rescue ActiveRecord::RecordNotUnique
  444. next
  445. end
  446. end
  447. end
  448. StatusPin.where(account_id: main_status.account_id, status_id: duplicate_status.id).find_each do |record|
  449. begin
  450. record.update_attribute(:status_id, main_status.id)
  451. rescue ActiveRecord::RecordNotUnique
  452. next
  453. end
  454. end
  455. Status.where(in_reply_to_id: duplicate_status.id).find_each do |record|
  456. begin
  457. record.update_attribute(:in_reply_to_id, main_status.id)
  458. rescue ActiveRecord::RecordNotUnique
  459. next
  460. end
  461. end
  462. Status.where(reblog_of_id: duplicate_status.id).find_each do |record|
  463. begin
  464. record.update_attribute(:reblog_of_id, main_status.id)
  465. rescue ActiveRecord::RecordNotUnique
  466. next
  467. end
  468. end
  469. end
  470. def merge_tags!(main_tag, duplicate_tag)
  471. [FeaturedTag].each do |klass|
  472. klass.where(tag_id: duplicate_tag.id).find_each do |record|
  473. begin
  474. record.update_attribute(:tag_id, main_tag.id)
  475. rescue ActiveRecord::RecordNotUnique
  476. next
  477. end
  478. end
  479. end
  480. end
  481. def find_duplicate_accounts
  482. ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM accounts GROUP BY lower(username), COALESCE(lower(domain), '') HAVING count(*) > 1")
  483. end
  484. def remove_index_if_exists!(table, name)
  485. ActiveRecord::Base.connection.remove_index(table, name: name)
  486. rescue ArgumentError
  487. nil
  488. rescue ActiveRecord::StatementInvalid
  489. nil
  490. end
  491. end
  492. end