From 15cafb35902b8c4cfbc57a4d8ed982ede4ed6183 Mon Sep 17 00:00:00 2001 From: Diggory Blake Date: Wed, 13 Oct 2021 16:32:17 +0100 Subject: [PATCH] Fix bug with `mq_latest_message` where the wrong message may be returned if multiple messages have the exact same creation date. --- Cargo.toml | 4 ++-- ...11013151757_fix_mq_latest_message.down.sql | 15 +++++++++++++++ ...0211013151757_fix_mq_latest_message.up.sql | 19 +++++++++++++++++++ sqlxmq_macros/Cargo.toml | 2 +- ...11013151757_fix_mq_latest_message.down.sql | 15 +++++++++++++++ ...0211013151757_fix_mq_latest_message.up.sql | 19 +++++++++++++++++++ 6 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 migrations/20211013151757_fix_mq_latest_message.down.sql create mode 100644 migrations/20211013151757_fix_mq_latest_message.up.sql create mode 100644 sqlxmq_stress/migrations/20211013151757_fix_mq_latest_message.down.sql create mode 100644 sqlxmq_stress/migrations/20211013151757_fix_mq_latest_message.up.sql diff --git a/Cargo.toml b/Cargo.toml index 2623231..6740ce3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sqlxmq" -version = "0.3.1" +version = "0.3.2" authors = ["Diggory Blake "] edition = "2018" license = "MIT OR Apache-2.0" @@ -23,7 +23,7 @@ uuid = { version = "0.8.2", features = ["v4"] } log = "0.4.14" serde_json = "1.0.64" serde = "1.0.124" -sqlxmq_macros = { version = "0.3.1", path = "sqlxmq_macros" } +sqlxmq_macros = { version = "0.3.2", path = "sqlxmq_macros" } anymap2 = "0.13.0" [features] diff --git a/migrations/20211013151757_fix_mq_latest_message.down.sql b/migrations/20211013151757_fix_mq_latest_message.down.sql new file mode 100644 index 0000000..d09bd4a --- /dev/null +++ b/migrations/20211013151757_fix_mq_latest_message.down.sql @@ -0,0 +1,15 @@ +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; diff --git a/migrations/20211013151757_fix_mq_latest_message.up.sql b/migrations/20211013151757_fix_mq_latest_message.up.sql new file mode 100644 index 0000000..b987c5e --- /dev/null +++ b/migrations/20211013151757_fix_mq_latest_message.up.sql @@ -0,0 +1,19 @@ +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() + AND NOT EXISTS( + SELECT * FROM mq_msgs AS mq_msgs2 + WHERE mq_msgs2.after_message_id = mq_msgs.id + ) + ORDER BY created_at DESC + LIMIT 1 + ), + uuid_nil() + ) +$$ LANGUAGE SQL STABLE; \ No newline at end of file diff --git a/sqlxmq_macros/Cargo.toml b/sqlxmq_macros/Cargo.toml index f1955c0..6b904e5 100644 --- a/sqlxmq_macros/Cargo.toml +++ b/sqlxmq_macros/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sqlxmq_macros" -version = "0.3.1" +version = "0.3.2" authors = ["Diggory Blake "] edition = "2018" license = "MIT OR Apache-2.0" diff --git a/sqlxmq_stress/migrations/20211013151757_fix_mq_latest_message.down.sql b/sqlxmq_stress/migrations/20211013151757_fix_mq_latest_message.down.sql new file mode 100644 index 0000000..d09bd4a --- /dev/null +++ b/sqlxmq_stress/migrations/20211013151757_fix_mq_latest_message.down.sql @@ -0,0 +1,15 @@ +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; diff --git a/sqlxmq_stress/migrations/20211013151757_fix_mq_latest_message.up.sql b/sqlxmq_stress/migrations/20211013151757_fix_mq_latest_message.up.sql new file mode 100644 index 0000000..b987c5e --- /dev/null +++ b/sqlxmq_stress/migrations/20211013151757_fix_mq_latest_message.up.sql @@ -0,0 +1,19 @@ +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() + AND NOT EXISTS( + SELECT * FROM mq_msgs AS mq_msgs2 + WHERE mq_msgs2.after_message_id = mq_msgs.id + ) + ORDER BY created_at DESC + LIMIT 1 + ), + uuid_nil() + ) +$$ LANGUAGE SQL STABLE; \ No newline at end of file