Rake task for merging users


(Gerhard Schlager) #27

You need to upgrade PostgreSQL to version 9.5 or greater. The current docker container uses Postgres 10 by default.


(Artur Shaik) #28

Ou, I see now. Thank you very much!


(Yaw Anokwa) #29

@gerhard Any chance that merging users on a site with the Discourse Voting plugin will prevent one of those users from logging in with the following error and log message?

The software powering this discussion forum encountered an unexpected problem. We apologize for the inconvenience.

Disabling the plugin solves the problem, but I like the plugin, so ideas on how I could fix it are very welcome!

The line in question is at https://github.com/discourse/discourse-voting/blob/master/plugin.rb#L112 and I think I’ve fixed it at https://github.com/discourse/discourse-voting/pull/33.


(Gerhard Schlager) #30

Maybe. Plugins can subscribe to the :merging_users event. It’s possible that the Voting plugin needs to do that.


(Stephen Chung) #31

Just found out about this and immediately tried it out. Works like a charm. Thank you for this nice feature!


(Tobias Eigen) #32

Is there any way to empty the second emails field? I merged two users and then later realized I should not have done so. Now I can’t create the user again because the email address is in the second emails field.

Obviously some sort of UI fix would be lovely long term for emptying/changing these addresses, but I know that’s likely a ways off. In the meantime I’d be grateful for command line steps. Thanks in advance! :seedling:


(Gerhard Schlager) #33

Sure, this can be done in the rails console within the container.

rails c

# restrict to secondary emails, just in case you try to delete the wrong email
UserEmail.where(email: "foo@example.com", primary: false).destroy_all

# probably not needed, but better safe than sorry
EmailToken.where(email: "foo@example.com").destroy_all

(Jay Pfaffman) #34

This is helpful. This seems like something that people will need with some frequency; it’d probably be good if this were a rake task.


(Tobias Eigen) #35

I have spent some time using this super handy user merge rake command combined with @gerhard’s rails commands to remove secondary emails. Thank you both.

These commands have become an important and handy part of my workflow to bring back defunct users who have become unreachable, or to invite people who attend or apply for our events to join our community. Often there is a need to change the email address of the existing account, while retaining the correspondence with a new, staged user.

I do share @pfaffman’s challenge.

The answer for now seems to be to merge the user, delete the secondary email address of the remaining user using @gerhard’s rails commands, then change the email address via the web interface. (in my case it’s via wordpress SSO). Presto, done. But it’s three commands involving some copy pasting and is a bit time consuming.

Some suggestions follow. Curious to hear reactions.

How about a users:swapemail command to run before the merge?

rake users:swapemail[user1,user2]
rake users:merge[user1,user2] 

Add ability to edit secondary email addresses via the admin UI?

I have SSO on my site so can’t use this, but for those who don’t it would be useful to be able to edit the primary and secondary email addresses directly via the user admin.

Add ability to update secondary email via SSO?

This might already be possible via API, but support for a secondary email field would need to be added to the SSO provider, in my case wordpress.

Move primary email to secondary when primary email is changed?

Perhaps a simpler, more sustainable long-term solution that doesn’t require alot of other changes would be to simply change it so when a user or admin (via web or SSO) changes the primary email, it moves the old email to the secondary email field first. This could be enabled with an admin site setting.


Tickets Plugin :tickets:
(Tobias Eigen) #36

Is there any risk to merging someone’s shadow/anonymous account into their account? I just did it and it seemed to work fine.


(Michael Ball) #37

This seems incredibly helpful!

I’m trying to do the following:

root@snap-forum-app:/var/www/discourse# rake users:merge ['blob', 'tjvr'] --trace
** Invoke users:merge (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute users:merge
ERROR: Expecting rake users:merge[source_username,target_username]

Any ideas on how to proceed? I’ve got SSO enabled, and am trying to merge the non-sso user into the SSO user. I have a few accounts which existed prior to enabling SSO on the forum.


(Joshua Rosenfeld) #38

Try removing the space after the comma. You also shouldn’t need quotes around the username.


(Michael Ball) #39

Ah, thanks! Quotes break it… I originally tried no spaces, and no quotes, but somehow missed no quotes and no spaces… :frowning:

So, now when I run this I encounter the following exception, PostsExistError. This seems a bit odd to me, since isn’t goal to move all posts? I also ran the change ownership task for good measure and I got the same result. The admin page says this user has “-6” posts created, and none listed on the user replies view.

root@snap-forum-app:/var/www/discourse# rake users:merge[blob,tjvr] --trace
** Invoke users:merge (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute users:merge
rake aborted!
UserDestroyer::PostsExistError: UserDestroyer::PostsExistError
/var/www/discourse/app/services/user_destroyer.rb:18:in `destroy'
/var/www/discourse/app/services/user_merger.rb:360:in `delete_source_user'
/var/www/discourse/app/services/user_merger.rb:22:in `merge!'
/var/www/discourse/lib/tasks/users.rake:48:in `block in <top (required)>'

Edit: Well, I went into the console and just updated the owner of 5 posts that seems to be assigned to blob’s account. Not sure what happened, but after that the merge was successful. :slight_smile:


(Tobias Eigen) #40

I’m running into trouble with this command and my SSO setup, which appears to fail silently if a user changes their email address to an address that exists already in the secondary email field. The email address changes in wordpress and all appears to be well, but it does not change in discourse.

When I run @gerhard’s rails commands to remove secondary emails first, then change the email address in wordpress, it changes successfully in discourse.

The primary email address is not an issue, because if someone tries to change their email address to an address that already exists in wordpress, it doesn’t let them.

Does anyone have any suggestions? Is it possible to disable the secondary email address field, or to have the API ignore it or use it better? E.g. if the email address exists as secondary, just move what was primary to secondary and move what was secondary to primary?


Additional email address per user account support
(Stephen) #41

If you’re using Wordpress as the authority for SSO what benefit is there to storing a second email in Discourse?


(Tobias Eigen) #42

right-o. unless I store it in wordpress too for the same reasons. but I don’t know how to prevent this rake task from storing a secondary email when merging users.


(Stephen) #43

If secondary emails are an issue can’t they just be removed?


(Tobias Eigen) #44

Yes, that is one easy solution. Is there a rake task someone can share to list all the users with secondary emails, and to delete their secondary emails?

That said, I’d prefer to see discourse evolve to take advantage of a cool new feature like secondary emails rather than have to find ways to work around it. Other people are likely to bump up against this issue too at some point and wonder why the email address is not updating in discourse when updated in wordpress.


(Jay Pfaffman) #45

I don’t think there’s a rake task, but you could do it from the rails console. There should be some example queries about secondary emails for data explorer and some sample .update_all code for other rails examples. It’ll take more time than I can spare here today. If you’re stuck and want to pay, my contact info is in my profile.


(Jay Pfaffman) #46

See Additional email address per user account support

Here’s what you’re looking for: Additional email address per user account support