Upload schema.sql
Browse files- schema.sql +116 -0
schema.sql
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-- Supabase schema for FIPI tasks and user practice attempts.
|
| 2 |
+
|
| 3 |
+
CREATE EXTENSION IF NOT EXISTS pg_trgm;
|
| 4 |
+
|
| 5 |
+
CREATE TABLE IF NOT EXISTS public.fipi_tasks (
|
| 6 |
+
id BIGSERIAL PRIMARY KEY,
|
| 7 |
+
title TEXT NOT NULL,
|
| 8 |
+
content TEXT NOT NULL,
|
| 9 |
+
source_url TEXT UNIQUE NOT NULL,
|
| 10 |
+
task_type TEXT DEFAULT 'other',
|
| 11 |
+
images TEXT[] DEFAULT '{}',
|
| 12 |
+
variants TEXT[] DEFAULT '{}',
|
| 13 |
+
rubert_analysis JSONB DEFAULT '{}',
|
| 14 |
+
scraped_at TIMESTAMPTZ DEFAULT NOW(),
|
| 15 |
+
created_at TIMESTAMPTZ DEFAULT NOW(),
|
| 16 |
+
updated_at TIMESTAMPTZ DEFAULT NOW()
|
| 17 |
+
);
|
| 18 |
+
|
| 19 |
+
CREATE INDEX IF NOT EXISTS idx_fipi_tasks_task_type ON public.fipi_tasks(task_type);
|
| 20 |
+
CREATE INDEX IF NOT EXISTS idx_fipi_tasks_scraped_at ON public.fipi_tasks(scraped_at DESC);
|
| 21 |
+
CREATE INDEX IF NOT EXISTS idx_fipi_tasks_source_url ON public.fipi_tasks(source_url);
|
| 22 |
+
CREATE INDEX IF NOT EXISTS idx_fipi_tasks_title ON public.fipi_tasks USING gin(title gin_trgm_ops);
|
| 23 |
+
CREATE INDEX IF NOT EXISTS idx_fipi_tasks_content ON public.fipi_tasks USING gin(content gin_trgm_ops);
|
| 24 |
+
|
| 25 |
+
ALTER TABLE public.fipi_tasks ENABLE ROW LEVEL SECURITY;
|
| 26 |
+
|
| 27 |
+
DROP POLICY IF EXISTS "Public can view all tasks" ON public.fipi_tasks;
|
| 28 |
+
DROP POLICY IF EXISTS "Service key can insert tasks" ON public.fipi_tasks;
|
| 29 |
+
DROP POLICY IF EXISTS "Service key can update tasks" ON public.fipi_tasks;
|
| 30 |
+
DROP POLICY IF EXISTS "Service key can delete tasks" ON public.fipi_tasks;
|
| 31 |
+
|
| 32 |
+
CREATE POLICY "Public can view all tasks"
|
| 33 |
+
ON public.fipi_tasks
|
| 34 |
+
FOR SELECT
|
| 35 |
+
USING (true);
|
| 36 |
+
|
| 37 |
+
CREATE POLICY "Service key can insert tasks"
|
| 38 |
+
ON public.fipi_tasks
|
| 39 |
+
FOR INSERT
|
| 40 |
+
WITH CHECK (true);
|
| 41 |
+
|
| 42 |
+
CREATE POLICY "Service key can update tasks"
|
| 43 |
+
ON public.fipi_tasks
|
| 44 |
+
FOR UPDATE
|
| 45 |
+
USING (true);
|
| 46 |
+
|
| 47 |
+
CREATE POLICY "Service key can delete tasks"
|
| 48 |
+
ON public.fipi_tasks
|
| 49 |
+
FOR DELETE
|
| 50 |
+
USING (true);
|
| 51 |
+
|
| 52 |
+
CREATE OR REPLACE FUNCTION public.update_updated_at_column()
|
| 53 |
+
RETURNS TRIGGER AS $$
|
| 54 |
+
BEGIN
|
| 55 |
+
NEW.updated_at = NOW();
|
| 56 |
+
RETURN NEW;
|
| 57 |
+
END;
|
| 58 |
+
$$ LANGUAGE plpgsql;
|
| 59 |
+
|
| 60 |
+
DROP TRIGGER IF EXISTS update_fipi_tasks_updated_at ON public.fipi_tasks;
|
| 61 |
+
CREATE TRIGGER update_fipi_tasks_updated_at
|
| 62 |
+
BEFORE UPDATE ON public.fipi_tasks
|
| 63 |
+
FOR EACH ROW
|
| 64 |
+
EXECUTE FUNCTION public.update_updated_at_column();
|
| 65 |
+
|
| 66 |
+
CREATE OR REPLACE VIEW public.fipi_tasks_stats AS
|
| 67 |
+
SELECT
|
| 68 |
+
COUNT(*) AS total_tasks,
|
| 69 |
+
COUNT(*) FILTER (WHERE task_type = 'writing') AS writing_tasks,
|
| 70 |
+
COUNT(*) FILTER (WHERE task_type = 'test') AS test_tasks,
|
| 71 |
+
COUNT(*) FILTER (WHERE task_type = 'listening') AS listening_tasks,
|
| 72 |
+
COUNT(*) FILTER (WHERE task_type = 'reading') AS reading_tasks,
|
| 73 |
+
COUNT(*) FILTER (WHERE task_type = 'other') AS other_tasks,
|
| 74 |
+
MAX(scraped_at) AS last_scrape
|
| 75 |
+
FROM public.fipi_tasks;
|
| 76 |
+
|
| 77 |
+
CREATE TABLE IF NOT EXISTS public.task_attempts (
|
| 78 |
+
id BIGSERIAL PRIMARY KEY,
|
| 79 |
+
created_at TIMESTAMPTZ DEFAULT NOW() NOT NULL,
|
| 80 |
+
user_id UUID REFERENCES auth.users NOT NULL,
|
| 81 |
+
task_id BIGINT REFERENCES public.fipi_tasks(id) ON DELETE CASCADE NOT NULL,
|
| 82 |
+
submitted_answer TEXT NOT NULL,
|
| 83 |
+
is_correct BOOLEAN NOT NULL,
|
| 84 |
+
check_status TEXT NOT NULL DEFAULT 'not_checked'
|
| 85 |
+
);
|
| 86 |
+
|
| 87 |
+
ALTER TABLE public.task_attempts ENABLE ROW LEVEL SECURITY;
|
| 88 |
+
|
| 89 |
+
DROP POLICY IF EXISTS "Users can insert their own task attempts" ON public.task_attempts;
|
| 90 |
+
DROP POLICY IF EXISTS "Users can view their own task attempts" ON public.task_attempts;
|
| 91 |
+
DROP POLICY IF EXISTS "Users can update their own task attempts" ON public.task_attempts;
|
| 92 |
+
DROP POLICY IF EXISTS "Users can delete their own task attempts" ON public.task_attempts;
|
| 93 |
+
|
| 94 |
+
CREATE POLICY "Users can insert their own task attempts"
|
| 95 |
+
ON public.task_attempts
|
| 96 |
+
FOR INSERT
|
| 97 |
+
WITH CHECK (auth.uid() = user_id);
|
| 98 |
+
|
| 99 |
+
CREATE POLICY "Users can view their own task attempts"
|
| 100 |
+
ON public.task_attempts
|
| 101 |
+
FOR SELECT
|
| 102 |
+
USING (auth.uid() = user_id);
|
| 103 |
+
|
| 104 |
+
CREATE POLICY "Users can update their own task attempts"
|
| 105 |
+
ON public.task_attempts
|
| 106 |
+
FOR UPDATE
|
| 107 |
+
USING (auth.uid() = user_id)
|
| 108 |
+
WITH CHECK (auth.uid() = user_id);
|
| 109 |
+
|
| 110 |
+
CREATE POLICY "Users can delete their own task attempts"
|
| 111 |
+
ON public.task_attempts
|
| 112 |
+
FOR DELETE
|
| 113 |
+
USING (auth.uid() = user_id);
|
| 114 |
+
|
| 115 |
+
CREATE INDEX IF NOT EXISTS idx_task_attempts_user_created_at ON public.task_attempts(user_id, created_at DESC);
|
| 116 |
+
CREATE INDEX IF NOT EXISTS idx_task_attempts_task_id ON public.task_attempts(task_id);
|