maintenance_cli.rb 30 KB

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