Last active
April 18, 2022 12:53
-
-
Save jorgemanrubia/9445948 to your computer and use it in GitHub Desktop.
Script for merging existing discourse users
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DiscourseUsersMerger | |
class Merger | |
def merge(target_username, source_username) | |
raise "User to merge and target user can't be the same" if target_username == source_username | |
target_user = User.find_by_username!(target_username) | |
source_user = User.find_by_username!(source_username) | |
puts "About to merge #{source_username} (#{source_user.email}) into #{target_username} (#{target_user.email})" | |
puts "#{source_user.topics.count} topics with #{source_user.posts.count} posts will be moved. Ok to continue? (y/n)" | |
continue = STDIN.gets.chomp.downcase == 'y' | |
perform_merge(target_user, source_user) if continue | |
end | |
private | |
def perform_merge(target_user, source_user) | |
puts "Merging..." | |
source_user.posts.update_all user_id: target_user.id | |
source_user.topics.update_all user_id: target_user.id | |
source_user.user_actions.each { |ua| ua.update_column(:user_id, target_user.id) rescue puts "error #{ua.inspect}" } | |
puts "Deleting #{source_user.email}..." | |
source_user.reload.destroy | |
puts "Updating topics statistics" | |
target_user.topics.each { |topic| topic.update_statistics } | |
target_user.user_stat.update_topic_reply_count | |
create_user_actions(target_user) | |
end | |
def create_user_actions(target_user) | |
target_user.user_actions.where("action_type in (?)", [4, 5]).destroy_all | |
UserAction.record_timestamps = false | |
target_user.topics.each { |topic| create_user_action_for_topic(target_user, topic) } | |
target_user.posts.each { |post| create_user_action_for_post(post, target_user) } | |
end | |
def create_user_action_for_post(post, target_user) | |
UserAction.create!(user_id: target_user.id, target_topic_id: post.topic.id, target_post_id: post.id, action_type: 5, | |
created_at: post.created_at, updated_at: post.updated_at, acting_user_id: target_user.id) rescue puts "Error" | |
end | |
def create_user_action_for_topic(target_user, topic) | |
UserAction.create!(user_id: target_user.id, target_topic_id: topic.id, target_post_id: -1, action_type: 4, | |
created_at: topic.created_at, updated_at: topic.updated_at, acting_user_id: target_user.id) rescue puts "Error" | |
end | |
end | |
end | |
raise "Usage: 'discourse_users_merger <target_username> <source_username>'" unless ARGV.length==2 | |
DiscourseUsersMerger::Merger.new.merge(ARGV[0], ARGV[1]) |
From here:
Just copy it to app/lib. Open rails console and type the last line of that gist (change username)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you please explain where and how this script is to be executed ?