File size: 3,930 Bytes
162c9bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
-- ================================================================================
--                   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;