Spaces:
Paused
Paused
File size: 8,026 Bytes
dc1b3fc | 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | -- =====================================================================
-- MindPulse — Supabase / PostgreSQL database schema
-- =====================================================================
-- Run this in the Supabase SQL Editor (or psql) against a fresh database
-- to recreate all tables, indexes, Row Level Security, policies, and grants.
--
-- Notes:
-- * Auth users live in Supabase's built-in `auth.users` table; the tables
-- below reference it via foreign keys.
-- * Also create a PUBLIC Storage bucket named `roi-analysis` (used by
-- backend/app/services/supabase_service.py) — buckets are not created
-- by SQL.
-- =====================================================================
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- ---------------------------------------------------------------------
-- Tables
-- ---------------------------------------------------------------------
-- User profiles
CREATE TABLE user_profiles (
id UUID PRIMARY KEY REFERENCES auth.users ON DELETE CASCADE,
email TEXT UNIQUE NOT NULL,
full_name TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT now(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT now()
);
-- File uploads
CREATE TABLE file_uploads (
upload_id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID NOT NULL REFERENCES auth.users ON DELETE CASCADE,
file_name TEXT NOT NULL,
file_size BIGINT NOT NULL,
file_path TEXT NOT NULL,
status TEXT DEFAULT 'uploaded',
created_at TIMESTAMP WITH TIME ZONE DEFAULT now(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT now(),
CONSTRAINT valid_status CHECK (status IN ('uploaded', 'processing', 'completed', 'failed'))
);
-- Model executions
CREATE TABLE model_executions (
execution_id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
upload_id UUID NOT NULL REFERENCES file_uploads ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES auth.users ON DELETE CASCADE,
status TEXT DEFAULT 'queued',
results JSONB,
error_message TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT now(),
completed_at TIMESTAMP WITH TIME ZONE,
processing_time_ms INTEGER,
CONSTRAINT valid_execution_status CHECK (status IN ('queued', 'processing', 'completed', 'failed'))
);
-- ROI analyses metadata (for supabase_service.py)
CREATE TABLE roi_analyses (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
execution_id UUID NOT NULL REFERENCES model_executions ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES auth.users ON DELETE CASCADE,
metadata JSONB NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT now()
);
-- Prediction summaries
CREATE TABLE prediction_summaries (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
execution_id UUID NOT NULL REFERENCES model_executions ON DELETE CASCADE,
upload_id UUID NOT NULL REFERENCES file_uploads ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES auth.users ON DELETE CASCADE,
score_id TEXT NOT NULL,
predicted_value DOUBLE PRECISION NOT NULL,
top_regions JSONB DEFAULT '[]'::jsonb,
completed_at TIMESTAMP WITH TIME ZONE DEFAULT now()
);
-- ---------------------------------------------------------------------
-- Indexes
-- ---------------------------------------------------------------------
CREATE INDEX idx_file_uploads_user_id ON file_uploads(user_id);
CREATE INDEX idx_file_uploads_status ON file_uploads(status);
CREATE INDEX idx_file_uploads_created_at ON file_uploads(created_at DESC);
CREATE INDEX idx_model_executions_user_id ON model_executions(user_id);
CREATE INDEX idx_model_executions_upload_id ON model_executions(upload_id);
CREATE INDEX idx_model_executions_status ON model_executions(status);
CREATE INDEX idx_model_executions_created_at ON model_executions(created_at DESC);
CREATE INDEX idx_roi_analyses_user_id ON roi_analyses(user_id);
CREATE INDEX idx_roi_analyses_execution_id ON roi_analyses(execution_id);
CREATE INDEX idx_prediction_summaries_user_id ON prediction_summaries(user_id);
CREATE INDEX idx_prediction_summaries_score_id ON prediction_summaries(score_id);
CREATE INDEX idx_prediction_summaries_completed_at ON prediction_summaries(completed_at DESC);
CREATE INDEX idx_prediction_summaries_execution_id ON prediction_summaries(execution_id);
-- ---------------------------------------------------------------------
-- Row Level Security
-- ---------------------------------------------------------------------
ALTER TABLE user_profiles ENABLE ROW LEVEL SECURITY;
ALTER TABLE file_uploads ENABLE ROW LEVEL SECURITY;
ALTER TABLE model_executions ENABLE ROW LEVEL SECURITY;
ALTER TABLE roi_analyses ENABLE ROW LEVEL SECURITY;
ALTER TABLE prediction_summaries ENABLE ROW LEVEL SECURITY;
-- User profiles policies
CREATE POLICY "Users can view own profile" ON user_profiles
FOR SELECT USING (auth.uid() = id);
CREATE POLICY "Users can update own profile" ON user_profiles
FOR UPDATE USING (auth.uid() = id);
CREATE POLICY "Users can create profile" ON user_profiles
FOR INSERT WITH CHECK (auth.uid() = id);
-- File uploads policies
CREATE POLICY "Users can view own uploads" ON file_uploads
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can create uploads" ON file_uploads
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own uploads" ON file_uploads
FOR UPDATE USING (auth.uid() = user_id);
CREATE POLICY "Users can delete own uploads" ON file_uploads
FOR DELETE USING (auth.uid() = user_id);
-- Model executions policies
CREATE POLICY "Users can view own executions" ON model_executions
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can create executions" ON model_executions
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own executions" ON model_executions
FOR UPDATE USING (auth.uid() = user_id);
-- ROI analyses policies
CREATE POLICY "Users can view own roi analyses" ON roi_analyses
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can create roi analyses" ON roi_analyses
FOR INSERT WITH CHECK (auth.uid() = user_id);
-- Prediction summaries policies
CREATE POLICY "Users can view own prediction summaries" ON prediction_summaries
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can create prediction summaries" ON prediction_summaries
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can delete own prediction summaries" ON prediction_summaries
FOR DELETE USING (auth.uid() = user_id);
-- ---------------------------------------------------------------------
-- Grants (service_role used by the backend)
-- ---------------------------------------------------------------------
grant usage on schema public to service_role;
grant select, insert, update, delete
on table public.user_profiles
to service_role;
grant select, insert, update, delete
on table public.file_uploads
to service_role;
grant select, insert, update, delete
on table public.model_executions
to service_role;
grant select, insert, update, delete
on table public.roi_analyses
to service_role;
grant select, insert, update, delete
on table public.prediction_summaries
to service_role;
grant usage, select, update
on all sequences in schema public
to service_role;
-- ---------------------------------------------------------------------
-- auto-create user_profiles row after email confirmation
-- ---------------------------------------------------------------------
create or replace function public.handle_user_email_confirmed()
returns trigger as $$
begin
if new.email_confirmed_at is not null
and (old.email_confirmed_at is null or old.email_confirmed_at <> new.email_confirmed_at) then
insert into public.user_profiles (id, email, created_at, updated_at)
values (new.id, new.email, now(), now())
on conflict (id) do nothing;
end if;
return new;
end;
$$ language plpgsql security definer;
drop trigger if exists on_auth_user_confirmed on auth.users;
create trigger on_auth_user_confirmed
after update on auth.users
for each row execute function public.handle_user_email_confirmed();
|