diff options
author | Matt Jankowski <matt@jankowski.online> | 2023-05-26 03:42:16 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-26 09:42:16 +0200 |
commit | 55785b160320783392ffe3f24c5ca48e6ee7a5f2 (patch) | |
tree | 8bc197c0b81d7b2019b4803f5ffcadfecf5576aa /lib | |
parent | 0f2c16ac4b6a26a6394f80af6e2cd9338dfffde0 (diff) |
Extract methods for user de-duping in maintenance CLI (#25117)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/mastodon/cli/maintenance.rb | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/lib/mastodon/cli/maintenance.rb b/lib/mastodon/cli/maintenance.rb index 660adef80e8..b107480359e 100644 --- a/lib/mastodon/cli/maintenance.rb +++ b/lib/mastodon/cli/maintenance.rb @@ -231,6 +231,23 @@ module Mastodon::CLI end end + deduplicate_users_process_confirmation_token + deduplicate_users_process_remember_token + deduplicate_users_process_password_token + + @prompt.say 'Restoring users indexes…' + ActiveRecord::Base.connection.add_index :users, ['confirmation_token'], name: 'index_users_on_confirmation_token', unique: true + ActiveRecord::Base.connection.add_index :users, ['email'], name: 'index_users_on_email', unique: true + ActiveRecord::Base.connection.add_index :users, ['remember_token'], name: 'index_users_on_remember_token', unique: true if ActiveRecord::Migrator.current_version < 2022_01_18_183010 + + if ActiveRecord::Migrator.current_version < 2022_03_10_060641 + ActiveRecord::Base.connection.add_index :users, ['reset_password_token'], name: 'index_users_on_reset_password_token', unique: true + else + 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 + 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) @prompt.warn "Unsetting confirmation token for those accounts: #{users.map(&:account).map(&:acct).join(', ')}" @@ -239,7 +256,9 @@ module Mastodon::CLI user.update!(confirmation_token: nil) end end + end + def deduplicate_users_process_remember_token if ActiveRecord::Migrator.current_version < 2022_01_18_183010 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| users = User.where(id: row['ids'].split(',')).sort_by(&:updated_at).reverse.drop(1) @@ -250,7 +269,9 @@ module Mastodon::CLI end end end + end + def deduplicate_users_process_password_token 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| users = User.where(id: row['ids'].split(',')).sort_by(&:updated_at).reverse.drop(1) @prompt.warn "Unsetting password reset token for those accounts: #{users.map(&:account).map(&:acct).join(', ')}" @@ -259,17 +280,6 @@ module Mastodon::CLI user.update!(reset_password_token: nil) end end - - @prompt.say 'Restoring users indexes…' - ActiveRecord::Base.connection.add_index :users, ['confirmation_token'], name: 'index_users_on_confirmation_token', unique: true - ActiveRecord::Base.connection.add_index :users, ['email'], name: 'index_users_on_email', unique: true - ActiveRecord::Base.connection.add_index :users, ['remember_token'], name: 'index_users_on_remember_token', unique: true if ActiveRecord::Migrator.current_version < 2022_01_18_183010 - - if ActiveRecord::Migrator.current_version < 2022_03_10_060641 - ActiveRecord::Base.connection.add_index :users, ['reset_password_token'], name: 'index_users_on_reset_password_token', unique: true - else - 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 - end end def deduplicate_account_domain_blocks! |