-
Notifications
You must be signed in to change notification settings - Fork 4
[f] 116 - Implement sort feature (BE) #6
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,9 @@ | ||
| UPDATE snippets s | ||
| SET comment_count = ( | ||
| SELECT COUNT(*) | ||
| FROM comments c | ||
| WHERE c.room_id = s.id | ||
| and deleted_at is null | ||
| ); | ||
|
|
||
| CREATE INDEX idx_snippets_comment_count ON snippets(comment_count DESC); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| UPDATE snippets s | ||
| SET like_count = ( | ||
| SELECT COUNT(*) | ||
| FROM user_like_snippets uls | ||
| WHERE uls.snippet = s.id | ||
| AND uls.value = 1 | ||
| ); | ||
|
|
||
| CREATE INDEX idx_snippets_like_count ON snippets(like_count DESC); | ||
|
Collaborator
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. Perhaps creating index for comment_count and upvote_count as well? 🤔 |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| CREATE TRIGGER update_like_count | ||
| AFTER INSERT OR UPDATE OR DELETE ON user_like_snippets | ||
| FOR EACH ROW | ||
| EXECUTE FUNCTION update_snippet_like_count(); | ||
|
giahung68 marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,14 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CREATE OR REPLACE FUNCTION update_snippet_comment_count() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| RETURNS TRIGGER AS $$ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BEGIN | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| UPDATE snippets | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SET comment_count = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SELECT COUNT(*) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FROM comments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| WHERE room_id = COALESCE(NEW.room_id, OLD.room_id) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| WHERE id = COALESCE(NEW.room_id, OLD.room_id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| RETURN NULL; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| END; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $$ LANGUAGE plpgsql; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1
to
+14
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. 🛠️ Refactor suggestion Add input validation and error handling While the recalculation approach is good, the function needs additional safeguards: CREATE OR REPLACE FUNCTION update_snippet_comment_count()
RETURNS TRIGGER AS $$
BEGIN
+ -- Validate input
+ IF (TG_OP = 'INSERT' AND NEW.room_id IS NULL) OR
+ (TG_OP = 'DELETE' AND OLD.room_id IS NULL) THEN
+ RAISE EXCEPTION 'room_id cannot be null';
+ END IF;
+
+ -- Verify snippet exists
+ IF NOT EXISTS (
+ SELECT 1
+ FROM snippets
+ WHERE id = COALESCE(NEW.room_id, OLD.room_id)
+ ) THEN
+ RAISE EXCEPTION 'Snippet with id % does not exist',
+ COALESCE(NEW.room_id, OLD.room_id);
+ END IF;
+
UPDATE snippets
SET comment_count = (
SELECT COUNT(*)
FROM comments
- WHERE room_id = COALESCE(NEW.room_id, OLD.room_id)
+ WHERE room_id = COALESCE(NEW.room_id, OLD.room_id)
+ AND deleted_at IS NULL -- Only count non-deleted comments
)
WHERE id = COALESCE(NEW.room_id, OLD.room_id);
+
+ IF NOT FOUND THEN
+ RAISE EXCEPTION 'Failed to update comment count for snippet %',
+ COALESCE(NEW.room_id, OLD.room_id);
+ END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;📝 Committable suggestion
Suggested change
💡 Codebase verification Create trigger to execute the update_snippet_comment_count function Add the following trigger definition to the same SQL file: CREATE TRIGGER update_comment_count
AFTER INSERT OR DELETE ON comments
FOR EACH ROW
EXECUTE FUNCTION update_snippet_comment_count();Based on the verification:
🔗 Analysis chainCreate trigger to execute the function The trigger function is defined but the corresponding trigger is missing. Add the following trigger definition: CREATE TRIGGER update_comment_count
AFTER INSERT OR DELETE ON comments
FOR EACH ROW
EXECUTE FUNCTION update_snippet_comment_count();🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Check if trigger exists
rg "CREATE TRIGGER.*update_snippet_comment_count"
Length of output: 51 Script: #!/bin/bash
# Let's search for any trigger definitions in SQL files
fd -e sql -x cat {} | rg -i "CREATE\s+TRIGGER"
# Also search for any files that might contain trigger definitions
fd -e sql | grep -i "trigger"
Length of output: 1997 Script: #!/bin/bash
# Let's check the content of like_count_trigger.sql as it might have a similar pattern
cat supabase/database/sql/like_count_trigger.sql
# Also let's check for any other files that might contain our function name
fd -e sql -x cat {} | rg -A 5 "update_snippet_comment_count"
Length of output: 417 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| 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 | ||
| ) | ||
|
Comment on lines
+6
to
+11
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. 💡 Codebase verification Performance optimization needed for user_like_snippets table Based on the verification attempts, we couldn't find the table definition or any existing indexes for the user_like_snippets table in the codebase. This suggests that the recommended index on (snippet, value) is indeed missing, which could impact query performance for the COUNT operation.
🔗 Analysis chainConsider optimizing the COUNT query performance. For large tables, counting all matching rows can become a performance bottleneck. Consider these optimizations:
Let's verify if the required index exists: 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Check if the index exists on user_like_snippets table
rg -A 5 "CREATE.*INDEX.*user_like_snippets.*snippet.*value"
Length of output: 61 Script: #!/bin/bash
# Let's check for any index definitions in the schema files
fd -e sql | xargs rg -i "CREATE.*INDEX|CREATE TABLE.*user_like_snippets"
Length of output: 3456 Script: #!/bin/bash
# Let's check for the user_like_snippets table definition and any related indexes
rg -i "CREATE TABLE.*user_like_snippets" -A 10
Length of output: 48 Script: #!/bin/bash
# Let's search for any mention of user_like_snippets table in migration files
rg -i "user_like_snippets" supabase/migrations/
Length of output: 47 |
||
| WHERE id = COALESCE(NEW.snippet, OLD.snippet); | ||
|
|
||
| RETURN NULL; | ||
| END; | ||
| $$ LANGUAGE plpgsql; | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,25 @@ | ||||||||||||||||||||
| CREATE OR REPLACE FUNCTION update_snippet_upvote_count() | ||||||||||||||||||||
| RETURNS TRIGGER AS $$ | ||||||||||||||||||||
| DECLARE | ||||||||||||||||||||
| snippet_id UUID; | ||||||||||||||||||||
| BEGIN | ||||||||||||||||||||
|
Comment on lines
+1
to
+5
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. Add trigger creation statement The trigger function is defined but the corresponding trigger creation statement ( Add the trigger creation statement. Example: CREATE TRIGGER update_snippet_upvote_count_trigger
AFTER INSERT OR DELETE OR UPDATE ON label_upvotes
FOR EACH ROW
EXECUTE FUNCTION update_snippet_upvote_count(); |
||||||||||||||||||||
| -- Get the snippet_id from snippet_labels | ||||||||||||||||||||
| SELECT snippet INTO snippet_id | ||||||||||||||||||||
| FROM snippet_labels | ||||||||||||||||||||
| WHERE id = COALESCE(NEW.snippet_label, OLD.snippet_label); | ||||||||||||||||||||
|
Comment on lines
+7
to
+9
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. 🛠️ Refactor suggestion Add error handling for invalid snippet_label The query silently continues if the snippet_label is invalid. Consider adding explicit error handling to maintain data integrity. SELECT snippet INTO snippet_id
FROM snippet_labels
WHERE id = COALESCE(NEW.snippet_label, OLD.snippet_label);
+ IF NOT FOUND THEN
+ RAISE EXCEPTION 'Invalid snippet_label: %', COALESCE(NEW.snippet_label, OLD.snippet_label);
+ END IF;📝 Committable suggestion
Suggested change
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| -- 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 | ||||||||||||||||||||
| ) | ||||||||||||||||||||
| WHERE id = snippet_id; | ||||||||||||||||||||
|
Comment on lines
+13
to
+20
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. 💡 Codebase verification Add indexes to optimize label upvote queries Based on the codebase analysis, the tables are frequently joined and queried, but only one index (
CREATE INDEX IF NOT EXISTS idx_snippet_labels_snippet ON snippet_labels(snippet);
CREATE INDEX IF NOT EXISTS idx_label_upvotes_snippet_label ON label_upvotes(snippet_label);
CREATE UNIQUE INDEX IF NOT EXISTS idx_label_upvotes_user_label ON label_upvotes(upvoted_by, snippet_label);🔗 Analysis chainOptimize query performance with appropriate indexes The COUNT subquery with JOIN could be performance-intensive. Ensure proper indexes exist on the join columns and frequently queried fields. Let's verify the existing indexes: Consider adding these indexes if not present: CREATE INDEX IF NOT EXISTS idx_snippet_labels_snippet ON snippet_labels(snippet);
CREATE INDEX IF NOT EXISTS idx_label_upvotes_snippet_label ON label_upvotes(snippet_label);🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Check for existing indexes on relevant columns
# Search for index definitions
rg -A 3 'CREATE.*INDEX' supabase/database/sql/
# Search for table definitions to verify primary keys
rg -A 10 'CREATE TABLE (snippet_labels|label_upvotes|snippets)' supabase/database/sql/
Length of output: 251 Script: #!/bin/bash
# Let's search for table definitions in all SQL files
rg -A 10 'CREATE TABLE' supabase/database/sql/
# Also search for any foreign key constraints that might imply indexes
rg -B 2 -A 2 'FOREIGN KEY' supabase/database/sql/
# Search specifically for label_upvotes and snippet_labels related definitions
rg -A 10 '(label_upvotes|snippet_labels)' supabase/database/sql/
Length of output: 22235 |
||||||||||||||||||||
| END IF; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| RETURN NULL; | ||||||||||||||||||||
| END; | ||||||||||||||||||||
| $$ LANGUAGE plpgsql; | ||||||||||||||||||||
|
Comment on lines
+1
to
+25
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. 💡 Codebase verification Missing trigger definition for upvote count updates The function
CREATE TRIGGER update_upvote_count
AFTER INSERT OR UPDATE OR DELETE ON label_upvotes
FOR EACH ROW
EXECUTE FUNCTION update_snippet_upvote_count();🔗 Analysis chainVerify trigger execution order with related triggers This trigger function is part of a set of related triggers (comment_count, like_count). Ensure proper trigger execution order to maintain data consistency. Let me gather more information about the trigger definitions for all these functions. Let me try one more search to find the trigger definition for the upvote count function and table definitions using a different pattern. Let me try one final search with a different approach to find all SQL files and their content. Let me check the content of the relevant files for trigger definitions and table relationships. 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Check for related triggers and their execution order
# Search for other trigger definitions
rg -A 5 'CREATE TRIGGER' supabase/database/sql/
# Search for related trigger functions
rg -A 5 'update_snippet.*_count\(\)' supabase/database/sql/
Length of output: 2003 Script: #!/bin/bash
# Search for trigger definitions for all count-related functions
rg -A 3 'CREATE TRIGGER.*update_.*_count' supabase/database/sql/
# Search for table definitions to understand the relationships
rg -A 10 'CREATE TABLE (snippets|snippet_labels|label_upvotes)' supabase/database/sql/
Length of output: 482 Script: #!/bin/bash
# Search for trigger definition for upvote count
rg -A 3 'CREATE TRIGGER' supabase/database/sql/update_snippet_upvote_count.sql
# Search for table definitions with a broader pattern
rg -A 10 'CREATE TABLE' supabase/database/sql/schema.sql
Length of output: 212 Script: #!/bin/bash
# Find all SQL files in the database directory
fd -e sql . supabase/database/
# Search for trigger definition in all SQL files
rg -A 3 'CREATE TRIGGER.*upvote' supabase/database/
# Search for table definitions in all SQL files
rg -A 5 'CREATE TABLE' supabase/database/
Length of output: 1670 Script: #!/bin/bash
# Check content of upvote-related files
cat supabase/database/sql/update_snippet_upvote_count.sql
cat supabase/database/sql/toggle_upvote_label_function.sql
# Check content of table definitions in migration files
cat supabase/database/sql/comment_count_migration.sql
cat supabase/database/sql/like_count_migration.sql
Length of output: 3621 |
||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| WITH upvote_counts AS ( | ||
| SELECT sl.snippet AS snippet_id, | ||
| COUNT(lu.id) AS total_upvotes | ||
| FROM snippet_labels sl | ||
| LEFT JOIN label_upvotes lu ON lu.snippet_label = sl.id | ||
| GROUP BY sl.snippet | ||
| ) | ||
| UPDATE snippets s | ||
| SET upvote_count = COALESCE(uc.total_upvotes, 0) | ||
| FROM upvote_counts uc | ||
| WHERE s.id = uc.snippet_id; | ||
|
Comment on lines
+8
to
+11
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 RIGHT JOIN to update all snippets The current UPDATE might miss snippets that have no entries in WITH upvote_counts AS (
- SELECT sl.snippet AS snippet_id,
+ SELECT s.id AS snippet_id,
COUNT(lu.id) AS total_upvotes
- FROM snippet_labels sl
- LEFT JOIN label_upvotes lu ON lu.snippet_label = sl.id
- GROUP BY sl.snippet
+ FROM snippets s
+ LEFT JOIN snippet_labels sl ON sl.snippet = s.id
+ LEFT JOIN label_upvotes lu ON lu.snippet_label = sl.id
+ GROUP BY s.id
)
|
||
|
|
||
| CREATE INDEX idx_snippets_upvote_count ON snippets(upvote_count DESC); | ||
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.
🛠️ Refactor suggestion
The SQL logic looks correct but consider adding safety measures.
While the UPDATE query is logically correct, consider these improvements for safer execution:
📝 Committable suggestion
💡 Codebase verification
Migration needs concurrent update handling
The trigger function
update_snippet_comment_countuses simple increment/decrement operations which could lead to race conditions during the migration. The migration'sUPDATEquery should use a transaction-safe approach to count comments. Consider using:update_snippet_comment_count.sqluses non-atomiccomment_count + 1andcomment_count - 1operationsFOR UPDATE🔗 Analysis chain
Verify concurrent update handling.
Ensure that the trigger function
update_snippet_comment_countmentioned in the summary is properly handling concurrent updates during this migration.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 163
Script:
Length of output: 2462
Script:
Length of output: 575