-
Notifications
You must be signed in to change notification settings - Fork 4
[f] 241 - Update the last activity user only for user action #12
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| UPDATE snippets s | ||
| SET user_last_activity = ( | ||
| SELECT MAX(activity_time) | ||
| FROM ( | ||
| -- Get label upvotes timestamps | ||
| SELECT created_at as activity_time | ||
| FROM label_upvotes | ||
| WHERE snippet_label IN ( | ||
| SELECT id | ||
| FROM snippet_labels | ||
| WHERE snippet = s.id | ||
| ) | ||
|
|
||
| UNION ALL | ||
|
|
||
| -- Get likes timestamps | ||
| SELECT created_at as activity_time | ||
| FROM user_like_snippets | ||
| WHERE snippet = s.id | ||
| AND (value = 1 OR value = -1) | ||
|
|
||
| UNION ALL | ||
|
|
||
| SELECT created_at as activity_time | ||
| FROM comments | ||
| WHERE room_id = s.id | ||
| AND deleted_at is null | ||
|
|
||
| ) all_activities | ||
| ); | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,14 +1,15 @@ | ||||||||||||||||||||||||||||||||||||||||
| CREATE OR REPLACE FUNCTION update_snippet_like_count() | ||||||||||||||||||||||||||||||||||||||||
| RETURNS TRIGGER AS $$ | ||||||||||||||||||||||||||||||||||||||||
| BEGIN | ||||||||||||||||||||||||||||||||||||||||
| -- Update the like count for the affected snippet | ||||||||||||||||||||||||||||||||||||||||
| UPDATE snippets | ||||||||||||||||||||||||||||||||||||||||
| SET like_count = ( | ||||||||||||||||||||||||||||||||||||||||
| SELECT COUNT(*) | ||||||||||||||||||||||||||||||||||||||||
| FROM user_like_snippets | ||||||||||||||||||||||||||||||||||||||||
| WHERE snippet = COALESCE(NEW.snippet, OLD.snippet) | ||||||||||||||||||||||||||||||||||||||||
| AND value = 1 | ||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||
| SET | ||||||||||||||||||||||||||||||||||||||||
| like_count = ( | ||||||||||||||||||||||||||||||||||||||||
| SELECT COUNT(*) | ||||||||||||||||||||||||||||||||||||||||
| FROM user_like_snippets | ||||||||||||||||||||||||||||||||||||||||
| WHERE snippet = COALESCE(NEW.snippet, OLD.snippet) | ||||||||||||||||||||||||||||||||||||||||
| AND value = 1 | ||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||
| user_last_activity = NOW() | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+5
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider handling unlike actions differently Similar to the comment count update, we should consider differentiating between like and unlike actions for updating Consider modifying the trigger to only update UPDATE snippets
SET
like_count = (
SELECT COUNT(*)
FROM user_like_snippets
WHERE snippet = COALESCE(NEW.snippet, OLD.snippet)
AND value = 1
),
- user_last_activity = NOW()
+ user_last_activity = CASE
+ WHEN TG_OP = 'INSERT' AND NEW.value = 1 THEN NOW()
+ ELSE user_last_activity
+ END
WHERE id = COALESCE(NEW.snippet, OLD.snippet);📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||
| WHERE id = COALESCE(NEW.snippet, OLD.snippet); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| RETURN NULL; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,20 +3,20 @@ RETURNS TRIGGER AS $$ | |
| DECLARE | ||
| snippet_id UUID; | ||
| BEGIN | ||
| -- Get the snippet_id from snippet_labels | ||
| SELECT snippet INTO snippet_id | ||
| FROM snippet_labels | ||
| WHERE id = COALESCE(NEW.snippet_label, OLD.snippet_label); | ||
|
|
||
| -- Update the upvote count for the affected snippet | ||
| IF snippet_id IS NOT NULL THEN | ||
| UPDATE snippets | ||
| SET upvote_count = ( | ||
| SELECT COUNT(*) | ||
| FROM label_upvotes lu | ||
| JOIN snippet_labels sl ON lu.snippet_label = sl.id | ||
| WHERE sl.snippet = snippet_id | ||
| ) | ||
| SET | ||
| upvote_count = ( | ||
| SELECT COUNT(*) | ||
| FROM label_upvotes lu | ||
| JOIN snippet_labels sl ON lu.snippet_label = sl.id | ||
| WHERE sl.snippet = snippet_id | ||
| ), | ||
| user_last_activity = NOW() | ||
|
Comment on lines
+12
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider handling upvote removal differently Similar to the previous files, we should differentiate between upvote and upvote removal actions for updating Consider modifying the trigger to only update UPDATE snippets
SET
upvote_count = (
SELECT COUNT(*)
FROM label_upvotes lu
JOIN snippet_labels sl ON lu.snippet_label = sl.id
WHERE sl.snippet = snippet_id
),
- user_last_activity = NOW()
+ user_last_activity = CASE
+ WHEN TG_OP = 'INSERT' THEN NOW()
+ ELSE user_last_activity
+ END
WHERE id = snippet_id;Consider extracting the user activity update logic into a separate function to maintain consistency across all three triggers and make future modifications easier.
|
||
| WHERE id = snippet_id; | ||
| END IF; | ||
|
|
||
|
|
||
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.
💡 Codebase verification
Race condition confirmed between aggregate and trigger-based updates of user_last_activity
The verification confirms a critical race condition:
update_last_user_activity.sqlcalculates MAX(activity_time) from historical recordsupdate_snippet_comment_count,update_snippet_like_count,update_snippet_upvote_count) setuser_last_activity = NOW()Recommendations:
WHERE user_last_activity < calculated_maxcondition to the aggregate update to prevent overwriting newer timestamps🔗 Analysis chain
Potential race condition with concurrent updates.
The AI summary indicates that other functions (
update_snippet_comment_count,update_snippet_like_count,update_snippet_upvote_count) also updateuser_last_activityusingNOW(). This could lead to race conditions where this aggregate update overwrites more recent activity timestamps.Consider:
Let's verify the other function implementations:
The search results confirm the review comment's concern. Let me check the full context of these functions and their triggers to understand the complete picture:
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 1379
Script:
Length of output: 1960