Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ This repo contains a mix of SQL queries that were found [on this repo](https://g
- [Direct Messages between two users](#direct-messages-between-two-users)
- [Get all messages for a user](#get-all-messages-for-a-user)
- [Disk space usage from all databases](#disk-space-usage-from-all-databases)
- [Swap the Id value of two users](#swap-the-id-value-of-two-users)

# System Console Metrics

Expand Down Expand Up @@ -671,3 +672,30 @@ GROUP BY
ORDER BY
size_mb DESC;
```

## Swap the Id value of two users

This query will swap the `Users.Id` value of two users in the `Users` table. Most commonly, this query is for when SAML is used for authentication, and the `SamlSettings.IdAttribute` is set to a value that can potentially change, such as `email` or `username`. A user logging in after a change to their email or username will create a new user in the database, and this query can be used to swap the IDs of the old and new users. This will restore their channels, posts, and other data to the state it was prior to the change. Once everything is verified, the old user account can be safely deleted with `mmctl` to remove it from the database.

Note: It is recommended that you revoke the sessions of both the original account and new account prior to making this change. This can be done by going to System Console -> User Management -> Users, searching for the users, and then under the actions for each user select "remove sessions".

```sql
DO $$
DECLARE
id_old TEXT;
id_new TEXT;
BEGIN
-- Step 1: Get original IDs
SELECT id INTO id_old FROM users WHERE email = '<oldUserEmail>';
SELECT id INTO id_new FROM users WHERE email = '<newUserEmail>';

-- Step 2: Update <oldUserEmail> to a temp ID
UPDATE users SET id = 'temp-swap-id' WHERE id = id_old;

-- Step 3: Update <newUserEmail> to <oldUserEmail>'s original ID
UPDATE users SET id = id_old WHERE id = id_new;

-- Step 4: Update <oldUserEmail> to <newUserEmail>'s original ID
UPDATE users SET id = id_new WHERE id = 'temp-swap-id';
END $$;
```