-- Supabase Setup Script for AI Tester Platform -- Run this in Supabase SQL Editor -- Note: RLS on auth.users is enabled by default by Supabase and the table -- is owned by the supabase_auth_admin role; we don't ALTER it from here -- (would fail with "must be owner of table users" via the postgres role). -- Create profiles table CREATE TABLE IF NOT EXISTS public.profiles ( id uuid PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE, email text NOT NULL, full_name text, avatar_url text, role text DEFAULT 'user' CHECK (role IN ('admin', 'user')), created_at timestamp with time zone DEFAULT timezone('utc'::text, now()), updated_at timestamp with time zone DEFAULT timezone('utc'::text, now()) ); -- Enable RLS on profiles ALTER TABLE public.profiles ENABLE ROW LEVEL SECURITY; -- Create policies (idempotent: drop+recreate so re-running doesn't fail) DROP POLICY IF EXISTS "Public profiles are viewable by everyone" ON public.profiles; DROP POLICY IF EXISTS "Users can insert their own profile" ON public.profiles; DROP POLICY IF EXISTS "Users can update own profile" ON public.profiles; CREATE POLICY "Public profiles are viewable by everyone" ON public.profiles FOR SELECT USING (true); CREATE POLICY "Users can insert their own profile" ON public.profiles FOR INSERT WITH CHECK (auth.uid() = id); CREATE POLICY "Users can update own profile" ON public.profiles FOR UPDATE USING (auth.uid() = id); -- Function to handle user creation CREATE OR REPLACE FUNCTION public.handle_new_user() RETURNS trigger AS $$ BEGIN INSERT INTO public.profiles (id, email, full_name, role) VALUES ( NEW.id, NEW.email, NEW.raw_user_meta_data->>'full_name', COALESCE(NEW.raw_user_meta_data->>'role', 'user') ); RETURN NEW; END; $$ LANGUAGE plpgsql SECURITY DEFINER; -- Trigger for new user CREATE OR REPLACE TRIGGER on_auth_user_created AFTER INSERT ON auth.users FOR EACH ROW EXECUTE FUNCTION public.handle_new_user(); -- Create admin user (run this after creating user through UI or API) -- UPDATE public.profiles SET role = 'admin' WHERE email = 'your-admin-email@example.com'; -- Migration from SQLite to Supabase (optional) -- If you have existing data in SQLite, you can migrate it here -- CREATE TABLE IF NOT EXISTS public.bots ( -- id uuid PRIMARY KEY DEFAULT gen_random_uuid(), -- name text NOT NULL, -- url text NOT NULL, -- description text, -- selectors jsonb DEFAULT '{}', -- expected_tone jsonb DEFAULT '{}', -- rag_config jsonb DEFAULT '{}', -- is_active boolean DEFAULT true, -- created_by uuid REFERENCES public.profiles(id), -- created_at timestamp with time zone DEFAULT timezone('utc'::text, now()), -- updated_at timestamp with time zone DEFAULT timezone('utc'::text, now()) -- ); -- CREATE TABLE IF NOT EXISTS public.personas ( -- id uuid PRIMARY KEY DEFAULT gen_random_uuid(), -- name text NOT NULL, -- mood text DEFAULT 'neutral', -- goal text NOT NULL, -- background text, -- constraints text[], -- language text DEFAULT 'italian', -- communication_style text, -- typing_speed text, -- tech_proficiency text, -- patience_level integer DEFAULT 5, -- is_template boolean DEFAULT false, -- tags text[], -- created_by uuid REFERENCES public.profiles(id), -- created_at timestamp with time zone DEFAULT timezone('utc'::text, now()) -- ); -- CREATE TABLE IF NOT EXISTS public.scenarios ( -- id uuid PRIMARY KEY DEFAULT gen_random_uuid(), -- name text NOT NULL, -- description text, -- steps jsonb NOT NULL DEFAULT '[]', -- success_criteria jsonb DEFAULT '{}', -- is_template boolean DEFAULT false, -- tags text[], -- category text, -- priority integer DEFAULT 1, -- created_by uuid REFERENCES public.profiles(id), -- created_at timestamp with time zone DEFAULT timezone('utc'::text, now()), -- updated_at timestamp with time zone DEFAULT timezone('utc'::text, now()) -- ); -- CREATE TABLE IF NOT EXISTS public.test_sessions ( -- id uuid PRIMARY KEY DEFAULT gen_random_uuid(), -- bot_id uuid REFERENCES public.bots(id), -- bot_version text NOT NULL, -- session_name text, -- description text, -- config jsonb DEFAULT '{}', -- status text DEFAULT 'pending', -- created_by uuid REFERENCES public.profiles(id), -- started_at timestamp with time zone, -- completed_at timestamp with time zone, -- total_tests integer DEFAULT 0, -- passed integer DEFAULT 0, -- failed integer DEFAULT 0, -- skipped integer DEFAULT 0, -- created_at timestamp with time zone DEFAULT timezone('utc'::text, now()) -- ); -- CREATE TABLE IF NOT EXISTS public.test_results ( -- id uuid PRIMARY KEY DEFAULT gen_random_uuid(), -- session_id uuid REFERENCES public.test_sessions(id) ON DELETE CASCADE, -- scenario_id uuid REFERENCES public.scenarios(id), -- persona_id uuid REFERENCES public.personas(id), -- steps_results jsonb DEFAULT '[]', -- status text NOT NULL, -- total_steps integer DEFAULT 0, -- passed_steps integer DEFAULT 0, -- failed_steps integer DEFAULT 0, -- total_duration_ms integer, -- avg_response_time_ms integer, -- rag_faithfulness_avg float, -- tone_match_avg float, -- error_message text, -- failure_reason text, -- screenshot_urls text[], -- conversation_log text, -- started_at timestamp with time zone, -- completed_at timestamp with time zone, -- created_at timestamp with time zone DEFAULT timezone('utc'::text, now()) -- ); -- Indexes for better performance -- CREATE INDEX idx_bots_created_by ON public.bots(created_by); -- CREATE INDEX idx_personas_created_by ON public.personas(created_by); -- CREATE INDEX idx_scenarios_created_by ON public.scenarios(created_by); -- CREATE INDEX idx_sessions_created_by ON public.test_sessions(created_by); -- CREATE INDEX idx_results_session ON public.test_results(session_id);