Skip to content

Commit

Permalink
More duplicates in cli maintenance spec, misc bug fixes (#28449)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjankowski committed Dec 21, 2023
1 parent 01f0a6c commit 2463b53
Show file tree
Hide file tree
Showing 2 changed files with 443 additions and 23 deletions.
40 changes: 21 additions & 19 deletions lib/mastodon/cli/maintenance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class Webhook < ApplicationRecord; end
class BulkImport < ApplicationRecord; end
class SoftwareUpdate < ApplicationRecord; end

class DomainBlock < ApplicationRecord
scope :by_severity, -> { order(Arel.sql('(CASE severity WHEN 0 THEN 1 WHEN 1 THEN 2 WHEN 2 THEN 0 END), domain')) }
end

class PreviewCard < ApplicationRecord
self.inheritance_column = false
end
Expand Down Expand Up @@ -249,19 +253,7 @@ def deduplicate_users!

say 'Deduplicating user records…'

# Deduplicating email
ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM users GROUP BY email HAVING count(*) > 1").each do |row|
users = User.where(id: row['ids'].split(',')).sort_by(&:updated_at).reverse
ref_user = users.shift
say "Multiple users registered with e-mail address #{ref_user.email}.", :yellow
say "e-mail will be disabled for the following accounts: #{users.map { |user| user.account.acct }.join(', ')}", :yellow
say 'Please reach out to them and set another address with `tootctl account modify` or delete them.', :yellow

users.each_with_index do |user, index|
user.update!(email: "#{index} " + user.email)
end
end

deduplicate_users_process_email
deduplicate_users_process_confirmation_token
deduplicate_users_process_remember_token
deduplicate_users_process_password_token
Expand All @@ -280,6 +272,20 @@ def deduplicate_users!
ActiveRecord::Base.connection.execute('REINDEX INDEX index_users_on_unconfirmed_email;') if migrator_version >= 2023_07_02_151753
end

def deduplicate_users_process_email
ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM users GROUP BY email HAVING count(*) > 1").each do |row|
users = User.where(id: row['ids'].split(',')).sort_by(&:updated_at).reverse
ref_user = users.shift
say "Multiple users registered with e-mail address #{ref_user.email}.", :yellow
say "e-mail will be disabled for the following accounts: #{users.map { |user| user.account.acct }.join(', ')}", :yellow
say 'Please reach out to them and set another address with `tootctl account modify` or delete them.', :yellow

users.each_with_index do |user, index|
user.update!(email: "#{index} " + user.email)
end
end
end

def deduplicate_users_process_confirmation_token
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|
users = User.where(id: row['ids'].split(',')).sort_by(&:created_at).reverse.drop(1)
Expand Down Expand Up @@ -571,7 +577,7 @@ def deduplicate_webhooks!

say 'Deduplicating webhooks…'
ActiveRecord::Base.connection.select_all("SELECT string_agg(id::text, ',') AS ids FROM webhooks GROUP BY url HAVING count(*) > 1").each do |row|
Webhooks.where(id: row['ids'].split(',')).sort_by(&:id).reverse.drop(1).each(&:destroy)
Webhook.where(id: row['ids'].split(',')).sort_by(&:id).reverse.drop(1).each(&:destroy)
end

say 'Restoring webhooks indexes…'
Expand Down Expand Up @@ -604,11 +610,7 @@ def deduplicate_local_accounts!(accounts)

say 'Please chose the one to keep unchanged, other ones will be automatically renamed.'

ref_id = ask('Account to keep unchanged:') do |q|
q.required true
q.default 0
q.convert :int
end
ref_id = ask('Account to keep unchanged:', required: true, default: 0).to_i

accounts.delete_at(ref_id)

Expand Down
Loading

0 comments on commit 2463b53

Please sign in to comment.