Merging Confluence users

Sep 30, 2014 · 467 words · 3 minute read

One of my clients is running Confluence. Somewhere along the line, two user accounts had been created for one user, and content had been added using both users.

So muggins here to the rescue. Unfortunately, not much help to be found Googling, so I roll up my sleeves and dig into the Confluence DB schema. Oh what fun.

The main users table appears to be the ‘user_mapping’ table, where each user has a record. The user appears to have a long hash-like ID that represents them in any other records. I chose the ID of the account I would be merging from, and attempted to find the other tables involved in the database that would link to the record I am about to delete.

mysqldump --extended-insert=0 confluence | grep 8a8181c846f172ee014700f866ee0003 | cut -d\` -f2 | uniq

Which returns…

AO_6384AB_DISCOVERED
AO_92296B_AORECENTLY_VIEWED
AO_9412A1_AOUSER
AO_B8E7F9_TALK_SETTINGS
AO_CB7416_KARMA_USER
ATTACHMENTS
BODYCONTENT
CONTENT
CONTENT_LABEL
CONTENT_PERM
FOLLOW_CONNECTIONS
LABEL
NOTIFICATIONS
OS_PROPERTYENTRY
logininfo
user_mapping

Great. That’s 15 tables I need to process, excluding the user_mapping table. By process, I mean I need to check the schema, identify the user ID field, what it’s used for in that table and write an appropriate SQL statement to repoint things to the target user.

So, as they say on Blue Peter, here’s one I prepared earlier.

#!/bin/sh

OLDID=8a8181c846f172ee014700f866ee0003
NEWID=8a8181b34817190d014855c3954c0003

cat <<EOF | mysql -vv -f confluence
UPDATE AO_6384AB_DISCOVERED SET USER_KEY = '$NEWID' WHERE USER_KEY = '$OLDID';
UPDATE AO_92296B_AORECENTLY_VIEWED SET USER_KEY = '$NEWID' WHERE USER_KEY = '$OLDID';
DELETE FROM AO_9412A1_AOUSER WHERE USERNAME = '$OLDID';
DELETE FROM AO_B8E7F9_TALK_SETTINGS WHERE KEY LIKE "%$OLDID";
DELETE FROM AO_CB7416_KARMA_USER WHERE USER_KEY = '$OLDID';
UPDATE ATTACHMENTS SET CREATOR = '$NEWID' WHERE CREATOR = '$OLDID';
UPDATE ATTACHMENTS SET LASTMODIFIER = '$NEWID' WHERE LASTMODIFIER = '$OLDID';
UPDATE BODYCONTENT SET BODY = REPLACE(BODY, '$OLDID', '$NEWID') WHERE BODY LIKE "%$OLDID%";
UPDATE CONTENT SET CREATOR = '$NEWID' WHERE CREATOR = '$OLDID';
UPDATE CONTENT SET LASTMODIFIER = '$NEWID' WHERE LASTMODIFIER = '$OLDID';
UPDATE CONTENT SET USERNAME = '$NEWID' WHERE USERNAME = '$OLDID';
UPDATE CONTENT_PERM SET CREATOR = '$NEWID' WHERE CREATOR = '$OLDID';
UPDATE CONTENT_PERM SET LASTMODIFIER = '$NEWID' WHERE LASTMODIFIER = '$OLDID';
UPDATE CONTENT_PERM SET USERNAME = '$NEWID' WHERE USERNAME = '$OLDID';
UPDATE FOLLOW_CONNECTIONS SET FOLLOWER = '$NEWID' WHERE FOLLOWER = '$OLDID';
UPDATE FOLLOW_CONNECTIONS SET FOLLOWEE = '$NEWID' WHERE FOLLOWEE = '$OLDID';
UPDATE LABEL SET OWNER = '$NEWID' WHERE OWNER = '$OLDID';
UPDATE NOTIFICATIONS SET CREATOR = '$NEWID' WHERE CREATOR = '$OLDID';
UPDATE NOTIFICATIONS SET LASTMODIFIER = '$NEWID' WHERE LASTMODIFIER = '$OLDID';
UPDATE NOTIFICATIONS SET USERNAME = '$NEWID' WHERE USERNAME = '$OLDID';
DELETE FROM OS_PROPERTYENTRY WHERE entity_name LIKE "%-$OLDID";
DELETE FROM logininfo WHERE USERNAME = '$OLDID';
EOF

This may or may not be useful to other Confluence admins. This user had only done a handful of edits, so there may be more tables involved in your case (YMMV).

And don’t forget your backups.