Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions supabase/database/sql/search_related_snippets.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
CREATE OR REPLACE FUNCTION search_related_snippets(
snippet_id uuid,
p_language TEXT DEFAULT 'english',
match_threshold float DEFAULT 0.7,
match_count int DEFAULT 10
match_count int DEFAULT 3
)
RETURNS jsonb
SECURITY DEFINER AS $$
Expand Down Expand Up @@ -31,8 +32,12 @@ BEGIN
s.id,
s.title,
a.radio_station_name,
a.radio_station_code,
a.location_state,
s.summary,
CASE
WHEN p_language = 'spanish' THEN s.summary ->> 'spanish'
ELSE s.summary ->> 'english'
END AS summary,
Comment on lines +37 to +40
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add input validation for p_language parameter

The CASE statement assumes p_language will be either 'spanish' or defaults to 'english', but there's no validation for unsupported language values. Consider adding explicit validation.

CREATE OR REPLACE FUNCTION search_related_snippets(
    snippet_id uuid,
    p_language TEXT DEFAULT 'english',
    match_threshold float DEFAULT 0.7,
    match_count int DEFAULT 3
)
RETURNS jsonb
SECURITY DEFINER AS $$
DECLARE
    current_user_id UUID;
    source_embedding vector(3072);
    result jsonb;
BEGIN
    -- Check if the user is authenticated
    current_user_id := auth.uid();
    IF current_user_id IS NULL THEN
        RAISE EXCEPTION 'Only logged-in users can call this function';
    END IF;

+   -- Validate language parameter
+   IF p_language NOT IN ('english', 'spanish') THEN
+       RAISE EXCEPTION 'Unsupported language: %. Supported values are: english, spanish', p_language;
+   END IF;

Committable suggestion skipped: line range outside the PR's diff.

s.recorded_at,
s.comment_count,
1 - (se.embedding <=> source_embedding) as similarity
Expand All @@ -50,22 +55,10 @@ BEGIN
'id', ss.id,
'title', ss.title,
'radio_station_name', ss.radio_station_name,
'radio_station_code', ss.radio_station_code,
'location_state', ss.location_state,
'summary', ss.summary,
'labels', COALESCE(
(
SELECT jsonb_agg(
jsonb_build_object(
'text', l.text,
'text_spanish', l.text_spanish
)
)
FROM snippet_labels sl
JOIN labels l ON l.id = sl.label
WHERE sl.snippet = ss.id
),
'[]'::jsonb
),
'labels', get_snippet_labels(ss.id, p_language) -> 'labels',
'recorded_at', ss.recorded_at,
'comment_count', ss.comment_count,
'similarity', ss.similarity
Expand Down