Steps to reproduce
When using watermill-sql with MySQL and a custom GenerateWhereClause that contains an OR condition (e.g., for delayed messages), the final SQL query logic becomes corrupted because AND has higher precedence than OR.
Example Scenario:
- Base condition:
acked = 0
- Dynamic condition:
delayed_until <= NOW() OR delayed_until IS NULL
The generated SQL currently looks like:
SELECT ... WHERE acked = 0 AND delayed_until <= NOW() OR delayed_until IS NULL
Expected behavior
The dynamic condition should be wrapped in parentheses to maintain logical integrity:
SELECT ... WHERE acked = 0 AND (delayed_until <= NOW() OR delayed_until IS NULL)
Actual behavior
The SQL is interpreted as (acked = 0 AND delayed_until <= NOW()) OR (delayed_until IS NULL).
This causes messages where delayed_until IS NULL to be picked up even if they are already acked (acked = 1). This leads to infinite message duplication and high database load, as the same message is processed over and over again.
Possible solution
I have already submitted a fix here: ThreeDotsLabs/watermill-sql/pull/58.
This is a critical logic bug affecting data consistency in MySQL-based queues. Please consider reviewing the linked PR.
Steps to reproduce
When using
watermill-sqlwith MySQL and a customGenerateWhereClausethat contains anORcondition (e.g., for delayed messages), the final SQL query logic becomes corrupted becauseANDhas higher precedence thanOR.Example Scenario:
acked = 0delayed_until <= NOW() OR delayed_until IS NULLThe generated SQL currently looks like:
Expected behavior
The dynamic condition should be wrapped in parentheses to maintain logical integrity:
Actual behavior
The SQL is interpreted as
(acked = 0 AND delayed_until <= NOW()) OR (delayed_until IS NULL).This causes messages where
delayed_until IS NULLto be picked up even if they are already acked (acked = 1). This leads to infinite message duplication and high database load, as the same message is processed over and over again.Possible solution
I have already submitted a fix here: ThreeDotsLabs/watermill-sql/pull/58.
This is a critical logic bug affecting data consistency in MySQL-based queues. Please consider reviewing the linked PR.