sqlxmq/migrations/20211013151757_fix_mq_latest_message.down.sql
Diggory Blake 15cafb3590
Fix bug with mq_latest_message where the wrong message may be returned
if multiple messages have the exact same creation date.
2021-10-13 16:32:17 +01:00

15 lines
482 B
PL/PgSQL

CREATE OR REPLACE FUNCTION mq_latest_message(from_channel_name TEXT, from_channel_args TEXT)
RETURNS UUID AS $$
SELECT COALESCE(
(
SELECT id FROM mq_msgs
WHERE channel_name = from_channel_name
AND channel_args = from_channel_args
AND after_message_id IS NOT NULL
AND id != uuid_nil()
ORDER BY created_at DESC, id DESC
LIMIT 1
),
uuid_nil()
)
$$ LANGUAGE SQL STABLE;