Spaces:
Runtime error

File size: 6,040 Bytes
ed86a97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
-- 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);