Spaces:
Runtime error
Runtime error
Download supabase_security_setup.sql from vaishnav019/humres-api: direct link, hf CLI and curl.
- Browser
- Download file 3.93 kB
-
https://huggingface.co/spaces/vaishnav019/humres-api/resolve/main/supabase_security_setup.sql
- Command line
-
hf download hf://spaces/vaishnav019/humres-api/supabase_security_setup.sql
-
curl -L -o supabase_security_setup.sql https://huggingface.co/spaces/vaishnav019/humres-api/resolve/main/supabase_security_setup.sql
3.93 kB
| -- ================================================================================ | |
| -- SUPABASE SECURITY & USER ISOLATION SCRIPT | |
| -- ================================================================================ | |
| -- This script ensures user embeddings, resume text, AI profiles, saved jobs, and | |
| -- alert preferences are strictly isolated under each user's unique user_id. | |
| -- | |
| -- DATA ISOLATION IS HANDLED BY FASTAPI BACKEND: | |
| -- - Every API endpoint requires a valid JWT token (get_current_user) | |
| -- - Every Supabase query uses .eq("user_id", user_id) for strict scoping | |
| -- - RLS is DISABLED to avoid conflicts with service_role key usage | |
| -- | |
| -- Run this in your Supabase SQL Editor. | |
| -- 1. Enable Vector Extension (if not already enabled) | |
| CREATE EXTENSION IF NOT EXISTS vector; | |
| -- 2. Ensure user_resumes table structure and PK/FK isolation | |
| CREATE TABLE IF NOT EXISTS public.user_resumes ( | |
| user_id INT PRIMARY KEY REFERENCES public.users(id) ON DELETE CASCADE, | |
| resume_text TEXT NOT NULL, | |
| embedding vector(768), | |
| created_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL | |
| ); | |
| -- 3. Ensure user_resume_chunks table structure and user_id indexing | |
| CREATE TABLE IF NOT EXISTS public.user_resume_chunks ( | |
| id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, | |
| user_id INT NOT NULL REFERENCES public.users(id) ON DELETE CASCADE, | |
| chunk_index INT NOT NULL, | |
| content TEXT NOT NULL, | |
| token_count INT, | |
| embedding vector(768), | |
| created_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL | |
| ); | |
| CREATE INDEX IF NOT EXISTS idx_user_resume_chunks_user_id ON public.user_resume_chunks(user_id); | |
| -- 4. Ensure user_ai_profiles table structure and PK/FK isolation | |
| CREATE TABLE IF NOT EXISTS public.user_ai_profiles ( | |
| user_id INT PRIMARY KEY REFERENCES public.users(id) ON DELETE CASCADE, | |
| top_skills TEXT[], | |
| experience_level TEXT, | |
| preferred_roles TEXT[], | |
| education TEXT, | |
| projects_summary TEXT, | |
| job_fit_score INT, | |
| missing_skills TEXT[], | |
| created_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL | |
| ); | |
| -- 5. DISABLE Row Level Security (RLS) on all tables | |
| -- RLS is not needed because FastAPI backend handles user isolation via JWT + .eq("user_id", user_id) | |
| -- The service_role key bypasses RLS, but disabling avoids any client library edge-case conflicts. | |
| -- Drop existing RLS policies first (if they exist) | |
| DROP POLICY IF EXISTS user_resumes_owner_policy ON public.user_resumes; | |
| DROP POLICY IF EXISTS user_resume_chunks_owner_policy ON public.user_resume_chunks; | |
| DROP POLICY IF EXISTS user_ai_profiles_owner_policy ON public.user_ai_profiles; | |
| DROP POLICY IF EXISTS saved_jobs_owner_policy ON public.saved_jobs; | |
| DROP POLICY IF EXISTS alert_preferences_owner_policy ON public.alert_preferences; | |
| -- Disable RLS on all user-related tables | |
| ALTER TABLE public.user_resumes DISABLE ROW LEVEL SECURITY; | |
| ALTER TABLE public.user_resume_chunks DISABLE ROW LEVEL SECURITY; | |
| ALTER TABLE public.user_ai_profiles DISABLE ROW LEVEL SECURITY; | |
| ALTER TABLE public.saved_jobs DISABLE ROW LEVEL SECURITY; | |
| ALTER TABLE public.alert_preferences DISABLE ROW LEVEL SECURITY; | |
| -- 6. PL/pgSQL Function for Vector Similarity Job Matching | |
| CREATE OR REPLACE FUNCTION match_jobs ( | |
| query_embedding vector(768), | |
| match_threshold float, | |
| match_count int | |
| ) RETURNS TABLE ( | |
| id int, | |
| title text, | |
| company text, | |
| location text, | |
| salary int, | |
| description text, | |
| job_url text, | |
| similarity float | |
| ) AS $$ | |
| BEGIN | |
| RETURN QUERY | |
| SELECT jobs.id, jobs.title, jobs.company, jobs.location, | |
| jobs.salary, jobs.description, jobs.job_url, | |
| 1 - (jobs.embedding <=> query_embedding) AS similarity | |
| FROM jobs | |
| WHERE 1 - (jobs.embedding <=> query_embedding) > match_threshold | |
| ORDER BY jobs.embedding <=> query_embedding ASC | |
| LIMIT match_count; | |
| END; | |
| $$ LANGUAGE plpgsql; | |