Skip to content

Commit

Permalink
Fix bug with mq_latest_message where the wrong message may be returned
Browse files Browse the repository at this point in the history
if multiple messages have the exact same creation date.
  • Loading branch information
Diggsey committed Oct 13, 2021
1 parent 59adc63 commit 15cafb3
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sqlxmq"
version = "0.3.1"
version = "0.3.2"
authors = ["Diggory Blake <[email protected]>"]
edition = "2018"
license = "MIT OR Apache-2.0"
Expand All @@ -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]
Expand Down
15 changes: 15 additions & 0 deletions migrations/20211013151757_fix_mq_latest_message.down.sql
Original file line number Diff line number Diff line change
@@ -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;
19 changes: 19 additions & 0 deletions migrations/20211013151757_fix_mq_latest_message.up.sql
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion sqlxmq_macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sqlxmq_macros"
version = "0.3.1"
version = "0.3.2"
authors = ["Diggory Blake <[email protected]>"]
edition = "2018"
license = "MIT OR Apache-2.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 15cafb3

Please sign in to comment.