Created
June 6, 2019 16:24
-
-
Save lampholder/dbe6bdc0ab33f314b103b393919e23d6 to your computer and use it in GitHub Desktop.
Postgres function to pull the 'best available name' for any given room_id in the Synapse Homeserver db
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
CREATE OR REPLACE FUNCTION pg_temp.name(TEXT) RETURNS TEXT AS $$ | |
DECLARE | |
rm_id TEXT; | |
rm_name TEXT; | |
rm_alias TEXT; | |
BEGIN | |
rm_id := $1; | |
rm_name := ( | |
SELECT name | |
FROM matrix.room_names | |
WHERE room_id = rm_id | |
); | |
rm_alias := ( | |
SELECT room_alias | |
FROM room_aliases | |
WHERE room_id = rm_id | |
LIMIT 1 | |
); | |
RETURN COALESCE(rm_name, rm_alias, rm_id); | |
END; | |
$$ LANGUAGE PLPGSQL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can add this to your
.psqlrc
file and your function will be ready for you to use as soon as you connect. As an aside, the idea of a.psqlrc
file that runs a bunch of SQL as soon as you connect to the DB is absolutely terrifying :|