-
-
Notifications
You must be signed in to change notification settings - Fork 13
Ensured calculation of a negative repost count does not happen #1103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
WalkthroughA new integration test was added to the test suite for the Estimated code review effort🎯 2 (Simple) | ⏱️ ~7 minutes
Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
@@ -403,7 +403,7 @@ export class KnexPostRepository { | |||
.update({ | |||
repost_count: post.isInternal | |||
? transaction.raw( | |||
`repost_count + ${insertedRepostsCount - removedRepostsCount}`, | |||
`GREATEST(0, CAST(repost_count AS SIGNED) + ${insertedRepostsCount - removedRepostsCount})`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why use CAST
? From the AI:
In MySQL, the repost_count column is defined as BIGINT UNSIGNED. When MySQL performs arithmetic operations on unsigned integers, it tries to keep the result unsigned as well.
Here's what happens:
1. Without CAST:
- repost_count is 1 (unsigned)
- We try to calculate: 1 + (-2)
- MySQL attempts to calculate this as unsigned arithmetic
- Since unsigned integers can't be negative, MySQL throws an error: "BIGINT UNSIGNED value is out of range"
2. With CAST AS SIGNED:
- repost_count is cast to a signed integer first
- The calculation becomes: 1 + (-2) = -1 (as a signed integer)
- Then GREATEST(0, -1) evaluates to 0
- The final result 0 is valid for an unsigned column
The CAST is necessary because MySQL's type system is strict about unsigned arithmetic. It won't allow intermediate calculations that would result in negative values, even if the final
result (after applying GREATEST) would be non-negative.
caef097
to
2f118cf
Compare
2f118cf
to
0fc4e3d
Compare
0fc4e3d
to
c924572
Compare
c924572
to
48a1f1a
Compare
ref https://linear.app/ghost/issue/PROD-2332 It was possible to get a negative repost count if the post repost count had already been updated via another process whilst the post was being updated
48a1f1a
to
ebf3c09
Compare
ref https://linear.app/ghost/issue/PROD-2332
It was possible to get a negative repost count if the post repost count had already been updated via another process whilst the post was being updated